/
status_logs.go
109 lines (92 loc) · 2.49 KB
/
status_logs.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package domain
import (
"database/sql/driver"
"encoding/json"
"fmt"
"sort"
"time"
)
type StatusLogs struct {
Entries []*StatusLogEntry `json:"entries"`
}
func (self *StatusLogs) Len() int {
return len(self.Entries)
}
func (self *StatusLogs) Swap(i, j int) {
self.Entries[i], self.Entries[j] = self.Entries[j], self.Entries[i]
}
// Less compares status log entries by occurrence date, ascending.
func (self *StatusLogs) Less(i, j int) bool {
return self.Entries[i].OccurredOn.Before(self.Entries[j].OccurredOn)
}
type StatusLogEntry struct {
OccurredOn time.Time `json:"occurredOn"`
Type string `json:"type"`
Subject string `json:"subject"`
Body string `json:"body"`
}
// NewStatusLogEntry returns a new empty status log entry of the given
// type.
func NewStatusLogEntry(entryType string) *StatusLogEntry {
return &StatusLogEntry{
Type: entryType,
}
}
// NewStatusLogs returns an empty list of status logs.
func NewStatusLogs() *StatusLogs {
return &StatusLogs{
Entries: []*StatusLogEntry{},
}
}
// Log adds entry to the end of this log and sorts entries by
// ocurrence date.
func (self *StatusLogs) Log(entry *StatusLogEntry) *StatusLogs {
self.Entries = append(self.Entries, entry)
if len(self.Entries) == 1 {
return self
}
mostRecent := self.Entries[len(self.Entries)-2:]
if mostRecent[0].OccurredOn.After(mostRecent[1].OccurredOn) {
sort.Sort(self)
}
return self
}
// Newest returns the newest entry in this log going by the entries'
// occurrence date.
func (self *StatusLogs) Newest() *StatusLogEntry {
return self.Entries[len(self.Entries)-1]
}
// Value serializes this log as a JSON array.
func (self *StatusLogs) Value() (driver.Value, error) {
marshaled, err := json.Marshal(self.Entries)
if err != nil {
return nil, err
}
return driver.Value(marshaled), nil
}
// Scan deserializes value as a JSON array.
func (self *StatusLogs) Scan(data interface{}) error {
if self == nil {
*self = StatusLogs{}
}
src := []byte{}
switch raw := data.(type) {
case []byte:
src = raw
default:
return fmt.Errorf("StatusLogs: cannot scan from %T", data)
}
return json.Unmarshal(src, &self.Entries)
}
// HandleEvent adds new entries to the logs
func (self *StatusLogs) HandleEvent(payload EventPayload) {
if payload.Get("event") != "status" {
return
}
entryType := payload.Get("type")
newEntry := NewStatusLogEntry(entryType)
newEntry.Subject = payload.Get("subject")
newEntry.Body = payload.Get("body")
newEntry.OccurredOn = Clock.Now()
self.Log(newEntry)
}