protected long aggregateMetricValue(MetricTimeSeries metricTimeSery) { long aggregateValue = 0L; for (TimeValue timeValue : metricTimeSery.getTimeValues()) { aggregateValue += timeValue.getValue(); } return aggregateValue; } }
@Override protected long limitedInterpolate(TimeValue start, TimeValue end, long ts) { return (ts < end.getTimestamp()) ? start.getValue() : end.getValue(); } }
@Override public TimeValue apply(TimeValue input) { return new TimeValue(input.getTimestamp(), input.getValue()); } }).iterator();
protected List<TimeValue> timeValues(long... longs) { List<TimeValue> timeValues = Lists.newArrayList(); for (int i = 0; i < longs.length; i += 2) { timeValues.add(new TimeValue(longs[i], longs[i + 1])); } return timeValues; }
@Override public long interpolate(TimeValue start, TimeValue end, long ts) { if (start == null) { throw new NullPointerException("start cannot be null"); } if (end == null) { throw new NullPointerException("end cannot be null"); } if (ts > end.getTimestamp() || ts < start.getTimestamp()) { throw new IllegalArgumentException("ts must be within given start and end"); } // if its been too many seconds between datapoints, return a 0 for everything in between. if ((end.getTimestamp() - start.getTimestamp()) > maxAllowedGap) { return 0; } return limitedInterpolate(start, end, ts); }
private List<TimeValue> timeValues(long ts, int resolution, long... values) { List<TimeValue> timeValues = Lists.newArrayList(); for (int i = 0; i < values.length; i++) { timeValues.add(new TimeValue(ts + i * resolution, values[i])); } return timeValues; }
InterpolatedAggregatorIterator() { timeseries = new BiDirectionalPeekingIterator(Iterators.peekingIterator(timeSeries.iterator())); if (timeseries.hasNext()) { currentTs = timeseries.peek().getTimestamp(); } }
private long getTotals(MetricDataQuery query) throws Exception { // query must have resolution set to Integer.MAX_VALUE (i.e. "totals") Collection<MetricTimeSeries> result = metricStore.query(query); if (result.size() == 0) { return 0; } // since there's no group by condition, it'll return single time series always MetricTimeSeries timeSeries = result.iterator().next(); if (timeSeries.getTimeValues().isEmpty()) { return 0; } // since it is totals, it will have only one TimeValue or none return timeSeries.getTimeValues().get(0).getValue(); }
@Override protected long limitedInterpolate(TimeValue start, TimeValue end, long ts) { long deltaX = ts - start.getTimestamp(); long totalX = end.getTimestamp() - start.getTimestamp(); long totalY = end.getValue() - start.getValue(); long deltaY = (int) (totalY * deltaX / totalX); return start.getValue() + deltaY; } }
private Collection<TimeSeries> convertToQueryResult(CubeQuery query, Table<Map<String, String>, String, Map<Long, Long>> resultTable) { List<TimeSeries> result = Lists.newArrayList(); // iterating each groupValue dimensions for (Map.Entry<Map<String, String>, Map<String, Map<Long, Long>>> row : resultTable.rowMap().entrySet()) { // iterating each measure for (Map.Entry<String, Map<Long, Long>> measureEntry : row.getValue().entrySet()) { // generating time series for a grouping and a measure int count = 0; List<TimeValue> timeValues = Lists.newArrayList(); for (Map.Entry<Long, Long> timeValue : measureEntry.getValue().entrySet()) { timeValues.add(new TimeValue(timeValue.getKey(), timeValue.getValue())); } Collections.sort(timeValues); PeekingIterator<TimeValue> timeValueItor = Iterators.peekingIterator( new TimeSeriesInterpolator(timeValues, query.getInterpolator(), query.getResolution()).iterator()); List<TimeValue> resultTimeValues = Lists.newArrayList(); while (timeValueItor.hasNext()) { TimeValue timeValue = timeValueItor.next(); resultTimeValues.add(new TimeValue(timeValue.getTimestamp(), timeValue.getValue())); if (++count >= query.getLimit()) { break; } } result.add(new TimeSeries(measureEntry.getKey(), row.getKey(), resultTimeValues)); } } return result; }
TimeValue timeValue = new TimeValue(ts, Bytes.toLong(columnValue.getValue())); timeValues.add(timeValue);
InterpolatedAggregatorIterator() { timeseries = new BiDirectionalPeekingIterator(Iterators.peekingIterator(timeSeries.iterator())); if (timeseries.hasNext()) { currentTs = timeseries.peek().getTimestamp(); } }
private long getSingleValueFromTotals(MetricDataQuery query) { try { Collection<MetricTimeSeries> result = metricStore.query(query); if (result.isEmpty()) { return 0; } // since it is totals query and not groupBy specified, we know there's one time series List<TimeValue> timeValues = result.iterator().next().getTimeValues(); if (timeValues.isEmpty()) { return 0; } // since it is totals, we know there's one value only return timeValues.get(0).getValue(); } catch (Exception e) { throw Throwables.propagate(e); } } }
private MetricQueryResult.TimeValue[] decorate(List<TimeValue> points) { MetricQueryResult.TimeValue[] timeValues = new MetricQueryResult.TimeValue[points.size()]; int k = 0; for (TimeValue timeValue : points) { timeValues[k++] = new MetricQueryResult.TimeValue(timeValue.getTimestamp(), timeValue.getValue()); } return timeValues; }
private Collection<TimeSeries> convertToQueryResult(CubeQuery query, Table<Map<String, String>, String, Map<Long, Long>> resultTable) { List<TimeSeries> result = Lists.newArrayList(); // iterating each groupValue dimensions for (Map.Entry<Map<String, String>, Map<String, Map<Long, Long>>> row : resultTable.rowMap().entrySet()) { // iterating each measure for (Map.Entry<String, Map<Long, Long>> measureEntry : row.getValue().entrySet()) { // generating time series for a grouping and a measure int count = 0; List<TimeValue> timeValues = Lists.newArrayList(); for (Map.Entry<Long, Long> timeValue : measureEntry.getValue().entrySet()) { timeValues.add(new TimeValue(timeValue.getKey(), timeValue.getValue())); } Collections.sort(timeValues); PeekingIterator<TimeValue> timeValueItor = Iterators.peekingIterator( new TimeSeriesInterpolator(timeValues, query.getInterpolator(), query.getResolution()).iterator()); List<TimeValue> resultTimeValues = Lists.newArrayList(); while (timeValueItor.hasNext()) { TimeValue timeValue = timeValueItor.next(); resultTimeValues.add(new TimeValue(timeValue.getTimestamp(), timeValue.getValue())); if (++count >= query.getLimit()) { break; } } result.add(new TimeSeries(measureEntry.getKey(), row.getKey(), resultTimeValues)); } } return result; }
TimeValue timeValue = new TimeValue(ts, Bytes.toLong(columnValue.getValue())); timeValues.add(timeValue);
private long getSingleValueFromTotals(MetricDataQuery query) { try { Collection<MetricTimeSeries> result = metricStore.query(query); if (result.isEmpty()) { return 0; } // since it is totals query and not groupBy specified, we know there's one time series List<TimeValue> timeValues = result.iterator().next().getTimeValues(); if (timeValues.isEmpty()) { return 0; } // since it is totals, we know there's one value only return timeValues.get(0).getValue(); } catch (Exception e) { throw Throwables.propagate(e); } } }
private MetricQueryResult.TimeValue[] decorate(List<TimeValue> points) { MetricQueryResult.TimeValue[] timeValues = new MetricQueryResult.TimeValue[points.size()]; int k = 0; for (TimeValue timeValue : points) { timeValues[k++] = new MetricQueryResult.TimeValue(timeValue.getTimestamp(), timeValue.getValue()); } return timeValues; }
@Override protected TimeValue computeNext() { long currentTsValue = 0; // no more data points in the timeseries if (!timeseries.hasNext()) { return endOfData(); } // move the iterator to the next point in this timeseries if this is an actual data point and not interpolated. if (timeseries.peek().getTimestamp() == currentTs) { currentTsValue += timeseries.peek().getValue(); timeseries.next(); } else if (interpolator != null && timeseries.peekBefore() != null) { // don't interpolate unless we're in between data points currentTsValue += interpolator.interpolate(timeseries.peekBefore(), timeseries.peek(), currentTs); } TimeValue output = new TimeValue(currentTs, currentTsValue); if (timeseries.hasNext()) { // increment the currentTs by resolution to get the next data point. currentTs = (interpolator == null) ? timeseries.peek().getTimestamp() : currentTs + resolution; } return output; }
@Test public void testInterpolateLimit() { long limit = 20; Interpolator interpolator = new Interpolators.Step(limit); TimeValue start = new TimeValue(0, 10); TimeValue end = new TimeValue(limit + 1, 50); // time between points is greater than this limit, values in between should be 0 for (int i = 1; i < 1 + limit; i++) { Assert.assertEquals(0, interpolator.interpolate(start, end, i)); } // time between points is not greater than the limit, values in between should be interpolated end = new TimeValue(limit, 50); for (int i = 1; i < limit; i++) { Assert.assertEquals(10, interpolator.interpolate(start, end, i)); } } }