2026-03-06 20:14:50 +07:00
|
|
|
import uuid
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
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
|
|
|
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, UniqueConstraint
|
2026-03-06 20:14:50 +07:00
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
|
|
|
|
from app.database.session import Base
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Project(Base):
|
|
|
|
|
__tablename__ = "projects"
|
|
|
|
|
__table_args__ = (UniqueConstraint("user_id", "slug", name="uq_user_slug"),)
|
|
|
|
|
|
|
|
|
|
id: Mapped[str] = mapped_column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
|
|
|
|
user_id: Mapped[str] = mapped_column(String, ForeignKey("users.id"), nullable=False, index=True)
|
|
|
|
|
name: Mapped[str] = mapped_column(String(120), nullable=False)
|
|
|
|
|
slug: Mapped[str] = mapped_column(String(120), nullable=False)
|
|
|
|
|
description: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
|
|
|
|
is_public: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
|
board_type: Mapped[str] = mapped_column(String(50), default="arduino-uno")
|
|
|
|
|
code: Mapped[str] = mapped_column(Text, default="")
|
|
|
|
|
components_json: Mapped[str] = mapped_column(Text, default="[]")
|
|
|
|
|
wires_json: Mapped[str] = mapped_column(Text, default="[]")
|
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
|
|
|
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
|
|
|
|
|
)
|
|
|
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
|
|
|
DateTime(timezone=True),
|
|
|
|
|
default=lambda: datetime.now(timezone.utc),
|
|
|
|
|
onupdate=lambda: datetime.now(timezone.utc),
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
# Aggregate usage counters (kept in sync by MetricsService for O(1) reads)
|
|
|
|
|
compile_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
|
|
|
compile_error_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
|
|
|
run_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
|
|
|
update_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
|
|
|
last_compiled_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
|
last_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
|
|
2026-03-06 20:14:50 +07:00
|
|
|
owner: Mapped["User"] = relationship("User", back_populates="projects") # noqa: F821
|