refactor: log parser text writer and func types

This commit is contained in:
Inanc Gumus
2019-08-06 01:49:48 +03:00
parent eadf0c85e4
commit 69094da997
6 changed files with 34 additions and 16 deletions

View File

@@ -1,5 +1,7 @@
package main package main
type filterFunc func(result) (include bool)
func filterBy(results []result, filterer filterFunc) []result { func filterBy(results []result, filterer filterFunc) []result {
out := results[:0] out := results[:0]

View File

@@ -6,7 +6,7 @@ func noopFilter(r result) bool {
return true return true
} }
func not(filter filterFunc) filterFunc { func notUsing(filter filterFunc) filterFunc {
return func(r result) bool { return func(r result) bool {
return !filter(r) return !filter(r)
} }

View File

@@ -1,5 +1,7 @@
package main package main
type groupFunc func(result) (key string)
func groupBy(results []result, keyer groupFunc) []result { func groupBy(results []result, keyer groupFunc) []result {
grouped := make(map[string]result, len(results)) grouped := make(map[string]result, len(results))

View File

@@ -20,7 +20,7 @@ func main() {
to(os.Stdout). to(os.Stdout).
retrieveFrom(textReader). retrieveFrom(textReader).
filterBy(orgDomainsFilter). filterBy(orgDomainsFilter).
// filterBy(not(domainExtFilter("org", "io"))). // filterBy(notUsing(domainExtFilter("org", "io"))).
// groupBy(pageGrouper). // groupBy(pageGrouper).
groupBy(domainGrouper). groupBy(domainGrouper).
writeTo(textWriter). writeTo(textWriter).
@@ -44,6 +44,17 @@ func recoverErr() {
} }
/* /*
newReport -> stats.NewReport(). newReport -> report.New().
Result -> stats.Record Result -> report.Line
notUsing = report.Not
_, err := stats.Parse().
From(os.Stdin).
To(os.Stdout).
RetrieveFrom(stats.TextReader).
FilterBy(notUsing(stats.DomainExtFilter("org", "io"))).
GroupBy(stats.DomainGrouper).
WriteTo(stats.TextWriter).
Run()
*/ */

View File

@@ -4,8 +4,6 @@ import "io"
type ( type (
retrieveFunc func(io.Reader) ([]result, error) retrieveFunc func(io.Reader) ([]result, error)
filterFunc func(result) bool
groupFunc func(result) string
writeFunc func(io.Writer, []result) error writeFunc func(io.Writer, []result) error
) )

View File

@@ -8,23 +8,28 @@ import (
// TODO: sort by result key interfaces section // TODO: sort by result key interfaces section
func textWriter(w io.Writer, results []result) error { const (
fmt.Fprintf(w, "%-25s %-10s %10s %10s\n",
"DOMAINS", "PAGES", "VISITS", "UNIQUES")
fmt.Fprintln(w, strings.Repeat("-", 58)) // DOMAINS PAGES VISITS UNIQUES
// ^ ^ ^ ^
// | | | |
header = "%-25s %-10s %10s %10s\n"
line = "%-25s %-10s %10d %10d\n"
footer = "\n%-36s %10d %10d\n" // -> "" VISITS UNIQUES
dashLength = 58
)
func textWriter(w io.Writer, results []result) error {
fmt.Fprintf(w, header, "DOMAINS", "PAGES", "VISITS", "UNIQUES")
fmt.Fprintln(w, strings.Repeat("-", dashLength))
var total result var total result
for _, r := range results { for _, r := range results {
total = total.add(r) total = total.add(r)
fmt.Fprintf(w, line, r.domain, r.page, r.visits, r.uniques)
fmt.Fprintf(w, "%-25s %-10s %10d %10d\n",
r.domain, r.page, r.visits, r.uniques)
} }
fmt.Fprintf(w, "\n%-36s %10d %10d\n", fmt.Fprintf(w, footer, "", total.visits, total.uniques)
"", total.visits, total.uniques)
return nil return nil
} }