-
Notifications
You must be signed in to change notification settings - Fork 35
refactor(kiloclaw): move volume usage from DO state to Analytics Engine #2301
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
8 commits
Select commit
Hold shift + click to select a range
f25f050
feat(kiloclaw): move /root volume usage to Analytics Engine
evanjacobson b5cab0f
Use the standardized implementation model exemplified by other existi…
evanjacobson 1f2762e
test(kiloclaw): type AE controller mock payloads
evanjacobson ee2c45b
fix(kiloclaw): catch AE request failures in routes
evanjacobson 85d1be4
fix(kiloclaw): scope AE error handling to volume usage
evanjacobson 0ba5f92
refactor(admin): extract disk usage derivation from JSX IIFE to consts
evanjacobson 562439e
fix(kiloclaw): default missing disk usage to zero
evanjacobson 2165066
fix(kiloclaw): accept null disk usage checkins
evanjacobson 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
34 changes: 34 additions & 0 deletions
34
apps/web/src/app/admin/api/kiloclaw-controller-telemetry/hooks.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,34 @@ | ||
| 'use client'; | ||
|
|
||
| import { useQuery } from '@tanstack/react-query'; | ||
|
|
||
| export type ControllerTelemetryRow = { | ||
| timestamp: string; | ||
| sandbox_id: string; | ||
| machine_id: string; | ||
| disk_used_bytes: number; | ||
| disk_total_bytes: number; | ||
| }; | ||
|
|
||
| type AnalyticsEngineResponse<T> = { | ||
| data: T[]; | ||
| meta: { name: string; type: string }[]; | ||
| rows: number; | ||
| }; | ||
|
|
||
| export function useControllerTelemetryDiskUsage(sandboxId: string) { | ||
| return useQuery<AnalyticsEngineResponse<ControllerTelemetryRow>>({ | ||
| queryKey: ['kiloclaw-controller-telemetry', 'disk-usage', sandboxId], | ||
| queryFn: async () => { | ||
| const response = await fetch( | ||
| `/admin/api/kiloclaw-controller-telemetry?sandboxId=${encodeURIComponent(sandboxId)}` | ||
| ); | ||
| if (!response.ok) { | ||
| throw new Error('Failed to fetch controller telemetry disk usage'); | ||
| } | ||
| return response.json() as Promise<AnalyticsEngineResponse<ControllerTelemetryRow>>; | ||
| }, | ||
| enabled: !!sandboxId, | ||
| refetchInterval: 60_000, | ||
| }); | ||
| } |
82 changes: 82 additions & 0 deletions
82
apps/web/src/app/admin/api/kiloclaw-controller-telemetry/route.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,82 @@ | ||
| import type { NextRequest } from 'next/server'; | ||
| import { NextResponse } from 'next/server'; | ||
| import { getUserFromAuth } from '@/lib/user.server'; | ||
| import { getEnvVariable } from '@/lib/dotenvx'; | ||
|
|
||
| function isSafeIdentifier(value: string): boolean { | ||
| return /^[A-Za-z0-9_-]+$/.test(value); | ||
| } | ||
|
|
||
| function buildQuery(sandboxId: string): string { | ||
| return `SELECT | ||
| timestamp, | ||
| blob1 AS sandbox_id, | ||
| blob8 AS machine_id, | ||
| double7 AS disk_used_bytes, | ||
| double8 AS disk_total_bytes | ||
| FROM kiloclaw_controller_telemetry | ||
| WHERE index1 = '${sandboxId}' | ||
| ORDER BY timestamp DESC | ||
| LIMIT 1 | ||
| FORMAT JSON`; | ||
| } | ||
|
|
||
| type AnalyticsEngineResponse = { | ||
| data: Record<string, unknown>[]; | ||
| meta: { name: string; type: string }[]; | ||
| rows: number; | ||
| }; | ||
|
|
||
| export async function GET( | ||
| request: NextRequest | ||
| ): Promise<NextResponse<{ error: string } | AnalyticsEngineResponse>> { | ||
| const { authFailedResponse } = await getUserFromAuth({ adminOnly: true }); | ||
| if (authFailedResponse) { | ||
| return authFailedResponse; | ||
| } | ||
|
|
||
| const { searchParams } = new URL(request.url); | ||
| const sandboxId = searchParams.get('sandboxId'); | ||
|
|
||
| if (!sandboxId || !isSafeIdentifier(sandboxId)) { | ||
| return NextResponse.json({ error: 'Invalid or missing sandboxId' }, { status: 400 }); | ||
| } | ||
|
|
||
| const accountId = getEnvVariable('R2_ACCOUNT_ID'); | ||
| const token = getEnvVariable('CF_ANALYTICS_ENGINE_TOKEN'); | ||
|
|
||
| if (!accountId || !token) { | ||
| return NextResponse.json( | ||
| { error: 'Missing Cloudflare Analytics Engine configuration' }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
|
|
||
| const sqlQuery = buildQuery(sandboxId); | ||
|
|
||
| try { | ||
| const response = await fetch( | ||
| `https://api.cloudflare.com/client/v4/accounts/${accountId}/analytics_engine/sql`, | ||
| { | ||
| method: 'POST', | ||
| headers: { Authorization: `Bearer ${token}` }, | ||
| body: sqlQuery, | ||
| } | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
| console.error('Analytics Engine API error:', response.status, errorText); | ||
| return NextResponse.json( | ||
| { error: `Analytics Engine API error: ${response.status}` }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
|
|
||
| const result: AnalyticsEngineResponse = await response.json(); | ||
| return NextResponse.json(result); | ||
| } catch (error) { | ||
| console.error('Analytics Engine request failed:', error); | ||
| return NextResponse.json({ error: 'Failed to query Analytics Engine' }, { status: 500 }); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.