/** * Expand the result by adding a new metric column with the row number as the metric value. * * @param result Result to expand * @param metricColumn New column to add * @param rowNum Row number to use as the metric value * * @return the expanded Result */ private Result rowNumMap(Result result, MetricColumn metricColumn, int rowNum) { Map<MetricColumn, Object> metricValues = new LinkedHashMap<>(result.getMetricValues()); metricValues.put(metricColumn, BigDecimal.valueOf(rowNum)); return new Result(result.getDimensionRows(), metricValues, result.getTimeStamp()); } }
@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); }
/** * Copy the result, adding or replacing the value of a metric * <p> * Don't add a without column method because removing columns is far less safe. * * @param metricColumn The metric column * @param value The new value for that column * * @return A new result based on this one */ public Result withMetricValue(MetricColumn metricColumn, Object value) { Map<MetricColumn, Object> newMetricValues = new LinkedHashMap<>(metricValues); newMetricValues.put(metricColumn, value); return new Result(dimensionRows, newMetricValues, timeStamp); }
row.put("dateTime", result.getTimeStamp().toString(DateTimeFormatterFactory.getOutputFormatter())); Map<DimensionColumn, DimensionRow> dr = result.getDimensionRows(); for (Entry<DimensionColumn, DimensionRow> dce : dr.entrySet()) { DimensionRow drow = dce.getValue(); row.put(apiMetricColumn.getName(), result.getMetricValue(apiMetricColumn));
@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 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 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 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; }
/** * Generates the metric names and their value map from Result for custom serialization. * * @param result Result object for the custom serialization * * @return custom map of metric names and their values */ private Map<String, Object> getMetricValues(Result result) { return result.getMetricValues().entrySet().stream().collect(Collectors.toMap( metricColumn -> metricColumn.getKey().getName(), metricColumn -> metricColumn.getValue() )); } }
/** * Generates the dimension names and its unique id map from Result for custom serialization. * * @param result Result object for the custom serialization * * @return custom map of dimension names and their respective unique id */ private Map<String, String> getDimensionValues(Result result) { return result.getDimensionRows().entrySet().stream().collect(Collectors.toMap( columnRow -> columnRow.getKey().getName(), columnRow -> columnRow.getValue().get(columnRow.getKey().getDimension().getKey()) )); }
/** * Create a list of results from a JsonNode of a timeseries response. * * @param jsonResult current record to parse * @param metricColumns set of metric columns * @param dateTimeZone The date time zone to apply to timestamps * * @return list of results */ private List<Result> makeTimeSeriesResults( JsonNode jsonResult, Set<MetricColumn> metricColumns, DateTimeZone dateTimeZone ) { List<Result> results = new ArrayList<>(); /* loop over all records */ for (JsonNode record : jsonResult) { DateTime timeStamp = new DateTime(record.get("timestamp").asText(), dateTimeZone); JsonNode result = record.get("result"); LinkedHashMap<MetricColumn, Object> metricValues = extractMetricValues(metricColumns, result); results.add(new Result(new LinkedHashMap<>(), metricValues, timeStamp)); } return results; }
row.put("dateTime", result.getTimeStamp().toString(DateTimeFormatterFactory.getOutputFormatter())); Map<DimensionColumn, DimensionRow> dr = result.getDimensionRows(); for (Entry<DimensionColumn, DimensionRow> dimensionColumnEntry : dr.entrySet()) { row.put(apiMetricColumn.getName(), result.getMetricValue(apiMetricColumn));
@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; }
/** * 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 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 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; }
/** * Get type of class type of metric values mapped with metric names. * * @return Map of metric names and its value type */ @JsonIgnore public Map<String, String> getMetricValuesType() { return result.getMetricValues().entrySet().stream() .filter(metricValue -> metricValue != null) .collect(Collectors.toMap(k -> k.getKey().getName(), v -> getType(v.getValue()))); }
/** * Create a list of results from a JsonNode of a groupBy response. * * @param jsonResult current results to parse in json * @param dimensionColumns set of dimension columns * @param metricColumns set of metric columns * @param dateTimeZone The date time zone to apply to timestamps * * @return list of results */ private List<Result> makeGroupByResults( JsonNode jsonResult, Set<DimensionColumn> dimensionColumns, Set<MetricColumn> metricColumns, DateTimeZone dateTimeZone ) { List<Result> results = new ArrayList<>(); for (JsonNode record : jsonResult) { DateTime timeStamp = new DateTime(record.get("timestamp").asText(), dateTimeZone); JsonNode event = record.get("event"); LinkedHashMap<DimensionColumn, DimensionRow> dimensionRows = extractDimensionRows(dimensionColumns, event); LinkedHashMap<MetricColumn, Object> metricValues = extractMetricValues(metricColumns, event); results.add(new Result(dimensionRows, metricValues, timeStamp)); } return results; }