Files
learngo/logparser/v5/pipe/textreport.go

66 lines
1.3 KiB
Go
Raw Permalink Normal View History

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 (
"fmt"
"io"
"text/tabwriter"
)
const (
minWidth = 0
tabWidth = 4
padding = 4
flags = 0
)
// TextReport report generator.
type TextReport struct {
w io.Writer
}
// NewTextReport returns a TextReport report generator.
func NewTextReport(w io.Writer) *TextReport {
return &TextReport{w: w}
}
// Consume generates a text report.
func (t *TextReport) Consume(records Iterator) error {
w := tabwriter.NewWriter(t.w, minWidth, tabWidth, padding, ' ', flags)
2019-08-29 16:08:46 +03:00
fmt.Fprintf(w, "DOMAINS\tPAGES\tVISITS\tUNIQUES\n")
fmt.Fprintf(w, "-------\t-----\t------\t-------\n")
2019-08-28 23:46:42 +03:00
2019-08-29 01:50:04 +03:00
var total record
2019-08-28 23:46:42 +03:00
2019-08-29 16:08:46 +03:00
printLine := func(r Record) error {
2019-08-29 01:50:04 +03:00
total = r.sum(total)
2019-08-28 23:46:42 +03:00
2019-08-29 16:08:46 +03:00
fmt.Fprintf(w, "%s\t%s\t%d\t%d\n",
2019-08-29 01:32:25 +03:00
r.domain, r.page,
r.visits, r.uniques,
2019-08-28 23:46:42 +03:00
)
return nil
2019-08-29 16:08:46 +03:00
}
if err := records.Each(printLine); err != nil {
2019-08-28 23:46:42 +03:00
return err
}
2019-08-29 16:08:46 +03:00
fmt.Fprintf(w, "\t\t\t\n")
fmt.Fprintf(w, "%s\t%s\t%d\t%d\n", "TOTAL", "",
2019-08-29 01:32:25 +03:00
total.visits,
total.uniques,
2019-08-28 23:46:42 +03:00
)
return w.Flush()
}