commons-go/model/data/Metric.go
crusader 8fd9425dee ing
2018-07-04 17:35:46 +09:00

74 lines
1.7 KiB
Go

package data
import (
"encoding/json"
"fmt"
"git.loafle.net/overflow/commons-go/core/util"
)
type Metric struct {
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"`
}
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
}