Fix the usage report that groups costs by project
Your task
An AI wrote a Django usage report for an internal dashboard. It loaded every event for a tenant into memory and looked up each event's project one row at a time, and the worker ran out of memory on the largest tenant. The rewrite lets the database group the events, so the report receives one row per event with the project name already joined in and keeps one running total per project.
An assistant wrote usage_report_lines(rows). It is in the editor. It has a bug. Find it and fix it.
Input and output:
rows: one string per event, writtenproject|cost_cents, such asalpha|100. Costs can be negative: credits and refunds are recorded as negative costs.- Return one string per project,
"<project>=<total>", in the order described below.
Rules the tests check:
- Split each row on
|. Skip a row that does not have exactly 2 parts. Nothing is trimmed. - Skip a row whose project name is empty, or whose cost is not an integer: an optional
-followed by one or more digits 0 to 9, and nothing else. - Add up the costs of each project.
- Sort the projects by total, largest first. Break ties by project name, in ascending order as plain strings.
Examples
usage_report_lines(rows: list[str]) -> list[str]Example 1
Input
usage_report_lines(["alpha|100", "beta|50", "alpha|25"])Output
["alpha=125", "beta=50"]Example 2
Input
usage_report_lines(["a|10", "b|10"])Output
["a=10", "b=10"]Example 3
Input
usage_report_lines([])Output
[]