interfaces: refactor
This commit is contained in:
5
interfaces/05-log-parser/oop/Makefile
Normal file
5
interfaces/05-log-parser/oop/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
r:
|
||||
go run . < ../../logs/log.txt
|
||||
|
||||
t:
|
||||
time go run . < ../../logs/log.txt
|
||||
55
interfaces/05-log-parser/oop/analysis.go
Normal file
55
interfaces/05-log-parser/oop/analysis.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "sort"
|
||||
|
||||
type (
|
||||
groupFunc func(result) string
|
||||
filterFunc func(result) bool
|
||||
)
|
||||
|
||||
type analysis struct {
|
||||
sum map[string]result // metrics per domain
|
||||
keys []string // unique keys
|
||||
groupKey groupFunc
|
||||
filter filterFunc
|
||||
}
|
||||
|
||||
func newAnalysis() *analysis {
|
||||
return &analysis{
|
||||
sum: make(map[string]result),
|
||||
groupKey: domainGrouper,
|
||||
filter: noopFilter,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *analysis) groupBy(g groupFunc) { a.groupKey = g }
|
||||
func (a *analysis) filterBy(f filterFunc) { a.filter = f }
|
||||
|
||||
func (a *analysis) analyse(r result) {
|
||||
if !a.filter(r) {
|
||||
return
|
||||
}
|
||||
|
||||
key := a.groupKey(r)
|
||||
if _, ok := a.sum[key]; !ok {
|
||||
a.keys = append(a.keys, key)
|
||||
}
|
||||
|
||||
a.sum[key] = r.add(a.sum[key])
|
||||
}
|
||||
|
||||
// each analysed result will be sent by the given func
|
||||
func (a *analysis) each(f func(r result)) {
|
||||
sort.Strings(a.keys)
|
||||
|
||||
for _, domain := range a.keys {
|
||||
f(a.sum[domain])
|
||||
}
|
||||
}
|
||||
42
interfaces/05-log-parser/oop/chartsummary.go
Normal file
42
interfaces/05-log-parser/oop/chartsummary.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
c "github.com/wcharczuk/go-chart"
|
||||
)
|
||||
|
||||
type chartSummary struct {
|
||||
title string
|
||||
width, height int
|
||||
}
|
||||
|
||||
func (s *chartSummary) summarize(results iterator) error {
|
||||
w := os.Stdout
|
||||
|
||||
donut := c.DonutChart{
|
||||
Title: s.title,
|
||||
TitleStyle: c.Style{
|
||||
FontSize: 35,
|
||||
Show: true,
|
||||
FontColor: c.ColorAlternateGreen,
|
||||
},
|
||||
Width: s.width,
|
||||
Height: s.height,
|
||||
}
|
||||
|
||||
results.each(func(r result) {
|
||||
v := c.Value{
|
||||
Label: r.domain + r.page + ": " + strconv.Itoa(r.visits),
|
||||
Value: float64(r.visits),
|
||||
Style: c.Style{
|
||||
FontSize: 14,
|
||||
},
|
||||
}
|
||||
|
||||
donut.Values = append(donut.Values, v)
|
||||
})
|
||||
|
||||
return donut.Render(c.SVG, w)
|
||||
}
|
||||
34
interfaces/05-log-parser/oop/filters.go
Normal file
34
interfaces/05-log-parser/oop/filters.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
func noopFilter(r result) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func notUsing(filter filterFunc) filterFunc {
|
||||
return func(r result) bool {
|
||||
return !filter(r)
|
||||
}
|
||||
}
|
||||
|
||||
func domainExtFilter(domains ...string) filterFunc {
|
||||
return func(r result) bool {
|
||||
for _, domain := range domains {
|
||||
if strings.HasSuffix(r.domain, "."+domain) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func domainFilter(domain string) filterFunc {
|
||||
return func(r result) bool {
|
||||
return strings.Contains(r.domain, domain)
|
||||
}
|
||||
}
|
||||
|
||||
func orgDomainsFilter(r result) bool {
|
||||
return strings.HasSuffix(r.domain, ".org")
|
||||
}
|
||||
13
interfaces/05-log-parser/oop/groupers.go
Normal file
13
interfaces/05-log-parser/oop/groupers.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
// domainGrouper groups by domain.
|
||||
// but it keeps the other fields.
|
||||
// for example: it returns pages as well, but you shouldn't use them.
|
||||
// exercise: write a function that erases the unnecessary data.
|
||||
func domainGrouper(r result) string {
|
||||
return r.domain
|
||||
}
|
||||
|
||||
func pageGrouper(r result) string {
|
||||
return r.domain + r.page
|
||||
}
|
||||
69
interfaces/05-log-parser/oop/jsonparser.go
Normal file
69
interfaces/05-log-parser/oop/jsonparser.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// For more tutorials: https://bp.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type jsonParser struct {
|
||||
r io.Reader
|
||||
update func(r result)
|
||||
}
|
||||
|
||||
func newJSONParser(r io.Reader) *jsonParser {
|
||||
return &jsonParser{
|
||||
r: r,
|
||||
update: func(result) {},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *jsonParser) parse() error {
|
||||
defer readClose(p.r)
|
||||
|
||||
bytes, err := ioutil.ReadAll(p.r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return p.parseJSON(bytes)
|
||||
}
|
||||
|
||||
func (p *jsonParser) parseJSON(bytes []byte) error {
|
||||
var rs []struct {
|
||||
Domain string
|
||||
Page string
|
||||
Visits int
|
||||
Uniques int
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(bytes, &rs); err != nil {
|
||||
if serr, ok := err.(*json.SyntaxError); ok {
|
||||
return fmt.Errorf("%v %q", serr, bytes[:serr.Offset])
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
for _, r := range rs {
|
||||
p.update(result{
|
||||
domain: r.Domain,
|
||||
page: r.Page,
|
||||
visits: r.Visits,
|
||||
uniques: r.Uniques,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *jsonParser) notify(f func(r result)) {
|
||||
p.update = f
|
||||
}
|
||||
39
interfaces/05-log-parser/oop/main.go
Normal file
39
interfaces/05-log-parser/oop/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
an := newAnalysis()
|
||||
an.filterBy(notUsing(domainExtFilter("io", "com")))
|
||||
an.groupBy(domainGrouper)
|
||||
|
||||
// pars, err := parseTextFile("log.txt")
|
||||
// if err != nil {
|
||||
// log.Fatalln(err)
|
||||
// }
|
||||
|
||||
err := report(an, newTextParser(os.Stdin), summarizeFunc(textSummary))
|
||||
// err := report(an, newJSONParser(os.Stdin), newTextSummary())
|
||||
|
||||
// chart := &chartSummary{
|
||||
// title: "visits per domain",
|
||||
// width: 1920,
|
||||
// height: 800,
|
||||
// }
|
||||
|
||||
// err := report(an, newTextParser(os.Stdin), chart)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
18
interfaces/05-log-parser/oop/readclose.go
Normal file
18
interfaces/05-log-parser/oop/readclose.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
func readClose(r io.Reader) {
|
||||
if rc, ok := r.(io.Closer); ok {
|
||||
rc.Close()
|
||||
}
|
||||
}
|
||||
40
interfaces/05-log-parser/oop/report.go
Normal file
40
interfaces/05-log-parser/oop/report.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
type notifier interface {
|
||||
notify(func(r result))
|
||||
}
|
||||
|
||||
type parser interface {
|
||||
parse() error
|
||||
notifier
|
||||
}
|
||||
|
||||
type iterator interface {
|
||||
each(func(result))
|
||||
}
|
||||
|
||||
type analyser interface {
|
||||
analyse(result)
|
||||
iterator
|
||||
}
|
||||
|
||||
type summarizer interface {
|
||||
summarize(iterator) error
|
||||
}
|
||||
|
||||
func report(a analyser, p parser, s summarizer) error {
|
||||
p.notify(a.analyse)
|
||||
|
||||
if err := p.parse(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.summarize(a)
|
||||
}
|
||||
14
interfaces/05-log-parser/oop/result.go
Normal file
14
interfaces/05-log-parser/oop/result.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
type result struct {
|
||||
domain string
|
||||
page string
|
||||
visits int
|
||||
uniques int
|
||||
}
|
||||
|
||||
func (r result) add(other result) result {
|
||||
r.visits += other.visits
|
||||
r.uniques += other.uniques
|
||||
return r
|
||||
}
|
||||
86
interfaces/05-log-parser/oop/textparser.go
Normal file
86
interfaces/05-log-parser/oop/textparser.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// For more tutorials: https://bp.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const fieldsLength = 4
|
||||
|
||||
type textParser struct {
|
||||
r io.Reader
|
||||
update func(r result)
|
||||
}
|
||||
|
||||
func newTextParser(r io.Reader) *textParser {
|
||||
return &textParser{
|
||||
r: r,
|
||||
update: func(result) {},
|
||||
}
|
||||
}
|
||||
|
||||
func parseTextFile(path string) (*textParser, 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)
|
||||
|
||||
var (
|
||||
l = 1
|
||||
in = bufio.NewScanner(p.r)
|
||||
)
|
||||
|
||||
for in.Scan() {
|
||||
r, err := p.parseFields(in.Text())
|
||||
if err != nil {
|
||||
return fmt.Errorf("line %d: %v", l, err)
|
||||
}
|
||||
|
||||
p.update(r)
|
||||
l++
|
||||
}
|
||||
|
||||
return in.Err()
|
||||
}
|
||||
|
||||
func (p *textParser) parseFields(s string) (r result, err error) {
|
||||
fields := strings.Fields(s)
|
||||
if len(fields) != fieldsLength {
|
||||
return r, fmt.Errorf("wrong number of fields %q", fields)
|
||||
}
|
||||
|
||||
r.domain, r.page = fields[0], fields[1]
|
||||
|
||||
r.visits, err = strconv.Atoi(fields[2])
|
||||
if err != nil || r.visits < 0 {
|
||||
return r, fmt.Errorf("wrong input %q", fields[2])
|
||||
}
|
||||
|
||||
r.uniques, err = strconv.Atoi(fields[3])
|
||||
if err != nil || r.uniques < 0 {
|
||||
return r, fmt.Errorf("wrong input %q", fields[3])
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
56
interfaces/05-log-parser/oop/textsummary.go
Normal file
56
interfaces/05-log-parser/oop/textsummary.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"text/tabwriter"
|
||||
)
|
||||
|
||||
// TODO: make this configurable? or exercise?
|
||||
const (
|
||||
minWidth = 0
|
||||
tabWidth = 4
|
||||
padding = 4
|
||||
flags = 0
|
||||
)
|
||||
|
||||
type summarizeFunc func(iterator) error
|
||||
|
||||
func (f summarizeFunc) summarize(results iterator) error {
|
||||
return f(results)
|
||||
}
|
||||
|
||||
// type textSummary struct{}
|
||||
|
||||
// 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)
|
||||
|
||||
write := fmt.Fprintf
|
||||
|
||||
write(w, "DOMAINS\tPAGES\tVISITS\tUNIQUES\n")
|
||||
write(w, "-------\t-----\t------\t-------\n")
|
||||
|
||||
var total result
|
||||
results.each(func(r result) {
|
||||
total = total.add(r)
|
||||
|
||||
write(w, "%s\t%s\t%d\t%d\n", r.domain, r.page, r.visits, r.uniques)
|
||||
})
|
||||
|
||||
write(w, "\t\t\t\n")
|
||||
write(w, "%s\t%s\t%d\t%d\n", "TOTAL", "", total.visits, total.uniques)
|
||||
|
||||
return w.Flush()
|
||||
}
|
||||
Reference in New Issue
Block a user