Deduplicate performance snapshots: one row per platform per day
Added snapshot_date column with unique constraint on (platform_id, snapshot_date). Worker now upserts instead of inserting, so repeated scrapes on the same day update the existing row rather than creating duplicates that cause inflated chart values. Cleaned up existing duplicate rows in migration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -42,6 +42,7 @@ export const performanceSnapshots = pgTable('performance_snapshots', {
|
||||
.notNull()
|
||||
.references(() => platforms.id),
|
||||
capturedAt: timestamptz('captured_at').defaultNow().notNull(),
|
||||
snapshotDate: date('snapshot_date').defaultNow().notNull(),
|
||||
periodLabel: varchar('period_label').notNull(),
|
||||
viewsSearch: integer('views_search'),
|
||||
viewsListing: integer('views_listing'),
|
||||
@@ -51,7 +52,9 @@ export const performanceSnapshots = pgTable('performance_snapshots', {
|
||||
avgDailyRate: numeric('avg_daily_rate'),
|
||||
revenueTotal: numeric('revenue_total'),
|
||||
rawJson: jsonb('raw_json'),
|
||||
});
|
||||
}, (table) => [
|
||||
unique('perf_snap_platform_date_uniq').on(table.platformId, table.snapshotDate),
|
||||
]);
|
||||
|
||||
export const performanceSnapshotsRelations = relations(
|
||||
performanceSnapshots,
|
||||
|
||||
@@ -122,7 +122,7 @@ export default async function (fastify: FastifyInstance) {
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
date: performanceSnapshots.periodLabel,
|
||||
date: performanceSnapshots.snapshotDate,
|
||||
capturedAt: performanceSnapshots.capturedAt,
|
||||
platformId: performanceSnapshots.platformId,
|
||||
viewsSearch: performanceSnapshots.viewsSearch,
|
||||
|
||||
Reference in New Issue
Block a user