-
Notifications
You must be signed in to change notification settings - Fork 501
Sign up rules #1138
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
Sign up rules #1138
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
316ef37
Sign-up Rules
N2D4 f34b0c7
Merge remote-tracking branch 'origin/dev' into sign-up-rules
N2D4 587fc0d
signup -> sign up
N2D4 82c66a1
Fix types & lint
N2D4 768dab4
Fix various
N2D4 cb6477b
more fixes
N2D4 6555b5e
Various changes
N2D4 3e5179e
Cleanup
N2D4 71825ab
Test multiple rules
N2D4 ae41f7e
Fix tests
N2D4 7ce56c9
More fixes
N2D4 8aeafd0
Merge branch 'dev' into sign-up-rules
N2D4 650f09f
Test fixes
N2D4 77418fd
Fix tests
N2D4 138541d
More fixes
N2D4 7686d2b
Improved UI
N2D4 a878112
Fix comments
N2D4 1e7a2f4
Merge remote-tracking branch 'origin/dev' into sign-up-rules
N2D4 86851f7
merge
N2D4 bfee831
Fix tests
N2D4 78e2cab
more
N2D4 b58b070
more fixes
N2D4 ca2a82a
hopefully last
N2D4 252e327
more
N2D4 10ff605
Longer timeout for Docker server build
N2D4 add2294
Fix stuff
N2D4 2141e68
Merge branch 'dev' into sign-up-rules
N2D4 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
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
3 changes: 3 additions & 0 deletions
3
apps/backend/prisma/migrations/20260201400000_add_restricted_by_admin_fields/migration.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,3 @@ | ||
| -- Add restricted by admin fields to ProjectUser | ||
| ALTER TABLE "ProjectUser" ADD COLUMN "restrictedByAdmin" BOOLEAN NOT NULL DEFAULT false; | ||
| ALTER TABLE "ProjectUser" ADD COLUMN "restrictedByAdminReason" TEXT; |
10 changes: 10 additions & 0 deletions
10
...backend/prisma/migrations/20260201400001_add_restricted_by_admin_constraint/migration.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,10 @@ | ||
| -- Add restrictedByAdminPrivateDetails column | ||
| ALTER TABLE "ProjectUser" ADD COLUMN "restrictedByAdminPrivateDetails" TEXT; | ||
|
|
||
| -- Add constraint: When restrictedByAdmin is false, both reason and private details must be null | ||
| -- When restrictedByAdmin is true, reason and private details are optional | ||
| ALTER TABLE "ProjectUser" ADD CONSTRAINT "ProjectUser_restricted_by_admin_consistency" | ||
| CHECK ( | ||
| ("restrictedByAdmin" = true) OR | ||
| ("restrictedByAdmin" = false AND "restrictedByAdminReason" IS NULL AND "restrictedByAdminPrivateDetails" IS NULL) | ||
N2D4 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
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
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
136 changes: 136 additions & 0 deletions
136
apps/backend/src/app/api/latest/internal/sign-up-rules-stats/route.tsx
N2D4 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,136 @@ | ||
| import { getClickhouseAdminClient } from "@/lib/clickhouse"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, adminAuthTypeSchema, yupArray, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
|
|
||
| const ANALYTICS_HOURS = 48; | ||
|
|
||
| export const GET = createSmartRouteHandler({ | ||
| metadata: { | ||
| hidden: true, | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adminAuthTypeSchema.defined(), | ||
| tenancy: adaptSchema.defined(), | ||
| }), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| // Triggers per rule with hourly breakdown for sparklines | ||
| rule_triggers: yupArray(yupObject({ | ||
| rule_id: yupString().defined(), | ||
| total_count: yupNumber().integer().defined(), | ||
| hourly_counts: yupArray(yupObject({ | ||
| hour: yupString().defined(), | ||
| count: yupNumber().integer().defined(), | ||
| }).defined()).defined(), | ||
| }).defined()).defined(), | ||
| // Summary stats | ||
| total_triggers: yupNumber().integer().defined(), | ||
| triggers_by_action: yupObject({ | ||
| allow: yupNumber().integer().defined(), | ||
| reject: yupNumber().integer().defined(), | ||
| restrict: yupNumber().integer().defined(), | ||
| log: yupNumber().integer().defined(), | ||
| }).defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async (req) => { | ||
| const projectId = req.auth.tenancy.project.id; | ||
| const branchId = req.auth.tenancy.branchId; | ||
|
|
||
| // Generate hour keys for the sparkline | ||
| const now = new Date(); | ||
| now.setUTCMinutes(0, 0, 0); | ||
| const since = new Date(now.getTime() - (ANALYTICS_HOURS - 1) * 60 * 60 * 1000); | ||
| const hourKeys = Array.from({ length: ANALYTICS_HOURS }, (_, index) => { | ||
| const hour = new Date(since.getTime() + index * 60 * 60 * 1000); | ||
| return hour.toISOString().slice(0, 13) + ':00:00.000Z'; | ||
| }); | ||
|
|
||
| const client = getClickhouseAdminClient(); | ||
|
|
||
| const result = await client.query({ | ||
| query: ` | ||
| SELECT | ||
| data.ruleId as rule_id, | ||
| data.action as action, | ||
| toStartOfHour(event_at) as hour | ||
| FROM analytics_internal.events | ||
| WHERE event_type = '$sign-up-rule-trigger' | ||
| AND project_id = {projectId:String} | ||
| AND branch_id = {branchId:String} | ||
| AND event_at >= {since:DateTime} | ||
| ORDER BY event_at ASC | ||
| `, | ||
| query_params: { | ||
| projectId, | ||
| branchId, | ||
| since: since.toISOString().slice(0, 19), | ||
| }, | ||
| format: "JSONEachRow", | ||
| }); | ||
N2D4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const rows: { | ||
| rule_id: string, | ||
| action: "allow" | "reject" | "restrict" | "log", | ||
| hour: string, | ||
| }[] = await result.json(); | ||
|
|
||
| // Group by rule and hour for sparkline data | ||
| const ruleTriggersMap = new Map<string, { | ||
| totalCount: number, | ||
| hourlyMap: Map<string, number>, | ||
| }>(); | ||
|
|
||
| // Summary counts by action | ||
| const actionCounts = { | ||
| allow: 0, | ||
| reject: 0, | ||
| restrict: 0, | ||
| log: 0, | ||
| }; | ||
|
|
||
| for (const row of rows) { | ||
| // Update action counts | ||
| const action = row.action; | ||
| if (action in actionCounts) { | ||
| actionCounts[action]++; | ||
| } | ||
|
|
||
| // Update rule triggers | ||
| let ruleData = ruleTriggersMap.get(row.rule_id); | ||
| if (!ruleData) { | ||
| ruleData = { totalCount: 0, hourlyMap: new Map() }; | ||
| ruleTriggersMap.set(row.rule_id, ruleData); | ||
| } | ||
| ruleData.totalCount++; | ||
|
|
||
| // Group by hour (normalize to ISO format) | ||
| // ClickHouse returns datetime without timezone, treat as UTC | ||
| const hourKey = new Date(row.hour + 'Z').toISOString().slice(0, 13) + ':00:00.000Z'; | ||
| ruleData.hourlyMap.set(hourKey, (ruleData.hourlyMap.get(hourKey) ?? 0) + 1); | ||
| } | ||
|
|
||
| // Build hourly breakdown for each rule | ||
| const ruleTriggers = Array.from(ruleTriggersMap.entries()).map(([ruleId, data]) => ({ | ||
| rule_id: ruleId, | ||
| total_count: data.totalCount, | ||
| hourly_counts: hourKeys.map((hour) => ({ | ||
| hour, | ||
| count: data.hourlyMap.get(hour) ?? 0, | ||
| })), | ||
| })); | ||
N2D4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| statusCode: 200 as const, | ||
| bodyType: "json" as const, | ||
| body: { | ||
| rule_triggers: ruleTriggers, | ||
| total_triggers: rows.length, | ||
| triggers_by_action: actionCounts, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
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.