forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfaces.go
109 lines (89 loc) · 2.45 KB
/
interfaces.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 graph
import (
"github.com/gonum/graph"
)
// Marker is a struct that describes something interesting on a Node
type Marker struct {
// Node is the optional node that this message is attached to
Node graph.Node
// RelatedNodes is an optional list of other nodes that are involved in this marker.
RelatedNodes []graph.Node
// Severity indicates how important this problem is.
Severity Severity
// Key is a short string to identify this message
Key string
// Message is a human-readable string that describes what is interesting
Message string
// Suggestion is a human-readable string that holds advice for resolving this
// marker.
Suggestion Suggestion
}
// Severity indicates how important this problem is.
type Severity string
const (
// InfoSeverity is interesting
InfoSeverity Severity = "info"
// WarningSeverity is probably wrong, but we aren't certain
WarningSeverity Severity = "warning"
// ErrorSeverity is definitely wrong, this won't work
ErrorSeverity Severity = "error"
)
type Markers []Marker
// MarkerScanner is a function for analyzing a graph and finding interesting things in it
type MarkerScanner func(g Graph, f Namer) []Marker
func (m Markers) BySeverity(severity Severity) []Marker {
ret := []Marker{}
for i := range m {
if m[i].Severity == severity {
ret = append(ret, m[i])
}
}
return ret
}
type BySeverity []Marker
func (m BySeverity) Len() int { return len(m) }
func (m BySeverity) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m BySeverity) Less(i, j int) bool {
lhs := m[i]
rhs := m[j]
switch lhs.Severity {
case ErrorSeverity:
switch rhs.Severity {
case ErrorSeverity:
return false
}
case WarningSeverity:
switch rhs.Severity {
case ErrorSeverity, WarningSeverity:
return false
}
case InfoSeverity:
switch rhs.Severity {
case ErrorSeverity, WarningSeverity, InfoSeverity:
return false
}
}
return true
}
type ByNodeID []Marker
func (m ByNodeID) Len() int { return len(m) }
func (m ByNodeID) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m ByNodeID) Less(i, j int) bool {
if m[i].Node == nil {
return true
}
if m[j].Node == nil {
return false
}
return m[i].Node.ID() < m[j].Node.ID()
}
type ByKey []Marker
func (m ByKey) Len() int { return len(m) }
func (m ByKey) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m ByKey) Less(i, j int) bool {
return m[i].Key < m[j].Key
}
type Suggestion string
func (s Suggestion) String() string {
return string(s)
}