Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions scripts/devstream/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def read_json(gh, path):
return json.loads(base64.b64decode(cur["content"])), cur["sha"]


def list_dir(gh, path) -> list[str]:
"""File names in a stats-branch directory; a missing directory is empty."""
got = gh.get(f"{API}/contents/{path}", {"ref": "stats"})
if got is None:
return []
if not isinstance(got, list):
raise GitHubError(200, f"{path} is not a directory listing")
return [f["name"] for f in got if f.get("type") == "file"]


def write_json(gh, path, data, sha, message) -> None:
body = {
"message": message,
Expand Down
64 changes: 39 additions & 25 deletions scripts/sync_likes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
import json
import logging
import os
import re
import tempfile
from datetime import datetime, timedelta, timezone

from scripts.devstream import entries as entries_mod
from scripts.devstream.entries import normalize_repo
from scripts.devstream.gh import GitHub, GitHubError
from scripts.devstream.likes import (
YOUNG_DAYS,
count_likes,
detect_burst,
detect_drip,
fetch_meta,
fetch_reactors,
rank_entries,
young_count,
)
from scripts.devstream.stats import API, ensure_branch, read_json, write_json
from scripts.devstream.stats import API, ensure_branch, list_dir, read_json, write_json
from scripts.devstream.trust import TrustCache, ensure_block_access

# Flagged entries stay listed and ranked; only Trending hides them.
Expand Down Expand Up @@ -91,37 +90,51 @@ def _record(kind: str, issue: int, now: datetime, aggregate: bool = False) -> di
return rec


def _body(kind: str, repo: str, total: int, young: int) -> str:
# Fixed text and counts only: naming reactors would ping them.
def _body(kind: str, repo: str) -> str:
# Fixed text only: naming reactors would ping them, and numbers or windows would show how to tune around the checks.
if kind == "burst":
return (
f"{repo} picked up {total} Likes in the last 24 hours, and {young} of them came from "
f"accounts under {YOUNG_DAYS} days old. That's what sock-puppet voting usually looks like, "
"so we've flagged the entry and opened this for a maintainer to check.\n\n"
"The flag stays on while this issue is open, so close it once you've checked. "
"The entry stays listed either way."
"Likes on this entry jumped quickly, and an unusual share came from newer accounts. "
"It's held out of Trending until a maintainer closes this issue."
)
return (
f"{repo} picked up {total} Likes over the last 7 days, and {young} of them came from "
f"accounts under {YOUNG_DAYS} days old. That's slower than a burst, but it's the same "
"pattern spread thin, so it's worth a maintainer's look.\n\n"
"Nothing is flagged for this one. Close the issue once it's been checked."
"A steady stream of Likes on this entry came mostly from newer accounts. "
"Nothing is hidden; this is for a maintainer to look at."
)


def _aggregate(listed: list[tuple[str, str]]) -> dict:
n = len(listed)
lines = "\n".join(f"- {key} ({kind})" for key, kind in sorted(listed))
lines = "\n".join(f"- {key}" for key, _ in sorted(listed))
return {
"title": f"Like burst: {n} {'entry' if n == 1 else 'entries'}",
"body": (
"More entries showed a suspicious Like pattern than we open separate issues for, "
f"so they share this one:\n\n{lines}\n\n"
"Bursts stay flagged while this is open. Close it once they've all been checked."
),
"body": f"Too many entries tripped the Like checks for one issue each, so they share this one:\n\n{lines}",
}


def _snapshot_date(name: str):
m = re.fullmatch(r"([0-9]{4}-[0-9]{2}-[0-9]{2})\.json", name)
try:
return datetime.strptime(m.group(1), "%Y-%m-%d").date() if m else None
except ValueError:
return None


def _baseline(gh, now: datetime) -> dict | None:
"""The snapshot from 7 days ago, else the oldest one between 1 and 7 days old, else None."""
week, day = (now - WEEK).date(), (now - DAY).date()
dates = sorted(d for n in list_dir(gh, "snapshots") if (d := _snapshot_date(n)))
old = [d for d in dates if week < d <= day]
pick = week if week in dates else (old[0] if old else None)
if pick is None:
return None
data = read_json(gh, f"snapshots/{pick:%Y-%m-%d}.json")[0]
if data is None:
# Listed but unreadable: fail closed rather than read it as an empty baseline.
raise GitHubError(404, f"snapshot {pick} is listed but could not be read")
return data


def _report(gh, todo: list[tuple[str, str, str, str]], flags: dict, now: datetime) -> None:
"""todo is (kind, key, repo, body). Records are set right after each post, so a failure keeps them."""
for kind, key, repo, body in todo[:ISSUE_CAP]:
Expand Down Expand Up @@ -200,19 +213,20 @@ def trusted(login):
if detect_burst(today, ages):
bursts.add(k)
if _due(flags, k, "burst", now):
candidates.append(("burst", k, repo, _body("burst", repo, len(today), young_count(today, ages))))
candidates.append(("burst", k, repo, _body("burst", repo)))
elif detect_drip(week, ages) and _due(flags, k, "drip", now):
candidates.append(("drip", k, repo, _body("drip", repo, len(week), young_count(week, ages))))
candidates.append(("drip", k, repo, _body("drip", repo)))
candidates.sort(key=lambda c: c[0] != "burst")

ranks = rank_entries(likes, {k: m["stars"] for k, m in meta.items()})
old = read_json(gh, f"snapshots/{now - WEEK:%Y-%m-%d}.json")[0] or {}
old = _baseline(gh, now)
out = {}
for k in keys:
o = old.get(k)
o = (old or {}).get(k)
base = (o["likes"] if o else 0) if old is not None else likes[k]
out[k] = {
"likes": likes[k],
"delta_7d": likes[k] - o["likes"] if o else 0,
"delta_7d": likes[k] - base,
"rank": ranks[k],
"rank_delta_7d": o["rank"] - ranks[k] if o else 0,
"flagged": ranked[k]["status"] == "flagged" or k in bursts or _under_review(flags, k),
Expand Down
74 changes: 73 additions & 1 deletion scripts/tests/test_likes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def __init__(self, entries, discussions, users=None, stats=None, stars=None, iss
for path, data in self.stats.items():
routes[f"GET {API}/contents/{path}"] = {
"content": base64.b64encode(json.dumps(data).encode()).decode(), "sha": f"sha-{path}"}
snaps = sorted(p for p in self.stats if p.startswith("snapshots/"))
if snaps:
routes[f"GET {API}/contents/snapshots"] = [{"name": p.split("/")[1], "type": "file"} for p in snaps]
days = [f"snapshots/{NOW + timedelta(days=d):%Y-%m-%d}.json" for d in range(12)]
for path in ("meta.json", "likes.json", "flags.json", "trust-cache.json", *days):
routes[f"PUT {API}/contents/{path}"] = {}
Expand Down Expand Up @@ -513,7 +516,20 @@ def test_burst_report_has_no_logins(self):
self.assertEqual(sorted(issue["labels"]), ["needs-review", "report"])
self.assertNotIn("zqx", issue["body"])
self.assertNotIn("@", issue["body"])
self.assertIn("12", issue["body"])
for word in ("24", "hour", "7 day", "90", "days old"):
self.assertNotIn(word, issue["body"])

def test_issue_bodies_have_no_windows(self):
w = World([entry("o/a")], {"o/a": burst_nodes(10, 10)})
w.run()
[burst] = w.issues_opened()
self.assertIn("jumped quickly", burst["body"])
repos = [f"o/r{i}" for i in range(7)]
w2 = World([entry(r) for r in repos], {r: burst_nodes(10, 10, prefix=f"p{i}") for i, r in enumerate(repos)})
w2.run()
for issue in w2.issues_opened():
for word in ("24", "hour", "7 day", "90", "days old"):
self.assertNotIn(word, issue["body"])

def test_burst_report_deduped(self):
w = World([entry("o/a")], {"o/a": burst_nodes(10, 10)}, stats=self.flags(), issues=[{"number": 7}])
Expand Down Expand Up @@ -560,6 +576,62 @@ def test_delta_from_snapshot_7_days_back(self):
self.assertEqual((likes["o/b"]["delta_7d"], likes["o/b"]["rank_delta_7d"]), (-3, -1))
self.assertEqual((likes["o/c"]["delta_7d"], likes["o/c"]["rank_delta_7d"]), (0, 0))

def snap(self, days_ago, data):
return {f"snapshots/{NOW - timedelta(days=days_ago):%Y-%m-%d}.json": data}

def likes_for(self, stats):
w = World([entry("o/a"), entry("o/new")],
{"o/a": [node("x"), node("y"), node("z")], "o/new": [node("x"), node("y")]}, stats=stats)
w.run()
return w.written("likes.json")

def test_oldest_snapshot_used_when_no_7_day(self):
stats = {**self.snap(3, {"o/a": {"likes": 2, "rank": 2}}), **self.snap(1, {"o/a": {"likes": 3, "rank": 1}})}
a = self.likes_for(stats)["o/a"]
self.assertEqual((a["delta_7d"], a["rank_delta_7d"]), (1, 1))

def test_baseline_never_older_than_7_days(self):
stats = {**self.snap(10, {"o/a": {"likes": 0, "rank": 1}}), **self.snap(4, {"o/a": {"likes": 2, "rank": 3}})}
a = self.likes_for(stats)["o/a"]
self.assertEqual((a["delta_7d"], a["rank_delta_7d"]), (1, 2))

def test_impossible_and_non_ascii_dates_skipped(self):
stats = {**self.snap(3, {"o/a": {"likes": 2, "rank": 1}}),
"snapshots/2026-10-35.json": {"o/a": {"likes": 0, "rank": 1}},
"snapshots/2026-10-٣٠.json": {"o/a": {"likes": 0, "rank": 1}},
"snapshots/2026-10-31.json.bak": {"o/a": {"likes": 0, "rank": 1}}}
self.assertEqual(self.likes_for(stats)["o/a"]["delta_7d"], 1)

def test_unreadable_baseline_raises_without_writes(self):
w = World([entry("o/a")], {"o/a": [node("x")]}, stats=self.snap(3, {}))
del w.routes[f"GET {API}/contents/snapshots/{NOW - timedelta(days=3):%Y-%m-%d}.json"]
with self.assertRaises(GitHubError):
w.run()
self.assertEqual(w.puts(), [])

def test_empty_baseline_file_counts_from_zero(self):
got = self.likes_for(self.snap(3, {}))
self.assertEqual(got["o/a"]["delta_7d"], 3)

def test_only_today_snapshot_gives_zero(self):
got = self.likes_for(self.snap(0, {"o/a": {"likes": 0, "rank": 1}}))
self.assertEqual((got["o/a"]["delta_7d"], got["o/a"]["rank_delta_7d"]), (0, 0))

def test_new_entry_delta_is_its_likes(self):
got = self.likes_for(self.snap(3, {"o/a": {"likes": 1, "rank": 1}}))
self.assertEqual((got["o/new"]["delta_7d"], got["o/new"]["rank_delta_7d"]), (2, 0))

def test_exact_7_day_snapshot_preferred(self):
stats = {**self.snap(7, {"o/a": {"likes": 2, "rank": 1}}), **self.snap(3, {"o/a": {"likes": 3, "rank": 1}})}
self.assertEqual(self.likes_for(stats)["o/a"]["delta_7d"], 1)

def test_listing_error_raises_without_writes(self):
w = World([entry("o/a")], {"o/a": [node("x")]})
w.routes[f"GET {API}/contents/snapshots"] = Status(500)
with self.assertRaises(GitHubError):
w.run()
self.assertEqual(w.puts(), [])

def test_missing_snapshot_delta_zero(self):
w = World([entry("o/a")], {"o/a": [node("x"), node("y")]})
w.run()
Expand Down
21 changes: 21 additions & 0 deletions scripts/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest

from scripts.devstream.gh import GitHubError
from scripts.devstream.stats import API, list_dir
from scripts.tests.fake_gh import FakeGitHub


class ListDir(unittest.TestCase):
def test_keeps_files_only(self):
gh = FakeGitHub({f"GET {API}/contents/d": [
{"name": "a.json", "type": "file"}, {"name": "sub", "type": "dir"}, {"name": "l", "type": "symlink"}]})
self.assertEqual(list_dir(gh, "d"), ["a.json"])

def test_missing_directory_is_empty(self):
self.assertEqual(list_dir(FakeGitHub({}), "d"), [])

def test_non_list_raises(self):
# A path that is a file comes back as an object, not a listing.
gh = FakeGitHub({f"GET {API}/contents/d": {"name": "d", "type": "file", "content": ""}})
with self.assertRaises(GitHubError):
list_dir(gh, "d")
14 changes: 12 additions & 2 deletions site/src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,19 @@ export const liveList = (all: SiteEntry[]) =>
.sort(byTop)
.map((e, i) => ({ ...e, rank: i + 1 }));

export const trending = (all: SiteEntry[]) =>
// Trending needs a real climb, and a listing old enough (or curated) that a fresh account can't launch itself onto it.
export const TRENDING_MIN_DELTA = 5;
const TRENDING_MIN_AGE_MS = 7 * 24 * 60 * 60 * 1000;

export const trending = (all: SiteEntry[], now = Date.now()) =>
liveList(all)
.filter((e) => !isFlagged(e) && e.delta_7d > 0)
.filter(
(e) =>
!isFlagged(e) &&
e.delta_7d >= TRENDING_MIN_DELTA &&
(e.added_by_seed ||
now - Date.parse(e.submitted_at) >= TRENDING_MIN_AGE_MS),
)
.sort((a, b) => b.delta_7d - a.delta_7d);

export const newest = (all: SiteEntry[]) =>
Expand Down
37 changes: 37 additions & 0 deletions site/tests/data.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,40 @@ test("a Discussion link needs a positive integer", () => {
null,
);
});

const mk = (repo, o) => ({
repo,
status: "live",
flagged: false,
added_by_seed: false,
submitted_at: "2026-09-01T00:00:00Z",
likes: 1,
stars: 0,
delta_7d: 0,
...o,
});
const NOW = Date.parse("2026-11-05T12:00:00Z");

test("trending needs delta >= 5 and age or seed, and no flag", () => {
const list = [
mk("new/community", { delta_7d: 20, submitted_at: "2026-11-03T00:00:00Z" }),
mk("new/seed", {
delta_7d: 6,
submitted_at: "2026-11-03T00:00:00Z",
added_by_seed: true,
}),
mk("old/four", { delta_7d: 4 }),
mk("old/five", { delta_7d: 5 }),
mk("old/flagged", { delta_7d: 50, flagged: true }),
];
assert.deepEqual(names(trending(list, NOW)), ["new/seed", "old/five"]);
assert.equal(liveList(list).length, 5, "still listed and ranked");
});

test("trending age: exactly 7 days old qualifies", () => {
const e = mk("edge/seven", {
delta_7d: 9,
submitted_at: "2026-10-29T12:00:00Z",
});
assert.deepEqual(names(trending([e], NOW)), ["edge/seven"]);
});
Loading