|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.aggregations.bucket.timeseries; |
| 10 | + |
| 11 | +import org.apache.lucene.util.BytesRef; |
| 12 | +import org.elasticsearch.index.TimestampBounds; |
| 13 | +import org.elasticsearch.search.aggregations.AggregationExecutionContext; |
| 14 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 15 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 16 | +import org.elasticsearch.search.aggregations.CardinalityUpperBound; |
| 17 | +import org.elasticsearch.search.aggregations.InternalAggregation; |
| 18 | +import org.elasticsearch.search.aggregations.LeafBucketCollector; |
| 19 | +import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; |
| 20 | +import org.elasticsearch.search.aggregations.bucket.BucketsAggregator; |
| 21 | +import org.elasticsearch.search.aggregations.bucket.histogram.TimestampBoundsAware; |
| 22 | +import org.elasticsearch.search.aggregations.support.AggregationContext; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +public class TimeSeriesAggregator2 extends BucketsAggregator { |
| 31 | + |
| 32 | + private final TimestampBounds timestampBounds; |
| 33 | + private final TimestampBoundsAware parent; |
| 34 | + private final Map<Long, List<InternalTimeSeries.InternalBucket>> results; |
| 35 | + private final boolean keyed; |
| 36 | + |
| 37 | + public TimeSeriesAggregator2( |
| 38 | + String name, |
| 39 | + AggregatorFactories factories, |
| 40 | + boolean keyed, |
| 41 | + AggregationContext context, |
| 42 | + Aggregator parent, |
| 43 | + Map<String, Object> metadata |
| 44 | + ) throws IOException { |
| 45 | + super(name, factories, context, parent, CardinalityUpperBound.ONE, metadata); |
| 46 | + this.keyed = keyed; |
| 47 | + this.timestampBounds = context.getIndexSettings().getTimestampBounds(); |
| 48 | + this.parent = (TimestampBoundsAware) parent; |
| 49 | + this.results = new HashMap<>(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws IOException { |
| 54 | + // figure out running pipeline aggs here |
| 55 | + // context.pipelineTreeRoot().subTree(agg.getName()) |
| 56 | + completeBucket(); |
| 57 | + InternalAggregation[] result = new InternalAggregation[owningBucketOrds.length]; |
| 58 | + for (int ordIdx = 0; ordIdx < owningBucketOrds.length; ordIdx++) { |
| 59 | + long owningOrdinal = owningBucketOrds[ordIdx]; |
| 60 | + List<InternalTimeSeries.InternalBucket> buckets = results.get(owningOrdinal); |
| 61 | + if (buckets == null) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + result[ordIdx] = new InternalTimeSeries(name, buckets, keyed, metadata()); |
| 65 | + } |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public InternalAggregation buildEmptyAggregation() { |
| 71 | + return new InternalTimeSeries(name, new ArrayList<>(), false, metadata()); |
| 72 | + } |
| 73 | + |
| 74 | + BytesRef currentTsid; |
| 75 | + int currentTsidOrd = -1; |
| 76 | + long currentParentBucket; |
| 77 | + long docCount; |
| 78 | + |
| 79 | + @Override |
| 80 | + protected LeafBucketCollector getLeafCollector(AggregationExecutionContext aggCtx, LeafBucketCollector sub) throws IOException { |
| 81 | + return new LeafBucketCollectorBase(sub, null) { |
| 82 | + |
| 83 | + @Override |
| 84 | + public void collect(int doc, long bucket) throws IOException { |
| 85 | + // System.out.println("bucketId=" + bucket); |
| 86 | + // System.out.println("tsid=" + TimeSeriesIdFieldMapper.decodeTsid(aggCtx.getTsid())); |
| 87 | + |
| 88 | + if (currentTsidOrd == aggCtx.getTsidOrd() && currentParentBucket == bucket) { |
| 89 | + docCount++; |
| 90 | + sub.collect(doc, 0L); |
| 91 | + return; |
| 92 | + } |
| 93 | + if (currentTsid != null) { |
| 94 | + completeBucket(); |
| 95 | + } |
| 96 | + if (currentTsidOrd != aggCtx.getTsidOrd()) { |
| 97 | + currentTsidOrd = aggCtx.getTsidOrd(); |
| 98 | + currentTsid = BytesRef.deepCopyOf(aggCtx.getTsid()); |
| 99 | + } |
| 100 | + if (currentParentBucket != bucket) { |
| 101 | + currentParentBucket = bucket; |
| 102 | + } |
| 103 | + |
| 104 | + sub.clear(); |
| 105 | + docCount = 1; |
| 106 | + sub.collect(doc, 0L); |
| 107 | + } |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + private void completeBucket() throws IOException { |
| 112 | + InternalTimeSeries.InternalBucket bucket = new InternalTimeSeries.InternalBucket( |
| 113 | + currentTsid, |
| 114 | + docCount, |
| 115 | + buildSubAggsForBuckets(new long[] { 0L })[0], |
| 116 | + keyed |
| 117 | + ); |
| 118 | + // System.out.println("complete bucket=" + currentParentBucket); |
| 119 | + List<InternalTimeSeries.InternalBucket> result = results.get(currentParentBucket); |
| 120 | + if (result == null) { |
| 121 | + result = new ArrayList<>(); |
| 122 | + results.put(currentParentBucket, result); |
| 123 | + } |
| 124 | + result.add(bucket); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments