Skip to content

Commit

Permalink
add hotness queries and scaffolding for materializer (#25)
Browse files Browse the repository at this point in the history
also reformatted some sql
  • Loading branch information
itstolf committed Aug 14, 2023
1 parent cd9fa99 commit 40a6f88
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 63 deletions.
20 changes: 20 additions & 0 deletions hotness/materializer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hotness

import (
"github.com/strideynet/bsky-furry-feed/store"
"go.uber.org/zap"
)

type Materializer struct {
log *zap.Logger
store *store.PGXStore
}

func NewMaterializer(
log *zap.Logger, store *store.PGXStore,
) *Materializer {
return &Materializer{
log: log,
store: store,
}
}
169 changes: 139 additions & 30 deletions store/gen/candidate_posts.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions store/gen/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions store/gen/post_hotness.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions store/migrations/000017_create_post_hotness.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE post_hotness;
10 changes: 10 additions & 0 deletions store/migrations/000017_create_post_hotness.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE post_hotness (
uri TEXT PRIMARY KEY,
alg TEXT NOT NULL,
score REAL NOT NULL,
generated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX post_hotness_alg_score_idx ON post_hotness (alg, score);
CREATE INDEX post_hotness_alg_generated_at_idx ON post_hotness (
alg, generated_at
);
8 changes: 8 additions & 0 deletions store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,3 +703,11 @@ func (s *PGXStore) GetPostByURI(ctx context.Context, uri string) (out gen.Candid
// TODO: Return a proto type rather than exposing gen.CandidatePost
return s.queries.GetPostByURI(ctx, s.pool, uri)
}

func (s *PGXStore) MaterializePostHotness(ctx context.Context, lookbackPeriod time.Duration) error {
return s.queries.MaterializePostHotness(ctx, s.pool, pgtype.Interval{Valid: true, Microseconds: lookbackPeriod.Microseconds()})
}

func (s *PGXStore) DeleteOldPostHotness(ctx context.Context, retentionPeriod time.Duration) error {
return s.queries.DeleteOldPostHotness(ctx, s.pool, pgtype.Interval{Valid: true, Microseconds: retentionPeriod.Microseconds()})
}
Loading

0 comments on commit 40a6f88

Please sign in to comment.