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

Commit

Permalink
storage: use ns precision in archive timestamp filtering
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Ángel Ortuño <[email protected]>
  • Loading branch information
ortuman committed Sep 19, 2022
1 parent 4611f36 commit c139aa9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 10 additions & 2 deletions pkg/storage/pgsql/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package pgsqlrepository
import (
"context"
"database/sql"
"fmt"
"strconv"
"time"

sq "github.com/Masterminds/squirrel"
Expand Down Expand Up @@ -169,10 +171,10 @@ 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) > ?", f.Start.GetSeconds()))
pred = append(pred, sq.Expr("EXTRACT(epoch FROM created_at) > ?", toEpoch(f.Start)))
}
if f.End != nil {
pred = append(pred, sq.Expr("EXTRACT(epoch FROM created_at) < ?", f.End.GetSeconds()))
pred = append(pred, sq.Expr("EXTRACT(epoch FROM created_at) < ?", toEpoch(f.End)))
}
return pred, nil
}
Expand Down Expand Up @@ -212,3 +214,9 @@ func scanArchiveMessage(scanner rowsScanner, archiveID string) (*archivemodel.Me

return &ret, nil
}

func toEpoch(tm *timestamppb.Timestamp) float64 {
str := fmt.Sprintf("%d.%d", tm.GetSeconds(), tm.GetNanos())
f, _ := strconv.ParseFloat(str, 64)
return f
}
6 changes: 3 additions & 3 deletions pkg/storage/pgsql/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ func TestPgSQLArchive_FetchArchiveMessages(t *testing.T) {
},
"by start timestamp": {
filters: &archivemodel.Filters{Start: timestamppb.New(starTm)},
withArgs: []driver.Value{"ortuman", starTm.Unix()},
withArgs: []driver.Value{"ortuman", toEpoch(timestamppb.New(starTm))},
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": {
filters: &archivemodel.Filters{End: timestamppb.New(endTm)},
withArgs: []driver.Value{"ortuman", endTm.Unix()},
withArgs: []driver.Value{"ortuman", 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\) ORDER BY created_at`,
},
"by start and end timestamp": {
filters: &archivemodel.Filters{Start: timestamppb.New(starTm), End: timestamppb.New(endTm)},
withArgs: []driver.Value{"ortuman", starTm.Unix(), endTm.Unix()},
withArgs: []driver.Value{"ortuman", toEpoch(timestamppb.New(starTm)), 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
8 changes: 4 additions & 4 deletions pkg/util/xmpp/xmpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func TestMakeDelayStanza(t *testing.T) {
msg, _ := b.BuildMessage()

// when
stamp, _ := time.Parse(time.RFC3339, "2021-02-15T15:00:00Z")
stamp, _ := time.Parse(time.RFC3339, "2021-02-15T15:00:00.250Z")
dMsg := MakeDelayMessage(msg, stamp, "jackal.im", "Delayed IQ")

// then
dChild := dMsg.Child("delay")
require.NotNil(t, dChild)
require.Equal(t, "jackal.im", dChild.Attribute(stravaganza.From))
require.Equal(t, "2021-02-15T15:00:00Z", dChild.Attribute("stamp"))
require.Equal(t, "2021-02-15T15:00:00.250Z", dChild.Attribute("stamp"))
require.Equal(t, "Delayed IQ", dChild.Text())
}

Expand Down Expand Up @@ -162,15 +162,15 @@ func TestMakeForwardedElement(t *testing.T) {
)
msg, _ := b.BuildMessage()

stamp, _ := time.Parse(time.RFC3339, "2021-02-15T15:00:00Z")
stamp, _ := time.Parse(time.RFC3339, "2021-02-15T15:00:00.125Z")
forwarded := MakeForwardedStanza(msg, &stamp)

// when
require.Equal(t, "urn:xmpp:forward:0", forwarded.Attribute(stravaganza.Namespace))

dChild := forwarded.Child("delay")
require.NotNil(t, dChild)
require.Equal(t, "2021-02-15T15:00:00Z", dChild.Attribute("stamp"))
require.Equal(t, "2021-02-15T15:00:00.125Z", dChild.Attribute("stamp"))

msgEl := forwarded.Child("message")
require.NotNil(t, msgEl)
Expand Down

0 comments on commit c139aa9

Please sign in to comment.