Debian Codegraph @

search_aggs_pipeline_derivative.go

raw
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 // DerivativeAggregation is a parent pipeline aggregation which calculates
8 // the derivative of a specified metric in a parent histogram (or date_histogram)
9 // aggregation. The specified metric must be numeric and the enclosing
10 // histogram must have min_doc_count set to 0 (default for histogram aggregations).
11 //
12 // For more details, see
13 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-derivative-aggregation.html
14 type DerivativeAggregation struct {
15 format string
16 gapPolicy string
17 unit string
18
19 subAggregations map[string]Aggregation
20 meta map[string]interface{}
21 bucketsPaths []string
22 }
23
24 // NewDerivativeAggregation creates and initializes a new DerivativeAggregation.
25 func NewDerivativeAggregation() *DerivativeAggregation {
26 return &DerivativeAggregation{
27 subAggregations: make(map[string]Aggregation),
28 bucketsPaths: make([]string, 0),
29 }
30 }
31
32 func (a *DerivativeAggregation) Format(format string) *DerivativeAggregation {
33 a.format = format
34 return a
35 }
36
37 // GapPolicy defines what should be done when a gap in the series is discovered.
38 // Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
39 func (a *DerivativeAggregation) GapPolicy(gapPolicy string) *DerivativeAggregation {
40 a.gapPolicy = gapPolicy
41 return a
42 }
43
44 // GapInsertZeros inserts zeros for gaps in the series.
45 func (a *DerivativeAggregation) GapInsertZeros() *DerivativeAggregation {
46 a.gapPolicy = "insert_zeros"
47 return a
48 }
49
50 // GapSkip skips gaps in the series.
51 func (a *DerivativeAggregation) GapSkip() *DerivativeAggregation {
52 a.gapPolicy = "skip"
53 return a
54 }
55
56 // Unit sets the unit provided, e.g. "1d" or "1y".
57 // It is only useful when calculating the derivative using a date_histogram.
58 func (a *DerivativeAggregation) Unit(unit string) *DerivativeAggregation {
59 a.unit = unit
60 return a
61 }
62
63 // SubAggregation adds a sub-aggregation to this aggregation.
64 func (a *DerivativeAggregation) SubAggregation(name string, subAggregation Aggregation) *DerivativeAggregation {
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 *DerivativeAggregation) Meta(metaData map[string]interface{}) *DerivativeAggregation {
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 *DerivativeAggregation) BucketsPath(bucketsPaths ...string) *DerivativeAggregation {
77 a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...)
78 return a
79 }
80
81 func (a *DerivativeAggregation) Source() (interface{}, error) {
82 source := make(map[string]interface{})
83 params := make(map[string]interface{})
84 source["derivative"] = 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.unit != "" {
93 params["unit"] = a.unit
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 }