2026-03-07 09:46:36 +07:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
2026-03-07 23:55:11 +07:00
|
|
|
const API_BASE = import.meta.env.VITE_API_BASE || '/api';
|
2026-03-07 09:46:36 +07:00
|
|
|
|
|
|
|
|
const api = axios.create({ baseURL: API_BASE, withCredentials: true });
|
|
|
|
|
|
|
|
|
|
export interface AdminUserResponse {
|
|
|
|
|
id: string;
|
|
|
|
|
username: string;
|
|
|
|
|
email: string;
|
|
|
|
|
avatar_url: string | null;
|
|
|
|
|
is_active: boolean;
|
|
|
|
|
is_admin: boolean;
|
|
|
|
|
created_at: string;
|
|
|
|
|
project_count: number;
|
feat(metrics): add usage analytics dashboard with country tracking
Track per-user, per-project, and per-board usage to inform pricing tier
decisions. Adds an admin dashboard with KPIs (DAU/WAU/MAU, totals,
success rate), time-series charts for compiles/runs, board family +
FQBN breakdowns, "board diversity" pie chart (key freemium signal),
top users/projects, and per-country breakdown via Cloudflare's
CF-IPCountry header. Admin can now also view private projects.
Backend:
- New UsageEvent table (append-only event log with user_id, project_id,
event_type, board_fqbn/family, country, error_kind, duration_ms)
- Aggregate counters on User (total_compiles/runs/errors, last_active,
signup_country, last_country) and Project (compile/run/update counts,
last_compiled/run timestamps) kept in sync by MetricsService for O(1)
dashboard reads
- 10 admin endpoints under /api/admin/metrics/{overview, timeseries,
boards, board-diversity, top-users, top-projects, countries,
users/{id}, projects/{id}}
- POST /api/metrics/run for client-side run telemetry
- Country detection via cf-ipcountry header (no DB / no API calls)
- Auto-migrations in lifespan for legacy DBs
Frontend:
- recharts-powered Dashboard tab with KPI cards and 4 charts
- New Boards tab with per-family + per-FQBN breakdown
- Country column with flag emoji on Users tab
- Top countries card on Dashboard
- compileCode now forwards project_id; Run button reports via WS
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-26 05:46:52 +07:00
|
|
|
// Usage metrics
|
|
|
|
|
total_compiles: number;
|
|
|
|
|
total_compile_errors: number;
|
|
|
|
|
total_runs: number;
|
|
|
|
|
last_active_at: string | null;
|
|
|
|
|
boards_used: string[];
|
|
|
|
|
signup_country: string | null;
|
|
|
|
|
last_country: string | null;
|
2026-03-07 09:46:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AdminUserUpdateRequest {
|
|
|
|
|
username?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
password?: string;
|
|
|
|
|
is_active?: boolean;
|
|
|
|
|
is_admin?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AdminProjectResponse {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
slug: string;
|
|
|
|
|
description: string | null;
|
|
|
|
|
is_public: boolean;
|
|
|
|
|
board_type: string;
|
|
|
|
|
owner_username: string;
|
|
|
|
|
owner_id: string;
|
|
|
|
|
created_at: string;
|
|
|
|
|
updated_at: string;
|
feat(metrics): add usage analytics dashboard with country tracking
Track per-user, per-project, and per-board usage to inform pricing tier
decisions. Adds an admin dashboard with KPIs (DAU/WAU/MAU, totals,
success rate), time-series charts for compiles/runs, board family +
FQBN breakdowns, "board diversity" pie chart (key freemium signal),
top users/projects, and per-country breakdown via Cloudflare's
CF-IPCountry header. Admin can now also view private projects.
Backend:
- New UsageEvent table (append-only event log with user_id, project_id,
event_type, board_fqbn/family, country, error_kind, duration_ms)
- Aggregate counters on User (total_compiles/runs/errors, last_active,
signup_country, last_country) and Project (compile/run/update counts,
last_compiled/run timestamps) kept in sync by MetricsService for O(1)
dashboard reads
- 10 admin endpoints under /api/admin/metrics/{overview, timeseries,
boards, board-diversity, top-users, top-projects, countries,
users/{id}, projects/{id}}
- POST /api/metrics/run for client-side run telemetry
- Country detection via cf-ipcountry header (no DB / no API calls)
- Auto-migrations in lifespan for legacy DBs
Frontend:
- recharts-powered Dashboard tab with KPI cards and 4 charts
- New Boards tab with per-family + per-FQBN breakdown
- Country column with flag emoji on Users tab
- Top countries card on Dashboard
- compileCode now forwards project_id; Run button reports via WS
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-26 05:46:52 +07:00
|
|
|
// Usage metrics
|
|
|
|
|
compile_count: number;
|
|
|
|
|
compile_error_count: number;
|
|
|
|
|
run_count: number;
|
|
|
|
|
update_count: number;
|
|
|
|
|
last_compiled_at: string | null;
|
|
|
|
|
last_run_at: string | null;
|
2026-03-07 09:46:36 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getAdminSetupStatus(): Promise<{ has_admin: boolean }> {
|
|
|
|
|
const { data } = await api.get('/admin/setup/status');
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 02:45:45 +07:00
|
|
|
export async function createFirstAdmin(
|
|
|
|
|
username: string,
|
|
|
|
|
email: string,
|
|
|
|
|
password: string,
|
|
|
|
|
): Promise<AdminUserResponse> {
|
2026-03-07 10:00:22 +07:00
|
|
|
const { data } = await api.post('/admin/setup', { username, email, password });
|
2026-03-07 09:46:36 +07:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function adminListUsers(): Promise<AdminUserResponse[]> {
|
|
|
|
|
const { data } = await api.get('/admin/users');
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function adminGetUser(userId: string): Promise<AdminUserResponse> {
|
|
|
|
|
const { data } = await api.get(`/admin/users/${userId}`);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 02:45:45 +07:00
|
|
|
export async function adminUpdateUser(
|
|
|
|
|
userId: string,
|
|
|
|
|
body: AdminUserUpdateRequest,
|
|
|
|
|
): Promise<AdminUserResponse> {
|
2026-03-07 09:46:36 +07:00
|
|
|
const { data } = await api.put(`/admin/users/${userId}`, body);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function adminDeleteUser(userId: string): Promise<void> {
|
|
|
|
|
await api.delete(`/admin/users/${userId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function adminListProjects(): Promise<AdminProjectResponse[]> {
|
|
|
|
|
const { data } = await api.get('/admin/projects');
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function adminDeleteProject(projectId: string): Promise<void> {
|
|
|
|
|
await api.delete(`/admin/projects/${projectId}`);
|
|
|
|
|
}
|