93 lines
4.9 KiB
Python
93 lines
4.9 KiB
Python
import dataclasses
|
|
import statistics
|
|
import typing
|
|
|
|
from comprehension_history import HISTORY
|
|
from todo_table import Item, items
|
|
from reading_cost.figures import RECORD
|
|
from round_titles import round_counts, round_findings
|
|
|
|
# Fewest single-item chunks a score needs before its medians print: the 10 figures the reading-cost trend waits for; no published standard sets either.
|
|
ENOUGH = 10
|
|
# The plan, and the records tools append to: none of their lines are the chunk's work.
|
|
NOT_WORK = ("todo.md", HISTORY, RECORD)
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Outcome:
|
|
"""What the merge that shipped an item measured. findings holds the 🔴 and 🟡 counts, None where a round title lacks them or the branch has no round."""
|
|
item: Item
|
|
merge: str
|
|
merged: str
|
|
shipped_with: tuple[int, ...]
|
|
lines: int
|
|
files: int
|
|
rounds_past_first: int
|
|
findings: tuple[int, int] | None
|
|
|
|
|
|
# Each pass takes a first round whatever the change, so only the rounds after it measure how hard the change was to get right.
|
|
MEASURES: dict[str, tuple[str, typing.Callable[[Outcome], int | None]]] = {
|
|
"R": ("🔴 findings", lambda outcome: outcome.findings[0] if outcome.findings else None),
|
|
"S": ("lines changed", lambda outcome: outcome.lines),
|
|
"C": ("review rounds past each pass's first", lambda outcome: outcome.rounds_past_first),
|
|
}
|
|
|
|
|
|
def shipped(before: str, after: str) -> list[Item]:
|
|
"""Each item the todo.md before a change holds and the todo.md after it does not, as scored before; none where either holds no items table it can read."""
|
|
planned, left = items(before), items(after)
|
|
if not isinstance(planned, list) or not isinstance(left, list):
|
|
return []
|
|
kept = {item.id for item in left}
|
|
return sorted((item for item in planned if item.id not in kept), key=lambda item: item.id)
|
|
|
|
|
|
def outcomes_of(shipped: list[Item], merge: str, merged: str, numstat: str, titles: list[str]) -> list[Outcome]:
|
|
"""What one merge measured for each item it shipped, from `git diff --numstat` against its first parent and its branch's commit titles; none where it changed nothing but the plan and the records."""
|
|
changed = [row.split("\t", 2) for row in numstat.splitlines()]
|
|
work = [row for row in changed if len(row) == 3 and row[2] not in NOT_WORK]
|
|
if not work:
|
|
return []
|
|
# Git counts a binary file's lines as "-".
|
|
lines = sum(int(count) for row in work for count in row[:2] if count.isdigit())
|
|
past_first = sum(max(0, count - 1) for count in round_counts(titles).values())
|
|
ids = [item.id for item in shipped]
|
|
return [Outcome(item, merge, merged, tuple(id_ for id_ in ids if id_ != item.id), lines, len(work), past_first, round_findings(titles)) for item in shipped]
|
|
|
|
|
|
def nearest(outcomes: list[Outcome], given: dict[str, int], count: int = 5) -> list[Outcome]:
|
|
"""The outcomes whose guesses sit closest to the given scores: the least mean squared difference over the scores both hold, the newest first among equals."""
|
|
def distance(outcome: Outcome) -> float:
|
|
shared = [name for name in given if name in outcome.item.scores]
|
|
return sum((given[name] - outcome.item.scores[name]) ** 2 for name in shared) / len(shared)
|
|
newest_first = sorted(outcomes, key=lambda outcome: outcome.merged, reverse=True)
|
|
return sorted(newest_first, key=distance)[:count]
|
|
|
|
|
|
def report(outcomes: list[Outcome]) -> list[str]:
|
|
"""For each score, the median outcome at each value it was guessed at, among chunks that shipped one item: a chunk measures all its items at once."""
|
|
single = [outcome for outcome in outcomes if not outcome.shipped_with]
|
|
lines = []
|
|
for name, (what, measure) in MEASURES.items():
|
|
held = [(outcome.item.scores[name], value) for outcome in single if name in outcome.item.scores and (value := measure(outcome)) is not None]
|
|
if len(held) < ENOUGH:
|
|
chunks = "chunk that shipped one item records" if len(held) == 1 else "chunks that shipped one item record"
|
|
lines.append(f"- {name}, {what}: {len(held)} {chunks} {name} and its outcome; the report needs {ENOUGH}.")
|
|
continue
|
|
medians = [(guess, [value for at, value in held if at == guess]) for guess in sorted({guess for guess, _ in held})]
|
|
lines.append(f"- {name}, {what}: " + ", ".join(f"{guess} → {statistics.median(found):g} ({plural(len(found), 'chunk')})" for guess, found in medians))
|
|
return lines
|
|
|
|
|
|
def render(outcome: Outcome) -> str:
|
|
scores = ", ".join(f"{name} {outcome.item.scores[name]}" for name in MEASURES if name in outcome.item.scores)
|
|
findings = f"{outcome.findings[0]} 🔴, {outcome.findings[1]} 🟡" if outcome.findings else "findings unknown"
|
|
others = outcome.shipped_with
|
|
together = f", with item{'s' if len(others) > 1 else ''} {', '.join(map(str, others))}" if others else ""
|
|
return f"- Item {outcome.item.id}, {scores}: {plural(outcome.lines, 'line')} in {plural(outcome.files, 'file')}, {plural(outcome.rounds_past_first, 'round')} past each pass's first, {findings}; merged {outcome.merged} in {outcome.merge}{together}. {outcome.item.tagline}"
|
|
|
|
|
|
def plural(count: int, noun: str) -> str:
|
|
return f"{count} {noun}{'' if count == 1 else 's'}"
|