Dashboard Data Contract Map - Code Validation
Summary (3–6 bullets)
- Validates dashboard contract assumptions against actual query functions and SQL view definitions.
- Extracts concrete
FROMobjects for endpoint-related backend query paths. - Resolves naming and contract contradictions (table names, ranking behavior, source objects).
- Produces a dependency matrix from API endpoints to base BigQuery objects.
When to use this (3–6 bullets)
- When reconciling docs with backend/query reality.
- Before refactoring dashboard query functions or analytics views.
- During incident response for contract drift or data source confusion.
- When auditing whether endpoint behavior still matches documented contracts.
What you’ll walk away with (2–5 bullets)
- A code-backed contract map for dashboard endpoint dependencies.
- Clear resolutions for common source-of-truth ambiguities.
- A validation checklist you can reuse after query/view changes.
Purpose: Validate dashboard contract map against actual code and SQL
1. Query Functions - FROM Clauses
get_ceo_snapshot_from_view
Location: api/bigquery/queries.py:556FROM Object:
get_ceo_metrics_range
Location: api/bigquery/queries.py:8179FROM Objects (3 queries):
- Financial metrics:
- Distinct counts:
- Active snapshot:
- MoM fields (for single period):
get_agent_commissions_range
Location: api/bigquery/queries.py:8850FROM Object:
tpa_applied = TRUE
get_top_businesses_from_view
Location: api/bigquery/queries.py:5233FROM Object:
get_top_businesses_ytd
Location: api/bigquery/queries.py:9223FROM Object:
get_top_businesses_qtd
Location: api/bigquery/queries.py:9295FROM Object:
get_top_agents_from_view
Location: api/bigquery/queries.py:5300FROM Object:
get_top_agents_ytd
Location: api/bigquery/queries.py:9367FROM Object:
tpa_applied = TRUE
get_top_agents_qtd
Location: api/bigquery/queries.py:9438FROM Object:
tpa_applied = TRUE
get_growth_loss_summary
Location: api/bigquery/queries.py:677Delegates to:
get_growth_loss_summary_mom()→ callsget_growth_loss_details_from_view()→ queriesstage1_snapshots(FULL OUTER JOIN)get_growth_loss_summary_snapshot()→ queriesstage1_snapshotsdirectly
get_growth_loss_details
Location: api/bigquery/queries.py:5755FROM Object:
get_new_lost_businesses
Location: api/bigquery/queries.py:5511FROM Objects (FULL OUTER JOIN):
get_business_health_distribution
Location: api/bigquery/queries.py:5837FROM Object:
2. View SQL - SELECT Clauses
analytics_ceo_snapshot.sql
Location: integration/bigquery/sql/views/analytics_ceo_snapshot.sqlBase Table:
payroll-bi-gauntlet.payroll_raw.stage1_snapshots
SELECT Columns:
analytics_top_businesses.sql
Location: integration/bigquery/sql/views/analytics_top_businesses.sqlBase Table:
payroll-bi-gauntlet.payroll_raw.stage1_snapshots
SELECT Columns:
QUALIFY ROW_NUMBER() OVER (PARTITION BY tenant_id, period_label ORDER BY SUM(total) DESC) <= 10 but does NOT include rank column in SELECT. Rank is computed in backend query function.
analytics_top_agents.sql
Location: integration/bigquery/sql/views/analytics_top_agents.sqlBase Tables:
payroll-bi-gauntlet.payroll_processed.stage3_snapshots(for commissions)payroll-bi-gauntlet.payroll_raw.stage1_snapshots(for employee counts, via JOIN)
QUALIFY ROW_NUMBER() OVER (PARTITION BY c.tenant_id, c.period_label ORDER BY c.total_commission DESC) <= 10 but does NOT include rank column in SELECT. Rank is computed in backend query function: ROW_NUMBER() OVER (ORDER BY total_commission DESC) as rank.
analytics_business_growth_loss.sql
Location: integration/bigquery/sql/views/analytics_business_growth_loss.sqlBase Table:
payroll-bi-gauntlet.payroll_raw.stage1_snapshots
SELECT Columns:
3. Object Dependency Table
| Endpoint | Query Function | Base Objects (Tables/Views) | Notes |
|---|---|---|---|
/api/v1/ceo-metrics (MTD) | get_ceo_snapshot_from_view() | payroll_analytics.ceo_snapshot (view) | View aggregates from stage1_snapshots |
/api/v1/ceo-metrics (YTD/QTD) | get_ceo_metrics_range() | payroll_analytics.ceo_snapshot (view)payroll_raw.stage1_snapshots (table) | Hybrid: financial from view, counts from table |
/api/v1/ceo-metrics (agent commissions) | get_agent_commissions_range() | payroll_processed.stage3_snapshots (table) | Filter: tpa_applied = TRUE |
/api/v1/top-businesses (MTD) | get_top_businesses_from_view() | payroll_analytics.top_businesses (view) | View aggregates from stage1_snapshots |
/api/v1/top-businesses (YTD) | get_top_businesses_ytd() | payroll_raw.stage1_snapshots (table) | Direct query, aggregates across date range |
/api/v1/top-businesses (QTD) | get_top_businesses_qtd() | payroll_raw.stage1_snapshots (table) | Direct query, aggregates across date range |
/api/v1/top-agents (MTD) | get_top_agents_from_view() | payroll_analytics.top_agents (view) | View joins stage3_snapshots + stage1_snapshots |
/api/v1/top-agents (YTD) | get_top_agents_ytd() | payroll_processed.stage3_snapshots (table) | Filter: tpa_applied = TRUE |
/api/v1/top-agents (QTD) | get_top_agents_qtd() | payroll_processed.stage3_snapshots (table) | Filter: tpa_applied = TRUE |
/api/v1/growth-loss (MTD) | get_growth_loss_summary() → get_growth_loss_summary_mom() → get_growth_loss_details_from_view() | payroll_raw.stage1_snapshots (table) | FULL OUTER JOIN between current/previous month |
/api/v1/growth-loss (YTD/QTD) | get_growth_loss_summary() → get_growth_loss_summary_snapshot() | payroll_raw.stage1_snapshots (table) | Direct query with date range comparison |
/api/v1/growth-loss-details | get_growth_loss_details() | payroll_analytics.business_growth_loss (view) | View uses LAG() window function |
/api/v1/new-lost-businesses | get_new_lost_businesses() | payroll_raw.stage1_snapshots (table) | FULL OUTER JOIN between current/previous period |
/api/v1/business-health | get_business_health_distribution() | payroll_analytics.business_growth_loss (view) | View uses LAG() window function |
4. Contradiction Resolution
4.1 stage1_snapshot vs stage1_snapshots
RESOLUTION: ✅ Correct name is stage1_snapshots (plural)
Evidence:
- All query functions use:
payroll-bi-gauntlet.payroll_raw.stage1_snapshots - View SQL files use:
payroll-bi-gauntlet.payroll_raw.stage1_snapshots - No references to
stage1_snapshot(singular) found in codebase
stage1_snapshots (plural). Any documentation referring to stage1_snapshot (singular) is incorrect.
4.2 Rank Computed in View vs Rank Added in Backend
RESOLUTION: ✅ Rank is computed in BOTH places, but backend adds it to SELECT Evidence: Top Businesses View (analytics_top_businesses.sql):
- View uses
QUALIFY ROW_NUMBER() OVER (PARTITION BY tenant_id, period_label ORDER BY SUM(total) DESC) <= 10to LIMIT results - View does NOT include
rankcolumn in SELECT clause - Backend function (
get_top_businesses_from_view) adds:ROW_NUMBER() OVER (ORDER BY gross_payout DESC) as payout_rank - Backend also adds:
0 as employee_change(placeholder)
analytics_top_agents.sql):
- View uses
QUALIFY ROW_NUMBER() OVER (PARTITION BY c.tenant_id, c.period_label ORDER BY c.total_commission DESC) <= 10to LIMIT results - View does NOT include
rankcolumn in SELECT clause - Backend function (
get_top_agents_from_view) adds:ROW_NUMBER() OVER (ORDER BY total_commission DESC) as rank
get_top_businesses_ytd(): AddsROW_NUMBER() OVER (ORDER BY total_payout DESC) AS rankin SELECTget_top_businesses_qtd(): AddsROW_NUMBER() OVER (ORDER BY total_payout DESC) AS rankin SELECTget_top_agents_ytd(): AddsROW_NUMBER() OVER (ORDER BY total_commission DESC) AS rankin SELECTget_top_agents_qtd(): AddsROW_NUMBER() OVER (ORDER BY total_commission DESC) AS rankin SELECT
- Views use
QUALIFY ROW_NUMBER() ... <= 10to LIMIT results to top 10, but do NOT exposerankcolumn - Backend query functions add
rankcolumn usingROW_NUMBER()in their SELECT clauses - This is intentional: views filter to top 10, backend adds rank for display purposes
4.3 stage3_snapshots vs agent_report_v5
RESOLUTION: ✅ Dashboard uses stage3_snapshots, NOT agent_report_v5
Evidence:
- All dashboard query functions query
payroll_processed.stage3_snapshots get_top_agents_from_view()queriespayroll_analytics.top_agentsview, which joinsstage3_snapshotsget_agent_commissions_range()queriespayroll_processed.stage3_snapshotsdirectly- No references to
agent_report_v5found in dashboard-related query functions
agent_report_v5 may exist as a separate view for other purposes (agent dashboard, reports), but it is NOT used by the CEO dashboard endpoints.
Conclusion: Dashboard endpoints use stage3_snapshots table and top_agents view. agent_report_v5 is not part of the CEO dashboard data contract.
5. Summary of Base Objects
Tables (Source of Truth)
-
payroll_raw.stage1_snapshots- Used by: CEO metrics (distinct counts), Top Businesses (YTD/QTD), Growth/Loss, New/Lost Businesses
- Key fields:
tenant_id,period_label,business_label,member_id,credit,debit,total,payee_agent_id
-
payroll_processed.stage3_snapshots- Used by: Agent Commissions, Top Agents (YTD/QTD), Top Agents view (via JOIN)
- Key fields:
tenant_id,period_label,business_name,downline_agent_name,agent_total,tpa_applied
Views (Pre-aggregated)
-
payroll_analytics.ceo_snapshot- Base:
stage1_snapshots - Used by: CEO Metrics (MTD), CEO Metrics Range (financial metrics)
- Base:
-
payroll_analytics.top_businesses- Base:
stage1_snapshots - Used by: Top Businesses (MTD)
- Base:
-
payroll_analytics.top_agents- Base:
stage3_snapshots(commissions) +stage1_snapshots(employee counts, via JOIN) - Used by: Top Agents (MTD)
- Base:
-
payroll_analytics.business_growth_loss- Base:
stage1_snapshots - Used by: Growth/Loss Details, Business Health Distribution
- Base:
6. Validation Checklist
- All query functions mapped to FROM objects
- All view SQL SELECT clauses extracted
- Object dependency table created
-
stage1_snapshotsvsstage1_snapshotresolved (plural is correct) - Rank computation resolved (views filter, backend adds rank column)
-
stage3_snapshotsvsagent_report_v5resolved (dashboard usesstage3_snapshots)
Status: ✅ Validation Complete
Next Step: Update main dashboard contract map document with validated information