Skip to content

Add iter_finding_outcomes with per-denominator outcome rates - #156

Open
HemanthGangula wants to merge 1 commit into
prooflinedev:mainfrom
HemanthGangula:finding-outcome-rates
Open

HemanthGangula wants to merge 1 commit into
prooflinedev:mainfrom
HemanthGangula:finding-outcome-rates

Conversation

@HemanthGangula

Copy link
Copy Markdown

The finding_outcomes export docs (https://proofline.sh/docs/exports, "Read finding outcomes") say each count has to be divided by its own denominator rather than by published, and that a zero denominator has no rate. Today the SDK hands back raw dicts, so every caller has to get that right by hand, and dividing a human outcome by published is an easy mistake that produces a plausible but wrong number.

This adds iter_finding_outcomes on ProoflineClient and AsyncProoflineClient, which yields one typed FindingOutcomeGroup per row:

for group in client.iter_finding_outcomes(org_id, from_time=..., to_time=...):
    print(group.name, group.human_rate("DISMISSED"), group.post_merge_rate("ADDRESSED"))
# example/payments 1 of 6 (17%) 2 of 3 (67%)
# example/web 0 of 0, no rate 0 of 0, no rate
  • human_rate(outcome) divides by external_validation_denominator and only accepts FIXED, CONFIRMED, ACCEPTED_RISK and DISMISSED.
  • code_changed_rate() divides by published.
  • post_merge_rate(outcome) divides by post_merge.judged.
  • OutcomeRate keeps the count and its denominator together. value is None when the denominator is 0, and it prints 0 of 0, no rate, never 0% or 100%.

It is built on iter_export_pages (NDJSON only, since CSV flattens the nested counts to strings), so pagination, cursor=, max_pages and the transport retries behave exactly as they do there. group_by defaults to repository, like the server.

Row checks

Each row is checked against the documented counts before anything is yielded. A row that fails raises ProoflineError with urn:proofline:sdk:invalid-response, and no group from its page is yielded, which matches how iter_export_pages never yields a page with a bad cursor.

  • all six outcomes appear exactly once, and all three post-merge outcomes
  • published equals the sum of the six outcomes
  • external_validation_denominator equals the four human outcomes
  • the post-merge outcomes sum to judged
  • code_changed and judged are at most published
  • counts are non-negative ints (True and 3.0 are rejected, since they would pass the sums), and from/to carry a timezone

Unknown fields are ignored, so new server fields don't break the parser. The outcomes and post_merge mappings are read-only.

Choices worth a look

  • Rates print as whole percents, the same as the docs, so 1 of 200 prints 1 of 200 (0%). The count always comes first and value is exact.
  • validation_sources isn't checked against each outcome's count, because the group doesn't expose it.
  • "post-merge outcomes sum to judged" is implied by "one count per post-merge outcome" and by the docs' example rather than stated outright. If the server can ever disagree, I'm happy to drop that check.

Tests

python3 scripts/check.py passes (271 tests, ruff, mypy, contracts, wheel and sdist installs).

  • the docs' two example rows give exactly the docs' table
  • 28 malformed rows, one per broken rule, each raise invalid-response
  • unknown fields and an empty limitations are accepted
  • two pages through tests/http_server.py, sync and async, with cursor= forwarded
  • a bad row on page 2 yields nothing from that page, sync and async
  • a non-default group_by is sent, and max_pages ends in pagination-limit

scripts/contracts.py lists the new method as a client helper.

Read the finding_outcomes export as typed FindingOutcomeGroup rows on
the sync and async clients. Each rate divides by the denominator the
export docs give it: human outcomes by external_validation_denominator,
code_changed by published, post-merge outcomes by post_merge.judged.
A zero denominator has no rate (OutcomeRate.value is None), never 0%
or 100%.

Every row is checked against the documented counts before it is
yielded: all six outcomes once, published equals their sum, the human
denominator equals the four human outcomes, post-merge outcomes sum to
judged, and code_changed and judged stay within published. Counts must
be ints (bools and floats are rejected) and timestamps timezone-aware.
A bad row raises invalid-response and no group from its page is
yielded, matching iter_export_pages. Unknown fields are accepted.

Register the helper in the contract check.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant