|
1 | | -# Watt Admin Development Guide |
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
2 | 4 |
|
3 | 5 | ## Build/Test/Lint Commands |
4 | 6 | ```bash |
5 | 7 | # Development |
6 | | -npm run dev # Start development server |
| 8 | +npm run dev # Start development server (wattpm) |
7 | 9 | npm run build # Build for production |
8 | 10 | npm run start # Start in production mode |
9 | | -npm run clean # Remove build artifacts |
| 11 | +npm run clean # Remove build artifacts (web/*/dist) |
| 12 | +npm run typecheck # Type-check backend and frontend TypeScript |
10 | 13 |
|
11 | 14 | # Testing |
12 | | -npm run test # Run all tests |
| 15 | +npm run test # Run all tests (CLI, E2E, backend, frontend) |
13 | 16 | npm run test:cli # Run CLI tests only |
14 | 17 | npm run test:backend # Run backend tests only |
15 | | -npm run test:frontend # Run frontend tests only |
16 | | -node --test test/path/to/file.test.js # Run single Node.js test file |
17 | | -cd web/frontend && vitest run path/to/file.test.ts # Run single frontend test |
| 18 | +npm run test:frontend # Run frontend tests only (Vitest) |
| 19 | +npm run test:e2e # Run Playwright E2E tests |
| 20 | +npm run test:e2e:ui # Run Playwright with UI mode |
| 21 | + |
| 22 | +# Running single test files |
| 23 | +node --test test/path/to/file.test.ts # CLI test |
| 24 | +node --test web/backend/path/to/file.test.ts # Backend test |
| 25 | +cd web/frontend && vitest run path/to/file.test.ts # Frontend test |
18 | 26 |
|
19 | 27 | # Linting |
20 | 28 | npm run lint # Lint all code |
21 | 29 | npm run lint:fix # Fix linting issues automatically |
| 30 | + |
| 31 | +# Client generation (OpenAPI) |
| 32 | +npm run client:openapi # Generate OpenAPI spec from backend |
| 33 | +npm run client:generate # Generate frontend client from OpenAPI using massimo |
22 | 34 | ``` |
23 | 35 |
|
| 36 | +## Project Architecture |
| 37 | + |
| 38 | +### High-Level Structure |
| 39 | +This is a **Platformatic Watt monorepo** containing a CLI tool and full-stack admin dashboard for monitoring Platformatic runtimes. The project uses Platformatic's microservices architecture with three main applications orchestrated by wattpm: |
| 40 | + |
| 41 | +1. **Backend** (web/backend): Fastify service providing REST APIs and WebSocket endpoints |
| 42 | +2. **Composer** (web/composer): Platformatic Gateway that routes requests and proxies between frontend/backend |
| 43 | +3. **Frontend** (web/frontend): React + Vite SPA with real-time monitoring UI |
| 44 | + |
| 45 | +### CLI Tool (cli.js) |
| 46 | +- **Purpose**: Discovers and connects to running Platformatic runtimes using `@platformatic/control` |
| 47 | +- **Entry point**: `cli.js` (executable, can be installed globally as `watt-admin`) |
| 48 | +- **Key features**: |
| 49 | + - Auto-discovers runtimes via `RuntimeApiClient` |
| 50 | + - Interactive runtime selection with `@inquirer/prompts` |
| 51 | + - Launches admin dashboard against selected runtime |
| 52 | + - Supports flags: `--port`, `--record`, `--profile` (cpu/heap) |
| 53 | +- **Recording mode**: Can capture metrics/flamegraphs and generate offline HTML bundles |
| 54 | + |
| 55 | +### Wattpm Configuration (watt.json) |
| 56 | +Defines the multi-app structure: |
| 57 | +- **Entrypoint**: `composer` (gateway on port 4042 by default) |
| 58 | +- **Backend**: `/api` prefix, TypeScript with `--import=amaro/strip` |
| 59 | +- **Frontend**: `/` prefix (root path) |
| 60 | +- **Composer**: Routes between backend API and frontend assets |
| 61 | + |
| 62 | +### Backend Architecture (web/backend) |
| 63 | +- **Framework**: Fastify with `@fastify/type-provider-json-schema-to-ts` for type-safe routes |
| 64 | +- **Main routes**: |
| 65 | + - `routes/root.ts`: Core API endpoints (`/runtimes`, `/runtimes/:pid/services`, `/record/:pid`) |
| 66 | + - `routes/ws.ts`: WebSocket endpoint for live log streaming (`/runtimes/:pid/logs/ws`) |
| 67 | + - `routes/metrics.ts`: Metrics proxy endpoints |
| 68 | + - `routes/proxy.ts`: Generic proxy to runtime services |
| 69 | +- **Plugins**: |
| 70 | + - `plugins/metrics.ts`: Interval-based metrics collection using `RuntimeApiClient` |
| 71 | + - `plugins/websocket.ts`: WebSocket server setup |
| 72 | +- **Key utilities**: |
| 73 | + - `utils/runtimes.ts`: Runtime discovery and PID management |
| 74 | + - `utils/metrics.ts`: Metrics fetching and aggregation |
| 75 | + - `utils/states.ts`: State machine for recording modes |
| 76 | +- **Testing**: Node.js test runner with `--experimental-test-module-mocks` |
| 77 | + |
| 78 | +### Frontend Architecture (web/frontend) |
| 79 | +- **Framework**: React 19 with TypeScript, React Router v7 (HashRouter) |
| 80 | +- **Build tool**: Vite with `vite-plugin-singlefile` (generates single-file HTML for offline mode) |
| 81 | +- **State management**: Zustand (`useAdminStore.ts`) |
| 82 | +- **Key state**: `runtimePid`, `mode` (live/load), `record` (start/stop), navigation breadcrumbs |
| 83 | +- **Main routes**: |
| 84 | + - `/` (HOME_PATH): Application details overview |
| 85 | + - `/services` (POD_SERVICES_PATH): Service metrics charts |
| 86 | + - `/logs` (POD_LOGS_PATH): Real-time service logs |
| 87 | + - `/flamegraph` (POD_FLAMEGRAPH_PATH): CPU/heap flamegraph visualization |
| 88 | +- **Components structure**: |
| 89 | + - `components/application/`: App overview, metrics display |
| 90 | + - `components/services/`: Service charts, logs, flamegraphs |
| 91 | + - `components/application-logs/`: Log filtering and display |
| 92 | + - `layout/`: Navigation, header, containers |
| 93 | +- **Real-time data**: WebSocket connection via `react-use-websocket` for live logs |
| 94 | +- **Testing**: Vitest for unit tests, Playwright for E2E tests |
| 95 | + |
| 96 | +### API Communication |
| 97 | +- **Backend client**: Auto-generated from OpenAPI spec using `massimo-cli` (stored in `web/frontend/src/client/`) |
| 98 | +- **Generation flow**: |
| 99 | + 1. Backend exposes OpenAPI via Fastify |
| 100 | + 2. `npm run client:openapi` extracts spec to `web/backend/openapi.json` |
| 101 | + 3. `npm run client:generate` creates TypeScript client with types |
| 102 | +- **RuntimeApiClient**: Used by backend to communicate with target Platformatic runtimes |
| 103 | + |
| 104 | +### Recording/Offline Mode |
| 105 | +- **Purpose**: Capture runtime metrics/profiling data for offline analysis |
| 106 | +- **Flow**: |
| 107 | + 1. Start recording: CLI flag `--record --profile cpu|heap` |
| 108 | + 2. Backend calls `startApplicationProfiling` on all applications |
| 109 | + 3. Collects metrics at intervals (defined in MS_WAITING constant) |
| 110 | + 4. On SIGINT: Stops profiling, saves .pb files, embeds data in HTML bundle |
| 111 | + 5. Opens standalone HTML with embedded metrics/flamegraph data in `window.LOADED_JSON` |
| 112 | + |
24 | 113 | ## Code Style Guidelines |
25 | | -- **Formatting**: Project uses neostandard via ESLint (strict mode) |
26 | | -- **TypeScript**: Use explicit types for function params, returns, interfaces |
| 114 | +- **Formatting**: neostandard via ESLint (strict mode, TypeScript enabled) |
| 115 | +- **TypeScript**: |
| 116 | + - Explicit types for function params and returns |
| 117 | + - Use interface/type for complex structures |
| 118 | + - Backend uses `JsonSchemaToTsProvider` for route typing |
27 | 119 | - **Imports**: Group related imports, alphabetize within groups |
28 | 120 | - **Naming**: camelCase for variables/functions, PascalCase for components/classes/interfaces/types |
29 | | -- **Error Handling**: Log errors with fastify.log, handle async errors with try/catch |
30 | | -- **React**: Functional components with hooks, use React.ReactElement return types |
31 | | -- **Testing**: Node.js test module for backend, Vitest for frontend |
32 | | -- **Backend**: Follow Fastify conventions with typed routes using JsonSchemaToTsProvider |
| 121 | +- **Error Handling**: |
| 122 | + - Backend: Use `fastify.log` for logging, try/catch for async |
| 123 | + - Frontend: Error boundaries with `use-error-boundary` |
| 124 | +- **React**: |
| 125 | + - Functional components with hooks only |
| 126 | + - Return type `React.ReactElement` for components |
| 127 | + - Use Zustand for global state |
| 128 | +- **Testing**: |
| 129 | + - Backend/CLI: Node.js test module (`--test` flag) |
| 130 | + - Frontend: Vitest for unit, Playwright for E2E |
| 131 | + - Test files use `.test.ts` extension |
| 132 | + |
| 133 | +## Important Development Notes |
| 134 | +- **TypeScript transpilation**: Backend uses `amaro/strip` for fast TypeScript stripping (no type checking at runtime) |
| 135 | +- **Module mocking**: Tests use `--experimental-test-module-mocks` flag |
| 136 | +- **Hot reload**: Both frontend and backend support watch mode in dev |
| 137 | +- **Build artifacts**: Always in `web/*/dist` directories |
| 138 | +- **Environment**: Uses `.env` file (copy from `.env.sample`) |
| 139 | +- **Platformatic versions**: All Platformatic packages use same version (3.15.0+) |
0 commit comments