2019-08-28 23:46:42 +03:00
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
2019-10-30 19:34:44 +03:00
|
|
|
// For more tutorials : https://learngoprogramming.com
|
|
|
|
// In-person training : https://www.linkedin.com/in/inancgumus/
|
|
|
|
// Follow me on twitter: https://twitter.com/inancgumus
|
2019-08-28 23:46:42 +03:00
|
|
|
|
|
|
|
package pipe
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GroupFunc represents a grouping func that returns a grouping key.
|
2019-08-29 01:32:25 +03:00
|
|
|
// The type alias frees us from binding to a named type.
|
2019-08-28 23:46:42 +03:00
|
|
|
type GroupFunc = func(Record) (key string)
|
|
|
|
|
|
|
|
// Group records by a key.
|
|
|
|
type Group struct {
|
2019-08-29 01:50:04 +03:00
|
|
|
sum map[string]record // metrics per group key
|
2019-08-28 23:46:42 +03:00
|
|
|
keys []string // unique group keys
|
|
|
|
key GroupFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupBy returns a new Group.
|
|
|
|
// It takes a group func that returns a group key.
|
|
|
|
// The returned group will group the record using the key.
|
|
|
|
func GroupBy(key GroupFunc) *Group {
|
|
|
|
return &Group{
|
2019-08-29 01:50:04 +03:00
|
|
|
sum: make(map[string]record),
|
2019-08-28 23:46:42 +03:00
|
|
|
key: key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 16:08:46 +03:00
|
|
|
// Consume the records for grouping.
|
2019-08-28 23:46:42 +03:00
|
|
|
func (g *Group) Consume(records Iterator) error {
|
2019-08-29 16:08:46 +03:00
|
|
|
group := func(r Record) error {
|
2019-08-28 23:46:42 +03:00
|
|
|
k := g.key(r)
|
|
|
|
|
|
|
|
if _, ok := g.sum[k]; !ok {
|
|
|
|
g.keys = append(g.keys, k)
|
|
|
|
}
|
|
|
|
|
2019-08-29 01:50:04 +03:00
|
|
|
g.sum[k] = r.sum(g.sum[k])
|
2019-08-28 23:46:42 +03:00
|
|
|
|
|
|
|
return nil
|
2019-08-29 16:08:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return records.Each(group)
|
2019-08-28 23:46:42 +03:00
|
|
|
}
|
|
|
|
|
2019-08-29 16:08:46 +03:00
|
|
|
// Each sends the grouped and sorted records to upstream.
|
2019-08-28 23:46:42 +03:00
|
|
|
func (g *Group) Each(yield func(Record) error) error {
|
|
|
|
sort.Strings(g.keys)
|
|
|
|
|
|
|
|
for _, k := range g.keys {
|
2019-08-29 16:08:46 +03:00
|
|
|
err := yield(Record{g.sum[k]})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-08-28 23:46:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|