forked from evergreen-ci/logkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_models.go
136 lines (119 loc) · 3.39 KB
/
log_models.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package logkeeper
import (
"math"
"regexp"
"time"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"gopkg.in/mgo.v2/bson"
)
var colorRegex *regexp.Regexp = regexp.MustCompile(`([ \w]{2}\d{1,5}\|)`)
type LogLine []interface{}
type LogLineItem struct {
LineNum int
Timestamp time.Time
Data string
TestId *bson.ObjectId
}
// Global returns true if this log line comes from a global log, otherwise false (from a test log).
func (lli LogLineItem) Global() bool {
return lli.TestId == nil
}
//A "log" doc looks like this:
/*
{
"_id" : ObjectId("52e74ffd30dfa32be4877f47"),
"build_id" : ObjectId("52e74d583ae7400f1a000001"),
"test_id" : ObjectId("52e74ffb3ae74013e2000001"),
"seq" : 1,
"started" : null,
"lines" : [
[ ISODate("2014-01-28T06:36:43Z"), "log line 1..." ],
[ ISODate("2014-01-28T06:36:43Z"), "log line 2..." ],
[ ISODate("2014-01-28T06:36:43Z"), "log line 3..." ] //etc
...
]
}*/
type Log struct {
BuildId interface{} `bson:"build_id"`
TestId *bson.ObjectId `bson:"test_id"`
Seq int `bson:"seq"`
Started *time.Time `bson:"started"`
Lines []LogLine `bson:"lines"`
}
func NewLogLine(data []interface{}) *LogLine {
// timeField is generated client-side as the output of python's time.time(), which returns
// seconds since epoch as a floating point number
timeField, ok := data[0].(float64)
if !ok {
grip.Critical(message.Fields{
"message": "unable to convert time field",
"value": data[0],
})
timeField = float64(time.Now().Unix())
}
// extract fractional seconds from the total time and convert to nanoseconds
fractionalPart := timeField - math.Floor(timeField)
nSecPart := int64(fractionalPart * float64(int64(time.Second)/int64(time.Nanosecond)))
timeParsed := time.Unix(int64(timeField), nSecPart)
return &LogLine{timeParsed, data[1].(string)}
}
func (s LogLine) Time() time.Time {
return (s[0]).(time.Time)
}
func (s LogLine) Msg() string {
return (s[1]).(string)
}
func (self *LogLineItem) Color() string {
found := colorRegex.FindStringSubmatch(self.Data)
if len(found) > 0 {
return found[0]
} else {
return ""
}
}
func (self *LogLineItem) OlderThanThreshold(previousItem interface{}) bool {
if previousItem == nil {
return true
}
if previousLogLine, ok := previousItem.(*LogLineItem); ok {
diff := self.Timestamp.Sub(previousLogLine.Timestamp)
if diff > 1*time.Second {
return true
} else {
return false
}
}
return true
}
// MergeLog takes two channels of LogLineItem and returns a single channel that feeds
// the result of merging the two input channels sorted by timestamp.
func MergeLog(logger1 chan *LogLineItem, logger2 chan *LogLineItem) chan *LogLineItem {
outputChan := make(chan *LogLineItem)
go func() {
next1, ok1 := <-logger1
next2, ok2 := <-logger2
for {
if !ok1 && !ok2 { // both channels are empty - so stop.
close(outputChan)
return
}
if !ok2 { // only channel 1 had a value, so send that to output
outputChan <- next1
next1, ok1 = <-logger1 // get the next item from chan 1
} else if !ok1 { // only channel 2 had a value, so send that to output
outputChan <- next2
next2, ok2 = <-logger2 // get the next item from chan 2
} else {
if next1.Timestamp.Before(next2.Timestamp) {
outputChan <- next1
next1, ok1 = <-logger1
} else {
outputChan <- next2
next2, ok2 = <-logger2
}
}
}
}()
return outputChan
}