refactor: structs log parser

This commit is contained in:
Inanc Gumus
2019-04-26 00:27:54 +03:00
parent 4ebe1ee22b
commit 59e79993cf

View File

@ -11,34 +11,31 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"sort"
"strconv" "strconv"
"strings" "strings"
) )
// domain represents a domain log record // visit stores metrics for a domain
type domain struct { type visit struct {
name string domain string
visits int count int
// add more metrics if needed
} }
// parser parses a log file and provides an iterator to iterate upon the domains // parser keep tracks of the parsing
//
// the parser struct is carefully crafted to be usable using its zero values except the map field
type parser struct { type parser struct {
sum map[string]domain // visits per unique domain sum map[string]visit // metrics per domain
domains []string // unique domain names domains []string // unique domain names
total int // total visits to all domains total int // total visits for all domains
lines int // number of parsed lines (for the error messages) lines int // number of parsed lines (for the error messages)
} }
func main() { func main() {
in := bufio.NewScanner(os.Stdin) p := parser{sum: make(map[string]visit)}
p := parser{
sum: make(map[string]domain),
}
// Scan the standard-in line by line // Scan the standard-in line by line
in := bufio.NewScanner(os.Stdin)
for in.Scan() { for in.Scan() {
p.lines++ p.lines++
@ -48,34 +45,45 @@ func main() {
fmt.Printf("wrong input: %v (line #%d)\n", fields, p.lines) fmt.Printf("wrong input: %v (line #%d)\n", fields, p.lines)
return return
} }
name, visits := fields[0], fields[1]
// Sum the total visits per domain // Sum the total visits per domain
n, err := strconv.Atoi(visits) visits, err := strconv.Atoi(fields[1])
if n < 0 || err != nil { if visits < 0 || err != nil {
fmt.Printf("wrong input: %q (line #%d)\n", visits, p.lines) fmt.Printf("wrong input: %q (line #%d)\n", fields[1], p.lines)
return return
} }
d := domain{name: name, visits: n} name := fields[0]
// Collect the unique domains // Collect the unique domains
if _, ok := p.sum[d.name]; !ok { if _, ok := p.sum[name]; !ok {
p.domains = append(p.domains, name) p.domains = append(p.domains, name)
} }
p.total += d.visits // Keep track of total and per domain visits
d.visits += p.sum[name].visits p.total += visits
p.sum[name] = d
// You cannot assign to composite values
// p.sum[name].count += visits
// create and assign a new copy of `visit`
p.sum[name] = visit{
domain: name,
count: visits + p.sum[name].count,
}
} }
// Print the visits per domain // Print the visits per domain
for _, name := range p.domains { sort.Strings(p.domains)
d := p.sum[name]
fmt.Printf("%-25s -> %d\n", d.name, d.visits) fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
fmt.Println(strings.Repeat("-", 45))
for _, name := range p.domains {
visits := p.sum[name]
fmt.Printf("%-30s %10d\n", name, visits.count)
} }
// Print the total visits for all domains // Print the total visits for all domains
fmt.Printf("\n%-25s -> %d\n", "TOTAL", p.total) fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
} }