Merge pull request #2109 from influxdata/feat/si-time-units

feat(models): only use common SI time units for timestamp precision
pull/10616/head
Chris Goller 2018-12-21 09:22:14 -06:00 committed by GitHub
commit d4467949aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 63 deletions

View File

@ -739,7 +739,6 @@ paths:
enum: enum:
- ns - ns
- us - us
- u
- ms - ms
- s - s
responses: responses:

View File

@ -335,7 +335,7 @@ func ParseName(buf []byte) []byte {
// ValidPrecision checks if the precision is known. // ValidPrecision checks if the precision is known.
func ValidPrecision(precision string) bool { func ValidPrecision(precision string) bool {
switch precision { switch precision {
case "n", "ns", "u", "us", "ms", "s": case "ns", "us", "ms", "s":
return true return true
default: default:
return false return false
@ -479,16 +479,12 @@ func parsePoint(buf []byte, defaultTime time.Time, precision string) (Point, err
func GetPrecisionMultiplier(precision string) int64 { func GetPrecisionMultiplier(precision string) int64 {
d := time.Nanosecond d := time.Nanosecond
switch precision { switch precision {
case "u": case "us":
d = time.Microsecond d = time.Microsecond
case "ms": case "ms":
d = time.Millisecond d = time.Millisecond
case "s": case "s":
d = time.Second d = time.Second
case "m":
d = time.Minute
case "h":
d = time.Hour
} }
return int64(d) return int64(d)
} }
@ -1676,17 +1672,12 @@ func (p *point) Fields() (Fields, error) {
// SetPrecision will round a time to the specified precision. // SetPrecision will round a time to the specified precision.
func (p *point) SetPrecision(precision string) { func (p *point) SetPrecision(precision string) {
switch precision { switch precision {
case "n": case "us":
case "u":
p.SetTime(p.Time().Truncate(time.Microsecond)) p.SetTime(p.Time().Truncate(time.Microsecond))
case "ms": case "ms":
p.SetTime(p.Time().Truncate(time.Millisecond)) p.SetTime(p.Time().Truncate(time.Millisecond))
case "s": case "s":
p.SetTime(p.Time().Truncate(time.Second)) p.SetTime(p.Time().Truncate(time.Second))
case "m":
p.SetTime(p.Time().Truncate(time.Minute))
case "h":
p.SetTime(p.Time().Truncate(time.Hour))
} }
} }

View File

@ -266,7 +266,7 @@ func BenchmarkParsePointWithPrecisionN(b *testing.B) {
line := `cpu value=1i 1000000000` line := `cpu value=1i 1000000000`
defaultTime := time.Now().UTC() defaultTime := time.Now().UTC()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
models.ParsePointsWithPrecision([]byte(line), defaultTime, "n") models.ParsePointsWithPrecision([]byte(line), defaultTime, "ns")
b.SetBytes(int64(len(line))) b.SetBytes(int64(len(line)))
} }
} }
@ -275,7 +275,7 @@ func BenchmarkParsePointWithPrecisionU(b *testing.B) {
line := `cpu value=1i 1000000000` line := `cpu value=1i 1000000000`
defaultTime := time.Now().UTC() defaultTime := time.Now().UTC()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
models.ParsePointsWithPrecision([]byte(line), defaultTime, "u") models.ParsePointsWithPrecision([]byte(line), defaultTime, "us")
b.SetBytes(int64(len(line))) b.SetBytes(int64(len(line)))
} }
} }
@ -365,7 +365,7 @@ func NewTestPoint(name string, tags models.Tags, fields models.Fields, time time
} }
func test(t *testing.T, line string, point TestPoint) { func test(t *testing.T, line string, point TestPoint) {
pts, err := models.ParsePointsWithPrecision([]byte(line), time.Unix(0, 0), "n") pts, err := models.ParsePointsWithPrecision([]byte(line), time.Unix(0, 0), "ns")
if err != nil { if err != nil {
t.Fatalf(`ParsePoints("%s") mismatch. got %v, exp nil`, line, err) t.Fatalf(`ParsePoints("%s") mismatch. got %v, exp nil`, line, err)
} }
@ -1783,13 +1783,13 @@ func TestParsePointsWithPrecision(t *testing.T) {
{ {
name: "nanosecond", name: "nanosecond",
line: `cpu,host=serverA,region=us-east value=1.0 946730096789012345`, line: `cpu,host=serverA,region=us-east value=1.0 946730096789012345`,
precision: "n", precision: "ns",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345", exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
}, },
{ {
name: "microsecond", name: "microsecond",
line: `cpu,host=serverA,region=us-east value=1.0 946730096789012`, line: `cpu,host=serverA,region=us-east value=1.0 946730096789012`,
precision: "u", precision: "us",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012000", exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012000",
}, },
{ {
@ -1804,18 +1804,6 @@ func TestParsePointsWithPrecision(t *testing.T) {
precision: "s", precision: "s",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096000000000", exp: "cpu,host=serverA,region=us-east value=1.0 946730096000000000",
}, },
{
name: "minute",
line: `cpu,host=serverA,region=us-east value=1.0 15778834`,
precision: "m",
exp: "cpu,host=serverA,region=us-east value=1.0 946730040000000000",
},
{
name: "hour",
line: `cpu,host=serverA,region=us-east value=1.0 262980`,
precision: "h",
exp: "cpu,host=serverA,region=us-east value=1.0 946728000000000000",
},
} }
for _, test := range tests { for _, test := range tests {
pts, err := models.ParsePointsWithPrecision([]byte(test.line), time.Now().UTC(), test.precision) pts, err := models.ParsePointsWithPrecision([]byte(test.line), time.Now().UTC(), test.precision)
@ -1849,12 +1837,12 @@ func TestParsePointsWithPrecisionNoTime(t *testing.T) {
}, },
{ {
name: "nanosecond precision", name: "nanosecond precision",
precision: "n", precision: "ns",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345", exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012345",
}, },
{ {
name: "microsecond precision", name: "microsecond precision",
precision: "u", precision: "us",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012000", exp: "cpu,host=serverA,region=us-east value=1.0 946730096789012000",
}, },
{ {
@ -1867,16 +1855,6 @@ func TestParsePointsWithPrecisionNoTime(t *testing.T) {
precision: "s", precision: "s",
exp: "cpu,host=serverA,region=us-east value=1.0 946730096000000000", exp: "cpu,host=serverA,region=us-east value=1.0 946730096000000000",
}, },
{
name: "minute precision",
precision: "m",
exp: "cpu,host=serverA,region=us-east value=1.0 946730040000000000",
},
{
name: "hour precision",
precision: "h",
exp: "cpu,host=serverA,region=us-east value=1.0 946728000000000000",
},
} }
for _, test := range tests { for _, test := range tests {
@ -2041,7 +2019,7 @@ func TestPrecisionString(t *testing.T) {
}, },
{ {
name: "microsecond precision", name: "microsecond precision",
precision: "u", precision: "us",
exp: "cpu value=1 946730096789012", exp: "cpu value=1 946730096789012",
}, },
{ {
@ -2054,16 +2032,6 @@ func TestPrecisionString(t *testing.T) {
precision: "s", precision: "s",
exp: "cpu value=1 946730096", 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 { for _, test := range tests {
@ -2110,16 +2078,6 @@ func TestRoundedString(t *testing.T) {
precision: time.Second, precision: time.Second,
exp: "cpu value=1 946730097000000000", exp: "cpu value=1 946730097000000000",
}, },
{
name: "minute precision",
precision: time.Minute,
exp: "cpu value=1 946730100000000000",
},
{
name: "hour precision",
precision: time.Hour,
exp: "cpu value=1 946731600000000000",
},
} }
for _, test := range tests { for _, test := range tests {