From 8506cd5023c9c7c8b0663ef8bd89b5a1bb23a189 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 25 Jan 2017 12:15:46 -0800 Subject: [PATCH] Revert "update code" This reverts commit 2c2ca4655fece3f21d35c0cab57e47aeb2f1b900. --- main.go | 49 +++++++++++++++++-------------------------------- main_test.go | 10 ++++++++++ 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/main.go b/main.go index 60d2fcf..e097891 100644 --- a/main.go +++ b/main.go @@ -25,10 +25,6 @@ const dateFormat = "2006-01-02T15:04:05.999999Z07:00" const outDateFormat = "2006-01-02T15:04:05" const outDateFormatNoSeconds = "2006-01-02T15:04" -var ( - seenHosts []string -) - func ParseLogLine(line []byte) (*LogLine, error) { sl := bytes.Split(line, []byte(" ")) @@ -112,11 +108,10 @@ func main() { _, ltm, _ := lastTime.Clock() if llm != ltm { - - processBuckets(lastTime, ab) + processBuckets(ab) ab = map[string]*Bucket{} - lastTime = lastTime.Add(time.Minute) + lastTime = lastTime.Add(5 * time.Minute) } b := ab[ll.Host] @@ -134,41 +129,31 @@ func main() { ab[ll.Host] = b } - processBuckets(lastTime, ab) + processBuckets(ab) +} + +// sees if the minute and hour field of a == the minute and hour field of b +func cmpTime(a, b time.Time) bool { + _, am, _ := a.Clock() + _, bm, _ := b.Clock() + + return am == bm } func toMS(dur time.Duration) int64 { return dur.Nanoseconds() / 1000000 } -func contains(host string) bool { - for _, val := range seenHosts { - if val == host { - return true - } - } - - return false -} - -func processBuckets(lt time.Time, set map[string]*Bucket) { - log.Printf("printing buckets for %s", lt.Format(outDateFormatNoSeconds)) +func processBuckets(set map[string]*Bucket) { + hosts := []string{} for host, _ := range set { - if !contains(host) { - seenHosts = append(seenHosts, host) - } + hosts = append(hosts, host) } - sort.Sort(sort.StringSlice(seenHosts)) - - for _, host := range seenHosts { - bucket, ok := set[host] - - if !ok { - fmt.Printf("%s,%s,0,0,0,0\n", lt.Format(outDateFormat), host) - return - } + sort.Sort(sort.StringSlice(hosts)) + for _, host := range hosts { + bucket := set[host] var longest time.Duration var shortest time.Duration var total time.Duration diff --git a/main_test.go b/main_test.go index 8c5adc1..555a08c 100644 --- a/main_test.go +++ b/main_test.go @@ -2,6 +2,7 @@ package main import ( "testing" + "time" "github.com/kr/pretty" ) @@ -25,3 +26,12 @@ func TestParseLogLine(t *testing.T) { t.Fatal("invalid host") } } + +func TestCmpTime(t *testing.T) { + now := time.Date(2016, time.January, 1, 13, 37, 0, 0, time.UTC) + then := now.Add(5 * time.Minute) + + if cmpTime(then, now) { + t.Fatal("cmpTime error") + } +}