refactor: logparser

This commit is contained in:
Inanc Gumus
2019-08-19 20:00:27 +03:00
parent 158f475a2d
commit b39a9ea117
10 changed files with 94 additions and 94 deletions

View File

@ -9,11 +9,6 @@ package main
import "sort" import "sort"
type (
groupFunc func(result) string
filterFunc func(result) bool
)
type analysis struct { type analysis struct {
sum map[string]result // metrics per domain sum map[string]result // metrics per domain
keys []string // unique keys keys []string // unique keys
@ -29,9 +24,19 @@ func newAnalysis() *analysis {
} }
} }
func (a *analysis) groupBy(g groupFunc) { a.groupKey = g } func (a *analysis) groupBy(g groupFunc) {
func (a *analysis) filterBy(f filterFunc) { a.filter = f } if g != nil {
a.groupKey = g
}
}
func (a *analysis) filterBy(f filterFunc) {
if f != nil {
a.filter = f
}
}
// analyse the given result
func (a *analysis) analyse(r result) { func (a *analysis) analyse(r result) {
if !a.filter(r) { if !a.filter(r) {
return return
@ -45,11 +50,11 @@ func (a *analysis) analyse(r result) {
a.sum[key] = r.add(a.sum[key]) a.sum[key] = r.add(a.sum[key])
} }
// each analysed result will be sent by the given func // each sends an analysis result to `handle`
func (a *analysis) each(f func(r result)) { func (a *analysis) each(handle resultFn) {
sort.Strings(a.keys) sort.Strings(a.keys)
for _, domain := range a.keys { for _, domain := range a.keys {
f(a.sum[domain]) handle(a.sum[domain])
} }
} }

View File

@ -7,6 +7,9 @@ import (
c "github.com/wcharczuk/go-chart" c "github.com/wcharczuk/go-chart"
) )
// You need to run:
// go get -u github.com/wcharczuk/go-chart
type chartSummary struct { type chartSummary struct {
title string title string
width, height int width, height int

View File

@ -2,6 +2,8 @@ package main
import "strings" import "strings"
type filterFunc func(result) bool
func noopFilter(r result) bool { func noopFilter(r result) bool {
return true return true
} }

View File

@ -1,5 +1,7 @@
package main package main
type groupFunc func(result) string
// domainGrouper groups by domain. // domainGrouper groups by domain.
// but it keeps the other fields. // but it keeps the other fields.
// for example: it returns pages as well, but you shouldn't use them. // for example: it returns pages as well, but you shouldn't use them.

View File

@ -16,17 +16,13 @@ import (
type jsonParser struct { type jsonParser struct {
r io.Reader r io.Reader
update func(r result)
} }
func newJSONParser(r io.Reader) *jsonParser { func newJSONParser(r io.Reader) *jsonParser {
return &jsonParser{ return &jsonParser{r: r}
r: r,
update: func(result) {},
}
} }
func (p *jsonParser) parse() error { func (p *jsonParser) parse(handle resultFn) error {
defer readClose(p.r) defer readClose(p.r)
bytes, err := ioutil.ReadAll(p.r) bytes, err := ioutil.ReadAll(p.r)
@ -34,10 +30,10 @@ func (p *jsonParser) parse() error {
return err return err
} }
return p.parseJSON(bytes) return parseJSON(bytes, handle)
} }
func (p *jsonParser) parseJSON(bytes []byte) error { func parseJSON(bytes []byte, handle resultFn) error {
var rs []struct { var rs []struct {
Domain string Domain string
Page string Page string
@ -53,7 +49,7 @@ func (p *jsonParser) parseJSON(bytes []byte) error {
} }
for _, r := range rs { for _, r := range rs {
p.update(result{ handle(result{
domain: r.Domain, domain: r.Domain,
page: r.Page, page: r.Page,
visits: r.Visits, visits: r.Visits,
@ -63,7 +59,3 @@ func (p *jsonParser) parseJSON(bytes []byte) error {
return nil return nil
} }
func (p *jsonParser) notify(f func(r result)) {
p.update = f
}

View File

@ -9,31 +9,29 @@ package main
import ( import (
"log" "log"
// "fmt"
"os" "os"
) )
func main() { func main() {
an := newAnalysis() a := newAnalysis()
an.filterBy(notUsing(domainExtFilter("io", "com"))) // a.filterBy(notUsing(domainExtFilter("io", "com")))
an.groupBy(domainGrouper) // a.groupBy(domainGrouper)
// pars, err := parseTextFile("log.txt") p := newTextParser(os.Stdin)
// if err != nil { s := newTextSummary()
// log.Fatalln(err)
// }
err := report(an, newTextParser(os.Stdin), summarizeFunc(textSummary)) // s := &chartSummary{
// err := report(an, newJSONParser(os.Stdin), newTextSummary())
// chart := &chartSummary{
// title: "visits per domain", // title: "visits per domain",
// width: 1920, // width: 1920,
// height: 800, // height: 800,
// } // }
// err := report(an, newTextParser(os.Stdin), chart) if err := report(p, a, s); err != nil {
if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
// if err := reportFromFile(os.Args[1]); err != nil {
// log.Fatalln(err)
// }
} }

View File

@ -7,34 +7,57 @@
package main package main
type notifier interface { import (
notify(func(r result)) "errors"
} "os"
"strings"
)
type resultFn func(result)
type parser interface { type parser interface {
parse() error parse(resultFn) error
notifier
}
type iterator interface {
each(func(result))
} }
type analyser interface { type analyser interface {
analyse(result) analyse(result)
iterator }
type iterator interface {
each(resultFn)
} }
type summarizer interface { type summarizer interface {
summarize(iterator) error summarize(iterator) error
} }
func report(a analyser, p parser, s summarizer) error { func report(p parser, a analyser, s summarizer) error {
p.notify(a.analyse) if err := p.parse(a.analyse); err != nil {
if err := p.parse(); err != nil {
return err return err
} }
return s.summarize(a) it, ok := a.(iterator)
if !ok {
return errors.New("cannot iterate on analyser")
}
return s.summarize(it)
}
// reportFromFile generates a default report
func reportFromFile(path string) (err error) {
f, err := os.Open(path)
if err != nil {
return err
}
var p parser
switch {
case strings.HasSuffix(path, ".txt"):
p = newTextParser(f)
case strings.HasSuffix(path, ".json"):
p = newJSONParser(f)
}
return report(p, newAnalysis(), newTextSummary())
} }

View File

@ -1,5 +1,7 @@
package main package main
const fieldsLength = 4
type result struct { type result struct {
domain string domain string
page string page string

View File

@ -11,39 +11,19 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"os"
"strconv" "strconv"
"strings" "strings"
) )
const fieldsLength = 4
type textParser struct { type textParser struct {
r io.Reader r io.Reader
update func(r result)
} }
func newTextParser(r io.Reader) *textParser { func newTextParser(r io.Reader) *textParser {
return &textParser{ return &textParser{r: r}
r: r,
update: func(result) {},
}
} }
func parseTextFile(path string) (*textParser, error) { func (p *textParser) parse(handle resultFn) error {
f, err := os.Open(path)
if err != nil {
return nil, err
}
return newTextParser(f), nil
}
func (p *textParser) notify(f func(r result)) {
p.update = f
}
func (p *textParser) parse() error {
defer readClose(p.r) defer readClose(p.r)
var ( var (
@ -52,19 +32,19 @@ func (p *textParser) parse() error {
) )
for in.Scan() { for in.Scan() {
r, err := p.parseFields(in.Text()) r, err := parseFields(in.Text())
if err != nil { if err != nil {
return fmt.Errorf("line %d: %v", l, err) return fmt.Errorf("line %d: %v", l, err)
} }
p.update(r) handle(r)
l++ l++
} }
return in.Err() return in.Err()
} }
func (p *textParser) parseFields(s string) (r result, err error) { func parseFields(s string) (r result, err error) {
fields := strings.Fields(s) fields := strings.Fields(s)
if len(fields) != fieldsLength { if len(fields) != fieldsLength {
return r, fmt.Errorf("wrong number of fields %q", fields) return r, fmt.Errorf("wrong number of fields %q", fields)

View File

@ -21,20 +21,13 @@ const (
flags = 0 flags = 0
) )
type summarizeFunc func(iterator) error type textSummary struct{}
func (f summarizeFunc) summarize(results iterator) error { func newTextSummary() *textSummary {
return f(results) return new(textSummary)
} }
// type textSummary struct{} func (s *textSummary) summarize(results iterator) error {
// func newTextSummary() *textSummary {
// return new(textSummary)
// }
// func (s *textSummary) summarize(results iterator) error {
func textSummary(results iterator) error {
w := tabwriter.NewWriter(os.Stdout, minWidth, tabWidth, padding, ' ', flags) w := tabwriter.NewWriter(os.Stdout, minWidth, tabWidth, padding, ' ', flags)
write := fmt.Fprintf write := fmt.Fprintf