Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

storage/archive: fix timestamp range filtering #257

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/storage/pgsql/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ func filtersToPred(f *archivemodel.Filters, archiveID string) (interface{}, erro

// filtering by timestamp
if f.Start != nil {
pred = append(pred, sq.Expr("EXTRACT(epoch FROM created_at) > ?", toEpoch(f.Start)))
// due to higher precision of database timestamp we need to add an extra offset to discard first message.
epoch := toEpoch(f.Start)
epoch += float64(time.Millisecond)

pred = append(pred, sq.Expr("EXTRACT(epoch FROM created_at) > ?", epoch))
}
if f.End != nil {
pred = append(pred, sq.Expr("EXTRACT(epoch FROM created_at) < ?", toEpoch(f.End)))
Expand Down Expand Up @@ -216,9 +220,6 @@ func scanArchiveMessage(scanner rowsScanner, archiveID string) (*archivemodel.Me
}

func toEpoch(tm *timestamppb.Timestamp) float64 {
sec := tm.GetSeconds()
ns := tm.GetNanos() + int32(time.Millisecond) // add a millisecond offset to avoid rounding errors

f, _ := strconv.ParseFloat(fmt.Sprintf("%d.%d", sec, ns), 64)
f, _ := strconv.ParseFloat(fmt.Sprintf("%d.%d", tm.GetSeconds(), tm.GetNanos()), 64)
return f
}
4 changes: 2 additions & 2 deletions pkg/storage/pgsql/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestPgSQLArchive_FetchArchiveMessages(t *testing.T) {
},
"by start timestamp": {
filters: &archivemodel.Filters{Start: timestamppb.New(starTm)},
withArgs: []driver.Value{"ortuman", toEpoch(timestamppb.New(starTm))},
withArgs: []driver.Value{"ortuman", toEpoch(timestamppb.New(starTm)) + float64(time.Millisecond)},
expectQuery: `SELECT id, "from", "to", message, created_at FROM archives WHERE \(archive_id = \$1 AND EXTRACT\(epoch FROM created_at\) > \$2\) ORDER BY created_at`,
},
"by end timestamp": {
Expand All @@ -140,7 +140,7 @@ func TestPgSQLArchive_FetchArchiveMessages(t *testing.T) {
},
"by start and end timestamp": {
filters: &archivemodel.Filters{Start: timestamppb.New(starTm), End: timestamppb.New(endTm)},
withArgs: []driver.Value{"ortuman", toEpoch(timestamppb.New(starTm)), toEpoch(timestamppb.New(endTm))},
withArgs: []driver.Value{"ortuman", toEpoch(timestamppb.New(starTm)) + float64(time.Millisecond), toEpoch(timestamppb.New(endTm))},
expectQuery: `SELECT id, "from", "to", message, created_at FROM archives WHERE \(archive_id = \$1 AND EXTRACT\(epoch FROM created_at\) > \$2 AND EXTRACT\(epoch FROM created_at\) < \$3\) ORDER BY created_at`,
},
}
Expand Down