1// Copyright 2012-2015 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
5package elastic
6
7// PercentilesBucketAggregation is a sibling pipeline aggregation which calculates
8// percentiles across all bucket of a specified metric in a sibling aggregation.
9// The specified metric must be numeric and the sibling aggregation must
10// be a multi-bucket aggregation.
11//
12// For more details, see
13// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-aggregations-pipeline-percentiles-bucket-aggregation.html
14type PercentilesBucketAggregation struct {
15	format       string
16	gapPolicy    string
17	percents     []float64
18	bucketsPaths []string
19
20	meta map[string]interface{}
21}
22
23// NewPercentilesBucketAggregation creates and initializes a new PercentilesBucketAggregation.
24func NewPercentilesBucketAggregation() *PercentilesBucketAggregation {
25	return &PercentilesBucketAggregation{}
26}
27
28// Format to apply the output value of this aggregation.
29func (p *PercentilesBucketAggregation) Format(format string) *PercentilesBucketAggregation {
30	p.format = format
31	return p
32}
33
34// Percents to calculate percentiles for in this aggregation.
35func (p *PercentilesBucketAggregation) Percents(percents ...float64) *PercentilesBucketAggregation {
36	p.percents = percents
37	return p
38}
39
40// GapPolicy defines what should be done when a gap in the series is discovered.
41// Valid values include "insert_zeros" or "skip". Default is "insert_zeros".
42func (p *PercentilesBucketAggregation) GapPolicy(gapPolicy string) *PercentilesBucketAggregation {
43	p.gapPolicy = gapPolicy
44	return p
45}
46
47// GapInsertZeros inserts zeros for gaps in the series.
48func (p *PercentilesBucketAggregation) GapInsertZeros() *PercentilesBucketAggregation {
49	p.gapPolicy = "insert_zeros"
50	return p
51}
52
53// GapSkip skips gaps in the series.
54func (p *PercentilesBucketAggregation) GapSkip() *PercentilesBucketAggregation {
55	p.gapPolicy = "skip"
56	return p
57}
58
59// Meta sets the meta data to be included in the aggregation response.
60func (p *PercentilesBucketAggregation) Meta(metaData map[string]interface{}) *PercentilesBucketAggregation {
61	p.meta = metaData
62	return p
63}
64
65// BucketsPath sets the paths to the buckets to use for this pipeline aggregator.
66func (p *PercentilesBucketAggregation) BucketsPath(bucketsPaths ...string) *PercentilesBucketAggregation {
67	p.bucketsPaths = append(p.bucketsPaths, bucketsPaths...)
68	return p
69}
70
71// Source returns the a JSON-serializable interface.
72func (p *PercentilesBucketAggregation) Source() (interface{}, error) {
73	source := make(map[string]interface{})
74	params := make(map[string]interface{})
75	source["percentiles_bucket"] = params
76
77	if p.format != "" {
78		params["format"] = p.format
79	}
80	if p.gapPolicy != "" {
81		params["gap_policy"] = p.gapPolicy
82	}
83
84	// Add buckets paths
85	switch len(p.bucketsPaths) {
86	case 0:
87	case 1:
88		params["buckets_path"] = p.bucketsPaths[0]
89	default:
90		params["buckets_path"] = p.bucketsPaths
91	}
92
93	// Add percents
94	if len(p.percents) > 0 {
95		params["percents"] = p.percents
96	}
97
98	// Add Meta data if available
99	if len(p.meta) > 0 {
100		source["meta"] = p.meta
101	}
102
103	return source, nil
104}
105