2026-03-06 20:14:50 +07:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
2026-03-07 06:38:06 +07:00
|
|
|
class SketchFile(BaseModel):
|
|
|
|
|
name: str
|
|
|
|
|
content: str
|
|
|
|
|
|
|
|
|
|
|
2026-03-06 20:14:50 +07:00
|
|
|
class ProjectCreateRequest(BaseModel):
|
|
|
|
|
name: str
|
|
|
|
|
description: str | None = None
|
|
|
|
|
is_public: bool = True
|
|
|
|
|
board_type: str = "arduino-uno"
|
2026-03-07 06:38:06 +07:00
|
|
|
# Multi-file workspace. Falls back to legacy `code` field if omitted.
|
|
|
|
|
files: list[SketchFile] | None = None
|
|
|
|
|
code: str = "" # legacy single-file fallback
|
2026-03-06 20:14:50 +07:00
|
|
|
components_json: str = "[]"
|
|
|
|
|
wires_json: str = "[]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectUpdateRequest(BaseModel):
|
|
|
|
|
name: str | None = None
|
|
|
|
|
description: str | None = None
|
|
|
|
|
is_public: bool | None = None
|
|
|
|
|
board_type: str | None = None
|
2026-03-07 06:38:06 +07:00
|
|
|
files: list[SketchFile] | None = None
|
|
|
|
|
code: str | None = None # legacy
|
2026-03-06 20:14:50 +07:00
|
|
|
components_json: str | None = None
|
|
|
|
|
wires_json: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectResponse(BaseModel):
|
|
|
|
|
id: str
|
|
|
|
|
name: str
|
|
|
|
|
slug: str
|
|
|
|
|
description: str | None
|
|
|
|
|
is_public: bool
|
|
|
|
|
board_type: str
|
2026-03-07 06:38:06 +07:00
|
|
|
# Files loaded from disk volume
|
|
|
|
|
files: list[SketchFile] = []
|
|
|
|
|
# Legacy single-file code (kept for backwards compat)
|
2026-03-06 20:14:50 +07:00
|
|
|
code: str
|
|
|
|
|
components_json: str
|
|
|
|
|
wires_json: str
|
|
|
|
|
owner_username: str
|
|
|
|
|
created_at: datetime
|
|
|
|
|
updated_at: datetime
|
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 (kept in sync by MetricsService)
|
|
|
|
|
compile_count: int = 0
|
|
|
|
|
compile_error_count: int = 0
|
|
|
|
|
run_count: int = 0
|
|
|
|
|
update_count: int = 0
|
|
|
|
|
last_compiled_at: datetime | None = None
|
|
|
|
|
last_run_at: datetime | None = None
|
2026-03-06 20:14:50 +07:00
|
|
|
|
|
|
|
|
model_config = {"from_attributes": True}
|