Skip to content

Task/address aggregates mv#256

Merged
greatest0fallt1me merged 3 commits into
Predictify-org:mainfrom
CHKM001:task/address-aggregates-mv
Jun 29, 2026
Merged

Task/address aggregates mv#256
greatest0fallt1me merged 3 commits into
Predictify-org:mainfrom
CHKM001:task/address-aggregates-mv

Conversation

@CHKM001

@CHKM001 CHKM001 commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Description - Add predictions per address aggregated view (PR #145)

Description

This pull request adds an address_aggregates_mv materialized view that precomputes per-address prediction statistics for fast leaderboard queries. The view is refreshed hourly using CONCURRENTLY to avoid blocking reads, and is served via the /api/leaderboard endpoint.

Requirements Covered

  • MV definition: Aggregates total_predictions, correct_predictions, accuracy_percentage, and rank per user
  • Refresh job hourly: src/workers/refreshAggregates.ts runs on a 1-hour interval with .unref() for clean shutdown
  • Used by /api/leaderboard: Endpoint reads from address_aggregates_mv with pagination and optional ?refresh=true
  • Concurrent refresh: Uses REFRESH MATERIALIZED VIEW CONCURRENTLY; unique index on user_id is a prerequisite
  • Structured logging: Correlation IDs via pino-http; worker logs duration and errors
  • Input validation: Zod schema on query params (limit, offset, refresh)
  • Error envelope: Standardized error responses (e.g., 404 for unknown address)

Key Changes

New files

  • drizzle/migrations/0013_address_aggregates_mv.sql: MV DDL with CONCURRENTLY-compatible unique index and supporting indexes
  • src/services/addressAggregatesService.ts: Shared service with refreshAddressAggregates(), getAddressAggregates(), getAddressAggregate(), getAddressAggregatesWithRefresh()
  • src/workers/refreshAggregates.ts: Hourly interval worker that calls refreshAddressAggregates()
  • tests/addressAggregatesService.test.ts: Unit tests for all service functions (6 tests)
  • tests/refreshAggregates.test.ts: Worker lifecycle tests (interval ticking, error recovery, cleanup) (3 tests)
  • tests/leaderboard.test.ts: Route integration tests for GET /api/leaderboard and GET /api/leaderboard/user/:stellarAddress (6 tests)

Modified files

  • src/services/leaderboardService.ts: Refactored to delegate to addressAggregatesService instead of querying the database directly
  • src/index.ts: Imports and starts the refresh worker; cleans up on SIGTERM/SIGINT

Verification

All 15 tests pass across 3 test suites:

npm test -- --testPathPattern="refreshAggregates|addressAggregatesService|leaderboard"

Closes #145

CHKM001 added 3 commits June 28, 2026 20:19
- Fix broken src/routes/users.ts (missing closing brace, incomplete route)
- Remove unused imports in src/services/userService.ts and src/middleware/auth.ts
- Add 'silent' to LOG_LEVEL enum in env-schema
- Export Db type from src/db/index.ts
- Re-export AppError/ErrorCodes from src/errors.ts (resolve module conflict)
- Add eslint-disable for namespace declarations in middleware
- Add eslint-disable for pre-existing any types in market routes/service
- Fix duplicate UserProfile interface in userService.ts
- Remove claims table references (table doesn't exist in schema)
- Add address_aggregates_mv MV with rank, accuracy, and prediction counts
- Add hourly refresh worker (src/workers/refreshAggregates.ts)
- Add addressAggregatesService for CRUD on the MV
- Refactor leaderboardService to delegate to addressAggregatesService
- Add migration 0013_address_aggregates_mv.sql
- Add tests for service, worker, and leaderboard endpoint
@drips-wave

drips-wave Bot commented Jun 28, 2026

Copy link
Copy Markdown

@CHKM001 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me greatest0fallt1me merged commit ffd072d into Predictify-org:main Jun 29, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a 'predictions per address' aggregated view as a materialized view

2 participants