-
Notifications
You must be signed in to change notification settings - Fork 5
feat: connect cwv to ai provider #118
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
olekszczepanowski
wants to merge
20
commits into
main
Choose a base branch
from
cvw-112-connect-cwv-to-ai-provider
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 all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dfaf6dd
feat: add anomaly detection logic and a test for it
Levosilimo 66f6ff5
feat: improve anomaly detection with lognormal dist
Levosilimo a32ca20
fix: apply coderabbit suggestions
Levosilimo dcdc7c4
feat: add base agent
olekszczepanowski f06e834
fix: fix test flakyness
Levosilimo 8e79f7d
feat: improve system prompt and clickhouse tool
olekszczepanowski ab11532
Merge branch 'cwv-109-anomaly-detection-logic' into cvw-112-connect-c…
olekszczepanowski 72be373
feat: add variables in env ci
olekszczepanowski c65c1d3
fix: make ai configuration optional
olekszczepanowski 0fba5ec
feat: change preview value
olekszczepanowski c8929de
feat: move agent clickhouse client to utils directory
olekszczepanowski 37253b2
feat: add anomaly checker worker
Levosilimo 8b292f5
fix: fix docker setup
Levosilimo 2fd942a
fix: fix ci
Levosilimo aeb2e47
feat: add authorization
olekszczepanowski 3267c3c
fix: code rabbit comments
olekszczepanowski 660433e
feat: add adaptivecards types
Levosilimo 280c17f
Merge remote-tracking branch 'origin/cwv-115-anomaly-pipeline' into c…
olekszczepanowski 8aa2fbe
feat: load schema from catalog.yml
olekszczepanowski b32dccb
fix: ci
olekszczepanowski 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
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # ClickHouse connection details | ||
| CLICKHOUSE_HOST=localhost | ||
| CLICKHOUSE_PORT=8123 | ||
| CLICKHOUSE_USER=default | ||
| CLICKHOUSE_PASSWORD=secret | ||
| CLICKHOUSE_DB=cwv_monitor | ||
| AI_ANALYST_CLICKHOUSE_PASSWORD= | ||
| BETTER_AUTH_SECRET= | ||
| INITIAL_USER_EMAIL= | ||
| INITIAL_USER_PASSWORD= | ||
| INITIAL_USER_NAME= | ||
|
|
||
| # Application | ||
| LOG_LEVEL=info | ||
| NODE_ENV=development | ||
| AUTH_BASE_URL=http://localhost:3000 | ||
|
|
||
| # Anomaly Detection Webhooks (Optional) | ||
| SLACK_WEBHOOK_URL= | ||
| TEAMS_WEBHOOK_URL= |
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 @@ | ||
| .env |
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,41 @@ | ||
| { | ||
| "name": "anomaly-worker", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "description": "A lightweight worker for detecting anomalies.", | ||
| "main": "dist/anomaly-worker/src/index.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "prebuild": "rm -rf dist", | ||
| "build": "tsc", | ||
| "start": "cross-env NODE_OPTIONS=\"-r dotenv/config\" tsx src/index.ts", | ||
| "dev": "tsx src/index.ts | pino-pretty", | ||
| "test": "vitest", | ||
| "test:watch": "vitest --watch" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "@clickhouse/client": "^1.14.0", | ||
| "@t3-oss/env-nextjs": "^0.13.8", | ||
| "arktype": "^2.1.28", | ||
| "date-fns": "^4.1.0", | ||
| "node-cron": "^3.0.3", | ||
| "pino": "^10.1.0", | ||
| "remeda": "^2.32.0", | ||
| "waddler": "^0.1.1", | ||
| "zod": "^4.1.13", | ||
| "dotenv": "^16.4.5" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^24", | ||
| "@types/node-cron": "^3.0.11", | ||
| "pino-pretty": "^13.0.0", | ||
| "tsx": "^4.19.2", | ||
| "typescript": "^5.9.3", | ||
| "vitest": "^4.0.15", | ||
| "tsconfig-paths": "^4.2.0", | ||
| "cross-env": "^7.0.3" | ||
| } | ||
| } |
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,54 @@ | ||
| import { createEnv } from "@t3-oss/env-nextjs"; | ||
| import { z } from "zod"; | ||
|
|
||
| const LOG_LEVELS = ["fatal", "error", "warn", "info", "debug", "trace", "silent"] as const; | ||
|
|
||
| export const env = createEnv({ | ||
| server: { | ||
| AUTH_BASE_URL: z.url(), | ||
| TRUST_PROXY: z.enum(["true", "false"]).default("false"), | ||
| CLICKHOUSE_HOST: z.string().min(1, "CLICKHOUSE_HOST is required"), | ||
| CLICKHOUSE_PORT: z.string().min(1, "CLICKHOUSE_PORT is required"), | ||
| CLICKHOUSE_USER: z.string().min(1, "CLICKHOUSE_USER is required"), | ||
| CLICKHOUSE_PASSWORD: z.string(), | ||
| CLICKHOUSE_DB: z.string().min(1, "CLICKHOUSE_DB is required"), | ||
| AI_ANALYST_CLICKHOUSE_USER: z.string().min(1).default("ai_analyst_user"), | ||
| AI_ANALYST_CLICKHOUSE_PASSWORD: z.string().min(1), | ||
| BETTER_AUTH_SECRET: z.string(), | ||
| CLICKHOUSE_ADAPTER_DEBUG_LOGS: z.coerce.boolean().default(false), | ||
| MIN_PASSWORD_SCORE: z.coerce.number().min(0).max(4).default(2), | ||
| RATE_LIMIT_WINDOW_MS: z.coerce.number().positive().default(60_000), | ||
| MAX_LOGIN_ATTEMPTS: z.coerce.number().positive().default(5), | ||
| INITIAL_USER_EMAIL: z.email(), | ||
| INITIAL_USER_PASSWORD: z.string().min(8), | ||
| INITIAL_USER_NAME: z.string().min(3), | ||
| NODE_ENV: z.enum(["development", "test", "production"]).default("development"), | ||
| LOG_LEVEL: z.enum(LOG_LEVELS).default("info"), | ||
| SLACK_WEBHOOK_URL: z.url().optional(), | ||
| TEAMS_WEBHOOK_URL: z.url().optional(), | ||
| }, | ||
| client: {}, | ||
| runtimeEnv: { | ||
| AUTH_BASE_URL: process.env.AUTH_BASE_URL, | ||
| TRUST_PROXY: process.env.TRUST_PROXY, | ||
| CLICKHOUSE_HOST: process.env.CLICKHOUSE_HOST, | ||
| CLICKHOUSE_PORT: process.env.CLICKHOUSE_PORT, | ||
| CLICKHOUSE_USER: process.env.CLICKHOUSE_USER, | ||
| CLICKHOUSE_PASSWORD: process.env.CLICKHOUSE_PASSWORD, | ||
| CLICKHOUSE_DB: process.env.CLICKHOUSE_DB, | ||
| AI_ANALYST_CLICKHOUSE_USER: process.env.AI_ANALYST_CLICKHOUSE_USER, | ||
| AI_ANALYST_CLICKHOUSE_PASSWORD: process.env.AI_ANALYST_CLICKHOUSE_PASSWORD, | ||
| BETTER_AUTH_SECRET: process.env.BETTER_AUTH_SECRET, | ||
| CLICKHOUSE_ADAPTER_DEBUG_LOGS: process.env.CLICKHOUSE_ADAPTER_DEBUG_LOGS, | ||
| MIN_PASSWORD_SCORE: process.env.MIN_PASSWORD_SCORE, | ||
| RATE_LIMIT_WINDOW_MS: process.env.RATE_LIMIT_WINDOW_MS, | ||
| MAX_LOGIN_ATTEMPTS: process.env.MAX_LOGIN_ATTEMPTS, | ||
| INITIAL_USER_EMAIL: process.env.INITIAL_USER_EMAIL, | ||
| INITIAL_USER_PASSWORD: process.env.INITIAL_USER_PASSWORD, | ||
| INITIAL_USER_NAME: process.env.INITIAL_USER_NAME, | ||
| NODE_ENV: process.env.NODE_ENV, | ||
| LOG_LEVEL: process.env.LOG_LEVEL, | ||
| SLACK_WEBHOOK_URL: process.env.SLACK_WEBHOOK_URL, | ||
| TEAMS_WEBHOOK_URL: process.env.TEAMS_WEBHOOK_URL, | ||
| }, | ||
| }); | ||
olekszczepanowski 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import pino from 'pino'; | ||
| import cron from 'node-cron'; | ||
| import { env } from "./env"; | ||
|
|
||
| const logger = pino({ name: 'anomaly-worker', level: env.LOG_LEVEL }); | ||
|
|
||
| let isRunning = false; | ||
| let isShuttingDown = false; | ||
|
|
||
| async function runDetectionCycle() { | ||
| if (isRunning || isShuttingDown) { | ||
| return; | ||
| } | ||
|
|
||
| isRunning = true; | ||
| logger.info("Starting anomaly detection cycle..."); | ||
|
|
||
| try { | ||
| const { NotificationsService } = await import('@monitor-app/app/server/domain/notifications/service'); | ||
| const service = new NotificationsService(); | ||
olekszczepanowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| await service.notifyNewAnomalies(); | ||
| logger.info("Anomaly detection cycle finished."); | ||
| } catch (error) { | ||
| logger.error({ err: error }, "Error during detection cycle."); | ||
| } finally { | ||
| isRunning = false; | ||
| } | ||
| } | ||
|
|
||
| const task = cron.schedule('30 * * * *', runDetectionCycle); | ||
| logger.info("Anomaly worker scheduled (30 * * * *)."); | ||
|
|
||
| runDetectionCycle(); | ||
|
|
||
| process.on('SIGTERM', async () => { | ||
| isShuttingDown = true; | ||
| task.stop(); | ||
|
|
||
| if (isRunning) { | ||
| logger.info("Waiting for the current detection cycle to complete..."); | ||
|
|
||
| const shutdownTimeout = setTimeout(() => { | ||
| logger.error("Shutdown timed out; forcing exit."); | ||
| process.exit(1); | ||
| }, 20000); | ||
|
|
||
| while (isRunning) { | ||
| await new Promise(resolve => setTimeout(resolve, 500)); | ||
| } | ||
|
|
||
| clearTimeout(shutdownTimeout); | ||
| } | ||
|
|
||
| logger.info("Shutdown complete."); | ||
| process.exit(0); | ||
| }); | ||
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,18 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "module": "ESNext", | ||
| "moduleResolution": "bundler", | ||
| "outDir": "./dist", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "paths": { | ||
| "@/*": ["./src/*", "../../apps/monitor-app/src/*"], | ||
| "@monitor-app/*": ["../monitor-app/src/*"] | ||
| } | ||
| }, | ||
| "include": ["src/**/*.ts"], | ||
| "exclude": ["node_modules"] | ||
| } |
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.