Repository navigation
feat(data-machine-business): add generic revenue ledger with revenue-ingest/revenue-query - #4046
Merged
Merged
Conversation
…ingest/revenue-query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-ingestis the only write path. It replaces rows idempotently by source/account/kind/window and supports dry_run.datamachine/revenue-queryreturns totals by source/kind/currency plus coverage. Addswp datamachine analytics revenue <ingest|query|coverage>and opt-instore/dry_runingest paths on the Mediavine and Stripe report abilities. Live reads are unchanged whenstoreis false. Schema judgment calls: 1. Network-scoped table.{base_prefix}datamachine_revenue_ledger, with a per-rowblog_idand the schema version in a site option. One ledger across the network matches how analytics credentials are scoped;blog_idkeeps per-site attribution. 2. Exact amounts.amountis 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 inmetrics(JSON longtext, round-trips exactly). 3. No currency conversion.currencyis 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 explicitkindslist can clear a kind to empty. Rows outside the window are rejected. A window that partly overlaps stored rows is refused withrevenue_window_conflictinstead 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 achargerow on its created day (UTC). Any refunded amount becomes a negativerefundrow attributed to the charge's created day;metrics.attributed_to=charge_createdrecords this choice. Payouts becomepayoutrows on their arrival day. A truncated (MAX_PAGES) scan is refused rather than stored, so a partial window never replaces complete data. 9. Mediavine.pagesandbackfillrows become kindad_revenue, withurl= page slug and the remaining report fields inmetrics. Account is the decoded internal site id. Currency defaults to USD because the API does not return one; configcurrencyoverrides 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
datamachine analytics revenuein CommandRegistry: ingest (--file or STDIN, --dry-run), query and coverage.revenue-ledgermodule; provider-bootstrap-smoke expectation updated to match.Verification
1-verify-245279eb03e64b50dff437e5058bf732bcef73997ec33fbcc136e5ead5eae4c7.txt (
homeboy review test data-machine-business): passed on34b4b4f. Basemain@65ee030unchanged. 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 on34b4b4f. Basemain@65ee030unchanged. 20571 ms; retained gate metadata.repro: none (no repro gate was declared, so these gates do not prove the issue).
CI: pending
Cook run
ca18cc90ee6a2eaacc901fb77c2b7b3fa57f8a61ca18cc90ee6a2eaacc901fb77c2b7b3fa57f8a61Delivery
Evidence
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