- Create
.envin/server
PORT=3004- Run server
cd server
npm install
npm run devEnsure you have PostgreSQL installed and running. The database schema will be automatically initialized when the server starts for the first time.
Required environment variables in server/.env:
DB_USER=your_postgres_user
DB_HOST=localhost
DB_NAME=hab_dashboard
DB_PASSWORD=your_postgres_password
DB_PORT=5432
PORT=3004Tables created automatically on server startup:
system_metrics- Stores system performance metrics (CPU, memory, disk, network, PM2 processes)- Index on
system_metrics(measured_at DESC)for efficient time-range queries
The initialization script runs once when the server starts and will recreate the system_metrics table with the correct schema.
cd client
pnpm install
pnpm dev- GET /api/metrics/:scale
:scalecan be1h,6h,24h,7d, or30d.- Retrieves system metrics for the specified time scale.
- GET /api/logs
- Retrieves logs with optional filters.
- Query Parameters:
level: Filter by log level (e.g.,ERROR,INFO).method: Filter by HTTP method (e.g.,GET,POST).status_code: Filter by HTTP status code.endpoint: Filter by endpoint URL.id: Filter by a specific log ID.startDateandendDate: Filter by date range (Unix ms timestamps).response_time_minandresponse_time_max: Filter by response time range (ms).limitandoffset: Pagination parameters.sortandorder: Sorting parameters (e.g.,timestamp,response_time).
- GET /api/logs/id/:id
- Retrieves a specific log by its ID.
- DELETE /api/logs/all
- Deletes all logs.
- DELETE /api/logs
- Deletes logs based on filters.
- Query Parameters: Same as
GET /api/logs.
src/
├── app/
│ ├── layout.tsx
│ ├── page.tsx # redirect / dashboard
│ ├── (dashboard)/ # route group
│ │ ├── layout.tsx # sidebar layout
│ │ ├── metrics/
│ │ │ ├── page.tsx
│ │ ├── logs/
│ │ │ ├── page.tsx
│ │ ├── api-status/
│ │ │ ├── page.tsx
│
├── components/ # reusable UI (dumb components)
│ ├── ui/
│ │ ├── card.tsx
│ │ ├── button.tsx
│ │ ├── badge.tsx
│ │ ├── chart.tsx
│ │
│ ├── layout/
│ │ ├── sidebar.tsx
│ │ ├── header.tsx
│
│ ├── charts/
│ │ ├── line-chart.tsx
│ │ ├── bar-chart.tsx
│
├── features/
│ ├── metrics/
│ │ ├── components/
│ │ │ ├── cpu-card.tsx
│ │ │ ├── memory-card.tsx
│ │ │ ├── uptime-card.tsx
│ │ │ ├── network-card.tsx
│ │ │
│ │ ├── hooks/
│ │ │ ├── useMetrics.ts
│ │ │
│ │ ├── services/
│ │ │ ├── metrics.service.ts
│ │
│ ├── logs/
│ │ ├── components/
│ │ │ ├── logs-table.tsx
│ │ │ ├── logs-filters.tsx
│ │ │ ├── logs-row.tsx
│ │ │
│ │ ├── hooks/
│ │ │ ├── useLogs.ts
│ │ │
│ │ ├── services/
│ │ │ ├── logs.service.ts
│ │
│ ├── api-status/
│ │ ├── components/
│ │ ├── hooks/
│ │ ├── services/
│
├── lib/
│ ├── api.ts # fetch wrapper
│ ├── socket.ts # socket.io client
│ ├── utils.ts
│
├── store/ # global state (optional)
│ ├── useDashboardStore.ts # Zustand / Redux
│
├── types/
│ ├── logs.ts
│ ├── metrics.ts
│
├── constants/
│ ├── routes.ts
│ ├── config.ts
Contains all pages and routing (Next.js App Router).
page.tsx→ redirects to dashboard(dashboard)/→ route group for all dashboard-related pages
Examples:
metrics/page.tsx→ metrics pagelogs/page.tsx→ logs page
Global reusable UI components. These are dumb/presentational components used across the app.
Examples:
- Buttons
- Cards
- Charts
- Layout components (sidebar, header)
Core of the application logic. Each feature (metrics, logs, etc.) is self-contained.
| Folder | Purpose |
|---|---|
components/ |
UI specific to the metrics page (e.g. CPUUsage, MemoryUsage, Uptime) — one component per metric |
services/ |
API calls (e.g. metrics.service.ts) |
hooks/ |
React hooks for state and logic (e.g. useMetrics.ts — handles fetching, loading, and error state) |
Same structure as metrics/:
| Folder | Purpose |
|---|---|
components/ |
Logs UI (table, filters, rows) |
services/ |
API calls (e.g. logs.service.ts) |
hooks/ |
State and logic (e.g. useLogs.ts) |
Shared utilities and integrations.
| File | Purpose |
|---|---|
api.ts |
Wrapper around fetch |
socket.ts |
Socket.IO client for realtime updates |
utils.ts |
Helper functions |
Verify that PostgreSQL is running and the connection is working:
cd server
npm run devLook for this output in the console:
Successfully connected to the PostgreSQL database
Metrics table initialized successfully.
PostgreSQL LISTEN client connected
Server running on http://localhost:3004
Once the server is running, metrics should be collected automatically every 30 seconds:
# Check for metrics collection logs
npm run devLook for output like:
Metrics collected and saved to DB
# Get metrics for the last 1 hour
curl http://localhost:3004/api/metrics/1h
# Get metrics for the last 24 hours
curl http://localhost:3004/api/metrics/24h
# Get metrics for the last 7 days
curl http://localhost:3004/api/metrics/7d
# Get metrics for the last 30 days
curl http://localhost:3004/api/metrics/30d# Get all logs
curl http://localhost:3004/api/logs
# Get logs with filters
curl "http://localhost:3004/api/logs?level=ERROR&limit=10"
# Get a specific log by ID (replace :id with an actual log ID)
curl http://localhost:3004/api/logs/id/:idConnect to PostgreSQL and verify the table was created:
# Access PostgreSQL
psql -h localhost -U <DB_USER> -d hab_dashboard
# Check if system_metrics table exists
\dt system_metrics
# View table structure
\d system_metrics
# Count metrics records
SELECT COUNT(*) FROM system_metrics;
# View recent metrics (last 5 records)
SELECT id, measured_at, cpu_usage, memory_used FROM system_metrics ORDER BY measured_at DESC LIMIT 5;Ensure both server and client are running:
# Terminal 1: Start server
cd server
npm run dev
# Terminal 2: Start client
cd client
pnpm devOpen your browser and navigate to:
- Dashboard: http://localhost:3005
- Metrics page: http://localhost:3005/metrics
- Logs page: http://localhost:3005/logs
- Open http://localhost:3005 in your browser
- Watch the metrics dashboard update in real-time
- Metrics should refresh automatically as new data is collected
- Resize your browser window
- Verify that the dashboard layout is responsive on different screen sizes
- Check mobile view (use browser DevTools - F12 → toggle device toolbar)
Verify TypeScript configuration is correct:
# Check client types
cd client && npx tsc --noEmit
# Check server types
cd server && npx tsc --noEmit- Server starts without errors
- Database connection is established
-
system_metricstable is created - Metrics are collected every 30 seconds
-
/api/metrics/:scalereturns data -
/api/logsendpoint responds - Client starts on http://localhost:3005
- Metrics dashboard displays data
- Logs page loads and displays logs
- Real-time updates work (data updates without page refresh)
- Responsive design works on mobile/tablet/desktop
Problem: The server fails to collect metrics with error: relation "system_metrics" does not exist
Solution: The metrics table is automatically created when the server starts. If this error occurs:
- Ensure PostgreSQL is running
- Verify environment variables in
server/.envare correct (DB_HOST, DB_NAME, DB_USER, DB_PASSWORD, DB_PORT) - Restart the server:
npm run dev
The initialization script will drop and recreate the system_metrics table with the correct schema on startup.
Problem: The server fails with a data type error when inserting metrics
Solution: This has been fixed in the current version. The schema uses DOUBLE PRECISION for numeric metrics fields (uptime, cpu_usage, memory_total, etc.) to handle floating-point values correctly.