Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HttpResponseMaker to allow for building custom ResponseData #605

Merged
merged 1 commit into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ Current

### Changed:

- [Refactored HttpResponseMaker to allow for custom ResponseData implementations](https://github.com/yahoo/fili/pull/605)
* Currently ResponseData is being directly created in when building a response in the HttpResponseMaker. This creation
has been extracted to a factory method, which subclasses of HttpResponseMaker can override.
* Changed relevant methods fields from private to protected.

- [Move makeRequest to JTB](https://github.com/yahoo/fili/pull/590)
* Move `makeRequest` from test to `JerseyTestBinder`
* Some tests uses variable name `jerseyTestBinder`; some uses `jtb`. They are all renamed to the former for naming
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.yahoo.bard.webservice.data.dimension.DimensionField;
import com.yahoo.bard.webservice.druid.model.query.DruidQuery;
import com.yahoo.bard.webservice.util.Pagination;
import com.yahoo.bard.webservice.util.SimplifiedIntervalList;
import com.yahoo.bard.webservice.web.ApiRequest;
import com.yahoo.bard.webservice.web.PreResponse;
import com.yahoo.bard.webservice.web.ResponseData;
Expand Down Expand Up @@ -46,9 +47,9 @@
@Singleton
public class HttpResponseMaker {

private final ObjectMappersSuite objectMappers;
private final DimensionDictionary dimensionDictionary;
private final ResponseWriter responseWriter;
protected final ObjectMappersSuite objectMappers;
protected final DimensionDictionary dimensionDictionary;
protected final ResponseWriter responseWriter;

/**
* Class constructor.
Expand Down Expand Up @@ -144,7 +145,7 @@ private ResponseBuilder createResponseBuilder(
LinkedHashMap::new
));

ResponseData responseData = new ResponseData(
ResponseData responseData = buildResponseData(
resultSet,
(LinkedHashSet<String>) responseContext.get(API_METRIC_COLUMN_NAMES.getName()),
requestedApiDimensionFields,
Expand Down Expand Up @@ -202,4 +203,37 @@ public javax.ws.rs.core.Response buildErrorResponse(
objectMappers.getMapper().writer()
);
}

/**
* Builds a ResponseData object.
*
* @param resultSet ResultSet to turn into response
* @param apiMetricColumnNames The names of the logical metrics requested
* @param requestedApiDimensionFields The fields for each dimension that should be shown in the response
* @param partialIntervals intervals over which partial data exists
* @param volatileIntervals intervals over which data is understood as 'best-to-date'
* @param pagination The object containing the pagination information. Null if we are not paginating
* @param paginationLinks A mapping from link names to links to be added to the end of the JSON response
*
* @return a new ResponseData object
*/
protected ResponseData buildResponseData(
ResultSet resultSet,
LinkedHashSet<String> apiMetricColumnNames,
LinkedHashMap<Dimension, LinkedHashSet<DimensionField>> requestedApiDimensionFields,
SimplifiedIntervalList partialIntervals,
SimplifiedIntervalList volatileIntervals,
Pagination pagination,
Map<String, URI> paginationLinks
) {
return new ResponseData(
resultSet,
apiMetricColumnNames,
requestedApiDimensionFields,
partialIntervals,
volatileIntervals,
pagination,
paginationLinks
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@
*/
public class ResponseData {

private static final Logger LOG = LoggerFactory.getLogger(ResponseData.class);
private static final Map<Dimension, Map<DimensionField, String>> DIMENSION_FIELD_COLUMN_NAMES = new HashMap<>();
protected static final Logger LOG = LoggerFactory.getLogger(ResponseData.class);
protected static final Map<Dimension, Map<DimensionField, String>> DIMENSION_FIELD_COLUMN_NAMES = new HashMap<>();

private final ResultSet resultSet;
private final LinkedHashSet<MetricColumn> apiMetricColumns;
private final LinkedHashMap<Dimension, LinkedHashSet<DimensionField>> requestedApiDimensionFields;
private final SimplifiedIntervalList missingIntervals;
private final SimplifiedIntervalList volatileIntervals;
private final Pagination pagination;
private final Map<String, URI> paginationLinks;
protected final ResultSet resultSet;
protected final LinkedHashSet<MetricColumn> apiMetricColumns;
protected final LinkedHashMap<Dimension, LinkedHashSet<DimensionField>> requestedApiDimensionFields;
protected final SimplifiedIntervalList missingIntervals;
protected final SimplifiedIntervalList volatileIntervals;
protected final Pagination pagination;
protected final Map<String, URI> paginationLinks;

/**
* Constructor.
Expand Down Expand Up @@ -153,7 +153,7 @@ public LinkedHashSet<MetricColumn> getApiMetricColumns() {
*
* @return set of metric columns
*/
private LinkedHashSet<MetricColumn> generateApiMetricColumns(Set<String> apiMetricColumnNames) {
protected LinkedHashSet<MetricColumn> generateApiMetricColumns(Set<String> apiMetricColumnNames) {
// Get the metric columns from the schema
Map<String, MetricColumn> metricColumnMap = resultSet.getSchema().getColumns(MetricColumn.class).stream()
.collect(StreamUtils.toLinkedDictionary(MetricColumn::getName));
Expand Down Expand Up @@ -291,7 +291,7 @@ public List<String> buildIntervalStringList(Collection<Interval> intervals) {
*
* @return The name for the dimension and column as it will appear in the response document
*/
private static String getDimensionColumnName(Dimension dimension, DimensionField dimensionField) {
protected static String getDimensionColumnName(Dimension dimension, DimensionField dimensionField) {
Map<DimensionField, String> columnNamesForDimensionFields;
columnNamesForDimensionFields = DIMENSION_FIELD_COLUMN_NAMES.computeIfAbsent(
dimension,
Expand Down