@Override public void onNext(PreResponse preResponse) { publishResponse(httpResponseMaker.buildResponse(preResponse, apiRequest, containerRequestContext)); }
/** * Builds the headers for the response and gives them to the provided response builder to be added to the response. * * @param rspBuilder ResponseBuilder that handles adding the headers to the response * @param responseFormatType The type of the response * @param containerRequestContext The request context * * @return the response builder that has had the headers added */ protected ResponseBuilder buildAndAddResponseHeaders( ResponseBuilder rspBuilder, ResponseFormatType responseFormatType, ContainerRequestContext containerRequestContext ) { return buildAndAddResponseHeaders(rspBuilder, responseFormatType, containerRequestContext, null); }
/** * Constructor. * * @param result Result row from a result set */ public ResultSerializationProxy(Result result) { this.result = result; this.dimensionValues = getDimensionValues(result); this.metricValues = getMetricValues(result); this.timeStamp = result.getTimeStamp().toString(); }
@Override protected Result map(Result result, ResultSetSchema schema) { MetricColumn stringColumn = schema.getColumn(A_STRING_METRIC.asName(), MetricColumn.class) .orElseThrow( () -> new IllegalStateException(UNEXPECTED_MISSING_COLUMN + A_STRING_METRIC.asName()) ); String stringValue = result.getMetricValueAsString(stringColumn); return result.withMetricValue(stringColumn, stringValue + stringValue); }
@Override protected Result map(Result result, ResultSetSchema schema) { MetricColumn column = schema.getColumn(A_NULL_METRIC.asName(), MetricColumn.class).orElseThrow( () -> new IllegalStateException(UNEXPECTED_MISSING_COLUMN + A_NULL_METRIC.asName()) ); Object nullMetric = result.getMetricValue(column); if (nullMetric != null) { throw new IllegalStateException( String.format("Metric 'nullMetric' should be null but is: %s", nullMetric) ); } return result; }
@Override public ResultSet map(ResultSet resultSet) { // TODO: Use only native stream operations in RxJava: GroupByTime -> Sort -> Take N -> Concat streams by time TopNAccumulator acc = new TopNAccumulator(); resultSet.stream().forEachOrdered(acc); return new ResultSet(resultSet.getSchema(), acc.data); }
/** * Creates new Result object from JsonNode. * * @param serializedResult JsonNode which contains all the serialized details to generate Result object * @param resultSetSchema Schema of the result to generate the Result object * * @return Result object generated from given JsonNode */ private Result getResult(JsonNode serializedResult, ResultSetSchema resultSetSchema) { return new Result( extractDimensionValues( serializedResult.get(DIMENSION_VALUES_KEY), resultSetSchema.getColumns(DimensionColumn.class) ), extractMetricValues( serializedResult.get(METRIC_VALUES_KEY), resultSetSchema.getColumns(MetricColumnWithValueType.class) ), DateTime.parse(serializedResult.get(TIMESTAMP_KEY).asText()) ); }
@Override protected Result map(Result result, ResultSetSchema schema) { if (columnName == null) { throw new IllegalStateException("Cannot map results without a column name"); } MetricColumn metricColumn = schema.getColumn(columnName, MetricColumn.class).orElseThrow( () -> new IllegalStateException("Unexpected missing column: " + columnName) ); BigDecimal value = result.getMetricValueAsNumber(metricColumn); if (value == null) { return result; } BigDecimal newValue = value.setScale(0, RoundingMode.CEILING); return result.withMetricValue(metricColumn, newValue); }
@Override protected Result map(Result result, ResultSetSchema schema) { MetricColumn column = schema.getColumn(A_BOOLEAN_METRIC.asName(), MetricColumn.class).orElseThrow( () -> new IllegalStateException(UNEXPECTED_MISSING_COLUMN + A_BOOLEAN_METRIC.asName()) ); return result.getMetricValueAsBoolean(column) ? result : null; }
/** * Create a new result set with an additional final column. * * @param column the column being added * * @return the result set being constructed */ public ResultSetSchema withAddColumn(Column column) { LinkedHashSet<Column> columns = new LinkedHashSet<>(this.getColumns()); columns.add(column); return new ResultSetSchema(this.getGranularity(), columns); } }
/** * Build a result set using the api request time grain. * * @param json The json representing the druid response. * @param druidQuery The druid query being processed * @param dateTimeZone The date time zone for parsing result rows * * @return The initial result set from the json node. */ public ResultSet buildResultSet(JsonNode json, DruidAggregationQuery<?> druidQuery, DateTimeZone dateTimeZone) { LinkedHashSet<Column> columns = druidResponseParser.buildSchemaColumns(druidQuery) .collect(Collectors.toCollection(LinkedHashSet::new)); ResultSetSchema resultSetSchema = new ResultSetSchema(granularity, columns); return druidResponseParser.parse(json, resultSetSchema, druidQuery.getQueryType(), dateTimeZone); } }
/** * Generates ResultSet object from the JsonNode which contains the serialized ResultSet. * * @param serializedResultSet JsonNode which contains the serialized ResultSet * * @return ResultSet object generated from JsonNode */ private ResultSet getResultSet(JsonNode serializedResultSet) { ResultSetSchema resultSetSchema = getResultSetSchema(serializedResultSet.get(SCHEMA_KEY)); List<Result> results = Streams.stream(serializedResultSet.get(RESULTS_KEY)) .map(serializedResult -> getResult(serializedResult, resultSetSchema)) .collect(Collectors.toList()); return new ResultSet(resultSetSchema, results); }
@Override protected Result map(Result result, ResultSetSchema schema) { MetricColumn column = schema.getColumn(A_JSON_NODE_METRIC.asName(), MetricColumn.class).orElseThrow( () -> new IllegalStateException(UNEXPECTED_MISSING_COLUMN + A_JSON_NODE_METRIC.asName()) ); ObjectNode node = (ObjectNode) result.getMetricValueAsJsonNode(column); node.put("length", node.get("clarification").textValue().length()); return result; }
@Override protected ResultSetSchema map(ResultSetSchema schema) { return schema.withAddColumn(new MetricColumn(ROW_NUM_COLUMN_NAME)); }
/** * Constructor. * * @param preResponse PreResponse object to be serialized * @param responseContextMapper ObjectMapper instance with custom configuration to preserve the types */ public PreResponseSerializationProxy(PreResponse preResponse, ObjectMapper responseContextMapper) { this.resultSetSerializationProxy = new ResultSetSerializationProxy(preResponse.getResultSet()); this.responseContext = getSerializedResponseContext(preResponse.getResponseContext(), responseContextMapper); }
/** * Initialize the PreResponse deserializer to deserialize the custom serialized PreResponse. * * @param dimensionDictionary Contains all the dimension details * @param nonResponseContextMapper ObjectMapper instance without any customization * @param responseContextMapper ObjectMapper instance with customization to store/retrieve the value type * @param granularityParser Granularity parser to get the time id * * @return instance of PreResponse deserializer */ protected PreResponseDeserializer initializePreResponseDeserializer( DimensionDictionary dimensionDictionary, ObjectMapper nonResponseContextMapper, ObjectMapper responseContextMapper, GranularityParser granularityParser ) { return new PreResponseDeserializer( dimensionDictionary, nonResponseContextMapper, responseContextMapper, granularityParser ); }
@Override public void accept(Result result) { DateTime timestamp = result.getTimeStamp(); if (!timestamp.equals(recentTimeStamp)) { filledBuckets = 0; recentTimeStamp = timestamp; } if (filledBuckets++ < topN) { data.add(result); } } }
@Override protected Result map(Result result, ResultSetSchema schema) { MetricColumn stringColumn = schema.getColumn(A_STRING_METRIC.asName(), MetricColumn.class) .orElseThrow( () -> new IllegalStateException(UNEXPECTED_MISSING_COLUMN + A_STRING_METRIC.asName()) ); String stringValue = result.getMetricValueAsString(stringColumn); return result.withMetricValue(stringColumn, stringValue + stringValue); }
@Override protected Result map(Result result, ResultSetSchema schema) { MetricColumn column = schema.getColumn(A_NULL_METRIC.asName(), MetricColumn.class).orElseThrow( () -> new IllegalStateException(UNEXPECTED_MISSING_COLUMN + A_NULL_METRIC.asName()) ); Object nullMetric = result.getMetricValue(column); if (nullMetric != null) { throw new IllegalStateException( String.format("Metric 'nullMetric' should be null but is: %s", nullMetric) ); } return result; }
@Override protected Result map(Result result, ResultSetSchema schema) { MetricColumn column = schema.getColumn(A_BOOLEAN_METRIC.asName(), MetricColumn.class).orElseThrow( () -> new IllegalStateException(UNEXPECTED_MISSING_COLUMN + A_BOOLEAN_METRIC.asName()) ); return result.getMetricValueAsBoolean(column) ? result : null; }