Skip to content

Commit a1a6284

Browse files
committed
feat average aggregation
1 parent 4a8b369 commit a1a6284

File tree

1 file changed

+130
-56
lines changed

1 file changed

+130
-56
lines changed

engine/aggregate.go

Lines changed: 130 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ func aggrigate(query gjson.Result) string {
8787
}
8888

8989
case "$avg":
90-
avrs := average(_id, fld.Str, data)
90+
avrs, err := average(_id, fld, data)
91+
if err != nil {
92+
message = err.Error()
93+
return false
94+
}
9195
for _id, avr := range avrs {
9296
mapData[_id], _ = sjson.Set(mapData[_id], key.Str, avr)
9397
}
@@ -122,8 +126,8 @@ func sum(_id string, field gjson.Result, records []string) (mp map[string]float6
122126
switch field.Type {
123127
case 3:
124128
for _, record := range records {
125-
id := gjson.Get(record, _id).Str // name of record
126-
val := gjson.Get(record, field.Str).Num // value of compared field
129+
id := gjson.Get(record, _id).Str
130+
val := gjson.Get(record, field.Str).Num
127131

128132
mp[id] += val
129133
}
@@ -135,9 +139,9 @@ func sum(_id string, field gjson.Result, records []string) (mp map[string]float6
135139
case "$multiply":
136140

137141
for _, record := range records {
138-
id := gjson.Get(record, _id).Str // name of record
139-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
140-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
142+
id := gjson.Get(record, _id).Str
143+
arg1 := gjson.Get(record, args.Get("0").Str).Num
144+
arg2 := gjson.Get(record, args.Get("1").Str).Num
141145

142146
val := arg1 * arg2
143147

@@ -148,9 +152,9 @@ func sum(_id string, field gjson.Result, records []string) (mp map[string]float6
148152
for _, record := range records {
149153
fmt.Println(record)
150154

151-
id := gjson.Get(record, _id).Str // name of record
152-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
153-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
155+
id := gjson.Get(record, _id).Str
156+
arg1 := gjson.Get(record, args.Get("0").Str).Num
157+
arg2 := gjson.Get(record, args.Get("1").Str).Num
154158

155159
val := arg1 + arg2
156160

@@ -162,9 +166,9 @@ func sum(_id string, field gjson.Result, records []string) (mp map[string]float6
162166
for _, record := range records {
163167
fmt.Println(record)
164168

165-
id := gjson.Get(record, _id).Str // name of record
166-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
167-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
169+
id := gjson.Get(record, _id).Str
170+
arg1 := gjson.Get(record, args.Get("0").Str).Num
171+
arg2 := gjson.Get(record, args.Get("1").Str).Num
168172

169173
val := arg1 - arg2
170174
mp[id] += val
@@ -175,9 +179,9 @@ func sum(_id string, field gjson.Result, records []string) (mp map[string]float6
175179
for _, record := range records {
176180
fmt.Println(record)
177181

178-
id := gjson.Get(record, _id).Str // name of record
179-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
180-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
182+
id := gjson.Get(record, _id).Str
183+
arg1 := gjson.Get(record, args.Get("0").Str).Num
184+
arg2 := gjson.Get(record, args.Get("1").Str).Num
181185
val := arg1 / arg2
182186

183187
mp[id] += val
@@ -213,8 +217,8 @@ func min(_id string, field gjson.Result, records []string) (mp map[string]float6
213217
switch field.Type {
214218
case 3:
215219
for _, record := range records {
216-
id := gjson.Get(record, _id).Str // name of record
217-
val := gjson.Get(record, field.Str).Num // value of compared field
220+
id := gjson.Get(record, _id).Str
221+
val := gjson.Get(record, field.Str).Num
218222
if mp[id] > val {
219223
mp[id] = val
220224
}
@@ -228,9 +232,9 @@ func min(_id string, field gjson.Result, records []string) (mp map[string]float6
228232

229233
for _, record := range records {
230234

231-
id := gjson.Get(record, _id).Str // name of record
232-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
233-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
235+
id := gjson.Get(record, _id).Str
236+
arg1 := gjson.Get(record, args.Get("0").Str).Num
237+
arg2 := gjson.Get(record, args.Get("1").Str).Num
234238

235239
val := arg1 * arg2
236240
if mp[id] > val {
@@ -242,9 +246,9 @@ func min(_id string, field gjson.Result, records []string) (mp map[string]float6
242246
for _, record := range records {
243247
fmt.Println(record)
244248

245-
id := gjson.Get(record, _id).Str // name of record
246-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
247-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
249+
id := gjson.Get(record, _id).Str
250+
arg1 := gjson.Get(record, args.Get("0").Str).Num
251+
arg2 := gjson.Get(record, args.Get("1").Str).Num
248252

249253
val := arg1 + arg2
250254
if mp[id] > val {
@@ -257,9 +261,9 @@ func min(_id string, field gjson.Result, records []string) (mp map[string]float6
257261
for _, record := range records {
258262
fmt.Println(record)
259263

260-
id := gjson.Get(record, _id).Str // name of record
261-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
262-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
264+
id := gjson.Get(record, _id).Str
265+
arg1 := gjson.Get(record, args.Get("0").Str).Num
266+
arg2 := gjson.Get(record, args.Get("1").Str).Num
263267

264268
val := arg1 - arg2
265269
if mp[id] > val {
@@ -272,9 +276,9 @@ func min(_id string, field gjson.Result, records []string) (mp map[string]float6
272276
for _, record := range records {
273277
fmt.Println(record)
274278

275-
id := gjson.Get(record, _id).Str // name of record
276-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
277-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
279+
id := gjson.Get(record, _id).Str
280+
arg1 := gjson.Get(record, args.Get("0").Str).Num
281+
arg2 := gjson.Get(record, args.Get("1").Str).Num
278282
val := arg1 / arg2
279283

280284
if mp[id] > val {
@@ -314,8 +318,8 @@ func max(_id string, field gjson.Result, records []string) (mp map[string]float6
314318
switch field.Type {
315319
case 3:
316320
for _, record := range records {
317-
id := gjson.Get(record, _id).Str // name of record
318-
val := gjson.Get(record, field.Str).Num // value of compared field
321+
id := gjson.Get(record, _id).Str
322+
val := gjson.Get(record, field.Str).Num
319323
if mp[id] < val {
320324
mp[id] = val
321325
}
@@ -329,9 +333,9 @@ func max(_id string, field gjson.Result, records []string) (mp map[string]float6
329333

330334
for _, record := range records {
331335

332-
id := gjson.Get(record, _id).Str // name of record
333-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
334-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
336+
id := gjson.Get(record, _id).Str
337+
arg1 := gjson.Get(record, args.Get("0").Str).Num
338+
arg2 := gjson.Get(record, args.Get("1").Str).Num
335339

336340
val := arg1 * arg2
337341
if mp[id] < val {
@@ -343,9 +347,9 @@ func max(_id string, field gjson.Result, records []string) (mp map[string]float6
343347
for _, record := range records {
344348
fmt.Println(record)
345349

346-
id := gjson.Get(record, _id).Str // name of record
347-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
348-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
350+
id := gjson.Get(record, _id).Str
351+
arg1 := gjson.Get(record, args.Get("0").Str).Num
352+
arg2 := gjson.Get(record, args.Get("1").Str).Num
349353

350354
val := arg1 + arg2
351355
if mp[id] < val {
@@ -358,9 +362,9 @@ func max(_id string, field gjson.Result, records []string) (mp map[string]float6
358362
for _, record := range records {
359363
fmt.Println(record)
360364

361-
id := gjson.Get(record, _id).Str // name of record
362-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
363-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
365+
id := gjson.Get(record, _id).Str
366+
arg1 := gjson.Get(record, args.Get("0").Str).Num
367+
arg2 := gjson.Get(record, args.Get("1").Str).Num
364368

365369
val := arg1 - arg2
366370
if mp[id] < val {
@@ -373,9 +377,9 @@ func max(_id string, field gjson.Result, records []string) (mp map[string]float6
373377
for _, record := range records {
374378
fmt.Println(record)
375379

376-
id := gjson.Get(record, _id).Str // name of record
377-
arg1 := gjson.Get(record, args.Get("0").Str).Num // value of compared field
378-
arg2 := gjson.Get(record, args.Get("1").Str).Num // value of compared field
380+
id := gjson.Get(record, _id).Str
381+
arg1 := gjson.Get(record, args.Get("0").Str).Num
382+
arg2 := gjson.Get(record, args.Get("1").Str).Num
379383
val := arg1 / arg2
380384

381385
if mp[id] < val {
@@ -389,48 +393,118 @@ func max(_id string, field gjson.Result, records []string) (mp map[string]float6
389393

390394
return false
391395
})
396+
392397
if err != nil {
393398
return nil, err
394399
}
395-
396-
fmt.Println(field, "is operation")
397-
fmt.Println()
398-
fmt.Println(field.Get("$min"))
399400
}
401+
400402
return mp, nil
401403
}
402404

403405
// not implemented yet
404-
func average(_id, field string, records []string) (mp map[string]float64) {
406+
func average(_id string, field gjson.Result, records []string) (mp map[string]float64, err error) {
405407
mp = map[string]float64{}
406408

407409
fieldCount := make(map[string]float64)
408410

409-
for _, record := range records {
410-
id := gjson.Get(record, _id).Str // name of record
411-
val := gjson.Get(record, field) // value of sumd field
411+
switch field.Type {
412+
case 3:
413+
for _, record := range records {
414+
id := gjson.Get(record, _id).Str
415+
val := gjson.Get(record, field.Str).Num
412416

413-
//
414-
if val.Exists() {
417+
//if val.Exists() {}
415418
fieldCount[id]++
419+
mp[id] += val //.Num
416420
}
417421

418-
mp[id] += val.Num
422+
case 5:
423+
424+
field.ForEach(func(op, args gjson.Result) bool {
425+
switch op.Str {
426+
case "$multiply":
427+
428+
for _, record := range records {
429+
430+
id := gjson.Get(record, _id).Str
431+
arg1 := gjson.Get(record, args.Get("0").Str).Num
432+
arg2 := gjson.Get(record, args.Get("1").Str).Num
433+
434+
val := arg1 * arg2
435+
436+
fieldCount[id]++
437+
mp[id] += val //.Num
438+
}
439+
case "$add":
440+
441+
for _, record := range records {
442+
fmt.Println(record)
419443

444+
id := gjson.Get(record, _id).Str
445+
arg1 := gjson.Get(record, args.Get("0").Str).Num
446+
arg2 := gjson.Get(record, args.Get("1").Str).Num
447+
448+
val := arg1 + arg2
449+
450+
mp[id] += val //.Num
451+
}
452+
453+
case "$sub":
454+
455+
for _, record := range records {
456+
fmt.Println(record)
457+
458+
id := gjson.Get(record, _id).Str
459+
arg1 := gjson.Get(record, args.Get("0").Str).Num
460+
arg2 := gjson.Get(record, args.Get("1").Str).Num
461+
462+
val := arg1 - arg2
463+
464+
mp[id] += val //.Num
465+
466+
}
467+
468+
case "$div":
469+
470+
for _, record := range records {
471+
fmt.Println(record)
472+
473+
id := gjson.Get(record, _id).Str
474+
arg1 := gjson.Get(record, args.Get("0").Str).Num
475+
arg2 := gjson.Get(record, args.Get("1").Str).Num
476+
val := arg1 / arg2
477+
478+
mp[id] += val //.Num
479+
480+
}
481+
482+
default:
483+
err = fmt.Errorf("unknown %s operator", op)
484+
}
485+
486+
return false
487+
})
488+
489+
if err != nil {
490+
return nil, err
491+
}
420492
}
493+
421494
for fld, count := range fieldCount {
422495
mp[fld] = mp[fld] / count
423496
}
424497

425-
return mp
426-
}
498+
return mp, nil
499+
} // average
427500

428501
func count(field string, records []string) (mp map[string]int) {
429502

430503
mp = map[string]int{}
431504
for _, record := range records {
432505
mp[gjson.Get(record, field).String()]++
433506
}
507+
434508
fmt.Println("result: ", mp)
435509
return mp
436510
}

0 commit comments

Comments
 (0)