-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add hotness queries and periodic materializer (#25)
also reformatted some sql
- Loading branch information
Showing
12 changed files
with
466 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package hotness | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/strideynet/bsky-furry-feed/store" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Materializer struct { | ||
log *zap.Logger | ||
store *store.PGXStore | ||
opts Opts | ||
} | ||
|
||
type Opts struct { | ||
MaterializationPeriod time.Duration | ||
RetentionPeriod time.Duration | ||
LookbackPeriod time.Duration | ||
} | ||
|
||
func NewMaterializer( | ||
log *zap.Logger, store *store.PGXStore, opts Opts, | ||
) *Materializer { | ||
return &Materializer{ | ||
log: log, | ||
store: store, | ||
opts: opts, | ||
} | ||
} | ||
|
||
func (m *Materializer) materialize(ctx context.Context) error { | ||
return m.store.MaterializeClassicPostHotness(ctx, m.opts.LookbackPeriod) | ||
} | ||
|
||
func (m *Materializer) cleanup(ctx context.Context) error { | ||
return m.store.DeleteOldPostHotness(ctx, m.opts.RetentionPeriod) | ||
} | ||
|
||
func (m *Materializer) step(ctx context.Context) error { | ||
// NOTE: materalize and cleanup don't run a transaction together (they don't need to, since it's okay if we keep old materialized results around for too long). | ||
// However, we should do the cleanup _after_ the materialization, in case the materialization fails and we converge on purging the entire table. | ||
if err := m.materialize(ctx); err != nil { | ||
return fmt.Errorf("materialize: %w", err) | ||
} | ||
if err := m.cleanup(ctx); err != nil { | ||
return fmt.Errorf("cleanup: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *Materializer) Run(ctx context.Context) error { | ||
t := time.NewTicker(m.opts.MaterializationPeriod) | ||
for { | ||
select { | ||
case <-t.C: | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
|
||
if err := m.step(ctx); err != nil { | ||
m.log.Error("step", zap.Error(err)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP SEQUENCE post_hotness_generation_seq; | ||
DROP TABLE post_hotness; |
Oops, something went wrong.