diff --git a/pkg/storage/pgsql/archive.go b/pkg/storage/pgsql/archive.go index 0586fc745..8774764a5 100644 --- a/pkg/storage/pgsql/archive.go +++ b/pkg/storage/pgsql/archive.go @@ -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))) @@ -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 } diff --git a/pkg/storage/pgsql/archive_test.go b/pkg/storage/pgsql/archive_test.go index 4d996e88b..843f88fcd 100644 --- a/pkg/storage/pgsql/archive_test.go +++ b/pkg/storage/pgsql/archive_test.go @@ -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": { @@ -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`, }, }