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
+12
View File
@@ -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()
+1
View File
@@ -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
+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
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
+15
View File
@@ -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",
+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:
# 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