Skip to content

Commit

Permalink
Add function to tsdb.point to get line-protocol string in the correct…
Browse files Browse the repository at this point in the history
… units
  • Loading branch information
sparrc committed Sep 16, 2015
1 parent 9cba278 commit 6d4319d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion client/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tsdb/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
56 changes: 56 additions & 0 deletions tsdb/points_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 6d4319d

Please sign in to comment.