refactor and add test

This commit is contained in:
reesporte 2021-11-16 14:53:30 -06:00
parent 9e041803fb
commit 1cc6a87d20
3 changed files with 28 additions and 3 deletions

View file

@ -332,7 +332,7 @@ func pgWriteGroupCount(w pg.QueryResultWriter, counts *pilosa.GroupCounts) error
switch {
case g.Value != nil:
if g.FieldOptions.Type == pilosa.FieldTypeTimestamp {
v = time.Unix(0, (int64(*g.Value)+int64(g.FieldOptions.Base))*pilosa.TimeUnitNanos(g.FieldOptions.TimeUnit)).UTC().Format(time.RFC3339Nano)
v = pilosa.FormatTimestampNano(int64(*g.Value), g.FieldOptions.Base, g.FieldOptions.TimeUnit)
} else {
v = strconv.FormatInt(*g.Value, 10)
}

11
util.go
View file

@ -115,8 +115,9 @@ func roaringFragmentHasData(path string, index, field, view string, shard uint64
return
}
// GetLoopProgress returns the estimated remaining time to iterate through some items
// as well as the loop completion percentage with the following parameters:
// GetLoopProgress returns the estimated remaining time to iterate through some
// items as well as the loop completion percentage with the following
// parameters:
// the start time, the current time, the iteration, and the number of items
func GetLoopProgress(start time.Time, now time.Time, iteration uint, total uint) (remaining time.Duration, pctDone float64) {
itemsLeft := total - (iteration + 1)
@ -124,3 +125,9 @@ func GetLoopProgress(start time.Time, now time.Time, iteration uint, total uint)
pctDone = (float64(iteration+1) / float64(total)) * 100
return time.Duration(avgItemTime * float64(itemsLeft)), pctDone
}
// FormatTimestampNano returns the string representation of a timestamp given:
// an epoch value, base, and time unit
func FormatTimestampNano(value, base int64, timeUnit string) string {
return time.Unix(0, (value+base)*TimeUnitNanos(timeUnit)).UTC().Format(time.RFC3339Nano)
}

View file

@ -85,3 +85,21 @@ func TestGetLoopProgress(t *testing.T) {
})
}
}
func TestFormatTimestampNano(t *testing.T) {
if FormatTimestampNano(0, 69, "s") != "1970-01-01T00:01:09Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(0, 420, "ms") != "1970-01-01T00:00:00.42Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(420, 0, "μs") != "1970-01-01T00:00:00.00000042Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(420, 69, "us") != "1970-01-01T00:00:00.000489Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(69, 420, "ns") != "1970-01-01T00:00:00.000000489Z" {
t.Fatal("Timestamp not formatted properly")
}
}