-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add anomaly detection logic #117
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
Levosilimo
wants to merge
4
commits into
main
Choose a base branch
from
cwv-109-anomaly-detection-logic
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.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
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
103 changes: 103 additions & 0 deletions
103
apps/monitor-app/clickhouse/migrations/007_anomaly_detection.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,103 @@ | ||
| CREATE TABLE IF NOT EXISTS cwv_stats_hourly | ||
| ( | ||
| project_id UUID, | ||
| route String, | ||
| device_type LowCardinality(String), | ||
| metric_name LowCardinality(String), | ||
| hour DateTime, | ||
| sum_value AggregateFunction(sum, Float64), | ||
| sum_squares AggregateFunction(sum, Float64), | ||
| count_value AggregateFunction(count, UInt64) | ||
| ) | ||
| ENGINE = AggregatingMergeTree | ||
| PARTITION BY toYYYYMM(hour) | ||
| ORDER BY (project_id, hour, metric_name, route, device_type) | ||
| TTL hour + INTERVAL 30 DAY; | ||
|
|
||
| CREATE MATERIALIZED VIEW IF NOT EXISTS mv_cwv_stats_hourly | ||
| TO cwv_stats_hourly | ||
| AS | ||
| SELECT | ||
| project_id, | ||
| route, | ||
| device_type, | ||
| metric_name, | ||
| toStartOfHour(recorded_at) AS hour, | ||
| sumState(metric_value) AS sum_value, | ||
| sumState(metric_value * metric_value) AS sum_squares, | ||
| countState() AS count_value | ||
| FROM cwv_events | ||
| GROUP BY project_id, route, device_type, metric_name, hour; | ||
|
|
||
| CREATE VIEW IF NOT EXISTS v_cwv_anomalies AS | ||
| WITH | ||
| toStartOfHour(now()) AS current_hour_mark, | ||
| current_hour AS ( | ||
| SELECT | ||
| project_id, route, device_type, metric_name, hour, | ||
| sumMerge(sum_value) / countMerge(count_value) AS avg_val, | ||
| countMerge(count_value) AS n | ||
| FROM cwv_stats_hourly | ||
| WHERE hour >= current_hour_mark - INTERVAL 1 HOUR | ||
| GROUP BY project_id, route, device_type, metric_name, hour | ||
| ), | ||
| baseline_stats AS ( | ||
| SELECT | ||
| project_id, route, device_type, metric_name, | ||
| sumMerge(sum_value) / countMerge(count_value) AS b_avg, | ||
| sqrt(max2((sumMerge(sum_squares) / countMerge(count_value)) - pow(b_avg, 2), 0)) AS b_stddev, | ||
| countMerge(count_value) AS b_n | ||
| FROM cwv_stats_hourly | ||
| WHERE hour >= current_hour_mark - INTERVAL 7 DAY | ||
| AND hour < current_hour_mark - INTERVAL 1 HOUR | ||
| GROUP BY project_id, route, device_type, metric_name | ||
| ) | ||
| SELECT | ||
| lower(hex(MD5(concat( | ||
| toString(curr.project_id), | ||
| curr.route, | ||
| curr.metric_name, | ||
| curr.device_type, | ||
| toString(curr.hour) | ||
| )))) AS anomaly_id, | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| curr.project_id, curr.route, curr.metric_name, curr.device_type, | ||
| curr.hour AS detection_time, | ||
| curr.avg_val AS current_avg, | ||
| base.b_avg AS baseline_avg, | ||
| base.b_stddev AS baseline_stddev, | ||
| (curr.avg_val - base.b_avg) / IF(base.b_stddev = 0, 0.00001, base.b_stddev) AS z_score, | ||
| curr.n AS sample_size | ||
| FROM current_hour curr | ||
| JOIN baseline_stats base ON | ||
| curr.project_id = base.project_id AND | ||
| curr.route = base.route AND | ||
| curr.metric_name = base.metric_name AND | ||
| curr.device_type = base.device_type | ||
| WHERE curr.n >= 20 AND base.b_n >= 100; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS processed_anomalies | ||
| ( | ||
| anomaly_id String, | ||
| project_id UUID, | ||
| metric_name LowCardinality(String), | ||
| route String, | ||
| last_z_score Float64, | ||
| notified_at DateTime DEFAULT now(), | ||
| status Enum8('new' = 1, 'notified' = 2, 'acknowledged' = 3, 'resolved' = 4) DEFAULT 'new' | ||
| ) | ||
| ENGINE = ReplacingMergeTree() | ||
| ORDER BY (project_id, anomaly_id); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| CREATE ROLE IF NOT EXISTS r_ai_analyst; | ||
| GRANT SELECT ON cwv_events TO r_ai_analyst; | ||
| GRANT SELECT ON custom_events TO r_ai_analyst; | ||
| GRANT SELECT ON cwv_daily_aggregates TO r_ai_analyst; | ||
| GRANT SELECT ON cwv_stats_hourly TO r_ai_analyst; | ||
| GRANT SELECT ON v_cwv_anomalies TO r_ai_analyst; | ||
| GRANT SELECT ON projects TO r_ai_analyst; | ||
| GRANT SELECT, INSERT, UPDATE ON processed_anomalies TO r_ai_analyst; | ||
|
|
||
| CREATE USER IF NOT EXISTS ai_analyst_user | ||
| IDENTIFIED WITH sha256_password BY 'ai_analyst_password'; | ||
| GRANT r_ai_analyst TO ai_analyst_user; | ||
Levosilimo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ALTER USER ai_analyst_user DEFAULT ROLE r_ai_analyst; | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
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
12 changes: 12 additions & 0 deletions
12
apps/monitor-app/src/app/server/lib/clickhouse/bootstrap.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,12 @@ | ||
| import { sql } from "@/app/server/lib/clickhouse/client"; | ||
| import { env } from "@/env"; | ||
|
|
||
| export async function syncDatabaseRoles() { | ||
| const aiUser = env.AI_ANALYST_CLICKHOUSE_USER; | ||
| const aiPass = env.AI_ANALYST_CLICKHOUSE_PASSWORD; | ||
|
|
||
| if (!aiUser || !aiPass) return; | ||
| await sql` | ||
| ALTER USER ${sql.identifier(aiUser)} IDENTIFIED WITH sha256_password BY ${aiPass} | ||
| `.command(); | ||
Levosilimo marked this conversation as resolved.
Outdated
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import { syncDatabaseRoles } from "@/app/server/lib/clickhouse/bootstrap"; | ||
| import { provisionInitialUser } from "@/lib/provision-initial-user"; | ||
|
|
||
| export async function register() { | ||
| await provisionInitialUser(); | ||
| await syncDatabaseRoles(); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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.