From 3ecb6ad67be89aa7d22a73b66a04db95a4d97e40 Mon Sep 17 00:00:00 2001 From: shay Date: Sat, 18 Apr 2026 14:52:32 -0400 Subject: [PATCH] feat(tags): implement custom tag color merging and label support --- generator/frontend/src/lib/renderWidgets.ts | 4 ++-- generator/src/db.py | 12 ++++++++++++ generator/src/models/types.py | 1 + generator/src/processor.py | 16 +++++++++++++++- generator/src/themes/themes.py | 15 +++++++++++++++ generator/src/widgets/grade.py | 10 +++++++++- 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/generator/frontend/src/lib/renderWidgets.ts b/generator/frontend/src/lib/renderWidgets.ts index d7211af..941661a 100644 --- a/generator/frontend/src/lib/renderWidgets.ts +++ b/generator/frontend/src/lib/renderWidgets.ts @@ -5,7 +5,7 @@ import { THEMES, TAG_COLORS, GRADE_COLORS, FOCUS_COLORS, LANG_COLORS, type Theme export type GradeData = { grade: string; score: number stats: Record - tags: { tag: string; source: string; confidence: number }[] + tags: { tag: string; source: string; confidence: number; label?: string | null }[] breakdown: Record } export type ImpactWeek = { week_start: string; commits: number } @@ -91,7 +91,7 @@ function renderGrade(data: GradeData, t: Theme, s?: { max_tags?: number }): stri let tagsSvg = '', tx = 0, ty = 0 for (const tag of tags) { const tc = TAG_COLORS[tag.tag] ?? t.accent - const label = formatTagLabel(tag.tag) + const label = tag.label || formatTagLabel(tag.tag) const tw = Math.round(label.length * 6.6 + 18) const po = tag.source === 'earned' ? 0.9 : 0.55 if (tx + tw > 340) { tx = 0; ty += 30 } diff --git a/generator/src/db.py b/generator/src/db.py index e13f9fa..3ee170c 100644 --- a/generator/src/db.py +++ b/generator/src/db.py @@ -153,6 +153,18 @@ def enrollments_today() -> int: return row["count"] if row else 0 +def enrollment_rank(username: str) -> Optional[int]: + """1-based position in overall enrollment order by enrolled_at. Used by + tag rules that reward early users (e.g. the 'founder' tag).""" + with _settings_conn() as c: + row = c.execute("SELECT enrolled_at FROM users WHERE username=?", (username,)).fetchone() + if row is None: + return None + return c.execute( + "SELECT COUNT(*) FROM users WHERE enrolled_at <= ?", (row["enrolled_at"],) + ).fetchone()[0] + + def get_settings(username: str) -> Optional[dict]: with _settings_conn() as c: row = c.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone() diff --git a/generator/src/models/types.py b/generator/src/models/types.py index 8a542aa..f8bf7c8 100644 --- a/generator/src/models/types.py +++ b/generator/src/models/types.py @@ -17,6 +17,7 @@ class TagData: tag: str source: str = "earned" # "earned" | "chosen" confidence: float = 1.0 + label: str | None = None # optional display override (e.g. "Founder #42") @dataclass diff --git a/generator/src/processor.py b/generator/src/processor.py index 529432c..bde2321 100644 --- a/generator/src/processor.py +++ b/generator/src/processor.py @@ -191,6 +191,7 @@ def _compute_tags(github_data: dict, max_tags: int = None, custom_tags: list[str # Apply threshold rules awarded_tags = [] + tag_label_overrides: dict[str, str] = {} # Fullstack: >25% frontend AND >25% backend if category_percentages.get("Frontend", 0) > 0.25 and category_percentages.get("Backend", 0) > 0.25: @@ -237,9 +238,22 @@ def _compute_tags(github_data: dict, max_tags: int = None, custom_tags: list[str for tag in custom_tags: awarded_tags.append((tag, 1.0)) + # Auto-awarded tags (username-specific + rule-based; see src/tag_rules.py). + from . import tag_rules + username = (github_data.get("user") or {}).get("login", "") + for auto_tag, label in tag_rules.evaluate(username, github_data): + awarded_tags.append((auto_tag, 1.0)) + if label: + tag_label_overrides[auto_tag] = label + # Sort by confidence and limit to max_tags for tag, conf in sorted(awarded_tags, key=lambda x: -x[1])[:max_tags]: - tags.append(TagData(tag=tag, source="earned" if conf < 1.0 else "custom", confidence=round(conf, 2))) + tags.append(TagData( + tag=tag, + source="earned" if conf < 1.0 else "custom", + confidence=round(conf, 2), + label=tag_label_overrides.get(tag), + )) return tags diff --git a/generator/src/themes/themes.py b/generator/src/themes/themes.py index 960e4a9..6e8e52b 100644 --- a/generator/src/themes/themes.py +++ b/generator/src/themes/themes.py @@ -94,6 +94,21 @@ TAG_COLORS = { "open-source": "#3fb950", } +# Pick up colors for custom/auto-awarded tags defined in src/tag_rules.py so +# adding a new tag there is a single-file change. +def _merge_custom_tag_colors() -> None: + try: + from .. import tag_rules + except Exception: + return + for tag_id, meta in tag_rules.TAG_DEFS.items(): + color = meta.get("color") + if color and tag_id not in TAG_COLORS: + TAG_COLORS[tag_id] = color + + +_merge_custom_tag_colors() + GRADE_COLORS = { "S": "#ff6b9d", "A": "#3fb950", diff --git a/generator/src/widgets/grade.py b/generator/src/widgets/grade.py index 8c68ebe..a59ee9f 100644 --- a/generator/src/widgets/grade.py +++ b/generator/src/widgets/grade.py @@ -11,6 +11,14 @@ _TAG_ACRONYMS = {"ml", "ai", "ui", "ux", "api", "cli", "sql", "css", "html", "js def _format_tag_label(tag: str) -> str: + # Custom tags may define an explicit label in src/tag_rules.py::TAG_DEFS. + try: + from .. import tag_rules + meta = tag_rules.TAG_DEFS.get(tag) + if meta and meta.get("label"): + return meta["label"] + except Exception: + pass return " ".join( w.upper() if w.lower() in _TAG_ACRONYMS else w.capitalize() for w in tag.split("-") @@ -74,7 +82,7 @@ def render_grade_widget(data: GradeData, theme_name: str = "dark", settings: dic for tag in (data.tags or []): tag_color = TAG_COLORS.get(tag.tag, t["accent"]) - label = _format_tag_label(tag.tag) + label = getattr(tag, "label", None) or _format_tag_label(tag.tag) tw = int(len(label) * 6.6 + 18) pill_opacity = 0.9 if tag.source == "earned" else 0.55