add: logparser v5 shared record
This commit is contained in:
@ -27,6 +27,7 @@ func main() {
|
|||||||
// pipe.NewJSONReport(os.Stdout),
|
// pipe.NewJSONReport(os.Stdout),
|
||||||
pipe.FilterBy(pipe.DomainExtFilter("com", "io")),
|
pipe.FilterBy(pipe.DomainExtFilter("com", "io")),
|
||||||
pipe.GroupBy(pipe.DomainGrouper),
|
pipe.GroupBy(pipe.DomainGrouper),
|
||||||
|
// new(passThrough),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := p.Run(); err != nil {
|
if err := p.Run(); err != nil {
|
||||||
|
31
logparser/v5/passthrough.go
Normal file
31
logparser/v5/passthrough.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// 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 (
|
||||||
|
"github.com/inancgumus/learngo/logparser/v5/pipe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type passThrough struct {
|
||||||
|
pipe.Iterator
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *passThrough) Consume(results pipe.Iterator) error {
|
||||||
|
t.Iterator = results
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *passThrough) Each(yield func(pipe.Record) error) error {
|
||||||
|
|
||||||
|
return t.Iterator.Each(func(r pipe.Record) error {
|
||||||
|
// fmt.Println(r.Fields())
|
||||||
|
// fmt.Println(r.Int("visits"))
|
||||||
|
return yield(r)
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
@ -8,6 +8,7 @@
|
|||||||
package pipe
|
package pipe
|
||||||
|
|
||||||
// FilterFunc represents a filtering pipeline func.
|
// FilterFunc represents a filtering pipeline func.
|
||||||
|
// The type alias frees us from binding to a named type.
|
||||||
type FilterFunc = func(Record) (pass bool)
|
type FilterFunc = func(Record) (pass bool)
|
||||||
|
|
||||||
// Filter the records.
|
// Filter the records.
|
||||||
|
@ -20,7 +20,7 @@ func NotFilter(filter FilterFunc) FilterFunc {
|
|||||||
func DomainExtFilter(domains ...string) FilterFunc {
|
func DomainExtFilter(domains ...string) FilterFunc {
|
||||||
return func(r Record) bool {
|
return func(r Record) bool {
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
if strings.HasSuffix(r.Domain, "."+domain) {
|
if strings.HasSuffix(r.domain, "."+domain) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,11 +31,11 @@ func DomainExtFilter(domains ...string) FilterFunc {
|
|||||||
// DomainFilter filters a domain if it contains the given text.
|
// DomainFilter filters a domain if it contains the given text.
|
||||||
func DomainFilter(text string) FilterFunc {
|
func DomainFilter(text string) FilterFunc {
|
||||||
return func(r Record) bool {
|
return func(r Record) bool {
|
||||||
return strings.Contains(r.Domain, text)
|
return strings.Contains(r.domain, text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DomainOrgFilter filters only the ".org" domains.
|
// DomainOrgFilter filters only the ".org" domains.
|
||||||
func DomainOrgFilter(r Record) bool {
|
func DomainOrgFilter(r Record) bool {
|
||||||
return strings.HasSuffix(r.Domain, ".org")
|
return strings.HasSuffix(r.domain, ".org")
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// GroupFunc represents a grouping func that returns a grouping key.
|
// GroupFunc represents a grouping func that returns a grouping key.
|
||||||
|
// The type alias frees us from binding to a named type.
|
||||||
type GroupFunc = func(Record) (key string)
|
type GroupFunc = func(Record) (key string)
|
||||||
|
|
||||||
// Group records by a key.
|
// Group records by a key.
|
||||||
|
@ -12,10 +12,10 @@ package pipe
|
|||||||
// For example: It returns the page field as well.
|
// For example: It returns the page field as well.
|
||||||
// Exercise: Write a solution that removes the unnecessary data.
|
// Exercise: Write a solution that removes the unnecessary data.
|
||||||
func DomainGrouper(r Record) string {
|
func DomainGrouper(r Record) string {
|
||||||
return r.Domain
|
return r.domain
|
||||||
}
|
}
|
||||||
|
|
||||||
// Page groups records by page.
|
// Page groups records by page.
|
||||||
func Page(r Record) string {
|
func Page(r Record) string {
|
||||||
return r.Domain + r.Page
|
return r.domain + r.page
|
||||||
}
|
}
|
||||||
|
@ -10,18 +10,18 @@ import (
|
|||||||
|
|
||||||
const fieldsLength = 4
|
const fieldsLength = 4
|
||||||
|
|
||||||
// Record stores fields of a log line.
|
// record stores fields of a log line.
|
||||||
type Record struct {
|
type record struct {
|
||||||
Domain string
|
domain string
|
||||||
Page string
|
page string
|
||||||
Visits int
|
visits int
|
||||||
Uniques int
|
uniques int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sum the numeric fields with another record.
|
// Sum the numeric fields with another record.
|
||||||
func (r Record) Sum(other Record) Record {
|
func (r Record) Sum(other Record) Record {
|
||||||
r.Visits += other.Visits
|
r.visits += other.visits
|
||||||
r.Uniques += other.Uniques
|
r.uniques += other.uniques
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,12 +32,12 @@ func (r *Record) UnmarshalText(p []byte) (err error) {
|
|||||||
return fmt.Errorf("wrong number of fields %q", fields)
|
return fmt.Errorf("wrong number of fields %q", fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Domain, r.Page = fields[0], fields[1]
|
r.domain, r.page = fields[0], fields[1]
|
||||||
|
|
||||||
if r.Visits, err = parseStr("visits", fields[2]); err != nil {
|
if r.visits, err = parseStr("visits", fields[2]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.Uniques, err = parseStr("uniques", fields[3]); err != nil {
|
if r.uniques, err = parseStr("uniques", fields[3]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return validate(*r)
|
return validate(*r)
|
||||||
@ -72,13 +72,13 @@ func parseStr(name, v string) (int, error) {
|
|||||||
// validate whether a parsed record is valid or not.
|
// validate whether a parsed record is valid or not.
|
||||||
func validate(r Record) (err error) {
|
func validate(r Record) (err error) {
|
||||||
switch {
|
switch {
|
||||||
case r.Domain == "":
|
case r.domain == "":
|
||||||
err = errors.New("record.domain cannot be empty")
|
err = errors.New("record.domain cannot be empty")
|
||||||
case r.Page == "":
|
case r.page == "":
|
||||||
err = errors.New("record.page cannot be empty")
|
err = errors.New("record.page cannot be empty")
|
||||||
case r.Visits < 0:
|
case r.visits < 0:
|
||||||
err = errors.New("record.visits cannot be negative")
|
err = errors.New("record.visits cannot be negative")
|
||||||
case r.Uniques < 0:
|
case r.uniques < 0:
|
||||||
err = errors.New("record.uniques cannot be negative")
|
err = errors.New("record.uniques cannot be negative")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
58
logparser/v5/pipe/recordshare.go
Normal file
58
logparser/v5/pipe/recordshare.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// 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 pipe
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Record stores the log line fields.
|
||||||
|
// The underlying fields are kept hidden.
|
||||||
|
// The users of the package are decoupled from the underlying record fields.
|
||||||
|
// When the fields change, the client won't feel the difference (at least in compile-time).
|
||||||
|
type Record struct {
|
||||||
|
record
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns a string field. Panics when the field doesn't exist.
|
||||||
|
func (r Record) String(field string) string {
|
||||||
|
return r.mustGet(field, reflect.String).String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Int returns an int field. Panics when the field doesn't exist.
|
||||||
|
func (r Record) Int(field string) int {
|
||||||
|
return int(r.mustGet(field, reflect.Int).Int())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fields returns all the field names.
|
||||||
|
// The names can be used to query the Record.
|
||||||
|
func (r Record) Fields() (fields []string) {
|
||||||
|
t := reflect.TypeOf(record{})
|
||||||
|
|
||||||
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
f := t.Field(i)
|
||||||
|
s := fmt.Sprintf("(%s: %s)", f.Name, f.Type.Name())
|
||||||
|
|
||||||
|
fields = append(fields, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// mustGet the field with the same kind or panics.
|
||||||
|
func (r Record) mustGet(field string, kind reflect.Kind) reflect.Value {
|
||||||
|
v := reflect.ValueOf(r.record).FieldByName(field)
|
||||||
|
if !v.IsValid() {
|
||||||
|
panic(fmt.Errorf("record.%s does not exist", field))
|
||||||
|
}
|
||||||
|
if v.Kind() != kind {
|
||||||
|
panic(fmt.Errorf("record.%s is not %q", field, kind))
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
@ -45,8 +45,8 @@ func (t *TextReport) Consume(records Iterator) error {
|
|||||||
total = r.Sum(total)
|
total = r.Sum(total)
|
||||||
|
|
||||||
write(w, "%s\t%s\t%d\t%d\n",
|
write(w, "%s\t%s\t%d\t%d\n",
|
||||||
r.Domain, r.Page,
|
r.domain, r.page,
|
||||||
r.Visits, r.Uniques,
|
r.visits, r.uniques,
|
||||||
)
|
)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -57,8 +57,8 @@ func (t *TextReport) Consume(records Iterator) error {
|
|||||||
|
|
||||||
write(w, "\t\t\t\n")
|
write(w, "\t\t\t\n")
|
||||||
write(w, "%s\t%s\t%d\t%d\n", "TOTAL", "",
|
write(w, "%s\t%s\t%d\t%d\n", "TOTAL", "",
|
||||||
total.Visits,
|
total.visits,
|
||||||
total.Uniques,
|
total.uniques,
|
||||||
)
|
)
|
||||||
|
|
||||||
return w.Flush()
|
return w.Flush()
|
||||||
|
Reference in New Issue
Block a user