← all branches

fix/mrr-base

needs attentionviewing older commit
69a0691 · fullpre-PRreviewed 2026-08-11 18:12 UTC3H · 7M · 5L · 4I
The branch
Purpose
Fix silent MRR under-reporting for base-fee credit-model orgs in the PostHog MRR pivot
Goal
Correct the credit MRR formula to include base_price (not just overage), matching Stripe/mrr_coverage_gap actuals
Sub-goals
  • SG-1: Add base_price_mxn + credits_included to the credit CTE
  • SG-2: Compute real monthly bill as base + overage via credit_mrr CTE
  • SG-3: Attribute v proportionally across Monitoreo/Descarga/Payment-status buckets
  • SG-4: Pin Smarter (ER) Feb–Jun 2026 to finance CSV actuals (BAT-314 temp override)
  • SG-5: Sync identical fix to scripts/posthog-mrr-pivot.mjs (script is SOT)
The changes (whole branch)
What
Both infra/posthog/insights.tf and scripts/posthog-mrr-pivot.mjs updated: credit CTE gains base_price_mxn and credits_included; new credit_mrr CTE computes real bill; attribution formula changed from count×rate to v×bucket/consumed; Smarter rows hardcoded for Feb–Jun 2026
Why
The old formula computed credit MRR as downloads × overage_rate, completely dropping base_price. For Bright this caused a −$18,563/mo under-report; basic-legacy −$3,000/mo. The fix aligns the pivot with mrr_coverage_gap + Stripe actuals.
Areas
infra/posthog+289scripts+3112
Blast
2 files, +59/−21 total; analytics-only change — no app code, no schema, no API. Only affects PostHog MRR pivot chart accuracy.
analytics-only no-schema-change posthog-sql
CI· No PR — CI status unavailable for pre-PR branchCodeRabbit· No .coderabbit.yaml in repo

Findings · 17

correctness3

high

credits_included NULL → v becomes NULL, silently drops active credit orgs

infra/posthog/insights.tf

If any plan has p.credits_included = NULL, then greatest((consumed - NULL), 0) evaluates to NULL in ClickHouse, making v = base + NULL*rate = NULL. The org's rows are then silently excluded by WHERE mrr > 0. Fix: add coalesce(p.credits_included, 0) AS incl in the credit CTE.

high

Zero-consumed orgs lose base_price MRR — WHERE consumed > 0 over-excludes

infra/posthog/insights.tf

The WHERE consumed > 0 guard correctly prevents div/0, but an org on a paid base plan with no credit transactions in a month is absent from the credit CTE entirely — its base_price contribution is lost. The script header now documents this as a known limitation, but MRR is still understated for those months. A separate base-fee UNION ALL from org-plan to credit_mrr with consumed=0 would fix this.

medium

Attribution rounding — three round() calls may not sum to org total

infra/posthog/insights.tf

round(v*mon_cr/consumed/100.0,0) + round(v*one_cr/consumed/100.0,0) + round(v*pay_cr/consumed/100.0,0) can differ from round(v/100.0,0) by ±2 MXN. Cosmetic for chart display, but produces unexplained discrepancies in reconciliation against org-level totals.

security2

info

HogQL reads sensitive billing data — unchanged scope

infra/posthog/insights.tf

Query joins org names, plan pricing, credit counts. No new exposure vs. the previous version. Access should remain restricted to internal team members.

info

Hardcoded historical MRR figures in SQL — not secrets

infra/posthog/insights.tf

The multiIf values are internal revenue figures for analytics accuracy, not secrets. Appropriate for an internal-only PostHog project.

conventions4

medium

BAT-314 Smarter override lacks expiry guard — infra/posthog/insights.tf

infra/posthog/insights.tf

The multiIf hardcode for Feb–Jun 2026 has no TODO or removal trigger. Add: -- TODO(BAT-314): remove after 2026-06, verify live formula for Jul+. Without it, the override silently persists and future maintainers won't know it's time-bounded.

medium

BAT-314 Smarter override lacks expiry guard — scripts/posthog-mrr-pivot.mjs

scripts/posthog-mrr-pivot.mjs

Same as the Terraform copy — the override is present without a removal comment or Linear-issue reference.

low

Column rename mon_dl→mon_cr, one_dl→one_cr — no grep audit documented

infra/posthog/insights.tf

Per canonical-form.md: audit before renaming any field. These are internal HogQL aliases, but a quick grep for mon_dl/one_dl in the Terraform file and dashboard configs would confirm no downstream breakage.

info

Dual-copy sync correct — follows established posthog-api-insights pattern

scripts/posthog-mrr-pivot.mjs

Both infra/posthog/insights.tf and scripts/posthog-mrr-pivot.mjs updated in the same commit — exactly the required convention for MRR · pivote maestro (HogQL variables prevent Terraform-only management).

tests3

medium

No test for credit MRR formula — silent calculation bug was undetectable

The formula v = base + greatest((consumed - incl), 0) * rate with proportional attribution is pure arithmetic expressible as a pure JS function testable with Vitest. Without a test, the same class of silent miscalculation can recur.

low

No test for zero-consumption guard (WHERE consumed > 0)

The guard prevents div/0 but also excludes base-fee orgs with no downloads. A boundary test would document the trade-off explicitly.

info

No HogQL test pattern exists — project-wide gap

Analytics SQL has no unit-test pattern in this project. This PR is consistent with existing practice but the gap increases silent regression risk across all PostHog insights.

improvement5

high

Duplicate HogQL in two files with no automated sync check

The same HogQL query lives in both infra/posthog/insights.tf and scripts/posthog-mrr-pivot.mjs. Both were correctly updated in this PR, but there is no CI check to detect future drift. The original bug (missing base_price) could silently recur if a future fix updates only one copy.

medium

Smarter override fallthrough after Jun 2026 is ambiguous — 'aug=0' unexplained

infra/posthog/insights.tf

The commit says 'aug=0' without explaining whether this is intentional (no reports yet) or accidental (live formula broken). If the fallthrough formula is temporarily invalid, extend the override to cover Jul–Aug with explicit 0.0 so zero is intentional rather than invisible. Otherwise a future reader can't distinguish correct from broken.

medium

Known zero-download gap unguarded in the SQL — documentation only in script header

scripts/posthog-mrr-pivot.mjs

The header documents the limitation but the SQL has no inline comment. A future developer diagnosing a MRR shortfall won't find the caveat in the query. Add a SQL comment on the credit CTE, and file a follow-up for a LEFT JOIN base-fee fix.

low

Magic 100.0 division unexplained — centavos → pesos

infra/posthog/insights.tf

The /100.0 throughout attribution formulas is a centavos-to-pesos conversion. A brief inline comment on the first occurrence would make this self-documenting.

low

Hardcoded Smarter MXN values have no documented source

infra/posthog/insights.tf

Values 71323.0, 70082.0, 73010.0, 69349.0 have no comment linking them to an invoice or spreadsheet. A brief audit trail comment helps if historical MRR is ever questioned.

History · 4 commits

  1. 2db5192needs attentionincremental0H · 4M · 5L2026-08-11 22:54
  2. 1a8164fneeds attentionincremental2H · 2M · 4L2026-08-11 22:46
  3. e807453needs attentionincremental0H · 2M · 3L2026-08-11 19:07
  4. 69a0691needs attentionfull3H · 7M · 5L2026-08-11 18:12current