| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
// SerialDiffAggregation implements serial differencing. |
| 8 |
// Serial differencing is a technique where values in a time series are |
| 9 |
// subtracted from itself at different time lags or periods. |
| 10 |
// |
| 11 |
// For more details, see |
| 12 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-serialdiff-aggregation.html |
| 13 |
type SerialDiffAggregation struct { |
| 14 |
format string |
| 15 |
gapPolicy string |
| 16 |
lag *int |
| 17 |
|
| 18 |
subAggregations map[string]Aggregation |
| 19 |
meta map[string]interface{} |
| 20 |
bucketsPaths []string |
| 21 |
} |
| 22 |
|
| 23 |
// NewSerialDiffAggregation creates and initializes a new SerialDiffAggregation. |
| 24 |
func NewSerialDiffAggregation() *SerialDiffAggregation { |
| 25 |
return &SerialDiffAggregation{ |
| 26 |
subAggregations: make(map[string]Aggregation), |
| 27 |
bucketsPaths: make([]string, 0), |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func (a *SerialDiffAggregation) Format(format string) *SerialDiffAggregation { |
| 32 |
a.format = format |
| 33 |
return a |
| 34 |
} |
| 35 |
|
| 36 |
// GapPolicy defines what should be done when a gap in the series is discovered. |
| 37 |
// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". |
| 38 |
func (a *SerialDiffAggregation) GapPolicy(gapPolicy string) *SerialDiffAggregation { |
| 39 |
a.gapPolicy = gapPolicy |
| 40 |
return a |
| 41 |
} |
| 42 |
|
| 43 |
// GapInsertZeros inserts zeros for gaps in the series. |
| 44 |
func (a *SerialDiffAggregation) GapInsertZeros() *SerialDiffAggregation { |
| 45 |
a.gapPolicy = "insert_zeros" |
| 46 |
return a |
| 47 |
} |
| 48 |
|
| 49 |
// GapSkip skips gaps in the series. |
| 50 |
func (a *SerialDiffAggregation) GapSkip() *SerialDiffAggregation { |
| 51 |
a.gapPolicy = "skip" |
| 52 |
return a |
| 53 |
} |
| 54 |
|
| 55 |
// Lag specifies the historical bucket to subtract from the current value. |
| 56 |
// E.g. a lag of 7 will subtract the current value from the value 7 buckets |
| 57 |
// ago. Lag must be a positive, non-zero integer. |
| 58 |
func (a *SerialDiffAggregation) Lag(lag int) *SerialDiffAggregation { |
| 59 |
a.lag = &lag |
| 60 |
return a |
| 61 |
} |
| 62 |
|
| 63 |
// SubAggregation adds a sub-aggregation to this aggregation. |
| 64 |
func (a *SerialDiffAggregation) SubAggregation(name string, subAggregation Aggregation) *SerialDiffAggregation { |
| 65 |
a.subAggregations[name] = subAggregation |
| 66 |
return a |
| 67 |
} |
| 68 |
|
| 69 |
// Meta sets the meta data to be included in the aggregation response. |
| 70 |
func (a *SerialDiffAggregation) Meta(metaData map[string]interface{}) *SerialDiffAggregation { |
| 71 |
a.meta = metaData |
| 72 |
return a |
| 73 |
} |
| 74 |
|
| 75 |
// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. |
| 76 |
func (a *SerialDiffAggregation) BucketsPath(bucketsPaths ...string) *SerialDiffAggregation { |
| 77 |
a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) |
| 78 |
return a |
| 79 |
} |
| 80 |
|
| 81 |
func (a *SerialDiffAggregation) Source() (interface{}, error) { |
| 82 |
source := make(map[string]interface{}) |
| 83 |
params := make(map[string]interface{}) |
| 84 |
source["serial_diff"] = params |
| 85 |
|
| 86 |
if a.format != "" { |
| 87 |
params["format"] = a.format |
| 88 |
} |
| 89 |
if a.gapPolicy != "" { |
| 90 |
params["gap_policy"] = a.gapPolicy |
| 91 |
} |
| 92 |
if a.lag != nil { |
| 93 |
params["lag"] = *a.lag |
| 94 |
} |
| 95 |
|
| 96 |
// Add buckets paths |
| 97 |
switch len(a.bucketsPaths) { |
| 98 |
case 0: |
| 99 |
case 1: |
| 100 |
params["buckets_path"] = a.bucketsPaths[0] |
| 101 |
default: |
| 102 |
params["buckets_path"] = a.bucketsPaths |
| 103 |
} |
| 104 |
|
| 105 |
// AggregationBuilder (SubAggregations) |
| 106 |
if len(a.subAggregations) > 0 { |
| 107 |
aggsMap := make(map[string]interface{}) |
| 108 |
source["aggregations"] = aggsMap |
| 109 |
for name, aggregate := range a.subAggregations { |
| 110 |
src, err := aggregate.Source() |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
aggsMap[name] = src |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// Add Meta data if available |
| 119 |
if len(a.meta) > 0 { |
| 120 |
source["meta"] = a.meta |
| 121 |
} |
| 122 |
|
| 123 |
return source, nil |
| 124 |
} |