prototype report page

This commit is contained in:
2025-12-28 14:38:06 -07:00
parent 49bbe281df
commit 051cf1da5d
13 changed files with 330 additions and 17 deletions
+65
View File
@@ -16,6 +16,12 @@ type (
db *pgxpool.Pool
}
// StoreWithContext allows currying all Store methods by context.Context
StoreWithContext struct {
ctx context.Context
db *Store
}
Event struct {
Platform string
StoreID string
@@ -51,6 +57,13 @@ func newPool(ctx context.Context) (*pgxpool.Pool, error) {
return pool, nil
}
func (db *Store) WithContext(ctx context.Context) *StoreWithContext {
return &StoreWithContext{
ctx: ctx,
db: db,
}
}
func (db *Store) Save(ctx context.Context, e *Event) error {
_, err := db.db.Exec(
ctx,
@@ -87,3 +100,55 @@ func (db *Store) Save(ctx context.Context, e *Event) error {
return nil
}
func (db *StoreWithContext) Save(e *Event) error {
return db.db.Save(db.ctx, e)
}
func (db *Store) LoadEventsForStore(
ctx context.Context,
platform string,
storeID string,
) ([]Event, error) {
rows, err := db.db.Query(
ctx,
`
SELECT
platform as Platform,
store_id as StoreID,
event_timestamp as EventTimestamp,
event_id as EventID,
raw_payload as Payload
FROM
raw_store_events
WHERE
platform = @platform
AND store_id = @store_id
ORDER BY
event_timestamp DESC
LIMIT
100
`,
pgx.NamedArgs{
"platform": platform,
"store_id": storeID,
},
)
if err != nil {
return nil, fmt.Errorf("failed to perform query: %w", err)
}
evts, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[Event])
if err != nil {
return nil, fmt.Errorf("failed to scan events: %w", err)
}
return evts, nil
}
func (db *StoreWithContext) LoadEventsForStore(
platform string,
storeID string,
) ([]Event, error) {
return db.db.LoadEventsForStore(db.ctx, platform, storeID)
}