Skip to content

Commit 7b5cf4f

Browse files
authored
fix analytics queries (#1141)
<!-- Make sure you've read the CONTRIBUTING.md guidelines: https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Improvements** * Added automatic retry with stricter result validation and clearer error handling for query profiling to improve reliability. * **Chores** * CI workflows updated to use larger runner instances (upgraded runner size across relevant jobs). <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 1fbae4f commit 7b5cf4f

5 files changed

Lines changed: 42 additions & 28 deletions

File tree

.github/workflows/restart-dev-and-test-with-custom-base-port.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env:
1616

1717
jobs:
1818
restart-dev-and-test-with-custom-base-port:
19-
runs-on: ubicloud-standard-8
19+
runs-on: ubicloud-standard-16
2020
env:
2121
NEXT_PUBLIC_STACK_PORT_PREFIX: "69"
2222

.github/workflows/restart-dev-and-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env:
1616

1717
jobs:
1818
restart-dev-and-test:
19-
runs-on: ubicloud-standard-8
19+
runs-on: ubicloud-standard-16
2020

2121
steps:
2222
- uses: actions/checkout@v6

.github/workflows/setup-tests-with-custom-base-port.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env:
1616

1717
jobs:
1818
setup-tests-with-custom-base-port:
19-
runs-on: ubicloud-standard-8
19+
runs-on: ubicloud-standard-16
2020
env:
2121
NEXT_PUBLIC_STACK_PORT_PREFIX: "69"
2222

.github/workflows/setup-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env:
1616

1717
jobs:
1818
setup-tests:
19-
runs-on: ubicloud-standard-8
19+
runs-on: ubicloud-standard-16
2020
steps:
2121
- uses: actions/checkout@v6
2222

apps/backend/src/lib/clickhouse.tsx

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,44 @@ export const getQueryTimingStats = async (client: ClickHouseClient, queryId: str
4848
password: clickhouseAdminPassword,
4949
},
5050
});
51-
const profile = await client.query({
52-
query: `
53-
SELECT
54-
ProfileEvents['CPUTimeMicroseconds'] / 1000 AS cpu_time_ms,
55-
ProfileEvents['RealTimeMicroseconds'] / 1000 AS wall_clock_time_ms
56-
FROM system.query_log
57-
WHERE query_id = {query_id:String} AND type = 'QueryFinish'
58-
ORDER BY event_time DESC
59-
LIMIT 1
60-
`,
61-
query_params: { query_id: queryId },
62-
auth: {
63-
username: clickhouseAdminUser,
64-
password: clickhouseAdminPassword,
65-
},
66-
format: "JSON",
67-
});
51+
const queryProfile = async () => {
52+
const profile = await client.query({
53+
query: `
54+
SELECT
55+
ProfileEvents['CPUTimeMicroseconds'] / 1000 AS cpu_time_ms,
56+
ProfileEvents['RealTimeMicroseconds'] / 1000 AS wall_clock_time_ms
57+
FROM system.query_log
58+
WHERE query_id = {query_id:String} AND type = 'QueryFinish'
59+
ORDER BY event_time DESC
60+
LIMIT 1
61+
`,
62+
query_params: { query_id: queryId },
63+
auth: {
64+
username: clickhouseAdminUser,
65+
password: clickhouseAdminPassword,
66+
},
67+
format: "JSON",
68+
});
69+
70+
return await profile.json<{
71+
cpu_time_ms: number,
72+
wall_clock_time_ms: number,
73+
}>();
74+
};
6875

69-
const stats = await profile.json<{
70-
cpu_time_ms: number,
71-
wall_clock_time_ms: number,
72-
}>();
73-
if (stats.data.length !== 1) {
74-
throw new StackAssertionError(`Unexpected number of query log results: ${stats.data.length}`, { data: stats.data });
76+
const retryDelaysMs = [75, 150, 300, 600, 1200];
77+
for (let attempt = 0; attempt <= retryDelaysMs.length; attempt += 1) {
78+
const stats = await queryProfile();
79+
if (stats.data.length === 1) {
80+
return stats.data[0];
81+
}
82+
if (stats.data.length > 1) {
83+
throw new StackAssertionError(`Unexpected number of query log results: ${stats.data.length}`, { data: stats.data });
84+
}
85+
if (attempt < retryDelaysMs.length) {
86+
await new Promise((resolve) => setTimeout(resolve, retryDelaysMs[attempt]));
87+
}
7588
}
76-
return stats.data[0];
89+
90+
throw new StackAssertionError("Unexpected number of query log results: 0", { data: [] });
7791
};

0 commit comments

Comments
 (0)