/
history.go
94 lines (80 loc) · 2.3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SWARM Distributed Monitoring System
// https://github.com/jbuchbinder/swarm-monitor
//
// vim: tabstop=2:softtabstop=2:shiftwidth=2:noexpandtab
package main
import (
"fmt"
"log"
"time"
"github.com/go-redis/redis/v8"
)
type HistoryEvent struct {
Timestamp time.Time
ID int64
HistoryKey string
SwarmHostID int
Host string
Check string
Status int32
StatusText string
// TODO: FIXME: More keys
}
func (ev *HistoryEvent) PersistAtomicHistoryKV(c *redis.Client, k string, v []byte) {
_, err := c.HSet(ctx, ev.HistoryKey, k, v).Result()
if err != nil {
log.Printf("ERR: " + fmt.Sprintf("Error persisting %s k %s v %s", ev.HistoryKey, k, v))
}
}
func (ev *HistoryEvent) PersistEvent(c *redis.Client) {
// Get new ever-incrementing event id
id, err := c.Incr(ctx, HISTORY_KEY).Result()
if err != nil {
log.Printf("ERR: " + err.Error())
return
}
// Persist to object internally
ev.ID = id
// New key
historyKey := HISTORY_BASE + ":id:" + fmt.Sprintf("%d", id)
ev.HistoryKey = historyKey
// Persist values to history key
ev.PersistAtomicHistoryKV(c, "timestamp", []byte(ev.Timestamp.String()))
// TODO: FIXME: More keys
// Build additional indices...
// 1. Master index.
_, err = c.ZAdd(ctx, HISTORY_LIST, &redis.Z{
Score: float64(ev.Timestamp.Unix()),
Member: []byte(historyKey),
}).Result()
if err != nil {
log.Printf("ERR: " + err.Error())
}
// 2. Date index
log.Printf("INFO: Logging to " + HISTORY_LIST + ":date:" + ev.Timestamp.Format("2006-01-02"))
_, err = c.ZAdd(ctx, HISTORY_LIST+":date:"+ev.Timestamp.Format("2006-01-02"), &redis.Z{
Score: float64(ev.Timestamp.Unix()),
Member: []byte(historyKey),
}).Result()
if err != nil {
log.Printf("ERR: " + err.Error())
}
// 3. Host index
log.Printf("INFO: Logging to " + HISTORY_LIST + ":host:" + ev.Host)
_, err = c.ZAdd(ctx, HISTORY_LIST+":host:"+ev.Host, &redis.Z{
Score: float64(ev.Timestamp.Unix()),
Member: []byte(historyKey),
}).Result()
if err != nil {
log.Printf("ERR: " + err.Error())
}
// 4. Check index
log.Printf("INFO: Logging to " + HISTORY_LIST + ":check:" + ev.Check)
_, err = c.ZAdd(ctx, HISTORY_LIST+":check:"+ev.Check, &redis.Z{
Score: float64(ev.Timestamp.Unix()),
Member: []byte(historyKey),
}).Result()
if err != nil {
log.Printf("ERR: " + err.Error())
}
}