package perfdata // import nocternity.net/gomonop/pkg/perfdata import ( "fmt" "strconv" ) // 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{} if min == "~" { min = "" } 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 } // Contains checks whether a numeric value is within the range. func (r *Range) Contains(value float64) bool { var inStart, inEnd bool if r.start != "" { startValue, err := strconv.ParseFloat(r.start, 64) if err != nil { panic(fmt.Sprintf("invalid performance data range start value: %v", err)) } inStart = value < startValue } if r.end != "" { endValue, err := strconv.ParseFloat(r.end, 64) if err != nil { panic(fmt.Sprintf("invalid performance data range end value: %v", err)) } inEnd = value > endValue } return (inStart || inEnd) != r.inside }