gomonop/pkg/perfdata/range.go
Emmanuel BENOîT 78af496fe9
All checks were successful
Run tests and linters / test (push) Successful in 44s
Run tests and linters / build (push) Successful in 47s
Run tests and linters / lint (push) Successful in 1m20s
refactor: make internals easier to test and add unit tests (#2)
This PR refactors most of the internals to make them easier to test (and also because the names didn't make sense). It adds unit tests for all internal components.

Reviewed-on: #2
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-20 10:01:05 +02:00

62 lines
1.3 KiB
Go

package perfdata // import nocternity.net/gomonop/pkg/perfdata
// Performance data range.
type Range struct {
start string
end string
inside bool
}
// Creates a performance data range from -inf to 0 and from the specified
// value to +inf.
func RangeMax(max string) *Range {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
pdRange := &Range{}
pdRange.start = "0"
pdRange.end = max
return pdRange
}
// Creates a performance data range from -inf to the specified minimal value
// and from the specified maximal value to +inf.
func RangeMinMax(min, max string) *Range {
if !valueCheck.MatchString(max) {
panic("invalid performance data range maximum value")
}
if !rangeMinCheck.MatchString(min) {
panic("invalid performance data range minimum value")
}
pdRange := &Range{}
pdRange.start = min
pdRange.end = max
return pdRange
}
// Inverts the range.
func (r *Range) Inside() *Range {
r.inside = true
return r
}
// Generates the range's string representation so it can be sent to the
// monitoring system.
func (r *Range) String() string {
var start, inside string
switch r.start {
case "":
start = "~"
case "0":
start = ""
default:
start = r.start
}
if r.inside {
inside = "@"
}
return inside + start + ":" + r.end
}