2015-04-06 16:32:10 +00:00
|
|
|
package testutil
|
|
|
|
|
2015-05-27 05:14:42 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-20 12:32:32 +00:00
|
|
|
"reflect"
|
2015-05-27 05:14:42 +00:00
|
|
|
"time"
|
|
|
|
)
|
2015-04-06 21:53:43 +00:00
|
|
|
|
2015-04-06 16:32:10 +00:00
|
|
|
type Point struct {
|
2015-05-29 20:25:48 +00:00
|
|
|
Measurement string
|
|
|
|
Tags map[string]string
|
|
|
|
Values map[string]interface{}
|
|
|
|
Time time.Time
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Accumulator struct {
|
|
|
|
Points []*Point
|
|
|
|
}
|
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) Add(measurement string, value interface{}, tags map[string]string) {
|
2015-06-20 12:32:32 +00:00
|
|
|
if tags == nil {
|
|
|
|
tags = map[string]string{}
|
|
|
|
}
|
2015-05-27 05:14:42 +00:00
|
|
|
a.Points = append(
|
|
|
|
a.Points,
|
|
|
|
&Point{
|
2015-06-18 04:43:45 +00:00
|
|
|
Measurement: measurement,
|
2015-07-07 01:20:11 +00:00
|
|
|
Values: map[string]interface{}{"value": value},
|
2015-05-29 20:25:48 +00:00
|
|
|
Tags: tags,
|
2015-05-27 05:14:42 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Accumulator) AddValuesWithTime(
|
2015-05-29 20:25:48 +00:00
|
|
|
measurement string,
|
2015-05-27 05:14:42 +00:00
|
|
|
values map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
timestamp time.Time,
|
|
|
|
) {
|
|
|
|
a.Points = append(
|
|
|
|
a.Points,
|
|
|
|
&Point{
|
2015-05-29 20:25:48 +00:00
|
|
|
Measurement: measurement,
|
|
|
|
Values: values,
|
|
|
|
Tags: tags,
|
|
|
|
Time: timestamp,
|
2015-05-27 05:14:42 +00:00
|
|
|
},
|
|
|
|
)
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) Get(measurement string) (*Point, bool) {
|
2015-05-18 17:46:44 +00:00
|
|
|
for _, p := range a.Points {
|
2015-05-29 20:25:48 +00:00
|
|
|
if p.Measurement == measurement {
|
2015-05-18 17:46:44 +00:00
|
|
|
return p, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) CheckValue(measurement string, val interface{}) bool {
|
2015-04-06 16:32:10 +00:00
|
|
|
for _, p := range a.Points {
|
2015-05-29 20:25:48 +00:00
|
|
|
if p.Measurement == measurement {
|
2015-07-07 01:20:11 +00:00
|
|
|
return p.Values["value"] == val
|
2015-04-06 16:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2015-04-06 17:34:55 +00:00
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) CheckTaggedValue(measurement string, val interface{}, tags map[string]string) bool {
|
|
|
|
return a.ValidateTaggedValue(measurement, val, tags) == nil
|
2015-04-06 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) ValidateTaggedValue(measurement string, val interface{}, tags map[string]string) error {
|
2015-06-20 12:32:32 +00:00
|
|
|
if tags == nil {
|
|
|
|
tags = map[string]string{}
|
|
|
|
}
|
2015-04-06 17:34:55 +00:00
|
|
|
for _, p := range a.Points {
|
2015-06-20 12:32:32 +00:00
|
|
|
if !reflect.DeepEqual(tags, p.Tags) {
|
|
|
|
continue
|
2015-04-06 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-20 12:32:32 +00:00
|
|
|
if p.Measurement == measurement {
|
2015-07-07 01:20:11 +00:00
|
|
|
if p.Values["value"] != val {
|
|
|
|
return fmt.Errorf("%v (%T) != %v (%T)", p.Values["value"], p.Values["value"], val, val)
|
2015-04-06 21:53:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-04-06 17:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-20 12:32:32 +00:00
|
|
|
return fmt.Errorf("unknown measurement %s with tags %v", measurement, tags)
|
2015-04-06 17:34:55 +00:00
|
|
|
}
|
2015-04-07 18:54:21 +00:00
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) ValidateValue(measurement string, val interface{}) error {
|
|
|
|
return a.ValidateTaggedValue(measurement, val, nil)
|
2015-04-07 18:54:21 +00:00
|
|
|
}
|
2015-05-18 17:46:44 +00:00
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) HasIntValue(measurement string) bool {
|
2015-05-18 17:46:44 +00:00
|
|
|
for _, p := range a.Points {
|
2015-05-29 20:25:48 +00:00
|
|
|
if p.Measurement == measurement {
|
2015-07-07 01:20:11 +00:00
|
|
|
_, ok := p.Values["value"].(int64)
|
2015-05-18 17:46:44 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-08-04 13:53:45 +00:00
|
|
|
func (a *Accumulator) HasUIntValue(measurement string) bool {
|
|
|
|
for _, p := range a.Points {
|
|
|
|
if p.Measurement == measurement {
|
|
|
|
_, ok := p.Values["value"].(uint64)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-29 20:25:48 +00:00
|
|
|
func (a *Accumulator) HasFloatValue(measurement string) bool {
|
2015-05-18 17:46:44 +00:00
|
|
|
for _, p := range a.Points {
|
2015-05-29 20:25:48 +00:00
|
|
|
if p.Measurement == measurement {
|
2015-07-07 01:20:11 +00:00
|
|
|
_, ok := p.Values["value"].(float64)
|
2015-05-18 17:46:44 +00:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|