| 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 |
// MovAvgAggregation operates on a series of data. It will slide a window |
| 8 |
// across the data and emit the average value of that window. |
| 9 |
// |
| 10 |
// For more details, see |
| 11 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html |
| 12 |
type MovAvgAggregation struct { |
| 13 |
format string |
| 14 |
gapPolicy string |
| 15 |
model MovAvgModel |
| 16 |
window *int |
| 17 |
predict *int |
| 18 |
minimize *bool |
| 19 |
|
| 20 |
subAggregations map[string]Aggregation |
| 21 |
meta map[string]interface{} |
| 22 |
bucketsPaths []string |
| 23 |
} |
| 24 |
|
| 25 |
// NewMovAvgAggregation creates and initializes a new MovAvgAggregation. |
| 26 |
func NewMovAvgAggregation() *MovAvgAggregation { |
| 27 |
return &MovAvgAggregation{ |
| 28 |
subAggregations: make(map[string]Aggregation), |
| 29 |
bucketsPaths: make([]string, 0), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func (a *MovAvgAggregation) Format(format string) *MovAvgAggregation { |
| 34 |
a.format = format |
| 35 |
return a |
| 36 |
} |
| 37 |
|
| 38 |
// GapPolicy defines what should be done when a gap in the series is discovered. |
| 39 |
// Valid values include "insert_zeros" or "skip". Default is "insert_zeros". |
| 40 |
func (a *MovAvgAggregation) GapPolicy(gapPolicy string) *MovAvgAggregation { |
| 41 |
a.gapPolicy = gapPolicy |
| 42 |
return a |
| 43 |
} |
| 44 |
|
| 45 |
// GapInsertZeros inserts zeros for gaps in the series. |
| 46 |
func (a *MovAvgAggregation) GapInsertZeros() *MovAvgAggregation { |
| 47 |
a.gapPolicy = "insert_zeros" |
| 48 |
return a |
| 49 |
} |
| 50 |
|
| 51 |
// GapSkip skips gaps in the series. |
| 52 |
func (a *MovAvgAggregation) GapSkip() *MovAvgAggregation { |
| 53 |
a.gapPolicy = "skip" |
| 54 |
return a |
| 55 |
} |
| 56 |
|
| 57 |
// Model is used to define what type of moving average you want to use |
| 58 |
// in the series. |
| 59 |
func (a *MovAvgAggregation) Model(model MovAvgModel) *MovAvgAggregation { |
| 60 |
a.model = model |
| 61 |
return a |
| 62 |
} |
| 63 |
|
| 64 |
// Window sets the window size for the moving average. This window will |
| 65 |
// "slide" across the series, and the values inside that window will |
| 66 |
// be used to calculate the moving avg value. |
| 67 |
func (a *MovAvgAggregation) Window(window int) *MovAvgAggregation { |
| 68 |
a.window = &window |
| 69 |
return a |
| 70 |
} |
| 71 |
|
| 72 |
// Predict sets the number of predictions that should be returned. |
| 73 |
// Each prediction will be spaced at the intervals in the histogram. |
| 74 |
// E.g. a predict of 2 will return two new buckets at the end of the |
| 75 |
// histogram with the predicted values. |
| 76 |
func (a *MovAvgAggregation) Predict(numPredictions int) *MovAvgAggregation { |
| 77 |
a.predict = &numPredictions |
| 78 |
return a |
| 79 |
} |
| 80 |
|
| 81 |
// Minimize determines if the model should be fit to the data using a |
| 82 |
// cost minimizing algorithm. |
| 83 |
func (a *MovAvgAggregation) Minimize(minimize bool) *MovAvgAggregation { |
| 84 |
a.minimize = &minimize |
| 85 |
return a |
| 86 |
} |
| 87 |
|
| 88 |
// SubAggregation adds a sub-aggregation to this aggregation. |
| 89 |
func (a *MovAvgAggregation) SubAggregation(name string, subAggregation Aggregation) *MovAvgAggregation { |
| 90 |
a.subAggregations[name] = subAggregation |
| 91 |
return a |
| 92 |
} |
| 93 |
|
| 94 |
// Meta sets the meta data to be included in the aggregation response. |
| 95 |
func (a *MovAvgAggregation) Meta(metaData map[string]interface{}) *MovAvgAggregation { |
| 96 |
a.meta = metaData |
| 97 |
return a |
| 98 |
} |
| 99 |
|
| 100 |
// BucketsPath sets the paths to the buckets to use for this pipeline aggregator. |
| 101 |
func (a *MovAvgAggregation) BucketsPath(bucketsPaths ...string) *MovAvgAggregation { |
| 102 |
a.bucketsPaths = append(a.bucketsPaths, bucketsPaths...) |
| 103 |
return a |
| 104 |
} |
| 105 |
|
| 106 |
func (a *MovAvgAggregation) Source() (interface{}, error) { |
| 107 |
source := make(map[string]interface{}) |
| 108 |
params := make(map[string]interface{}) |
| 109 |
source["moving_avg"] = params |
| 110 |
|
| 111 |
if a.format != "" { |
| 112 |
params["format"] = a.format |
| 113 |
} |
| 114 |
if a.gapPolicy != "" { |
| 115 |
params["gap_policy"] = a.gapPolicy |
| 116 |
} |
| 117 |
if a.model != nil { |
| 118 |
params["model"] = a.model.Name() |
| 119 |
settings := a.model.Settings() |
| 120 |
if len(settings) > 0 { |
| 121 |
params["settings"] = settings |
| 122 |
} |
| 123 |
} |
| 124 |
if a.window != nil { |
| 125 |
params["window"] = *a.window |
| 126 |
} |
| 127 |
if a.predict != nil { |
| 128 |
params["predict"] = *a.predict |
| 129 |
} |
| 130 |
if a.minimize != nil { |
| 131 |
params["minimize"] = *a.minimize |
| 132 |
} |
| 133 |
|
| 134 |
// Add buckets paths |
| 135 |
switch len(a.bucketsPaths) { |
| 136 |
case 0: |
| 137 |
case 1: |
| 138 |
params["buckets_path"] = a.bucketsPaths[0] |
| 139 |
default: |
| 140 |
params["buckets_path"] = a.bucketsPaths |
| 141 |
} |
| 142 |
|
| 143 |
// AggregationBuilder (SubAggregations) |
| 144 |
if len(a.subAggregations) > 0 { |
| 145 |
aggsMap := make(map[string]interface{}) |
| 146 |
source["aggregations"] = aggsMap |
| 147 |
for name, aggregate := range a.subAggregations { |
| 148 |
src, err := aggregate.Source() |
| 149 |
if err != nil { |
| 150 |
return nil, err |
| 151 |
} |
| 152 |
aggsMap[name] = src |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// Add Meta data if available |
| 157 |
if len(a.meta) > 0 { |
| 158 |
source["meta"] = a.meta |
| 159 |
} |
| 160 |
|
| 161 |
return source, nil |
| 162 |
} |
| 163 |
|
| 164 |
// -- Models for moving averages -- |
| 165 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html#_models |
| 166 |
|
| 167 |
// MovAvgModel specifies the model to use with the MovAvgAggregation. |
| 168 |
type MovAvgModel interface { |
| 169 |
Name() string |
| 170 |
Settings() map[string]interface{} |
| 171 |
} |
| 172 |
|
| 173 |
// -- EWMA -- |
| 174 |
|
| 175 |
// EWMAMovAvgModel calculates an exponentially weighted moving average. |
| 176 |
// |
| 177 |
// For more details, see |
| 178 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html#_ewma_exponentially_weighted |
| 179 |
type EWMAMovAvgModel struct { |
| 180 |
alpha *float64 |
| 181 |
} |
| 182 |
|
| 183 |
// NewEWMAMovAvgModel creates and initializes a new EWMAMovAvgModel. |
| 184 |
func NewEWMAMovAvgModel() *EWMAMovAvgModel { |
| 185 |
return &EWMAMovAvgModel{} |
| 186 |
} |
| 187 |
|
| 188 |
// Alpha controls the smoothing of the data. Alpha = 1 retains no memory |
| 189 |
// of past values (e.g. a random walk), while alpha = 0 retains infinite |
| 190 |
// memory of past values (e.g. the series mean). Useful values are somewhere |
| 191 |
// in between. Defaults to 0.5. |
| 192 |
func (m *EWMAMovAvgModel) Alpha(alpha float64) *EWMAMovAvgModel { |
| 193 |
m.alpha = &alpha |
| 194 |
return m |
| 195 |
} |
| 196 |
|
| 197 |
// Name of the model. |
| 198 |
func (m *EWMAMovAvgModel) Name() string { |
| 199 |
return "ewma" |
| 200 |
} |
| 201 |
|
| 202 |
// Settings of the model. |
| 203 |
func (m *EWMAMovAvgModel) Settings() map[string]interface{} { |
| 204 |
settings := make(map[string]interface{}) |
| 205 |
if m.alpha != nil { |
| 206 |
settings["alpha"] = *m.alpha |
| 207 |
} |
| 208 |
return settings |
| 209 |
} |
| 210 |
|
| 211 |
// -- Holt linear -- |
| 212 |
|
| 213 |
// HoltLinearMovAvgModel calculates a doubly exponential weighted moving average. |
| 214 |
// |
| 215 |
// For more details, see |
| 216 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html#_holt_linear |
| 217 |
type HoltLinearMovAvgModel struct { |
| 218 |
alpha *float64 |
| 219 |
beta *float64 |
| 220 |
} |
| 221 |
|
| 222 |
// NewHoltLinearMovAvgModel creates and initializes a new HoltLinearMovAvgModel. |
| 223 |
func NewHoltLinearMovAvgModel() *HoltLinearMovAvgModel { |
| 224 |
return &HoltLinearMovAvgModel{} |
| 225 |
} |
| 226 |
|
| 227 |
// Alpha controls the smoothing of the data. Alpha = 1 retains no memory |
| 228 |
// of past values (e.g. a random walk), while alpha = 0 retains infinite |
| 229 |
// memory of past values (e.g. the series mean). Useful values are somewhere |
| 230 |
// in between. Defaults to 0.5. |
| 231 |
func (m *HoltLinearMovAvgModel) Alpha(alpha float64) *HoltLinearMovAvgModel { |
| 232 |
m.alpha = &alpha |
| 233 |
return m |
| 234 |
} |
| 235 |
|
| 236 |
// Beta is equivalent to Alpha but controls the smoothing of the trend |
| 237 |
// instead of the data. |
| 238 |
func (m *HoltLinearMovAvgModel) Beta(beta float64) *HoltLinearMovAvgModel { |
| 239 |
m.beta = &beta |
| 240 |
return m |
| 241 |
} |
| 242 |
|
| 243 |
// Name of the model. |
| 244 |
func (m *HoltLinearMovAvgModel) Name() string { |
| 245 |
return "holt" |
| 246 |
} |
| 247 |
|
| 248 |
// Settings of the model. |
| 249 |
func (m *HoltLinearMovAvgModel) Settings() map[string]interface{} { |
| 250 |
settings := make(map[string]interface{}) |
| 251 |
if m.alpha != nil { |
| 252 |
settings["alpha"] = *m.alpha |
| 253 |
} |
| 254 |
if m.beta != nil { |
| 255 |
settings["beta"] = *m.beta |
| 256 |
} |
| 257 |
return settings |
| 258 |
} |
| 259 |
|
| 260 |
// -- Holt Winters -- |
| 261 |
|
| 262 |
// HoltWintersMovAvgModel calculates a triple exponential weighted moving average. |
| 263 |
// |
| 264 |
// For more details, see |
| 265 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html#_holt_winters |
| 266 |
type HoltWintersMovAvgModel struct { |
| 267 |
alpha *float64 |
| 268 |
beta *float64 |
| 269 |
gamma *float64 |
| 270 |
period *int |
| 271 |
seasonalityType string |
| 272 |
pad *bool |
| 273 |
} |
| 274 |
|
| 275 |
// NewHoltWintersMovAvgModel creates and initializes a new HoltWintersMovAvgModel. |
| 276 |
func NewHoltWintersMovAvgModel() *HoltWintersMovAvgModel { |
| 277 |
return &HoltWintersMovAvgModel{} |
| 278 |
} |
| 279 |
|
| 280 |
// Alpha controls the smoothing of the data. Alpha = 1 retains no memory |
| 281 |
// of past values (e.g. a random walk), while alpha = 0 retains infinite |
| 282 |
// memory of past values (e.g. the series mean). Useful values are somewhere |
| 283 |
// in between. Defaults to 0.5. |
| 284 |
func (m *HoltWintersMovAvgModel) Alpha(alpha float64) *HoltWintersMovAvgModel { |
| 285 |
m.alpha = &alpha |
| 286 |
return m |
| 287 |
} |
| 288 |
|
| 289 |
// Beta is equivalent to Alpha but controls the smoothing of the trend |
| 290 |
// instead of the data. |
| 291 |
func (m *HoltWintersMovAvgModel) Beta(beta float64) *HoltWintersMovAvgModel { |
| 292 |
m.beta = &beta |
| 293 |
return m |
| 294 |
} |
| 295 |
|
| 296 |
func (m *HoltWintersMovAvgModel) Gamma(gamma float64) *HoltWintersMovAvgModel { |
| 297 |
m.gamma = &gamma |
| 298 |
return m |
| 299 |
} |
| 300 |
|
| 301 |
func (m *HoltWintersMovAvgModel) Period(period int) *HoltWintersMovAvgModel { |
| 302 |
m.period = &period |
| 303 |
return m |
| 304 |
} |
| 305 |
|
| 306 |
func (m *HoltWintersMovAvgModel) SeasonalityType(typ string) *HoltWintersMovAvgModel { |
| 307 |
m.seasonalityType = typ |
| 308 |
return m |
| 309 |
} |
| 310 |
|
| 311 |
func (m *HoltWintersMovAvgModel) Pad(pad bool) *HoltWintersMovAvgModel { |
| 312 |
m.pad = &pad |
| 313 |
return m |
| 314 |
} |
| 315 |
|
| 316 |
// Name of the model. |
| 317 |
func (m *HoltWintersMovAvgModel) Name() string { |
| 318 |
return "holt_winters" |
| 319 |
} |
| 320 |
|
| 321 |
// Settings of the model. |
| 322 |
func (m *HoltWintersMovAvgModel) Settings() map[string]interface{} { |
| 323 |
settings := make(map[string]interface{}) |
| 324 |
if m.alpha != nil { |
| 325 |
settings["alpha"] = *m.alpha |
| 326 |
} |
| 327 |
if m.beta != nil { |
| 328 |
settings["beta"] = *m.beta |
| 329 |
} |
| 330 |
if m.gamma != nil { |
| 331 |
settings["gamma"] = *m.gamma |
| 332 |
} |
| 333 |
if m.period != nil { |
| 334 |
settings["period"] = *m.period |
| 335 |
} |
| 336 |
if m.pad != nil { |
| 337 |
settings["pad"] = *m.pad |
| 338 |
} |
| 339 |
if m.seasonalityType != "" { |
| 340 |
settings["type"] = m.seasonalityType |
| 341 |
} |
| 342 |
return settings |
| 343 |
} |
| 344 |
|
| 345 |
// -- Linear -- |
| 346 |
|
| 347 |
// LinearMovAvgModel calculates a linearly weighted moving average, such |
| 348 |
// that older values are linearly less important. "Time" is determined |
| 349 |
// by position in collection. |
| 350 |
// |
| 351 |
// For more details, see |
| 352 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html#_linear |
| 353 |
type LinearMovAvgModel struct { |
| 354 |
} |
| 355 |
|
| 356 |
// NewLinearMovAvgModel creates and initializes a new LinearMovAvgModel. |
| 357 |
func NewLinearMovAvgModel() *LinearMovAvgModel { |
| 358 |
return &LinearMovAvgModel{} |
| 359 |
} |
| 360 |
|
| 361 |
// Name of the model. |
| 362 |
func (m *LinearMovAvgModel) Name() string { |
| 363 |
return "linear" |
| 364 |
} |
| 365 |
|
| 366 |
// Settings of the model. |
| 367 |
func (m *LinearMovAvgModel) Settings() map[string]interface{} { |
| 368 |
return nil |
| 369 |
} |
| 370 |
|
| 371 |
// -- Simple -- |
| 372 |
|
| 373 |
// SimpleMovAvgModel calculates a simple unweighted (arithmetic) moving average. |
| 374 |
// |
| 375 |
// For more details, see |
| 376 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-aggregations-pipeline-movavg-aggregation.html#_simple |
| 377 |
type SimpleMovAvgModel struct { |
| 378 |
} |
| 379 |
|
| 380 |
// NewSimpleMovAvgModel creates and initializes a new SimpleMovAvgModel. |
| 381 |
func NewSimpleMovAvgModel() *SimpleMovAvgModel { |
| 382 |
return &SimpleMovAvgModel{} |
| 383 |
} |
| 384 |
|
| 385 |
// Name of the model. |
| 386 |
func (m *SimpleMovAvgModel) Name() string { |
| 387 |
return "simple" |
| 388 |
} |
| 389 |
|
| 390 |
// Settings of the model. |
| 391 |
func (m *SimpleMovAvgModel) Settings() map[string]interface{} { |
| 392 |
return nil |
| 393 |
} |