forked from openshift/origin
/
types.go
95 lines (76 loc) · 2 KB
/
types.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
package monitor
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
)
type SamplerFunc func(time.Time) []*Condition
type Interface interface {
Events(from, to time.Time) EventIntervals
Conditions(from, to time.Time) EventIntervals
}
type Recorder interface {
Record(conditions ...Condition)
AddSampler(fn SamplerFunc)
}
type EventLevel int
const (
Info EventLevel = iota
Warning
Error
)
var eventString = []string{
"I",
"W",
"E",
}
type Event struct {
Condition
At time.Time
}
func (e *Event) String() string {
return fmt.Sprintf("%s.%03d %s %s %s", e.At.Format("Jan 02 15:04:05"), e.At.Nanosecond()/1000000, eventString[e.Level], e.Locator, strings.Replace(e.Message, "\n", "\\n", -1))
}
type sample struct {
at time.Time
conditions []*Condition
}
type Condition struct {
Level EventLevel
Locator string
Message string
}
type EventInterval struct {
*Condition
From time.Time
To time.Time
}
func (i *EventInterval) String() string {
if i.From.Equal(i.To) {
return fmt.Sprintf("%s.%03d %s %s %s", i.From.Format("Jan 02 15:04:05"), i.From.Nanosecond()/int(time.Millisecond), eventString[i.Level], i.Locator, strings.Replace(i.Message, "\n", "\\n", -1))
}
return fmt.Sprintf("%s.%03d - %-5s %s %s %s", i.From.Format("Jan 02 15:04:05"), i.From.Nanosecond()/int(time.Millisecond), strconv.Itoa(int(i.To.Sub(i.From)/time.Second))+"s", eventString[i.Level], i.Locator, strings.Replace(i.Message, "\n", "\\n", -1))
}
type EventIntervals []*EventInterval
var _ sort.Interface = EventIntervals{}
func (intervals EventIntervals) Less(i, j int) bool {
switch d := intervals[i].From.Sub(intervals[j].From); {
case d < 0:
return true
case d > 0:
return false
}
switch d := intervals[i].To.Sub(intervals[j].To); {
case d < 0:
return true
case d > 0:
return false
}
return intervals[i].Message < intervals[j].Message
}
func (intervals EventIntervals) Len() int { return len(intervals) }
func (intervals EventIntervals) Swap(i, j int) {
intervals[i], intervals[j] = intervals[j], intervals[i]
}