Skip to content

Commit

Permalink
average aggregate operator
Browse files Browse the repository at this point in the history
  • Loading branch information
baxiry committed Aug 16, 2024
1 parent f09874b commit 8264905
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions engine/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,15 @@ func aggrigate(query gjson.Result) string {

case 5:
val.ForEach(func(opr, fld gjson.Result) bool { // opperation & field name
fmt.Println(fld.Str, fld.Type)

switch opr.Str {
case "$count":
counted := count(fld.Str, data)
fmt.Println("counted : ", counted)
for _id, count := range counted {
mapData[_id], _ = sjson.Set(mapData[_id], key.Str, count)
}

case "$max":
fmt.Println("$max: ")
maxs := max(_id, fld.Str, data)
for _id, max := range maxs {
mapData[_id], _ = sjson.Set(mapData[_id], key.Str, max)
Expand All @@ -108,26 +106,30 @@ func aggrigate(query gjson.Result) string {
for _id, min := range mins {
mapData[_id], _ = sjson.Set(mapData[_id], key.Str, min)
}

case "$sum":
sums := sum(_id, fld.Str, data)
for _id, sumd := range sums {
mapData[_id], _ = sjson.Set(mapData[_id], key.Str, sumd)
}

case "$avg":
avrs := average(_id, fld.Str, data)
for _id, avr := range avrs {
mapData[_id], _ = sjson.Set(mapData[_id], key.Str, avr)
}

default:
fmt.Println("unknown aggr operation", fld.Str)
message = "unknown " + fld.Str + " aggregator !"
}

return true
})

default:
fmt.Println("unknown type", val.Type)
message = "opss! something wrrong!"
}

fmt.Println()
return true
})

Expand Down Expand Up @@ -158,7 +160,6 @@ func min(_id, field string, records []string) (mp map[string]float64) {
if mp[id] > val {
mp[id] = val
}

}

return mp
Expand Down Expand Up @@ -199,10 +200,23 @@ func max(_id, field string, records []string) (mp map[string]float64) {
// not implemented yet
func average(_id, field string, records []string) (mp map[string]float64) {
mp = map[string]float64{}

fieldCount := make(map[string]float64)

for _, record := range records {
id := gjson.Get(record, _id).Str // name of record
val := gjson.Get(record, field).Num // value of sumd field
mp[id] += val
id := gjson.Get(record, _id).Str // name of record
val := gjson.Get(record, field) // value of sumd field

//
if val.Exists() {
fieldCount[id]++
}

mp[id] += val.Num

}
for fld, count := range fieldCount {
mp[fld] = mp[fld] / count
}

return mp
Expand Down

0 comments on commit 8264905

Please sign in to comment.