Skip to content

Commit 4ad1e1d

Browse files
committed
perf(webapp): prune the live delivery hydration query to the visible days
getDeliveriesByFriendlyIds looked rows up by id only, so the live poll probed every retained daily partition every few seconds per open dashboard. The ids are time-encoded, so when they all decode we bound the query to the span of their mint timestamps (which equal createdAt), pruning to the visible page's few days. A legacy id in the set falls back to the unbounded lookup.
1 parent e002afc commit 4ad1e1d

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

apps/webapp/app/services/webhookDeliveriesRepository/clickhouseWebhookDeliveriesRepository.server.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import {
1717

1818
type DeliveryCursorRow = { deliveryId: string; createdAt: number };
1919

20-
const WEBHOOK_DELIVERY_ID_PREFIX = "whd_";
21-
2220
const DELIVERY_DETAIL_SELECT = {
2321
id: true,
2422
friendlyId: true,
@@ -306,19 +304,37 @@ export class ClickHouseWebhookDeliveriesRepository implements IWebhookDeliveries
306304
/**
307305
* Hydrate a known set of deliveries by friendlyId. Pure Postgres: the caller (the live poll)
308306
* already has the ids, so there is nothing for ClickHouse to filter or order.
307+
*
308+
* The ids are time-encoded (see `WebhookDeliveryId`), so when every id decodes we bound the query
309+
* to the span of their mint timestamps, which equal the rows' `createdAt`. That prunes the
310+
* RANGE-partitioned table to the visible page's few days instead of probing all of retention on
311+
* every poll. A mix that includes a legacy id falls back to the unbounded lookup.
309312
*/
310313
async getDeliveriesByFriendlyIds(
311314
options: GetDeliveriesByFriendlyIdsOptions
312315
): Promise<ListedWebhookDelivery[]> {
313-
const ids = options.friendlyIds.map((friendlyId) =>
314-
friendlyId.startsWith(WEBHOOK_DELIVERY_ID_PREFIX)
315-
? friendlyId.slice(WEBHOOK_DELIVERY_ID_PREFIX.length)
316-
: friendlyId
317-
);
316+
const ids = options.friendlyIds.map((friendlyId) => WebhookDeliveryId.toId(friendlyId));
318317
if (ids.length === 0) return [];
319318

319+
const timestamps = options.friendlyIds
320+
.map((friendlyId) => WebhookDeliveryId.parseTimestamp(friendlyId))
321+
.filter((value): value is Date => value != null);
322+
const createdAtBound =
323+
timestamps.length === options.friendlyIds.length
324+
? {
325+
createdAt: {
326+
gte: new Date(Math.min(...timestamps.map((value) => value.getTime()))),
327+
lte: new Date(Math.max(...timestamps.map((value) => value.getTime()))),
328+
},
329+
}
330+
: {};
331+
320332
return this.options.prisma.webhookDelivery.findMany({
321-
where: { id: { in: boundedIn(ids) }, runtimeEnvironmentId: options.environmentId },
333+
where: {
334+
id: { in: boundedIn(ids) },
335+
runtimeEnvironmentId: options.environmentId,
336+
...createdAtBound,
337+
},
322338
select: DELIVERY_LIST_SELECT,
323339
});
324340
}

0 commit comments

Comments
 (0)