Skip to content

fix(data-api): port neuron_daily-history handlers to Postgres#4838

Merged
JSONbored merged 1 commit into
mainfrom
fix/tier2b-neuron-daily-history-postgres-tier
Jul 11, 2026
Merged

fix(data-api): port neuron_daily-history handlers to Postgres#4838
JSONbored merged 1 commit into
mainfrom
fix/tier2b-neuron-daily-history-postgres-tier

Conversation

@JSONbored

Copy link
Copy Markdown
Owner

Summary

  • Tier 2b of fix(data-api): 25 more D1-only handlers — some serving frozen data since the streamer stopped #4832 (the last batch): ports the 9 remaining neuron_daily-derived handlers to the Postgres tier — handleValidatorHistory, handleNeuronHistory, handleSubnetHistory, handleSubnetConcentrationHistory, handleSubnetPerformanceHistory, handleSubnetYieldHistory, handleChainTurnover, handleSubnetTurnover, handleSubnetMovers.
  • Translates SQLite's date(MAX(snapshot_date), '-N days') boundary-anchoring (used by chain/subnet turnover and movers) to Postgres's native MAX(snapshot_date) - N::int DATE arithmetic.
  • Casts every read snapshot_date to ::text — postgres.js parses DATE columns to JS Date objects by default, which would silently break the pure builders' row.snapshot_date === startDate string-equality comparisons.
  • Casts validator_permit to ::int in the two SUM(validator_permit) aggregate queries — it's BOOLEAN in Postgres (vs D1's 0/1 integer), and SUM(boolean) is a Postgres type error.
  • Exports a few previously-private *_WINDOWS/DEFAULT_*_WINDOW constants and turnoverChangeDetail from src/concentration.mjs, src/subnet-performance.mjs, src/subnet-yield.mjs, src/turnover.mjs so the Postgres routes mirror their D1 siblings' window handling exactly.

Test plan

  • npx vitest run tests/data-api.test.mjs tests/request-handlers-entities.test.mjs — 186 + 488 tests pass (21 new data-api route tests covering happy path + window-fallback + cold-store branches; 18 new entities wiring tests for postgres-wins/falls-back-to-D1)
  • npx vitest run (full repo) — 259 files / 7369 tests pass
  • npm run lint / npx prettier --check on touched files — clean
  • npm run scan:public-safety — clean
  • npm run test:coverage + manual codecov/patch-style branch-coverage isolation on the diff (cross-referenced against git diff origin/main -U0 added-line ranges) — every uncovered branch confirmed pre-existing/outside the patch

This closes out #4832's full scope (Tier 1a/1b/1c + Tier 2a/2b) — every D1-only handler flagged in the umbrella issue now has a Postgres tier with fallback.

Refs #4832

Tier 2b of #4832: handleValidatorHistory, handleNeuronHistory,
handleSubnetHistory, handleSubnetConcentrationHistory,
handleSubnetPerformanceHistory, handleSubnetYieldHistory,
handleChainTurnover, handleSubnetTurnover, and handleSubnetMovers were
still D1-only. Adds the matching 9 routes to the data Worker and wires
tryPostgresTier into each handler.

The boundary-snapshot routes (chain/subnet turnover, movers) translate
SQLite's date(MAX(snapshot_date), '-N days') to Postgres's native
`MAX(snapshot_date) - N::int` (DATE arithmetic). snapshot_date is cast
to ::text on every read since postgres.js parses DATE columns to JS
Date objects by default, which would break the pure builders' string
equality comparisons against D1's plain YYYY-MM-DD rows. validator_permit
is BOOLEAN in Postgres (vs D1's 0/1 integer), so the two routes that
SUM() it cast to ::int first (SUM(boolean) is a Postgres type error).

Exports a few previously-private WINDOWS/DEFAULT_WINDOW constants and
turnoverChangeDetail from src/concentration.mjs, subnet-performance.mjs,
subnet-yield.mjs, and turnover.mjs so the Postgres routes can mirror
their D1 siblings' window handling exactly.

Refs #4832
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
metagraphed-data-api 3e1887d Commit Preview URL

Branch Preview URL
Jul 11 2026, 04:11 AM

@superagent-security

Copy link
Copy Markdown

Superagent didn't find any vulnerabilities or security issues in this PR.

@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.77%. Comparing base (59ecd58) to head (3e1887d).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4838      +/-   ##
==========================================
+ Coverage   97.76%   97.77%   +0.01%     
==========================================
  Files         162      162              
  Lines       19038    19124      +86     
  Branches     7141     7185      +44     
==========================================
+ Hits        18613    18699      +86     
  Misses         59       59              
  Partials      366      366              
Files with missing lines Coverage Δ
src/concentration.mjs 96.96% <100.00%> (ø)
src/subnet-performance.mjs 97.29% <100.00%> (ø)
src/subnet-yield.mjs 99.28% <100.00%> (ø)
src/turnover.mjs 97.41% <ø> (ø)
workers/data-api.mjs 99.57% <100.00%> (+0.06%) ⬆️
workers/request-handlers/entities.mjs 99.34% <100.00%> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JSONbored JSONbored merged commit e3246cf into main Jul 11, 2026
14 checks passed
@JSONbored JSONbored deleted the fix/tier2b-neuron-daily-history-postgres-tier branch July 11, 2026 04:13
@JSONbored JSONbored self-assigned this Jul 11, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Block-Explorer Completion Jul 11, 2026
@github-actions github-actions Bot mentioned this pull request Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant