-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggs-tree.go
438 lines (347 loc) · 9.16 KB
/
aggs-tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package aggretastic
import (
"fmt"
"github.com/olivere/elastic/v7"
"log"
)
var (
ErrNoPath = fmt.Errorf("no path")
ErrPathNotSelectable = fmt.Errorf("path is not selectable")
ErrAggIsNotInjectable = fmt.Errorf("agg is not injectable")
)
// Aggregation is a tree-ish version of original elastic.Aggregation
// Besides just attaching subAggregations it can get any of children subAggregations
// and add another subAggregation to it
type Aggregation interface {
// embedding original elastic.Aggregation interface
// is used to support call of `.Source()` method from aggregations' code
elastic.Aggregation
// GetAllSubs returns the map of this aggregation's subAggregations
GetAllSubs() map[string]Aggregation
// Inject sets new subAgg into the map of subAggregations
Inject(subAgg Aggregation, path ...string) (resultPaths [][]string, err error)
// InjectX sets new subAgg into the map of subAggregations only if it NOT exists already
InjectX(subAgg Aggregation, path ...string) (resultPaths [][]string, err error)
// InjectSafe sets new subAgg into the map of subAggregations in the SAFE mode
InjectSafe(subAgg Aggregation, path ...string) (resultPaths [][]string, err error)
// Select returns any subAgg by it's path
Select(path ...string) Aggregation
// Pop returns a subAgg by it's path and remove it from tree
Pop(path ...string) Aggregation
// Export returns the same object in original Agg interface
Export() elastic.Aggregation
// ExtractLeafPaths returns paths the leafs
ExtractLeafPaths() [][]string
}
func IsNilTree(t Aggregation) bool {
return t == nil || t.Export() == nil
}
type tree struct {
root elastic.Aggregation
subAggregations map[string]Aggregation
}
func nilAggregationTree(root elastic.Aggregation) *tree {
return &tree{
root: root,
subAggregations: make(map[string]Aggregation),
}
}
func (a *tree) ExtractLeafPaths() (leafs [][]string) {
leafs = make([][]string, 0)
if len(a.subAggregations) == 0 {
leafs = append(leafs, []string{})
return
}
for leafName, leafAgg := range a.subAggregations {
extractedLeafs := leafAgg.ExtractLeafPaths()
if len(extractedLeafs) == 0 {
leafs = append(leafs, []string{leafName})
break
}
for _, leaf := range extractedLeafs {
leafs = append(leafs, append([]string{leafName}, leaf...))
}
}
return leafs
}
func (a *tree) Inject(subAggregation Aggregation, path ...string) (resultPaths [][]string, err error) {
resultPaths = make([][]string, 0)
if len(path) == 0 {
err = ErrNoPath
return
}
if len(path) == 1 {
a.subAggregations[path[0]] = subAggregation
for _, leaf := range subAggregation.ExtractLeafPaths() {
resultPaths = append(resultPaths, append(path, leaf...))
}
return
}
// deeper inject
cursor := a.Select(path[:len(path)-1]...)
if IsNilTree(cursor) {
err = ErrPathNotSelectable
return
}
if resultPaths, err = cursor.Inject(subAggregation, path[len(path)-1]); err != nil {
return
} else {
for j := range resultPaths {
resultPaths[j] = append(path[:len(path)-1], resultPaths[j]...)
}
}
return
}
func (a *tree) InjectX(subAggregation Aggregation, path ...string) (resultPaths [][]string, err error) {
resultPaths = make([][]string, 0)
if len(path) == 0 {
err = ErrNoPath
return
}
if alreadyInjected := a.Select(path...); IsNilTree(alreadyInjected) {
resultPaths, err = a.Inject(subAggregation, path...)
if err != nil {
return
}
}
return
}
func (a *tree) InjectSafe(subAggregation Aggregation, path ...string) (resultPaths [][]string, err error) {
resultPaths = make([][]string, 0)
if len(path) == 0 {
err = ErrNoPath
return
}
// extracting the sub tree
subTree := a.Select(path...)
if IsNilTree(subTree) {
return a.Inject(subAggregation, path...)
}
for k, subAggDeep := range subAggregation.GetAllSubs() {
kResultPaths, injectErr := subTree.InjectSafe(subAggDeep, k)
if injectErr != nil {
err = injectErr
return
}
if len(kResultPaths) > 0 {
for j := range kResultPaths {
kResultPaths[j] = append(path, kResultPaths[j]...)
}
} else {
kResultPaths = append(kResultPaths, append(path, k))
}
resultPaths = append(resultPaths, kResultPaths...)
}
return
}
func (a *tree) GetAllSubs() map[string]Aggregation {
return a.subAggregations
}
func (a *tree) Select(path ...string) Aggregation {
if len(path) == 0 {
return nil
}
subAgg, ok := a.subAggregations[path[0]]
if !ok {
return nil
}
if len(path) == 1 {
return subAgg
}
return subAgg.Select(path[1:]...)
}
func (a *tree) Pop(path ...string) Aggregation {
if len(path) == 0 {
return nil
}
subAgg, ok := a.subAggregations[path[0]]
if !ok {
return nil
}
if len(path) == 1 {
delete(a.subAggregations, path[0])
return subAgg
}
return subAgg.Pop(path[1:]...)
}
func (a *tree) Export() elastic.Aggregation {
return a.root
}
// Shorthand type for collection of Aggregations
type Aggregations map[string]Aggregation
// Export does export() on the map of aggregations
func (a *Aggregations) Export() map[string]elastic.Aggregation {
result := make(map[string]elastic.Aggregation)
if a == nil {
return result
}
for k, v := range *a {
result[k] = v.Export()
}
return result
}
// Select selects an aggregation from the map (going deep forwarding the agg.Select() method)
func (a *Aggregations) Select(path ...string) Aggregation {
if len(path) == 0 {
return nil
}
base, ok := (*a)[path[0]]
if !ok {
return nil
}
if len(path) == 1 {
return base
}
return base.Select(path[1:]...)
}
// Pop pops an aggregation from the map (going deep forwarding the agg.Pop() method)
func (a *Aggregations) Pop(path ...string) Aggregation {
if len(path) == 0 {
return nil
}
base, ok := (*a)[path[0]]
if !ok {
return nil
}
if len(path) == 1 {
delete(*a, path[0])
return base
}
return base.Pop(path[1:]...)
}
// Inject just puts agg into the map of aggregations
func (a *Aggregations) Inject(subAgg Aggregation, path ...string) (resultPaths [][]string, err error) {
resultPaths = make([][]string, 0)
if a == nil {
err = ErrAggIsNotInjectable
return
}
if len(path) == 0 {
err = ErrNoPath
return
}
name := path[0]
if len(path) == 1 {
(*a)[name] = subAgg
for _, leaf := range subAgg.ExtractLeafPaths() {
resultPaths = append(resultPaths, append(path, leaf...))
}
return
}
path = path[1:]
if _, ok := (*a)[name]; !ok {
err = ErrAggIsNotInjectable
return
}
resultPaths, err = (*a)[name].Inject(subAgg, path...)
if err == nil {
for i := range resultPaths {
resultPaths[i] = append([]string{name}, resultPaths[i]...)
}
}
return
}
func (a *Aggregations) InjectX(subAgg Aggregation, path ...string) (resultPaths [][]string, err error) {
resultPaths = make([][]string, 0)
if a == nil {
err = ErrAggIsNotInjectable
return
}
if len(path) == 0 {
err = ErrNoPath
return
}
name := path[0]
if len(path) == 1 {
if _, ok := (*a)[name]; !ok {
(*a)[name] = subAgg
}
// @tody return path
return
}
path = path[1:]
if _, ok := (*a)[name]; !ok {
err = ErrAggIsNotInjectable
return
}
return (*a)[name].InjectX(subAgg, path...)
}
func (a *Aggregations) InjectSafe(subAgg Aggregation, path ...string) (resultPaths [][]string, err error) {
resultPaths = make([][]string, 0)
if len(path) == 0 {
err = ErrNoPath
return
}
name := path[0]
if len(path) == 1 {
if _, ok := (*a)[name]; !ok {
(*a)[name] = subAgg
resultPaths = subAgg.ExtractLeafPaths()
for i := range resultPaths {
// prepend name
resultPaths[i] = append([]string{name}, resultPaths[i]...)
}
return
}
// @todo
log.Println("warning! maybe unexpected behaviour. Edge case, need handling")
return
}
path = path[1:]
if _, ok := (*a)[name]; !ok {
err = ErrAggIsNotInjectable
return
}
if resultPaths, err = (*a)[name].InjectSafe(subAgg, path...); err == nil {
// prepend name to result paths
for i := range resultPaths {
resultPaths[i] = append([]string{name}, resultPaths[i]...)
}
}
return
}
//
// helpers
//
// getIntersectedPaths returns leafs' paths that intersects of agg and its subAgg
func getIntersectedPaths(rootAggs map[string]Aggregation, injectingAgg Aggregation, injectPath []string) [][]string {
paths := make([][]string, 0)
rootLeafs := make([][]string, 0)
if len(rootAggs) == 0 {
rootLeafs = append(rootLeafs, []string{})
} else {
for leafName, leafAgg := range rootAggs {
for _, leaf := range leafAgg.ExtractLeafPaths() {
rootLeafs = append(rootLeafs, append([]string{leafName}, leaf...))
}
}
}
givenLeafPaths := injectingAgg.ExtractLeafPaths()
for i := range givenLeafPaths {
givenLeafPaths[i] = append(injectPath, givenLeafPaths[i]...)
}
for _, resultLeaf := range rootLeafs {
for _, givenLeaf := range givenLeafPaths {
if pathIsLeafOf(givenLeaf, resultLeaf) {
paths = append(paths, resultLeaf)
}
}
}
return paths
}
// pathIsLeafOf checks if childPath is a finite leaf of parentPath
func pathIsLeafOf(childPath, parentPath []string) bool {
if len(parentPath) < len(childPath) {
return false
}
if len(parentPath) > len(childPath) {
parentPath = parentPath[len(parentPath)-len(childPath):]
}
var diff bool
for i := range childPath {
if childPath[i] != parentPath[i] {
diff = true
}
}
return !diff
}