-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_collection.go
138 lines (113 loc) · 3.52 KB
/
result_collection.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
137
138
/*
* nagopher - Library for writing Nagios plugins in Go
* Copyright (C) 2018-2019 Pascal Mathis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package nagopher
import (
"github.com/markphelps/optional"
"sort"
)
// ResultCollection contains an arbitrary amount of Result instances and methods to sort them by relevance
type ResultCollection interface {
Add(results ...Result)
Get() []Result
Count() int
MostSignificantResult() OptionalResult
MostSignificantState() OptionalState
GetByMetricName(name string) OptionalResult
GetMetricByName(name string) OptionalMetric
GetNumericMetricValue(name string) optional.Float64
GetStringMetricValue(name string) optional.String
}
type resultCollection struct {
results []Result
}
// NewResultCollection instantiates a new ResultCollection object without any items
func NewResultCollection() ResultCollection {
return &resultCollection{}
}
func (c *resultCollection) Add(results ...Result) {
c.results = append(c.results, results...)
c.sort()
}
func (c resultCollection) Get() []Result {
return c.results
}
func (c resultCollection) Count() int {
return len(c.results)
}
func (c resultCollection) MostSignificantResult() OptionalResult {
if len(c.results) >= 1 {
return NewOptionalResult(c.results[0])
}
return OptionalResult{}
}
func (c resultCollection) MostSignificantState() OptionalState {
mostSignificantResult := c.MostSignificantResult()
if result, err := mostSignificantResult.Get(); err == nil {
return result.State()
}
return OptionalState{}
}
func (c resultCollection) GetByMetricName(name string) OptionalResult {
for _, result := range c.results {
metric, err := result.Metric().Get()
if err != nil || metric == nil {
continue
}
if metric.Name() == name {
return NewOptionalResult(result)
}
}
return OptionalResult{}
}
func (c resultCollection) GetMetricByName(name string) OptionalMetric {
result, err := c.GetByMetricName(name).Get()
if err == nil && result != nil {
return result.Metric()
}
return OptionalMetric{}
}
func (c resultCollection) GetNumericMetricValue(name string) optional.Float64 {
metric, err := c.GetMetricByName(name).Get()
if err == nil && metric != nil {
if numericMetric, ok := metric.(NumericMetric); ok {
return optional.NewFloat64(numericMetric.Value())
}
}
return optional.Float64{}
}
func (c resultCollection) GetStringMetricValue(name string) optional.String {
metric, err := c.GetMetricByName(name).Get()
if err == nil && metric != nil {
if stringMetric, ok := metric.(StringMetric); ok {
return optional.NewString(stringMetric.Value())
}
}
return optional.String{}
}
func (c *resultCollection) sort() {
sort.SliceStable(c.results, func(a int, b int) bool {
stateA, errA := c.results[a].State().Get()
stateB, errB := c.results[b].State().Get()
if errA == nil && errB != nil {
return true
} else if errA == nil && errB == nil {
return stateA.Priority() > stateB.Priority()
}
return false
})
}