Add function to tsdb.point to get line-protocol string in the correct units
parent
9cba278ca3
commit
6d4319d244
|
@ -460,7 +460,11 @@ func (p *Point) MarshalJSON() ([]byte, error) {
|
|||
}
|
||||
|
||||
func (p *Point) MarshalString() string {
|
||||
return tsdb.NewPoint(p.Measurement, p.Tags, p.Fields, p.Time).String()
|
||||
pt := tsdb.NewPoint(p.Measurement, p.Tags, p.Fields, p.Time)
|
||||
if p.Precision == "" || p.Precision == "ns" || p.Precision == "n" {
|
||||
return pt.String()
|
||||
}
|
||||
return pt.PrecisionString(p.Precision)
|
||||
}
|
||||
|
||||
// UnmarshalJSON decodes the data into the Point struct
|
||||
|
|
|
@ -33,7 +33,15 @@ type Point interface {
|
|||
Data() []byte
|
||||
SetData(buf []byte)
|
||||
|
||||
// String returns a string representation of the point object, if there is a
|
||||
// timestamp associated with the point then it will be specified with the default
|
||||
// precision of nanoseconds
|
||||
String() string
|
||||
|
||||
// PrecisionString returns a string representation of the point object, if there
|
||||
// is a timestamp associated with the point then it will be specified in the
|
||||
// given unit
|
||||
PrecisionString(precision string) string
|
||||
}
|
||||
|
||||
// Points represents a sortable list of points by timestamp.
|
||||
|
@ -1166,6 +1174,14 @@ func (p *point) String() string {
|
|||
return fmt.Sprintf("%s %s %d", p.Key(), string(p.fields), p.UnixNano())
|
||||
}
|
||||
|
||||
func (p *point) PrecisionString(precision string) string {
|
||||
if p.Time().IsZero() {
|
||||
return fmt.Sprintf("%s %s", p.Key(), string(p.fields))
|
||||
}
|
||||
return fmt.Sprintf("%s %s %d", p.Key(), string(p.fields),
|
||||
p.UnixNano()/p.GetPrecisionMultiplier(precision))
|
||||
}
|
||||
|
||||
func (p *point) unmarshalBinary() Fields {
|
||||
return newFieldsFromBinary(p.fields)
|
||||
}
|
||||
|
|
|
@ -1357,3 +1357,59 @@ func TestMakeKeyEscaped(t *testing.T) {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPrecisionString(t *testing.T) {
|
||||
tags := map[string]interface{}{"value": float64(1)}
|
||||
tm, _ := time.Parse(time.RFC3339Nano, "2000-01-01T12:34:56.789012345Z")
|
||||
tests := []struct {
|
||||
name string
|
||||
precision string
|
||||
exp string
|
||||
}{
|
||||
{
|
||||
name: "no precision",
|
||||
precision: "",
|
||||
exp: "cpu value=1 946730096789012345",
|
||||
},
|
||||
{
|
||||
name: "nanosecond precision",
|
||||
precision: "ns",
|
||||
exp: "cpu value=1 946730096789012345",
|
||||
},
|
||||
{
|
||||
name: "microsecond precision",
|
||||
precision: "u",
|
||||
exp: "cpu value=1 946730096789012",
|
||||
},
|
||||
{
|
||||
name: "millisecond precision",
|
||||
precision: "ms",
|
||||
exp: "cpu value=1 946730096789",
|
||||
},
|
||||
{
|
||||
name: "second precision",
|
||||
precision: "s",
|
||||
exp: "cpu value=1 946730096",
|
||||
},
|
||||
{
|
||||
name: "minute precision",
|
||||
precision: "m",
|
||||
exp: "cpu value=1 15778834",
|
||||
},
|
||||
{
|
||||
name: "hour precision",
|
||||
precision: "h",
|
||||
exp: "cpu value=1 262980",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
pt := tsdb.NewPoint("cpu", nil, tags, tm)
|
||||
act := pt.PrecisionString(test.precision)
|
||||
|
||||
if act != test.exp {
|
||||
t.Errorf("%s: PrecisionString() mismatch:\n actual: %v\n exp: %v",
|
||||
test.name, act, test.exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue