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, Integer, String
|
2026-03-06 20:14:50 +07:00
|
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
|
|
|
|
|
|
from app.database.session import Base
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class User(Base):
|
|
|
|
|
__tablename__ = "users"
|
|
|
|
|
|
|
|
|
|
id: Mapped[str] = mapped_column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
|
|
|
|
|
username: Mapped[str] = mapped_column(String(30), unique=True, index=True, nullable=False)
|
|
|
|
|
email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False)
|
|
|
|
|
hashed_password: Mapped[str | None] = mapped_column(String, nullable=True)
|
|
|
|
|
google_id: Mapped[str | None] = mapped_column(String, unique=True, nullable=True)
|
|
|
|
|
avatar_url: Mapped[str | None] = mapped_column(String, nullable=True)
|
|
|
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
2026-03-07 09:46:36 +07:00
|
|
|
is_admin: Mapped[bool] = mapped_column(Boolean, default=False)
|
2026-03-06 20:14:50 +07:00
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
|
|
|
DateTime(timezone=True), default=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)
|
|
|
|
|
total_compiles: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
|
|
|
total_compile_errors: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
|
|
|
total_runs: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
|
|
|
last_active_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
|
# ISO-3166 alpha-2 country codes from CF-IPCountry. Country only (no city / IP).
|
|
|
|
|
signup_country: Mapped[str | None] = mapped_column(String(2), nullable=True, index=True)
|
|
|
|
|
last_country: Mapped[str | None] = mapped_column(String(2), nullable=True, index=True)
|
|
|
|
|
|
2026-05-05 20:21:37 +07:00
|
|
|
# Subscription state. Self-hosters never set these — defaults are safe
|
|
|
|
|
# (no paid features unlock). Production velxio.dev syncs them from an
|
|
|
|
|
# external billing system (Odoo) via webhook + periodic resync.
|
|
|
|
|
is_paid_subscriber: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
|
|
|
|
subscription_status: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
|
|
|
|
subscription_period_end: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
|
odoo_partner_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
|
|
|
|
2026-03-06 20:14:50 +07:00
|
|
|
projects: Mapped[list["Project"]] = relationship("Project", back_populates="owner", lazy="select") # noqa: F821
|