Skip to content

feat(data-machine-business): add generic revenue ledger with revenue-ingest/revenue-query - #4046

Merged
chubes4 merged 1 commit into
mainfrom
fix/issue-4037-data-machine-3
Nov 24, 2026
Merged

chubes4 merged 1 commit into
mainfrom
fix/issue-4037-data-machine-3

Conversation

@chubes4

@chubes4 chubes4 commented Nov 24, 2026

Copy link
Copy Markdown
Member

Summary

Implements #4037: a generic revenue ledger in data-machine-business. It is one table on BaseRepository, with no second table; coverage is worked out from the stored periods. datamachine/revenue-ingest is the only write path. It replaces rows idempotently by source/account/kind/window and supports dry_run. datamachine/revenue-query returns totals by source/kind/currency plus coverage. Adds wp datamachine analytics revenue <ingest|query|coverage> and opt-in store/dry_run ingest paths on the Mediavine and Stripe report abilities. Live reads are unchanged when store is false. Schema judgment calls: 1. Network-scoped table. {base_prefix}datamachine_revenue_ledger, with a per-row blog_id and the schema version in a site option. One ledger across the network matches how analytics credentials are scoped; blog_id keeps per-site attribution. 2. Exact amounts. amount is DECIMAL(20,6). It travels as a decimal string and is summed exactly with integer micros arithmetic (RevenueDecimal), never floats. Source-native numbers such as minor units and RPM go in metrics (JSON longtext, round-trips exactly). 3. No currency conversion. currency is char(3), uppercased. Totals are always split per currency. 4. Replace scope. A write covers source + account + kind rows fully inside [start_date, end_date]. Kinds default to those in the payload; an explicit kinds list can clear a kind to empty. Rows outside the window are rejected. A window that partly overlaps stored rows is refused with revenue_window_conflict instead of silently deleting or duplicating. Replace runs inside a TransactionScope. 5. Query totals use fully-contained periods only. Partly overlapping periods are counted separately and never prorated. 6. Coverage per source/account. Built by merging stored [period_start, period_end] intervals, with gaps reported inside the queried window. No second coverage table. 7. Grain values. day, week, month, quarter, year or range, derived from the period when not given. 8. Stripe. Each succeeded charge becomes a charge row on its created day (UTC). Any refunded amount becomes a negative refund row attributed to the charge's created day; metrics.attributed_to=charge_created records this choice. Payouts become payout rows on their arrival day. A truncated (MAX_PAGES) scan is refused rather than stored, so a partial window never replaces complete data. 9. Mediavine. pages and backfill rows become kind ad_revenue, with url = page slug and the remaining report fields in metrics. Account is the decoded internal site id. Currency defaults to USD because the API does not return one; config currency overrides it. Backfill ingests each period as its own window through an optional per-period callback. Verification caveat: the new PHPUnit file has not run under real WordPress. Homeboy's WP Codebox PHPUnit stage ran 0 tests (PHPUNIT_ZERO_TESTS cause=recipe_run_no_executions), an infrastructure failure seen on every attempt. To exercise the test file anyway, I ran all 12 test methods against an in-memory SQLite wpdb shim, and they all passed. That run found and fixed a read path that could return float-formatted amounts.

What changed

  • New inc/Abilities/Revenue/RevenueLedger.php: a BaseRepository table with schema/upgrade, window counts, a straddling-row check, transactional replace_window, find and hydrate.
  • New inc/Abilities/Revenue/RevenueDecimal.php: exact decimal-string ⇄ micros conversion, plus tolerant reads of stored values.
  • New inc/Abilities/Revenue/RevenueLedgerAbilities.php: datamachine/revenue-ingest (validation, window conflict guard, dry_run) and datamachine/revenue-query (per-currency totals, excluded partial overlaps, coverage with gaps, provenance). Both use PermissionHelper::can_manage and show_in_rest false, matching the reader abilities.
  • New inc/Cli/RevenueCommand.php, registered as datamachine analytics revenue in CommandRegistry: ingest (--file or STDIN, --dry-run), query and coverage.
  • ProviderModules: new revenue-ledger module; provider-bootstrap-smoke expectation updated to match.
  • StripeReportsAbilities and StripeCommand: opt-in store, dry_run, start_date, end_date and account for charges/payouts, plus ledger normalization (charges, negative refunds, payouts). StripeMoney::decimal() added.
  • MediavineReportsAbilities: opt-in store/dry_run for pages and backfill, plus ledger normalization; fetchBackfill gained an optional per-period callback. phpcbf also realigned existing arrays in this file, because the changed-file lint gate failed on old formatting issues.
  • New tests/Unit/Abilities/Revenue/RevenueLedgerAbilitiesTest.php covering idempotent replace, dry_run, replace scope, window conflict, validation, multi-currency totals, partial-overlap exclusion, coverage gaps, metrics round-trip, Mediavine/Stripe normalization and exact decimals.
  • Docs: README ability table, docs/mediavine.md and docs/stripe.md describe the opt-in store paths. CHANGELOG and version untouched.

Verification

  • 1-verify-245279eb03e64b50dff437e5058bf732bcef73997ec33fbcc136e5ead5eae4c7.txt (homeboy review test data-machine-business): passed on 34b4b4f. Base main@65ee030 unchanged. 176719 ms; retained gate metadata.

  • ( if git diff --quiet --no-renames 65ee030 -- ' (( if git diff --quiet --no-renames 65ee030a1c0418de488425dfe7f4ad12b0118b07 -- ':(literal)plugins/data-machine'; then :…): passed on 34b4b4f. Base main@65ee030 unchanged. 20571 ms; retained gate metadata.

  • repro: none (no repro gate was declared, so these gates do not prove the issue).

  • CI: pending

Cook run
  • Gate 1: tree ca18cc90ee6a2eaacc901fb77c2b7b3fa57f8a61
  • Gate 2: tree ca18cc90ee6a2eaacc901fb77c2b7b3fa57f8a61

Delivery

  • Delivered: Single-table revenue ledger on BaseRepository with coverage derived from stored periods
  • Delivered: datamachine/revenue-ingest (idempotent replace by source/account/kind/window, dry_run)
  • Delivered: datamachine/revenue-query (totals by source/kind/currency plus coverage)
  • Delivered: wp datamachine analytics revenue <ingest|query|coverage>
  • Delivered: Opt-in ingest paths on MediavineReportsAbilities and StripeReportsAbilities with live reads unchanged
  • Delivered: PHPUnit tests: idempotent replace, multi-currency totals, coverage gaps, metrics round-trip, Mediavine/Stripe normalization
  • Delivered: Commit feat(data-machine-business): add generic revenue ledger with revenue-ingest/revenue-query (34b4b4f65d6aa78f96bb2590df00958d5ce461b6)

Evidence

  • PHPUnit stage via WP Codebox ran 0 tests (PHPUNIT_ZERO_TESTS cause=recipe_run_no_executions), an infrastructure failure where no sandbox step was dispatched, so the new test did not run under real WordPress
  • RevenueLedgerAbilitiesTest, all 12 methods, run in a local in-memory SQLite wpdb shim: 12/12 passed
  • homeboy review lint data-machine-business --changed-since 65ee030: exit 0 (PHPCS passed, PHPStan level 7 passed)
  • homeboy review test data-machine-business: standalone PHP smokes 28/28 passed (including stripe-reports-smoke 107 assertions, provider-bootstrap-smoke 39, mediavine-dimensional-smoke 36); WP smokes 4/4 passed
  • php -l clean on all changed files

AI disclosure

Tool: Homeboy Cook. Agent: Homeboy (roadie). Model: roadie · route cook-heavy · served claude-opus-5-5. Cook time: 47m10s.

Implementation and reviewer documentation were written by the AI, under the direction of the Cook requester. Gates were run by Homeboy. A human reviews before merge.

Closes #4037

@chubes4
chubes4 marked this pull request as ready for review November 24, 2026 09:32
@chubes4
chubes4 merged commit 66860f3 into main Nov 24, 2026
3 checks passed
@chubes4
chubes4 deleted the fix/issue-4037-data-machine-3 branch November 24, 2026 09:32
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.

feat(data-machine-business): generic revenue ledger primitive (revenue-ingest / revenue-query) that all source readers feed

1 participant