Skip to content

Decentralization country/continent code: reduce duplication and redundant node_ip_classification reads #151

Description

@2ndtlmining

Two related nits from the ultrareview of PR #145/#147 (issue #138's country/continent work), not fixed yet since they're efficiency/maintainability, not correctness:

1. Redundant full-table reads per snapshot cycle

snapshotManager.js's takeSnapshot() now calls getDecentralizationStats(), getFullDatacenterBreakdown(), getFullCountryBreakdown(), and getFullContinentBreakdown() -- each of which independently calls getAllNodeIpClassifications() (decentralizationService.js), which fully paginates the whole node_ip_classification table under Supabase. That's up to 4 full-table reads of the same data, once per snapshot cycle, in every environment.

Fix direction: one shared fetch of allClassifications + candidateIps in snapshotManager.js (or a single computeAllBreakdowns() helper in decentralizationService.js), then three in-memory group-bys instead of three separate DB round-trips.

2. Country/continent logic duplicated across 4 files

Country and continent are handled by near-identical parallel copies in:

  • decentralizationService.js: getFullCountryBreakdown() / getFullContinentBreakdown() (differ only by field name)
  • sqliteAdapter.js: createDecentralizationCountrySnapshots/getDecentralizationCountrySnapshotHistory and their continent twins
  • supabaseAdapter.js: the same 4 functions
  • snapshotManager.js: two near-identical try/catch blocks (fetch + write) for country vs. continent

Adding a third dimension (e.g. ASN, region) currently means editing all four modules, and every future bugfix has to be applied twice.

Fix direction: a single set of functions parameterized by dimension (name field, code field, table name) -- analogous to how getFullDatacenterBreakdown() already handles one dimension alone -- would remove roughly 150 duplicated lines and make future dimensions a one-file change.

Scope

  • src/lib/services/decentralizationService.js
  • src/lib/db/adapters/sqliteAdapter.js / supabaseAdapter.js
  • src/lib/db/snapshotManager.js

Out of scope

No behavior change intended -- this is a pure refactor. Any change here needs the existing decentralization test suite (decentralizationService.test.js, nodeIpClassification.test.js, snapshotManager.test.js, decentralizationSnapshots.test.js) to keep passing unmodified in outcome, even if the internals move.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    tech-debtCleanup and consistency work

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions