File size: 4,421 Bytes
895687d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Formatting helpers, matching the design's own conventions.

Two rules run through all of this and are worth stating once:

  * A number that does not exist renders as an em dash, never as `0`. The
    Atlas is a tool for judging models, and a fabricated zero is worse than an
    admitted gap.
  * Counts are grouped with commas the way the design writes them
    ("1,842,301"), and abbreviated only where the design abbreviates.
"""

from __future__ import annotations

from datetime import datetime, timezone

DASH = "—"


def num(value) -> str:
    """1842301 -> '1,842,301'. None or unparseable -> em dash."""
    if value is None:
        return DASH
    try:
        return f"{int(value):,}"
    except (TypeError, ValueError):
        return DASH


def compact(value) -> str:
    """731455 -> '731K'. Used in the lineage tree, where space is tight.

    A decimal is kept only below 10, matching how the design writes these:
    "731K", "188K", "1.8M" -- never "731.5K", which is both wider than the
    column and more precision than the number deserves.
    """
    if value is None:
        return DASH
    try:
        n = int(value)
    except (TypeError, ValueError):
        return DASH
    for cutoff, suffix in ((1_000_000_000, "B"), (1_000_000, "M"), (1_000, "K")):
        if abs(n) >= cutoff:
            scaled = n / cutoff
            if abs(scaled) < 10:
                text = f"{scaled:.1f}".rstrip("0").rstrip(".")
            else:
                text = str(round(scaled))
            return f"{text}{suffix}"
    return str(n)


def parse_stamp(value):
    """ISO 8601 -> aware datetime, or None. Never raises."""
    if not value or not isinstance(value, str):
        return None
    try:
        when = datetime.fromisoformat(value.replace("Z", "+00:00"))
    except ValueError:
        return None
    return when.replace(tzinfo=timezone.utc) if when.tzinfo is None else when


def months_since(value, now=None) -> float | None:
    when = parse_stamp(value)
    if when is None:
        return None
    now = now or datetime.now(timezone.utc)
    return (now - when).days / 30.44


def age_label(value, now=None) -> str:
    """The design's age wording: '3w ago', '9mo ago', '2.6y ago'."""
    months = months_since(value, now)
    if months is None:
        return DASH
    if months < 1:
        days = max(0, int(months * 30.44))
        if days < 7:
            return f"{days}d ago" if days else "today"
        return f"{days // 7}w ago"
    if months < 12:
        return f"{int(months)}mo ago"
    years = f"{months / 12:.1f}".rstrip("0").rstrip(".")
    return f"{years}y ago"


def age_tone(value, now=None) -> tuple:
    """(colour, glyph) for a commit age, on the design's three-band scale."""
    months = months_since(value, now)
    if months is None:
        return "var(--text-tertiary)", "·"
    if months < 3:
        return "var(--fin-up)", "●"
    if months < 12:
        return "var(--accent-amber-strong)", "◐"
    return "var(--fin-down)", "○"


def short_ago(value, now=None) -> str:
    """Compact 'how long since the last crawl', for the header pill."""
    when = parse_stamp(value)
    if when is None:
        return DASH
    now = now or datetime.now(timezone.utc)
    seconds = max(0, (now - when).total_seconds())
    if seconds < 3600:
        return f"{int(seconds // 60)}m"
    if seconds < 86400:
        return f"{int(seconds // 3600)}h"
    return f"{int(seconds // 86400)}d"


def utc_stamp(value) -> str:
    """'2026-08-20 06:14 UTC', the design's header format."""
    when = parse_stamp(value)
    if when is None:
        return DASH
    return when.astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")


def iso_date(value) -> str:
    when = parse_stamp(value)
    return when.astimezone(timezone.utc).strftime("%Y-%m-%d") if when else DASH


def percent(part, whole) -> str:
    """'61%'. A zero denominator is an em dash, not a division by zero."""
    try:
        whole = int(whole)
        if whole <= 0:
            return DASH
        return f"{round(int(part) / whole * 100)}%"
    except (TypeError, ValueError, ZeroDivisionError):
        return DASH


def initials(model_id: str) -> str:
    """The two-character author avatar the design puts on every row."""
    author = (model_id or "").split("/")[0]
    if not author:
        return "??"
    return (author[0] + (author[1] if len(author) > 1 else "")).upper()