Skip to content

Commit 6f111ad

Browse files
authored
fix: keep a table of latest stacks node event timestamps (#2266)
1 parent 78ebad0 commit 6f111ad

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable camelcase */
2+
3+
exports.shorthands = undefined;
4+
5+
exports.up = pgm => {
6+
pgm.createTable('event_observer_timestamps', {
7+
event_path: {
8+
type: 'string',
9+
primaryKey: true,
10+
},
11+
id: {
12+
type: 'bigint',
13+
notNull: true,
14+
},
15+
receive_timestamp: {
16+
type: 'timestamptz',
17+
notNull: true,
18+
},
19+
});
20+
};

src/datastore/pg-store.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4684,9 +4684,7 @@ export class PgStore extends BasePgStore {
46844684
/// by event type.
46854685
async getLastStacksNodeEventTimestamps() {
46864686
return await this.sql<{ event_path: string; receive_timestamp: Date }[]>`
4687-
SELECT DISTINCT ON (event_path) event_path, receive_timestamp
4688-
FROM event_observer_requests
4689-
ORDER BY event_path, receive_timestamp DESC
4687+
SELECT event_path, receive_timestamp FROM event_observer_timestamps
46904688
`;
46914689
}
46924690
}

src/datastore/pg-write-store.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,20 @@ export class PgWriteStore extends PgStore {
178178
receive_timestamp: string;
179179
event_path: string;
180180
}[]
181-
>`INSERT INTO event_observer_requests(
182-
event_path, payload
183-
) values(${eventPath}, ${payload})
184-
RETURNING id, receive_timestamp::text, event_path
181+
>`WITH inserted AS (
182+
INSERT INTO event_observer_requests(
183+
event_path, payload
184+
) values(${eventPath}, ${payload})
185+
RETURNING id, receive_timestamp, event_path
186+
),
187+
latest AS (
188+
INSERT INTO event_observer_timestamps (id, receive_timestamp, event_path)
189+
(SELECT id, receive_timestamp, event_path FROM inserted)
190+
ON CONFLICT (event_path) DO UPDATE SET
191+
id = EXCLUDED.id,
192+
receive_timestamp = EXCLUDED.receive_timestamp
193+
)
194+
SELECT id, receive_timestamp::text, event_path FROM inserted
185195
`;
186196
if (insertResult.length !== 1) {
187197
throw new Error(

0 commit comments

Comments
 (0)