commons-go/model/data/Metric.go

74 lines
1.7 KiB
Go
Raw Normal View History

2018-05-11 05:02:01 +00:00
package data
2018-07-04 08:25:36 +00:00
import (
"encoding/json"
"fmt"
"git.loafle.net/overflow/commons-go/core/util"
)
2018-05-11 05:02:01 +00:00
type Metric struct {
2018-07-04 08:35:46 +00:00
SensorID string `json:"sensorID,omitempty"`
StartDate *util.Timestamp `json:"startDate,omitempty"`
EndDate *util.Timestamp `json:"endDate,omitempty"`
Success bool `json:"success,omitempty"`
// Status MetricStatusEnum `json:"status,omitempty"`
// Extra map[string]string `json:"extra,omitempty"`
Data map[string]string `json:"data,omitempty"`
2018-07-04 08:25:36 +00:00
}
type MetricStatusEnum int
const (
MetricStatusEnumOK MetricStatusEnum = iota + 1
MetricStatusEnumINFO
MetricStatusEnumWARN
MetricStatusEnumERROR
MetricStatusEnumCRITICAL
)
var (
metricStatusEnumID = map[MetricStatusEnum]string{
MetricStatusEnumOK: "OK",
MetricStatusEnumINFO: "INFO",
MetricStatusEnumWARN: "WARN",
MetricStatusEnumERROR: "ERROR",
MetricStatusEnumCRITICAL: "CRITICAL",
}
metricStatusEnumKey = map[string]MetricStatusEnum{
"OK": MetricStatusEnumOK,
"INFO": MetricStatusEnumINFO,
"WARN": MetricStatusEnumWARN,
"ERROR": MetricStatusEnumERROR,
"CRITICAL": MetricStatusEnumCRITICAL,
}
)
func (e MetricStatusEnum) String() string {
return metricStatusEnumID[e]
}
func (e MetricStatusEnum) MarshalJSON() ([]byte, error) {
value, ok := metricStatusEnumID[e]
if !ok {
return nil, fmt.Errorf("Invalid EnumType[%s] value", e)
}
return json.Marshal(value)
}
func (e MetricStatusEnum) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
value, ok := metricStatusEnumKey[s]
if !ok {
return fmt.Errorf("Invalid EnumType[%s] value", s)
}
e = value
return nil
2018-05-11 05:02:01 +00:00
}