Spaces:
Sleeping
Sleeping
Remove Most improved, score every ranked file, and feed ranked files into stats
Browse files- TELEMETRY.md +3 -4
- server/app.py +3 -1
- server/hygiene.py +10 -1
- server/leaderboard.py +47 -75
- server/telemetry.py +45 -4
- web/src/board.css +1 -35
- web/src/board.html +0 -4
- web/src/board.js +5 -19
TELEMETRY.md
CHANGED
|
@@ -83,13 +83,12 @@ The leaderboard page (`/leaderboard/`, opened in a new tab from the banner) rank
|
|
| 83 |
|
| 84 |
The browser sends only the owner and repository name to `POST /api/leaderboard/rank`. Any other field, a score included, is rejected. The server then:
|
| 85 |
|
| 86 |
-
1. Reads the repo's metadata from the GitHub API. Private repos and forks are refused. If the API is rate limited, the repo is ranked from its archive alone
|
| 87 |
2. Downloads the default branch as a `tar.gz` archive from `codeload.github.com` (at most 80 MB) and reads it in memory. Nothing is written to disk. The commit SHA comes from the archive header.
|
| 88 |
3. Keeps hand-written Python, TypeScript, JavaScript, Go and Rust files. Tests, vendored and build folders, minified, generated and type-declaration files, files over 150 KB and binary files are skipped.
|
| 89 |
4. Requires at least 5 such files and 1,000 lines.
|
| 90 |
-
5. Scores
|
| 91 |
-
6.
|
| 92 |
-
7. Refuses a commit that is already ranked under another name, which catches mirrors and forks.
|
| 93 |
|
| 94 |
A ranked repo is reused for 24 hours. Each address may ask for 2 ranks a minute, the whole Space 20, and at most 3 wait at once.
|
| 95 |
|
|
|
|
| 83 |
|
| 84 |
The browser sends only the owner and repository name to `POST /api/leaderboard/rank`. Any other field, a score included, is rejected. The server then:
|
| 85 |
|
| 86 |
+
1. Reads the repo's metadata from the GitHub API. Private repos and forks are refused. If the API is rate limited, the repo is ranked from its archive alone.
|
| 87 |
2. Downloads the default branch as a `tar.gz` archive from `codeload.github.com` (at most 80 MB) and reads it in memory. Nothing is written to disk. The commit SHA comes from the archive header.
|
| 88 |
3. Keeps hand-written Python, TypeScript, JavaScript, Go and Rust files. Tests, vendored and build folders, minified, generated and type-declaration files, files over 150 KB and binary files are skipped.
|
| 89 |
4. Requires at least 5 such files and 1,000 lines.
|
| 90 |
+
5. Scores every kept file in the archive with the Python `codehygiene` scorer and feeds each file's aggregate digest, extension, size band, score and issue counts into `/api/stats`. The repo score is the line-weighted mean across all ranked files, out of 100.
|
| 91 |
+
6. Refuses a commit that is already ranked under another name, which catches mirrors and forks.
|
|
|
|
| 92 |
|
| 93 |
A ranked repo is reused for 24 hours. Each address may ask for 2 ranks a minute, the whole Space 20, and at most 3 wait at once.
|
| 94 |
|
server/app.py
CHANGED
|
@@ -52,7 +52,9 @@ def create_app(
|
|
| 52 |
) -> FastAPI:
|
| 53 |
settings = settings or load_settings()
|
| 54 |
telemetry = Telemetry(excluded=preset_digests(settings.presets_path))
|
| 55 |
-
board = board or Leaderboard(settings, github_transport)
|
|
|
|
|
|
|
| 56 |
dataset = (settings.stats_dataset_repo, settings.hf_token)
|
| 57 |
synced = (
|
| 58 |
SyncedFile(StatsStore(settings.stats_path, *dataset), telemetry),
|
|
|
|
| 52 |
) -> FastAPI:
|
| 53 |
settings = settings or load_settings()
|
| 54 |
telemetry = Telemetry(excluded=preset_digests(settings.presets_path))
|
| 55 |
+
board = board or Leaderboard(settings, github_transport, telemetry=telemetry)
|
| 56 |
+
if board.telemetry is None:
|
| 57 |
+
board.telemetry = telemetry
|
| 58 |
dataset = (settings.stats_dataset_repo, settings.hf_token)
|
| 59 |
synced = (
|
| 60 |
SyncedFile(StatsStore(settings.stats_path, *dataset), telemetry),
|
server/hygiene.py
CHANGED
|
@@ -2,10 +2,11 @@ from __future__ import annotations
|
|
| 2 |
|
| 3 |
import hashlib
|
| 4 |
import importlib.util
|
| 5 |
-
import sys
|
| 6 |
from pathlib import Path
|
|
|
|
| 7 |
from typing import Any
|
| 8 |
|
|
|
|
| 9 |
def _find_repo_root() -> Path:
|
| 10 |
current = Path(__file__).resolve().parent
|
| 11 |
for parent in current.parents:
|
|
@@ -34,3 +35,11 @@ def score_dict(content: str, threshold: float) -> dict[str, Any]:
|
|
| 34 |
|
| 35 |
def score_value(content: str) -> float:
|
| 36 |
return float(codehygiene.score(content).score)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import hashlib
|
| 4 |
import importlib.util
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
+
import sys
|
| 7 |
from typing import Any
|
| 8 |
|
| 9 |
+
|
| 10 |
def _find_repo_root() -> Path:
|
| 11 |
current = Path(__file__).resolve().parent
|
| 12 |
for parent in current.parents:
|
|
|
|
| 35 |
|
| 36 |
def score_value(content: str) -> float:
|
| 37 |
return float(codehygiene.score(content).score)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def score_file(content: str) -> tuple[float, dict[str, int]]:
|
| 41 |
+
result = codehygiene.score(content)
|
| 42 |
+
counts: dict[str, int] = {}
|
| 43 |
+
for squiggle in result.squiggles:
|
| 44 |
+
counts[squiggle.kind] = counts.get(squiggle.kind, 0) + 1
|
| 45 |
+
return float(result.score), counts
|
server/leaderboard.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
-
"""Public leaderboard of GitHub repos, each scored on the server. See space/TELEMETRY.md."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import asyncio
|
| 6 |
-
import
|
|
|
|
|
|
|
| 7 |
import io
|
| 8 |
import json
|
| 9 |
import logging
|
| 10 |
import re
|
| 11 |
import tarfile
|
| 12 |
import time
|
| 13 |
-
from collections.abc import Callable, Iterable
|
| 14 |
-
from dataclasses import asdict, dataclass
|
| 15 |
-
from datetime import datetime, timezone
|
| 16 |
from typing import Any
|
| 17 |
from urllib.parse import quote
|
| 18 |
|
|
@@ -20,26 +19,24 @@ import httpx
|
|
| 20 |
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
| 21 |
|
| 22 |
from .config import Settings
|
| 23 |
-
from .hygiene import SCORER_ID, SCORER_VERSION,
|
|
|
|
| 24 |
|
| 25 |
log = logging.getLogger("space.leaderboard")
|
| 26 |
|
| 27 |
API = "https://api.github.com"
|
| 28 |
ARCHIVES = "https://codeload.github.com"
|
| 29 |
BOARD_FILE = "leaderboard.json"
|
| 30 |
-
BOARD_VERSION =
|
| 31 |
BOARD_LIMIT = 100
|
| 32 |
|
| 33 |
MIN_FILES = 5
|
| 34 |
MIN_LINES = 1_000
|
| 35 |
MAX_ARCHIVE_BYTES = 80 * 1024 * 1024
|
| 36 |
MAX_FILE_BYTES = 150_000
|
| 37 |
-
MAX_SCORED_FILES = 300
|
| 38 |
-
MAX_SCORED_LINES = 20_000
|
| 39 |
|
| 40 |
FRESH_SECONDS = 24 * 3600
|
| 41 |
SEED_FRESH_SECONDS = 7 * 24 * 3600
|
| 42 |
-
PAST_SECONDS = 365 * 24 * 3600
|
| 43 |
MAX_WAITING = 3
|
| 44 |
GONE_CODES = frozenset({"not_found", "too_small", "fork", "duplicate"})
|
| 45 |
|
|
@@ -65,7 +62,8 @@ SEED_REPOS = (
|
|
| 65 |
"tinygrad/tinygrad", "pydantic/pydantic", "sindresorhus/ky", "anthropics/claude-code",
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
class RankError(Exception):
|
|
@@ -100,6 +98,7 @@ class Measure:
|
|
| 100 |
files: int
|
| 101 |
lines: int
|
| 102 |
score: float
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
@dataclass(frozen=True)
|
|
@@ -110,24 +109,16 @@ class Entry:
|
|
| 110 |
lines: int
|
| 111 |
sha: str
|
| 112 |
ranked_at: float
|
| 113 |
-
past_score: float | None = None
|
| 114 |
-
past_sha: str = ""
|
| 115 |
scorer: str = ""
|
| 116 |
|
| 117 |
@property
|
| 118 |
def key(self) -> str:
|
| 119 |
return self.repo.lower()
|
| 120 |
|
| 121 |
-
@property
|
| 122 |
-
def change(self) -> float | None:
|
| 123 |
-
return None if self.past_score is None else round(self.score - self.past_score, 1)
|
| 124 |
-
|
| 125 |
def row(self) -> dict[str, Any]:
|
| 126 |
return {
|
| 127 |
"repo": self.repo,
|
| 128 |
"score": self.score,
|
| 129 |
-
"past_score": self.past_score,
|
| 130 |
-
"change": self.change,
|
| 131 |
"files": self.files,
|
| 132 |
"lines": self.lines,
|
| 133 |
"sha": self.sha,
|
|
@@ -167,33 +158,31 @@ def archive_files(archive: bytes) -> tuple[str, list[tuple[str, str]]]:
|
|
| 167 |
continue
|
| 168 |
handle = tar.extractfile(member)
|
| 169 |
text = handle.read().decode("utf-8", errors="replace") if handle else ""
|
| 170 |
-
if text.strip() and "
|
| 171 |
files.append((path, text))
|
| 172 |
return (sha if COMMIT_SHA.match(sha) else ""), files
|
| 173 |
|
| 174 |
|
| 175 |
-
def
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
lines = 0
|
| 180 |
-
for _, text in ordered:
|
| 181 |
-
if len(picked) >= MAX_SCORED_FILES or lines >= MAX_SCORED_LINES:
|
| 182 |
-
break
|
| 183 |
-
picked.append(text)
|
| 184 |
-
lines += line_count(text)
|
| 185 |
-
return picked
|
| 186 |
|
| 187 |
|
| 188 |
def measure(archive: bytes, scorer: Scorer) -> Measure:
|
| 189 |
-
"""Line-weighted mean file score, 0 to 100,
|
| 190 |
sha, files = archive_files(archive)
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
|
| 199 |
def qualifies(files: int, lines: int) -> bool:
|
|
@@ -223,13 +212,14 @@ def entry_from(raw: Any) -> Entry | None:
|
|
| 223 |
)
|
| 224 |
if not shaped:
|
| 225 |
return None
|
| 226 |
-
past_sha = raw.get("past_sha", "")
|
| 227 |
-
past_score = _score(raw.get("past_score"))
|
| 228 |
-
has_past = past_score is not None and isinstance(past_sha, str) and COMMIT_SHA.match(past_sha)
|
| 229 |
scorer = raw.get("scorer")
|
| 230 |
return Entry(
|
| 231 |
-
repo=repo,
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
scorer=scorer[:64] if isinstance(scorer, str) else "",
|
| 234 |
)
|
| 235 |
|
|
@@ -248,12 +238,14 @@ class Leaderboard:
|
|
| 248 |
scorer: Scorer | None = None,
|
| 249 |
clock: Callable[[], float] = time.time,
|
| 250 |
scorer_id: str = SCORER_ID,
|
|
|
|
| 251 |
) -> None:
|
| 252 |
self.entries: dict[str, Entry] = {}
|
| 253 |
self.dirty = False
|
| 254 |
-
self.scorer = scorer or
|
| 255 |
self.scorer_id = scorer_id
|
| 256 |
self.clock = clock
|
|
|
|
| 257 |
self.api_headers = {"Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28"}
|
| 258 |
if settings.github_token:
|
| 259 |
self.api_headers["Authorization"] = f"Bearer {settings.github_token}"
|
|
@@ -290,10 +282,6 @@ class Leaderboard:
|
|
| 290 |
def cleanest(self) -> list[Entry]:
|
| 291 |
return sorted(self.current(), key=lambda entry: (-entry.score, entry.key))
|
| 292 |
|
| 293 |
-
def improved(self) -> list[Entry]:
|
| 294 |
-
rising = [entry for entry in self.current() if (entry.change or 0) >= 1]
|
| 295 |
-
return sorted(rising, key=lambda entry: (-(entry.change or 0), entry.key))
|
| 296 |
-
|
| 297 |
def public(self) -> dict[str, Any]:
|
| 298 |
stamps = [entry.ranked_at for entry in self.current()]
|
| 299 |
return {
|
|
@@ -301,7 +289,6 @@ class Leaderboard:
|
|
| 301 |
"updated_at": iso(max(stamps)) if stamps else None,
|
| 302 |
"scorer_version": SCORER_VERSION,
|
| 303 |
"cleanest": [entry.row() for entry in self.cleanest()[:BOARD_LIMIT]],
|
| 304 |
-
"improved": [entry.row() for entry in self.improved()[:BOARD_LIMIT]],
|
| 305 |
}
|
| 306 |
|
| 307 |
def standing(self, entry: Entry) -> dict[str, Any]:
|
|
@@ -320,11 +307,13 @@ class Leaderboard:
|
|
| 320 |
self._waiting += 1
|
| 321 |
try:
|
| 322 |
async with self._turn:
|
| 323 |
-
entry = await self._build(owner, repo)
|
| 324 |
finally:
|
| 325 |
self._waiting -= 1
|
| 326 |
self.entries[entry.key] = entry
|
| 327 |
self.dirty = True
|
|
|
|
|
|
|
| 328 |
return self.standing(entry)
|
| 329 |
|
| 330 |
async def maintain(self, seeds: Iterable[str], pause: float) -> None:
|
|
@@ -355,7 +344,7 @@ class Leaderboard:
|
|
| 355 |
del self.entries[known.key]
|
| 356 |
self.dirty = True
|
| 357 |
|
| 358 |
-
async def _build(self, owner: str, repo: str) -> Entry:
|
| 359 |
meta = await self._meta(owner, repo)
|
| 360 |
slug = str(meta.get("full_name") or f"{owner}/{repo}")
|
| 361 |
ref = str(meta.get("default_branch") or "HEAD")
|
|
@@ -368,14 +357,16 @@ class Leaderboard:
|
|
| 368 |
twin = self._twin(current.sha, slug)
|
| 369 |
if twin:
|
| 370 |
raise RankError(409, "duplicate", f"Already ranked as {twin.repo}.", repo=twin.repo)
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
| 374 |
ranked_at=self.clock(),
|
| 375 |
-
past_score=past.score if past else None,
|
| 376 |
-
past_sha=past.sha if past else "",
|
| 377 |
scorer=self.scorer_id,
|
| 378 |
)
|
|
|
|
| 379 |
|
| 380 |
def _twin(self, sha: str, slug: str) -> Entry | None:
|
| 381 |
"""An entry for the same commit under another name, which is a fork or mirror."""
|
|
@@ -416,22 +407,3 @@ class Leaderboard:
|
|
| 416 |
raise RankError(413, "too_large", "Repo is too large to rank.")
|
| 417 |
chunks.append(chunk)
|
| 418 |
return b"".join(chunks)
|
| 419 |
-
|
| 420 |
-
async def _past(self, slug: str, ref: str, current_sha: str) -> Measure | None:
|
| 421 |
-
"""The same branch a year earlier, for Most improved. None when it cannot be scored."""
|
| 422 |
-
try:
|
| 423 |
-
return await self._past_measure(slug, ref, current_sha)
|
| 424 |
-
except (RankError, httpx.HTTPError) as error:
|
| 425 |
-
log.info("past score unavailable: %s", type(error).__name__)
|
| 426 |
-
return None
|
| 427 |
-
|
| 428 |
-
async def _past_measure(self, slug: str, ref: str, current_sha: str) -> Measure | None:
|
| 429 |
-
params = {"sha": ref, "until": iso(self.clock() - PAST_SECONDS), "per_page": "1"}
|
| 430 |
-
url = f"{API}/repos/{slug}/commits"
|
| 431 |
-
response = await self.client.get(url, params=params, headers=self.api_headers)
|
| 432 |
-
rows = response.json() if response.status_code == 200 else []
|
| 433 |
-
sha = rows[0].get("sha", "") if rows and isinstance(rows[0], dict) else ""
|
| 434 |
-
if not COMMIT_SHA.match(sha) or sha == current_sha:
|
| 435 |
-
return None
|
| 436 |
-
past = await self._measure(slug, sha)
|
| 437 |
-
return past if qualifies(past.files, past.lines) else None
|
|
|
|
| 1 |
+
"""Public leaderboard of GitHub repos, each scored in full on the server. See space/TELEMETRY.md."""
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import asyncio
|
| 6 |
+
from collections.abc import Callable, Iterable
|
| 7 |
+
from dataclasses import asdict, dataclass
|
| 8 |
+
from datetime import datetime, timezone
|
| 9 |
import io
|
| 10 |
import json
|
| 11 |
import logging
|
| 12 |
import re
|
| 13 |
import tarfile
|
| 14 |
import time
|
|
|
|
|
|
|
|
|
|
| 15 |
from typing import Any
|
| 16 |
from urllib.parse import quote
|
| 17 |
|
|
|
|
| 19 |
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
| 20 |
|
| 21 |
from .config import Settings
|
| 22 |
+
from .hygiene import SCORER_ID, SCORER_VERSION, score_file
|
| 23 |
+
from .telemetry import ScanFile, Telemetry, scan_file_from
|
| 24 |
|
| 25 |
log = logging.getLogger("space.leaderboard")
|
| 26 |
|
| 27 |
API = "https://api.github.com"
|
| 28 |
ARCHIVES = "https://codeload.github.com"
|
| 29 |
BOARD_FILE = "leaderboard.json"
|
| 30 |
+
BOARD_VERSION = 2
|
| 31 |
BOARD_LIMIT = 100
|
| 32 |
|
| 33 |
MIN_FILES = 5
|
| 34 |
MIN_LINES = 1_000
|
| 35 |
MAX_ARCHIVE_BYTES = 80 * 1024 * 1024
|
| 36 |
MAX_FILE_BYTES = 150_000
|
|
|
|
|
|
|
| 37 |
|
| 38 |
FRESH_SECONDS = 24 * 3600
|
| 39 |
SEED_FRESH_SECONDS = 7 * 24 * 3600
|
|
|
|
| 40 |
MAX_WAITING = 3
|
| 41 |
GONE_CODES = frozenset({"not_found", "too_small", "fork", "duplicate"})
|
| 42 |
|
|
|
|
| 62 |
"tinygrad/tinygrad", "pydantic/pydantic", "sindresorhus/ky", "anthropics/claude-code",
|
| 63 |
)
|
| 64 |
|
| 65 |
+
ScoreVerdict = float | tuple[float, dict[str, int]]
|
| 66 |
+
Scorer = Callable[[str], ScoreVerdict]
|
| 67 |
|
| 68 |
|
| 69 |
class RankError(Exception):
|
|
|
|
| 98 |
files: int
|
| 99 |
lines: int
|
| 100 |
score: float
|
| 101 |
+
scans: tuple[ScanFile, ...] = ()
|
| 102 |
|
| 103 |
|
| 104 |
@dataclass(frozen=True)
|
|
|
|
| 109 |
lines: int
|
| 110 |
sha: str
|
| 111 |
ranked_at: float
|
|
|
|
|
|
|
| 112 |
scorer: str = ""
|
| 113 |
|
| 114 |
@property
|
| 115 |
def key(self) -> str:
|
| 116 |
return self.repo.lower()
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
def row(self) -> dict[str, Any]:
|
| 119 |
return {
|
| 120 |
"repo": self.repo,
|
| 121 |
"score": self.score,
|
|
|
|
|
|
|
| 122 |
"files": self.files,
|
| 123 |
"lines": self.lines,
|
| 124 |
"sha": self.sha,
|
|
|
|
| 158 |
continue
|
| 159 |
handle = tar.extractfile(member)
|
| 160 |
text = handle.read().decode("utf-8", errors="replace") if handle else ""
|
| 161 |
+
if text.strip() and "\ufffd" not in text and not generated(text):
|
| 162 |
files.append((path, text))
|
| 163 |
return (sha if COMMIT_SHA.match(sha) else ""), files
|
| 164 |
|
| 165 |
|
| 166 |
+
def _unpack_score(verdict: ScoreVerdict) -> tuple[float, dict[str, int]]:
|
| 167 |
+
if isinstance(verdict, tuple):
|
| 168 |
+
return verdict
|
| 169 |
+
return float(verdict), {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
def measure(archive: bytes, scorer: Scorer) -> Measure:
|
| 173 |
+
"""Line-weighted mean file score, 0 to 100, across every ranked file in the archive."""
|
| 174 |
sha, files = archive_files(archive)
|
| 175 |
+
total_lines = 0
|
| 176 |
+
weighted = 0.0
|
| 177 |
+
scans: list[ScanFile] = []
|
| 178 |
+
for path, text in files:
|
| 179 |
+
lines = line_count(text)
|
| 180 |
+
file_score, squiggles = _unpack_score(scorer(text))
|
| 181 |
+
total_lines += lines
|
| 182 |
+
weighted += file_score * lines
|
| 183 |
+
scans.append(scan_file_from(path, text, file_score, squiggles))
|
| 184 |
+
score = round(100 * weighted / total_lines, 1) if total_lines else 0.0
|
| 185 |
+
return Measure(sha=sha, files=len(files), lines=total_lines, score=score, scans=tuple(scans))
|
| 186 |
|
| 187 |
|
| 188 |
def qualifies(files: int, lines: int) -> bool:
|
|
|
|
| 212 |
)
|
| 213 |
if not shaped:
|
| 214 |
return None
|
|
|
|
|
|
|
|
|
|
| 215 |
scorer = raw.get("scorer")
|
| 216 |
return Entry(
|
| 217 |
+
repo=repo,
|
| 218 |
+
score=score,
|
| 219 |
+
files=files,
|
| 220 |
+
lines=lines,
|
| 221 |
+
sha=sha,
|
| 222 |
+
ranked_at=float(stamp),
|
| 223 |
scorer=scorer[:64] if isinstance(scorer, str) else "",
|
| 224 |
)
|
| 225 |
|
|
|
|
| 238 |
scorer: Scorer | None = None,
|
| 239 |
clock: Callable[[], float] = time.time,
|
| 240 |
scorer_id: str = SCORER_ID,
|
| 241 |
+
telemetry: Telemetry | None = None,
|
| 242 |
) -> None:
|
| 243 |
self.entries: dict[str, Entry] = {}
|
| 244 |
self.dirty = False
|
| 245 |
+
self.scorer: Scorer = scorer or score_file
|
| 246 |
self.scorer_id = scorer_id
|
| 247 |
self.clock = clock
|
| 248 |
+
self.telemetry = telemetry
|
| 249 |
self.api_headers = {"Accept": "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28"}
|
| 250 |
if settings.github_token:
|
| 251 |
self.api_headers["Authorization"] = f"Bearer {settings.github_token}"
|
|
|
|
| 282 |
def cleanest(self) -> list[Entry]:
|
| 283 |
return sorted(self.current(), key=lambda entry: (-entry.score, entry.key))
|
| 284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
def public(self) -> dict[str, Any]:
|
| 286 |
stamps = [entry.ranked_at for entry in self.current()]
|
| 287 |
return {
|
|
|
|
| 289 |
"updated_at": iso(max(stamps)) if stamps else None,
|
| 290 |
"scorer_version": SCORER_VERSION,
|
| 291 |
"cleanest": [entry.row() for entry in self.cleanest()[:BOARD_LIMIT]],
|
|
|
|
| 292 |
}
|
| 293 |
|
| 294 |
def standing(self, entry: Entry) -> dict[str, Any]:
|
|
|
|
| 307 |
self._waiting += 1
|
| 308 |
try:
|
| 309 |
async with self._turn:
|
| 310 |
+
entry, scans = await self._build(owner, repo)
|
| 311 |
finally:
|
| 312 |
self._waiting -= 1
|
| 313 |
self.entries[entry.key] = entry
|
| 314 |
self.dirty = True
|
| 315 |
+
if self.telemetry is not None and scans:
|
| 316 |
+
self.telemetry.record_files(scans)
|
| 317 |
return self.standing(entry)
|
| 318 |
|
| 319 |
async def maintain(self, seeds: Iterable[str], pause: float) -> None:
|
|
|
|
| 344 |
del self.entries[known.key]
|
| 345 |
self.dirty = True
|
| 346 |
|
| 347 |
+
async def _build(self, owner: str, repo: str) -> tuple[Entry, tuple[ScanFile, ...]]:
|
| 348 |
meta = await self._meta(owner, repo)
|
| 349 |
slug = str(meta.get("full_name") or f"{owner}/{repo}")
|
| 350 |
ref = str(meta.get("default_branch") or "HEAD")
|
|
|
|
| 357 |
twin = self._twin(current.sha, slug)
|
| 358 |
if twin:
|
| 359 |
raise RankError(409, "duplicate", f"Already ranked as {twin.repo}.", repo=twin.repo)
|
| 360 |
+
entry = Entry(
|
| 361 |
+
repo=slug,
|
| 362 |
+
score=current.score,
|
| 363 |
+
files=current.files,
|
| 364 |
+
lines=current.lines,
|
| 365 |
+
sha=current.sha,
|
| 366 |
ranked_at=self.clock(),
|
|
|
|
|
|
|
| 367 |
scorer=self.scorer_id,
|
| 368 |
)
|
| 369 |
+
return entry, current.scans
|
| 370 |
|
| 371 |
def _twin(self, sha: str, slug: str) -> Entry | None:
|
| 372 |
"""An entry for the same commit under another name, which is a fork or mirror."""
|
|
|
|
| 407 |
raise RankError(413, "too_large", "Repo is too large to rank.")
|
| 408 |
chunks.append(chunk)
|
| 409 |
return b"".join(chunks)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server/telemetry.py
CHANGED
|
@@ -120,6 +120,44 @@ def content_digest(ext: str, content: str) -> str:
|
|
| 120 |
return hashlib.sha256(payload).hexdigest()[:DIGEST_HEX]
|
| 121 |
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
class DigestSet:
|
| 124 |
"""Rolling set of the most recent 64-bit digests, oldest dropped first."""
|
| 125 |
|
|
@@ -222,11 +260,9 @@ class Telemetry:
|
|
| 222 |
for key, value in loaded[name].items():
|
| 223 |
_bump(self.stats[name], key, value)
|
| 224 |
|
| 225 |
-
def
|
| 226 |
-
if body.source_kind == "examples":
|
| 227 |
-
return 0
|
| 228 |
counted = 0
|
| 229 |
-
for item in
|
| 230 |
if item.digest16 in self.excluded or not self.seen_scored.add(item.digest16):
|
| 231 |
continue
|
| 232 |
counted += 1
|
|
@@ -245,6 +281,11 @@ class Telemetry:
|
|
| 245 |
self.dirty = True
|
| 246 |
return counted
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
def snapshot(self) -> str:
|
| 249 |
stats = {**self.stats}
|
| 250 |
stats["updated_at"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
| 120 |
return hashlib.sha256(payload).hexdigest()[:DIGEST_HEX]
|
| 121 |
|
| 122 |
|
| 123 |
+
def loc_bucket_of(content: str) -> Literal["0-50", "50-100", "100-300", "300-1000", "1000+"]:
|
| 124 |
+
lines = content.count("\n") + 1
|
| 125 |
+
if lines < 50:
|
| 126 |
+
return "0-50"
|
| 127 |
+
if lines < 100:
|
| 128 |
+
return "50-100"
|
| 129 |
+
if lines < 300:
|
| 130 |
+
return "100-300"
|
| 131 |
+
return "300-1000" if lines < 1000 else "1000+"
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def scan_file_from(
|
| 135 |
+
filename: str,
|
| 136 |
+
content: str,
|
| 137 |
+
score: float,
|
| 138 |
+
squiggles: Mapping[str, int],
|
| 139 |
+
score_ms: int | None = None,
|
| 140 |
+
) -> ScanFile:
|
| 141 |
+
raw_ext = extension_of(filename)
|
| 142 |
+
ext = raw_ext if len(raw_ext) <= 8 else ""
|
| 143 |
+
points = min(100, max(0, round(score * 100)))
|
| 144 |
+
allowed = TALLY_KEYS["squiggles_by_kind"]
|
| 145 |
+
clean_squiggles = {
|
| 146 |
+
kind: min(10_000, count)
|
| 147 |
+
for kind, count in squiggles.items()
|
| 148 |
+
if kind in allowed and count > 0
|
| 149 |
+
}
|
| 150 |
+
clamped_ms = min(60_000, max(0, score_ms)) if score_ms is not None else None
|
| 151 |
+
return ScanFile.model_construct(
|
| 152 |
+
digest16=content_digest(ext, content),
|
| 153 |
+
ext=ext,
|
| 154 |
+
loc_bucket=loc_bucket_of(content),
|
| 155 |
+
score=points,
|
| 156 |
+
squiggles=clean_squiggles,
|
| 157 |
+
score_ms=clamped_ms,
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
class DigestSet:
|
| 162 |
"""Rolling set of the most recent 64-bit digests, oldest dropped first."""
|
| 163 |
|
|
|
|
| 260 |
for key, value in loaded[name].items():
|
| 261 |
_bump(self.stats[name], key, value)
|
| 262 |
|
| 263 |
+
def record_files(self, files: Iterable[ScanFile]) -> int:
|
|
|
|
|
|
|
| 264 |
counted = 0
|
| 265 |
+
for item in files:
|
| 266 |
if item.digest16 in self.excluded or not self.seen_scored.add(item.digest16):
|
| 267 |
continue
|
| 268 |
counted += 1
|
|
|
|
| 281 |
self.dirty = True
|
| 282 |
return counted
|
| 283 |
|
| 284 |
+
def record_scan(self, body: ScanBody) -> int:
|
| 285 |
+
if body.source_kind == "examples":
|
| 286 |
+
return 0
|
| 287 |
+
return self.record_files(body.files)
|
| 288 |
+
|
| 289 |
def snapshot(self) -> str:
|
| 290 |
stats = {**self.stats}
|
| 291 |
stats["updated_at"] = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
web/src/board.css
CHANGED
|
@@ -81,29 +81,6 @@
|
|
| 81 |
padding-top: 8px;
|
| 82 |
}
|
| 83 |
|
| 84 |
-
.board-modes {
|
| 85 |
-
display: flex;
|
| 86 |
-
gap: 20px;
|
| 87 |
-
margin-bottom: 12px;
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
.board-mode {
|
| 91 |
-
height: 28px;
|
| 92 |
-
border-radius: var(--radius-sm);
|
| 93 |
-
color: var(--ink-3);
|
| 94 |
-
font-size: 14px;
|
| 95 |
-
transition: color var(--motion) var(--ease);
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
.board-mode:hover,
|
| 99 |
-
.board-mode[aria-pressed="true"] {
|
| 100 |
-
color: var(--ink);
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
.board-mode[aria-pressed="true"] {
|
| 104 |
-
font-weight: 500;
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
.board-list {
|
| 108 |
margin: 0;
|
| 109 |
padding: 0;
|
|
@@ -174,22 +151,12 @@
|
|
| 174 |
text-underline-offset: 4px;
|
| 175 |
}
|
| 176 |
|
| 177 |
-
.board-past {
|
| 178 |
-
color: var(--ink-3);
|
| 179 |
-
font-variant-numeric: tabular-nums;
|
| 180 |
-
text-align: right;
|
| 181 |
-
}
|
| 182 |
-
|
| 183 |
.board-score {
|
| 184 |
font-weight: 500;
|
| 185 |
font-variant-numeric: tabular-nums;
|
| 186 |
text-align: right;
|
| 187 |
}
|
| 188 |
|
| 189 |
-
.board-score.is-up {
|
| 190 |
-
color: var(--green);
|
| 191 |
-
}
|
| 192 |
-
|
| 193 |
.board-empty {
|
| 194 |
padding: 48px 0;
|
| 195 |
color: var(--ink-3);
|
|
@@ -374,8 +341,7 @@
|
|
| 374 |
padding: 0 14px;
|
| 375 |
}
|
| 376 |
|
| 377 |
-
.board-row .track
|
| 378 |
-
.board-row .board-past {
|
| 379 |
display: none;
|
| 380 |
}
|
| 381 |
|
|
|
|
| 81 |
padding-top: 8px;
|
| 82 |
}
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
.board-list {
|
| 85 |
margin: 0;
|
| 86 |
padding: 0;
|
|
|
|
| 151 |
text-underline-offset: 4px;
|
| 152 |
}
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
.board-score {
|
| 155 |
font-weight: 500;
|
| 156 |
font-variant-numeric: tabular-nums;
|
| 157 |
text-align: right;
|
| 158 |
}
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
.board-empty {
|
| 161 |
padding: 48px 0;
|
| 162 |
color: var(--ink-3);
|
|
|
|
| 341 |
padding: 0 14px;
|
| 342 |
}
|
| 343 |
|
| 344 |
+
.board-row .track {
|
|
|
|
| 345 |
display: none;
|
| 346 |
}
|
| 347 |
|
web/src/board.html
CHANGED
|
@@ -31,10 +31,6 @@
|
|
| 31 |
|
| 32 |
<main>
|
| 33 |
<section class="board-view" id="view-board" aria-labelledby="tab-board">
|
| 34 |
-
<div class="board-modes" role="group" aria-label="Order">
|
| 35 |
-
<button class="board-mode" type="button" data-mode="cleanest" aria-pressed="true">Cleanest</button>
|
| 36 |
-
<button class="board-mode" type="button" data-mode="improved" aria-pressed="false" title="Score a year ago → now">Most improved</button>
|
| 37 |
-
</div>
|
| 38 |
<ol class="board-list" id="board-list"></ol>
|
| 39 |
<p class="board-empty" id="board-empty" hidden>No repos yet</p>
|
| 40 |
</section>
|
|
|
|
| 31 |
|
| 32 |
<main>
|
| 33 |
<section class="board-view" id="view-board" aria-labelledby="tab-board">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
<ol class="board-list" id="board-list"></ol>
|
| 35 |
<p class="board-empty" id="board-empty" hidden>No repos yet</p>
|
| 36 |
</section>
|
web/src/board.js
CHANGED
|
@@ -18,7 +18,7 @@ const DAY = 24 * HOUR;
|
|
| 18 |
// benchmark table at each release. The strip stays hidden when the running scorer has no entry.
|
| 19 |
const README_AT = "https://github.com/brianbell-x/codehygiene/blob";
|
| 20 |
|
| 21 |
-
const state = {
|
| 22 |
|
| 23 |
function icon(name) {
|
| 24 |
const svg = document.createElementNS(SVG_NS, "svg");
|
|
@@ -70,28 +70,20 @@ function repoCell(repo) {
|
|
| 70 |
}
|
| 71 |
|
| 72 |
function boardRow(row, index) {
|
| 73 |
-
const improved = state.mode === "improved";
|
| 74 |
const yours = state.yours && row.repo.toLowerCase() === state.yours;
|
| 75 |
-
const middle = improved
|
| 76 |
-
? el("span", { class: "board-past", text: `${points(row.past_score)} → ${points(row.score)}` })
|
| 77 |
-
: track(row.score);
|
| 78 |
-
const value = improved ? `+${points(row.change)}` : points(row.score);
|
| 79 |
return el("li", { class: yours ? "board-row is-yours" : "board-row" }, [
|
| 80 |
el("span", { class: "board-rank", text: String(index + 1) }),
|
| 81 |
repoCell(row.repo),
|
| 82 |
-
|
| 83 |
-
el("span", { class:
|
| 84 |
]);
|
| 85 |
}
|
| 86 |
|
| 87 |
function renderBoard() {
|
| 88 |
-
const rows = state.board?.
|
| 89 |
byId("board-list").replaceChildren(...rows.map(boardRow));
|
| 90 |
byId("board-list").hidden = rows.length === 0;
|
| 91 |
byId("board-empty").hidden = rows.length > 0 || !state.board;
|
| 92 |
-
for (const button of document.querySelectorAll(".board-mode")) {
|
| 93 |
-
button.setAttribute("aria-pressed", String(button.dataset.mode === state.mode));
|
| 94 |
-
}
|
| 95 |
byId("board-list").querySelector(".is-yours")?.scrollIntoView({ block: "center" });
|
| 96 |
}
|
| 97 |
|
|
@@ -228,12 +220,6 @@ function readYours() {
|
|
| 228 |
|
| 229 |
function boot() {
|
| 230 |
state.yours = readYours();
|
| 231 |
-
for (const button of document.querySelectorAll(".board-mode")) {
|
| 232 |
-
button.addEventListener("click", () => {
|
| 233 |
-
state.mode = button.dataset.mode;
|
| 234 |
-
renderBoard();
|
| 235 |
-
});
|
| 236 |
-
}
|
| 237 |
window.addEventListener("hashchange", showView);
|
| 238 |
showView();
|
| 239 |
fetchLeaderboard().then((board) => {
|
|
@@ -241,7 +227,7 @@ function boot() {
|
|
| 241 |
renderBoard();
|
| 242 |
renderUpdated();
|
| 243 |
}, () => {
|
| 244 |
-
state.board = { cleanest: []
|
| 245 |
renderBoard();
|
| 246 |
});
|
| 247 |
fetchStats().then((stats) => {
|
|
|
|
| 18 |
// benchmark table at each release. The strip stays hidden when the running scorer has no entry.
|
| 19 |
const README_AT = "https://github.com/brianbell-x/codehygiene/blob";
|
| 20 |
|
| 21 |
+
const state = { board: null, stats: null, yours: null };
|
| 22 |
|
| 23 |
function icon(name) {
|
| 24 |
const svg = document.createElementNS(SVG_NS, "svg");
|
|
|
|
| 70 |
}
|
| 71 |
|
| 72 |
function boardRow(row, index) {
|
|
|
|
| 73 |
const yours = state.yours && row.repo.toLowerCase() === state.yours;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
return el("li", { class: yours ? "board-row is-yours" : "board-row" }, [
|
| 75 |
el("span", { class: "board-rank", text: String(index + 1) }),
|
| 76 |
repoCell(row.repo),
|
| 77 |
+
track(row.score),
|
| 78 |
+
el("span", { class: "board-score", text: points(row.score) }),
|
| 79 |
]);
|
| 80 |
}
|
| 81 |
|
| 82 |
function renderBoard() {
|
| 83 |
+
const rows = state.board?.cleanest ?? [];
|
| 84 |
byId("board-list").replaceChildren(...rows.map(boardRow));
|
| 85 |
byId("board-list").hidden = rows.length === 0;
|
| 86 |
byId("board-empty").hidden = rows.length > 0 || !state.board;
|
|
|
|
|
|
|
|
|
|
| 87 |
byId("board-list").querySelector(".is-yours")?.scrollIntoView({ block: "center" });
|
| 88 |
}
|
| 89 |
|
|
|
|
| 220 |
|
| 221 |
function boot() {
|
| 222 |
state.yours = readYours();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
window.addEventListener("hashchange", showView);
|
| 224 |
showView();
|
| 225 |
fetchLeaderboard().then((board) => {
|
|
|
|
| 227 |
renderBoard();
|
| 228 |
renderUpdated();
|
| 229 |
}, () => {
|
| 230 |
+
state.board = { cleanest: [] };
|
| 231 |
renderBoard();
|
| 232 |
});
|
| 233 |
fetchStats().then((stats) => {
|