You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.1 KiB
Go

package client
import (
"fmt"
"strconv"
"strings"
"github.com/gosimple/slug"
)
// Value from ohwm
type Value struct {
Hardware []Hardware
Unit Sensor
Label string
Value string
}
// Float64 gets value as float64
func (v *Value) Float64() float64 {
val := strings.Split(v.Value, " ")
if len(val) == 0 {
return 0
}
ret, err := strconv.ParseFloat(val[0], 64)
if err != nil {
return 0
}
return ret
}
// MetricName returns value as a metric name
func (v *Value) MetricName() string {
segments := []string{"ohwm"}
for _, hw := range v.Hardware {
metricname, ok := hardwareToMetric[hw.Type]
if !ok {
metricname = hardwareToMetric[UnknownHardware]
}
segments = append(segments, metricname)
if hw.TypeCount > 1 {
segments = append(segments, fmt.Sprintf("%d", hw.TypeIndex))
}
}
sensorname, ok := sensorToMetric[v.Unit]
if !ok {
sensorname = sensorToMetric[UnknownSensor]
}
segments = append(segments, sensorname)
sluglabel := slug.Make(v.Label)
segments = append(segments, sluglabel)
ret := strings.Join(segments, "_")
return strings.ReplaceAll(ret, "-", "_")
}