-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollinglog.go
66 lines (53 loc) · 1.41 KB
/
rollinglog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"container/list"
"time"
)
// LogEntry - a single entry plus timestamp
type LogEntry struct {
url string
timestamp time.Time
}
// RollingLog - Keeps a log of strings, descarding entries older than the specified duration
type RollingLog struct {
entries *list.List
keepfor time.Duration
}
// MakeRollingLog - makes a empty rolling log with the specified duration
func MakeRollingLog(k time.Duration) RollingLog {
return RollingLog{entries: list.New(),
keepfor: k}
}
// AddEntry adds a now entry to the rolling log, perhaps cleaning up old entries
func (rl *RollingLog) AddEntry(uri string) {
rl.trim()
newEntry := LogEntry{url: uri,
timestamp: time.Now()}
rl.entries.PushBack(newEntry)
}
// trims the entries at the start of the list if they fall before the cutoff duration
func (rl *RollingLog) trim() {
lastValidTime := time.Now().Add(-rl.keepfor)
e := rl.entries.Front()
for e != nil && e.Value.(LogEntry).timestamp.Before(lastValidTime) {
next := e.Next()
rl.entries.Remove(e)
e = next
}
}
// GetAllCounts - returns a map with all the count of values in the rolling log
func (rl *RollingLog) GetAllCounts() map[string]int {
rl.trim()
result := make(map[string]int)
for e := rl.entries.Front(); e != nil; e = e.Next() {
key := e.Value.(LogEntry).url
elem, ok := result[key]
if ok {
elem = elem + 1
} else {
elem = 1
}
result[key] = elem
}
return result
}