-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_timeline.go
55 lines (41 loc) · 1.04 KB
/
cmd_timeline.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
package main
import (
"time"
"github.com/asdine/storm/q"
)
// addTimelineEntry adds a new entry to the timeline based on the last timestamp
func (u *urlTester) addTimelineEntry(id, status int) (diff int64, err error) {
var (
entry timeline
)
entry.MonitorID = id
entry.Status = status
entry.Timestamp = time.Now().Unix()
if status == statusUp {
entry.Downtime = entry.Timestamp - u.lastStatus[id].Timestamp
diff = entry.Downtime
}
err = u.db.Save(&entry)
u.Lock()
u.lastStatus[id] = entry
u.Unlock()
return
}
// getLastTimelineEntry returns the last entry or a fake one to ensure one will be created
func (u *urlTester) getLastTimelineEntry(id int) (entry timeline) {
var err error
// get the last timeline entry for the monitor
var entries []timeline
err = u.db.Select(q.Eq("MonitorID", id)).OrderBy("Timestamp").Limit(1).Reverse().Find(&entries)
if len(entries) == 1 {
entry = entries[0]
}
if err != nil {
// create a new fake entry
entry = timeline{
MonitorID: id,
Timestamp: time.Now().Unix(),
}
}
return
}