Skip to content

Commit 909d177

Browse files
committed
Adjusts post metrics job to take scheduled posts into account
1 parent 40b272c commit 909d177

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/server/metrics/post.metrics.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { PostMetric } from '~/shared/utils/prisma/models';
77
import dayjs from 'dayjs';
88
import { templateHandler } from '~/server/db/db-helpers';
99
import { isDefined } from '~/utils/type-guards';
10-
import { capitalize } from '~/utils/string-helpers';
1110
import { getJobDate } from '~/server/jobs/job';
11+
import { capitalize } from '~/utils/string-helpers';
1212

1313
const log = createLogger('metrics:post');
1414

15-
const timePeriods = ['day', 'week', 'month', 'year', 'allTime'] as const;
15+
const timePeriods = ['now', 'day', 'week', 'month', 'year', 'allTime'] as const;
1616

1717
export const postMetrics = createMetricProcessor({
1818
name: 'Post',
@@ -91,8 +91,14 @@ export const postMetrics = createMetricProcessor({
9191
if (timePeriod === 'allTime') continue;
9292

9393
const windowDuration = lastRunStart - ctx.lastUpdate.valueOf();
94-
const windowEnd = dayjs(lastRunStart).subtract(1, timePeriod).valueOf();
95-
const windowStart = windowEnd - windowDuration;
94+
const windowEnd =
95+
timePeriod === 'now'
96+
? dayjs(lastRunStart).valueOf()
97+
: dayjs(lastRunStart).subtract(1, timePeriod).valueOf();
98+
const windowStart =
99+
timePeriod === 'now'
100+
? dayjs(windowEnd).subtract(1, 'day').valueOf() - windowDuration
101+
: windowEnd - windowDuration;
96102

97103
// Fetch affected posts between the timeframe
98104
const affectedPostIdsQuery = await ctx.pg.cancellableQuery<{ id: number }>(`

0 commit comments

Comments
 (0)