-
Notifications
You must be signed in to change notification settings - Fork 460
fix(db): count distinct profiles so the list and its total agree #474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
avionicharshit-byte
wants to merge
1
commit into
Openpanel-dev:main
Choose a base branch
from
avionicharshit-byte:fix/profile-list-count-dedup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−44
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * SQL-shape tests for the profile list and its count. | ||
| * | ||
| * Same strategy as profile-metrics-sql.test.ts: string assertions always run; | ||
| * `EXPLAIN` validation runs against a locally reachable ClickHouse | ||
| * (`pnpm dock:up`) and skips otherwise. | ||
| */ | ||
| import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { ch } from '../clickhouse/client'; | ||
| import { buildProfileListCountSql, buildProfileListSql } from './profile.service'; | ||
|
|
||
| const PROJECT_ID = 'test-sql-validation'; | ||
|
|
||
| let chReachable = false; | ||
|
|
||
| beforeAll(async () => { | ||
| vi.spyOn(console, 'log').mockImplementation(() => {}); | ||
| try { | ||
| await ch.command({ query: 'SELECT 1' }); | ||
| chReachable = true; | ||
| } catch { | ||
| chReachable = false; | ||
| } | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| const itCH = (name: string, fn: () => Promise<void>) => | ||
| it(name, async (ctx) => { | ||
| if (!chReachable) { | ||
| ctx.skip('ClickHouse not reachable at CLICKHOUSE_URL'); | ||
| } | ||
| await fn(); | ||
| }); | ||
|
|
||
| describe('buildProfileListCountSql', () => { | ||
| // One profile is several rows until a merge collapses them, so counting rows | ||
| // reports a total the FINAL list can never fill. | ||
| it('counts profiles, not undeduplicated rows', () => { | ||
| const sql = buildProfileListCountSql({ projectId: PROJECT_ID }); | ||
| expect(sql).toContain('uniqExact(id) as count'); | ||
| expect(sql).not.toContain('count(id)'); | ||
| }); | ||
|
|
||
| it('applies the same filters as the list', () => { | ||
| const options = { | ||
| projectId: PROJECT_ID, | ||
| search: 'john', | ||
| isExternal: true, | ||
| }; | ||
| const list = buildProfileListSql({ ...options, take: 50 }); | ||
| const count = buildProfileListCountSql(options); | ||
| for (const clause of [ | ||
| `project_id = '${PROJECT_ID}'`, | ||
| 'is_external = true', | ||
| "email ILIKE '%john%'", | ||
| ]) { | ||
| expect(list).toContain(clause); | ||
| expect(count).toContain(clause); | ||
| } | ||
| }); | ||
|
|
||
| it('escapes the project id', () => { | ||
| const sql = buildProfileListCountSql({ projectId: "x'--" }); | ||
| expect(sql).toContain("'x\\'--'"); | ||
| }); | ||
|
|
||
| itCH('parses and resolves against ClickHouse', async () => { | ||
| await ch.command({ | ||
| query: `EXPLAIN ${buildProfileListCountSql({ projectId: PROJECT_ID })}`, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildProfileListSql', () => { | ||
| it('pages with offset = cursor * take', () => { | ||
| expect(buildProfileListSql({ projectId: PROJECT_ID, take: 50 })).not.toContain( | ||
| 'OFFSET', | ||
| ); | ||
| expect( | ||
| buildProfileListSql({ projectId: PROJECT_ID, take: 50, cursor: 2 }), | ||
| ).toContain('OFFSET 100'); | ||
| }); | ||
|
|
||
| it('reads the deduplicated view of the table', () => { | ||
| expect(buildProfileListSql({ projectId: PROJECT_ID, take: 50 })).toContain( | ||
| 'FROM profiles FINAL', | ||
| ); | ||
| }); | ||
|
|
||
| itCH('parses and resolves against ClickHouse', async () => { | ||
| await ch.command({ | ||
| query: `EXPLAIN ${buildProfileListSql({ projectId: PROJECT_ID, take: 50 })}`, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.