You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_aggs_pipeline_mov_avg.go 10KB

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