forked from open-falcon/falcon-plus
/
history.go
65 lines (55 loc) · 1.4 KB
/
history.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
package store
import (
cmodel "github.com/open-falcon/falcon-plus/common/model"
tlist "github.com/toolkits/container/list"
tmap "github.com/toolkits/container/nmap"
)
const (
defaultHistorySize = 3
)
var (
// mem: front = = = back
// time: latest ... old
HistoryCache = tmap.NewSafeMap()
)
func GetLastItem(key string) *cmodel.GraphItem {
itemlist, found := HistoryCache.Get(key)
if !found || itemlist == nil {
return &cmodel.GraphItem{}
}
first := itemlist.(*tlist.SafeListLimited).Front()
if first == nil {
return &cmodel.GraphItem{}
}
return first.(*cmodel.GraphItem)
}
func GetAllItems(key string) []*cmodel.GraphItem {
ret := make([]*cmodel.GraphItem, 0)
itemlist, found := HistoryCache.Get(key)
if !found || itemlist == nil {
return ret
}
all := itemlist.(*tlist.SafeListLimited).FrontAll()
for _, item := range all {
if item == nil {
continue
}
ret = append(ret, item.(*cmodel.GraphItem))
}
return ret
}
func AddItem(key string, val *cmodel.GraphItem) {
itemlist, found := HistoryCache.Get(key)
var slist *tlist.SafeListLimited
if !found {
slist = tlist.NewSafeListLimited(defaultHistorySize)
HistoryCache.Put(key, slist)
} else {
slist = itemlist.(*tlist.SafeListLimited)
}
// old item should be drop
first := slist.Front()
if first == nil || first.(*cmodel.GraphItem).Timestamp < val.Timestamp { // first item or latest one
slist.PushFrontViolently(val)
}
}