update code
This commit is contained in:
parent
d6eeb15ccd
commit
2c2ca4655f
51
main.go
51
main.go
|
@ -25,6 +25,10 @@ const dateFormat = "2006-01-02T15:04:05.999999Z07:00"
|
||||||
const outDateFormat = "2006-01-02T15:04:05"
|
const outDateFormat = "2006-01-02T15:04:05"
|
||||||
const outDateFormatNoSeconds = "2006-01-02T15:04"
|
const outDateFormatNoSeconds = "2006-01-02T15:04"
|
||||||
|
|
||||||
|
var (
|
||||||
|
seenHosts []string
|
||||||
|
)
|
||||||
|
|
||||||
func ParseLogLine(line []byte) (*LogLine, error) {
|
func ParseLogLine(line []byte) (*LogLine, error) {
|
||||||
sl := bytes.Split(line, []byte(" "))
|
sl := bytes.Split(line, []byte(" "))
|
||||||
|
|
||||||
|
@ -108,10 +112,11 @@ func main() {
|
||||||
_, ltm, _ := lastTime.Clock()
|
_, ltm, _ := lastTime.Clock()
|
||||||
|
|
||||||
if llm != ltm {
|
if llm != ltm {
|
||||||
processBuckets(ab)
|
|
||||||
|
processBuckets(lastTime, ab)
|
||||||
ab = map[string]*Bucket{}
|
ab = map[string]*Bucket{}
|
||||||
|
|
||||||
lastTime = lastTime.Add(5 * time.Minute)
|
lastTime = lastTime.Add(time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
b := ab[ll.Host]
|
b := ab[ll.Host]
|
||||||
|
@ -129,31 +134,41 @@ func main() {
|
||||||
ab[ll.Host] = b
|
ab[ll.Host] = b
|
||||||
}
|
}
|
||||||
|
|
||||||
processBuckets(ab)
|
processBuckets(lastTime, 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 {
|
func toMS(dur time.Duration) int64 {
|
||||||
return dur.Nanoseconds() / 1000000
|
return dur.Nanoseconds() / 1000000
|
||||||
}
|
}
|
||||||
|
|
||||||
func processBuckets(set map[string]*Bucket) {
|
func contains(host string) bool {
|
||||||
hosts := []string{}
|
for _, val := range seenHosts {
|
||||||
for host, _ := range set {
|
if val == host {
|
||||||
hosts = append(hosts, host)
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(sort.StringSlice(hosts))
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func processBuckets(lt time.Time, set map[string]*Bucket) {
|
||||||
|
log.Printf("printing buckets for %s", lt.Format(outDateFormatNoSeconds))
|
||||||
|
for host, _ := range set {
|
||||||
|
if !contains(host) {
|
||||||
|
seenHosts = append(seenHosts, 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
|
||||||
|
}
|
||||||
|
|
||||||
for _, host := range hosts {
|
|
||||||
bucket := set[host]
|
|
||||||
var longest time.Duration
|
var longest time.Duration
|
||||||
var shortest time.Duration
|
var shortest time.Duration
|
||||||
var total time.Duration
|
var total time.Duration
|
||||||
|
|
10
main_test.go
10
main_test.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/kr/pretty"
|
"github.com/kr/pretty"
|
||||||
)
|
)
|
||||||
|
@ -26,12 +25,3 @@ func TestParseLogLine(t *testing.T) {
|
||||||
t.Fatal("invalid host")
|
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue