refactor: logparser v5 funcs

This commit is contained in:
Inanc Gumus
2019-08-29 16:08:46 +03:00
parent d28c6d54d2
commit 661e018258
10 changed files with 48 additions and 53 deletions

View File

@ -32,9 +32,9 @@ func GroupBy(key GroupFunc) *Group {
}
}
// Consume records for grouping.
// Consume the records for grouping.
func (g *Group) Consume(records Iterator) error {
return records.Each(func(r Record) error {
group := func(r Record) error {
k := g.key(r)
if _, ok := g.sum[k]; !ok {
@ -44,15 +44,19 @@ func (g *Group) Consume(records Iterator) error {
g.sum[k] = r.sum(g.sum[k])
return nil
})
}
return records.Each(group)
}
// Each sorts and yields the grouped records.
// Each sends the grouped and sorted records to upstream.
func (g *Group) Each(yield func(Record) error) error {
sort.Strings(g.keys)
for _, k := range g.keys {
if err := yield(Record{g.sum[k]}); err != nil {
err := yield(Record{g.sum[k]})
if err != nil {
return err
}
}