feat(tags): implement custom tag color merging and label support

This commit is contained in:
shay
2026-04-18 14:52:32 -04:00
parent 3e70e4b4ef
commit 3ecb6ad67b
6 changed files with 54 additions and 4 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ import { THEMES, TAG_COLORS, GRADE_COLORS, FOCUS_COLORS, LANG_COLORS, type Theme
export type GradeData = { export type GradeData = {
grade: string; score: number grade: string; score: number
stats: Record<string, number> stats: Record<string, number>
tags: { tag: string; source: string; confidence: number }[] tags: { tag: string; source: string; confidence: number; label?: string | null }[]
breakdown: Record<string, number> breakdown: Record<string, number>
} }
export type ImpactWeek = { week_start: string; commits: number } 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 let tagsSvg = '', tx = 0, ty = 0
for (const tag of tags) { for (const tag of tags) {
const tc = TAG_COLORS[tag.tag] ?? t.accent 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 tw = Math.round(label.length * 6.6 + 18)
const po = tag.source === 'earned' ? 0.9 : 0.55 const po = tag.source === 'earned' ? 0.9 : 0.55
if (tx + tw > 340) { tx = 0; ty += 30 } if (tx + tw > 340) { tx = 0; ty += 30 }
+12
View File
@@ -153,6 +153,18 @@ def enrollments_today() -> int:
return row["count"] if row else 0 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]: def get_settings(username: str) -> Optional[dict]:
with _settings_conn() as c: with _settings_conn() as c:
row = c.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone() row = c.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone()
+1
View File
@@ -17,6 +17,7 @@ class TagData:
tag: str tag: str
source: str = "earned" # "earned" | "chosen" source: str = "earned" # "earned" | "chosen"
confidence: float = 1.0 confidence: float = 1.0
label: str | None = None # optional display override (e.g. "Founder #42")
@dataclass @dataclass
+15 -1
View File
@@ -191,6 +191,7 @@ def _compute_tags(github_data: dict, max_tags: int = None, custom_tags: list[str
# Apply threshold rules # Apply threshold rules
awarded_tags = [] awarded_tags = []
tag_label_overrides: dict[str, str] = {}
# Fullstack: >25% frontend AND >25% backend # Fullstack: >25% frontend AND >25% backend
if category_percentages.get("Frontend", 0) > 0.25 and category_percentages.get("Backend", 0) > 0.25: 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: for tag in custom_tags:
awarded_tags.append((tag, 1.0)) 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 # Sort by confidence and limit to max_tags
for tag, conf in sorted(awarded_tags, key=lambda x: -x[1])[: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 return tags
+15
View File
@@ -94,6 +94,21 @@ TAG_COLORS = {
"open-source": "#3fb950", "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 = { GRADE_COLORS = {
"S": "#ff6b9d", "S": "#ff6b9d",
"A": "#3fb950", "A": "#3fb950",
+9 -1
View File
@@ -11,6 +11,14 @@ _TAG_ACRONYMS = {"ml", "ai", "ui", "ux", "api", "cli", "sql", "css", "html", "js
def _format_tag_label(tag: str) -> str: 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( return " ".join(
w.upper() if w.lower() in _TAG_ACRONYMS else w.capitalize() w.upper() if w.lower() in _TAG_ACRONYMS else w.capitalize()
for w in tag.split("-") 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 []): for tag in (data.tags or []):
tag_color = TAG_COLORS.get(tag.tag, t["accent"]) 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) tw = int(len(label) * 6.6 + 18)
pill_opacity = 0.9 if tag.source == "earned" else 0.55 pill_opacity = 0.9 if tag.source == "earned" else 0.55