-
Notifications
You must be signed in to change notification settings - Fork 607
fix: verification_v2 mvs are missing outcome in sort key #2724
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7202b90
fix: verification_v2 mvs are missing outcome in sort key
chronark 28ecb93
fix: populate queries
chronark d8697c9
chore: build fresh
chronark 39fc297
chore: remove file
chronark 347cbe5
Merge branch 'main' into verification_v3_mvs
chronark 67918a7
[autofix.ci] apply automated fixes
autofix-ci[bot] 42cb06d
docs: style
chronark 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
19 changes: 19 additions & 0 deletions
19
internal/clickhouse/schema/036_create_verifications.key_verifications_per_hour_v3.sql
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,19 @@ | ||
| -- +goose up | ||
| CREATE TABLE verifications.key_verifications_per_hour_v3 | ||
| ( | ||
| time DateTime, | ||
| workspace_id String, | ||
| key_space_id String, | ||
| identity_id String, | ||
| key_id String, | ||
| outcome LowCardinality(String), | ||
| tags Array(String), | ||
| count Int64 | ||
| ) | ||
| ENGINE = SummingMergeTree() | ||
| ORDER BY (workspace_id, key_space_id, identity_id, key_id, time, tags, outcome) | ||
| ; | ||
|
|
||
|
|
||
| -- +goose down | ||
| DROP TABLE verifications.key_verifications_per_hour_v3; |
49 changes: 49 additions & 0 deletions
49
internal/clickhouse/schema/037_create_verifications.key_verifications_per_hour_mv_v3.sql
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,49 @@ | ||
| -- +goose up | ||
| CREATE MATERIALIZED VIEW IF NOT EXISTS verifications.key_verifications_per_hour_mv_v3 | ||
| TO verifications.key_verifications_per_hour_v3 | ||
| AS | ||
| SELECT | ||
| workspace_id, | ||
| key_space_id, | ||
| identity_id, | ||
| key_id, | ||
| outcome, | ||
| count(*) as count, | ||
| toStartOfHour(fromUnixTimestamp64Milli(time)) AS time, | ||
| tags | ||
| FROM verifications.raw_key_verifications_v1 | ||
| GROUP BY | ||
| workspace_id, | ||
| key_space_id, | ||
| identity_id, | ||
| key_id, | ||
| outcome, | ||
| time, | ||
| tags | ||
| ; | ||
|
|
||
|
|
||
| -- populate from existing data | ||
| -- INSERT INTO verifications.key_verifications_per_hour_v3 | ||
| -- SELECT | ||
| -- toStartOfHour(fromUnixTimestamp64Milli(time)) AS time, | ||
| -- workspace_id, | ||
| -- key_space_id, | ||
| -- identity_id, | ||
| -- key_id, | ||
| -- outcome, | ||
| -- tags, | ||
| -- count(*) as count | ||
| -- FROM verifications.raw_key_verifications_v1 | ||
| -- GROUP BY | ||
| -- workspace_id, | ||
| -- key_space_id, | ||
| -- identity_id, | ||
| -- key_id, | ||
| -- outcome, | ||
| -- time, | ||
| -- tags | ||
| -- ; | ||
|
|
||
| -- +goose down | ||
| DROP VIEW verifications.key_verifications_per_hour_mv_v3; |
19 changes: 19 additions & 0 deletions
19
internal/clickhouse/schema/038_create_verifications.key_verifications_per_day_v3.sql
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,19 @@ | ||
| -- +goose up | ||
| CREATE TABLE verifications.key_verifications_per_day_v3 | ||
| ( | ||
| time DateTime, | ||
| workspace_id String, | ||
| key_space_id String, | ||
| identity_id String, | ||
| key_id String, | ||
| outcome LowCardinality(String), | ||
| tags Array(String), | ||
| count Int64 | ||
| ) | ||
| ENGINE = SummingMergeTree() | ||
| ORDER BY (workspace_id, key_space_id, identity_id, key_id, time, tags, outcome) | ||
| ; | ||
|
|
||
|
|
||
| -- +goose down | ||
| DROP TABLE verifications.key_verifications_per_day_v3; |
49 changes: 49 additions & 0 deletions
49
internal/clickhouse/schema/039_create_verifications.key_verifications_per_day_mv_v3.sql
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,49 @@ | ||
| -- +goose up | ||
| CREATE MATERIALIZED VIEW IF NOT EXISTS verifications.key_verifications_per_day_mv_v3 | ||
| TO verifications.key_verifications_per_day_v3 | ||
| AS | ||
| SELECT | ||
| workspace_id, | ||
| key_space_id, | ||
| identity_id, | ||
| key_id, | ||
| outcome, | ||
| count(*) as count, | ||
| toStartOfDay(fromUnixTimestamp64Milli(time)) AS time, | ||
| tags | ||
| FROM verifications.raw_key_verifications_v1 | ||
| GROUP BY | ||
| workspace_id, | ||
| key_space_id, | ||
| identity_id, | ||
| key_id, | ||
| outcome, | ||
| time, | ||
| tags | ||
| ; | ||
|
|
||
| -- populate from existing data | ||
| -- INSERT INTO verifications.key_verifications_per_day_v3 | ||
| -- SELECT | ||
| -- toStartOfDay(fromUnixTimestamp64Milli(time)) AS time, | ||
| -- workspace_id, | ||
| -- key_space_id, | ||
| -- identity_id, | ||
| -- key_id, | ||
| -- outcome, | ||
| -- tags, | ||
| -- count(*) as count | ||
| -- FROM verifications.raw_key_verifications_v1 | ||
| -- GROUP BY | ||
| -- workspace_id, | ||
| -- key_space_id, | ||
| -- identity_id, | ||
| -- key_id, | ||
| -- outcome, | ||
| -- time, | ||
| -- tags | ||
| -- ; | ||
|
|
||
|
|
||
| -- +goose down | ||
| DROP VIEW verifications.key_verifications_per_day_mv_v3; | ||
19 changes: 19 additions & 0 deletions
19
internal/clickhouse/schema/040_create_verifications.key_verifications_per_month_v3.sql
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,19 @@ | ||
| -- +goose up | ||
| CREATE TABLE verifications.key_verifications_per_month_v3 | ||
| ( | ||
| time DateTime, | ||
| workspace_id String, | ||
| key_space_id String, | ||
| identity_id String, | ||
| key_id String, | ||
| outcome LowCardinality(String), | ||
| tags Array(String), | ||
| count Int64 | ||
| ) | ||
| ENGINE = SummingMergeTree() | ||
| ORDER BY (workspace_id, key_space_id, identity_id, key_id, time, tags, outcome) | ||
| ; | ||
|
|
||
|
|
||
| -- +goose down | ||
| DROP TABLE verifications.key_verifications_per_month_v3; |
49 changes: 49 additions & 0 deletions
49
internal/clickhouse/schema/041_create_verifications.key_verifications_per_month_mv_v3.sql
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,49 @@ | ||
| -- +goose up | ||
| CREATE MATERIALIZED VIEW IF NOT EXISTS verifications.key_verifications_per_month_mv_v3 | ||
| TO verifications.key_verifications_per_month_v3 | ||
| AS | ||
| SELECT | ||
| workspace_id, | ||
| key_space_id, | ||
| identity_id, | ||
| key_id, | ||
| outcome, | ||
| count(*) as count, | ||
| toStartOfMonth(fromUnixTimestamp64Milli(time)) AS time, | ||
| tags | ||
| FROM verifications.raw_key_verifications_v1 | ||
| GROUP BY | ||
| workspace_id, | ||
| key_space_id, | ||
| identity_id, | ||
| key_id, | ||
| outcome, | ||
| time, | ||
| tags | ||
| ; | ||
|
|
||
| -- populate from existing data | ||
| -- INSERT INTO verifications.key_verifications_per_month_v3 | ||
| -- SELECT | ||
| -- toStartOfMonth(fromUnixTimestamp64Milli(time)) AS time, | ||
| -- workspace_id, | ||
| -- key_space_id, | ||
| -- identity_id, | ||
| -- key_id, | ||
| -- outcome, | ||
| -- tags, | ||
| -- count(*) as counts | ||
| -- FROM verifications.raw_key_verifications_v1 | ||
| -- GROUP BY | ||
| -- workspace_id, | ||
| -- key_space_id, | ||
| -- identity_id, | ||
| -- key_id, | ||
| -- outcome, | ||
| -- time, | ||
| -- tags | ||
| -- ; | ||
|
|
||
|
|
||
| -- +goose down | ||
| DROP VIEW verifications.key_verifications_per_month_mv_v3; |
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
109 changes: 109 additions & 0 deletions
109
internal/clickhouse/src/verification_outcomes_propagate_correctly.test.ts
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,109 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { ClickHouse } from "./index"; | ||
|
|
||
| import { randomUUID } from "node:crypto"; | ||
| import { z } from "zod"; | ||
| import { ClickHouseContainer } from "./testutil"; | ||
|
|
||
| describe.each([10, 100, 1_000, 10_000, 100_000])("with %i verifications", (n) => { | ||
| test( | ||
| "accurately aggregates outcomes", | ||
| async (t) => { | ||
| const container = await ClickHouseContainer.start(t); | ||
|
|
||
| const ch = new ClickHouse({ url: container.url() }); | ||
|
|
||
| const workspaceId = randomUUID(); | ||
| const keySpaceId = randomUUID(); | ||
| const keyId = randomUUID(); | ||
|
|
||
| const end = Date.now(); | ||
| const interval = 90 * 24 * 60 * 60 * 1000; // 90 days | ||
| const start = end - interval; | ||
| const outcomes = { | ||
| VALID: 0, | ||
| RATE_LIMITED: 0, | ||
| DISABLED: 0, | ||
| }; | ||
| const verifications = Array.from({ length: n }).map((_) => { | ||
| const outcome = Object.keys(outcomes)[ | ||
| Math.floor(Math.random() * Object.keys(outcomes).length) | ||
| ] as keyof typeof outcomes; | ||
| outcomes[outcome]++; | ||
| return { | ||
| request_id: randomUUID(), | ||
| time: Math.round(Math.random() * (end - start + 1) + start), | ||
| workspace_id: workspaceId, | ||
| key_space_id: keySpaceId, | ||
| key_id: keyId, | ||
| outcome, | ||
| region: "test", | ||
| tags: ["tag"], | ||
| }; | ||
| }); | ||
|
|
||
| for (let i = 0; i < verifications.length; i += 1000) { | ||
| await ch.verifications.insert(verifications.slice(i, i + 1000)); | ||
| } | ||
|
|
||
| // give clickhouse time to write to all tables | ||
| // await new Promise(r => setTimeout(r, 60_000)) | ||
|
|
||
| const count = await ch.querier.query({ | ||
| query: "SELECT count(*) as count FROM verifications.raw_key_verifications_v1", | ||
| schema: z.object({ count: z.number().int() }), | ||
| })({}); | ||
| expect(count.err).toBeUndefined(); | ||
| expect(count.val!.at(0)!.count).toBe(n); | ||
|
|
||
| const hourly = await ch.verifications.perHour({ | ||
| workspaceId, | ||
| keySpaceId, | ||
| keyId, | ||
| start: start - interval, | ||
| end, | ||
| }); | ||
| expect(hourly.err).toBeUndefined(); | ||
|
|
||
| const daily = await ch.verifications.perDay({ | ||
| workspaceId, | ||
| keySpaceId, | ||
| keyId, | ||
| start: start - interval, | ||
| end, | ||
| }); | ||
| expect(daily.err).toBeUndefined(); | ||
|
|
||
| const monthly = await ch.verifications.perMonth({ | ||
| workspaceId, | ||
| keySpaceId, | ||
| keyId, | ||
| start: start - interval, | ||
| end, | ||
| }); | ||
| expect(monthly.err).toBeUndefined(); | ||
|
|
||
| for (const buckets of [hourly.val!, daily.val!, monthly.val!]) { | ||
| let total = 0; | ||
| const sumByOutcome = buckets.reduce( | ||
| (acc, bucket) => { | ||
| total += bucket.count; | ||
| if (!acc[bucket.outcome]) { | ||
| acc[bucket.outcome] = 0; | ||
| } | ||
| acc[bucket.outcome] += bucket.count; | ||
| return acc; | ||
| }, | ||
| {} as Record<keyof typeof outcomes, number>, | ||
| ); | ||
|
|
||
| expect(total).toBe(n); | ||
|
|
||
| for (const [k, v] of Object.entries(outcomes)) { | ||
| expect(sumByOutcome[k]).toEqual(v); | ||
| } | ||
| } | ||
| }, | ||
| { timeout: 120_000 }, | ||
| ); | ||
chronark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
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
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
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
Oops, something went wrong.
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.