refactor: oop log parser names
This commit is contained in:
@ -33,7 +33,7 @@ func (g *group) digest(results iterator) error {
|
|||||||
g.keys = append(g.keys, k)
|
g.keys = append(g.keys, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
g.sum[k] = r.add(g.sum[k])
|
g.sum[k] = r.sum(g.sum[k])
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ func main() {
|
|||||||
pipe := newPipeline(
|
pipe := newPipeline(
|
||||||
newTextLog(os.Stdin),
|
newTextLog(os.Stdin),
|
||||||
newTextReport(),
|
newTextReport(),
|
||||||
filterBy(domainExtFilter("com", "io")),
|
filterBy(notUsing(domainExtFilter("com", "io"))),
|
||||||
groupBy(domainGrouper),
|
groupBy(domainGrouper),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ type result struct {
|
|||||||
uniques int
|
uniques int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r result) add(other result) result {
|
func (r result) sum(other result) result {
|
||||||
r.visits += other.visits
|
r.visits += other.visits
|
||||||
r.uniques += other.uniques
|
r.uniques += other.uniques
|
||||||
return r
|
return r
|
||||||
@ -31,18 +31,24 @@ func (r *result) UnmarshalText(p []byte) (err error) {
|
|||||||
|
|
||||||
r.domain, r.page = fields[0], fields[1]
|
r.domain, r.page = fields[0], fields[1]
|
||||||
|
|
||||||
r.visits, err = strconv.Atoi(fields[2])
|
if r.visits, err = parseStr("visits", fields[2]); err != nil {
|
||||||
if err != nil || r.visits < 0 {
|
return err
|
||||||
return fmt.Errorf("wrong input %q", fields[2])
|
|
||||||
}
|
}
|
||||||
|
if r.uniques, err = parseStr("uniques", fields[3]); err != nil {
|
||||||
r.uniques, err = strconv.Atoi(fields[3])
|
return err
|
||||||
if err != nil || r.uniques < 0 {
|
|
||||||
return fmt.Errorf("wrong input %q", fields[3])
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseStr helps UnmarshalText for string to positive int parsing
|
||||||
|
func parseStr(name, v string) (int, error) {
|
||||||
|
n, err := strconv.Atoi(v)
|
||||||
|
if err != nil || n < 0 {
|
||||||
|
return 0, fmt.Errorf("result.UnmarshalText %q: %v", name, err)
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalJSON to a *result
|
// UnmarshalJSON to a *result
|
||||||
func (r *result) UnmarshalJSON(data []byte) error {
|
func (r *result) UnmarshalJSON(data []byte) error {
|
||||||
var re struct {
|
var re struct {
|
||||||
|
@ -37,7 +37,7 @@ func (s *textReport) digest(results iterator) error {
|
|||||||
|
|
||||||
var total result
|
var total result
|
||||||
results.each(func(r result) {
|
results.each(func(r result) {
|
||||||
total = total.add(r)
|
total = total.sum(r)
|
||||||
|
|
||||||
write(w, "%s\t%s\t%d\t%d\n", r.domain, r.page, r.visits, r.uniques)
|
write(w, "%s\t%s\t%d\t%d\n", r.domain, r.page, r.visits, r.uniques)
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user