feat: read model rebuild data ingestion metrics - #980
Merged
Cedarich merged 8 commits intoJul 30, 2026
Merged
Conversation
titilayo967
marked this pull request as draft
June 28, 2026 18:46
…rebuild-data-ingestion-metrics
… volume from Crowdfund Vault events (Pulsefy#734) This commit implements the foundation for computing protocol-level KPIs from Crowdfund Vault contract events, providing TVL and cumulative volume series for dashboards with support for event replays and safe corrections. NEW FILES: - src/kpi_computer.py: Core KPI computation engine with support for: * TVL and cumulative volume calculation from contract events * Event normalization and deduplication by event_id * Correction handling via event replays * Incremental updates and full recomputation from raw events * Daily snapshot persistence to DailyOnchainKPISnapshot * Type-safe Decimal handling with proper scaling (10^7) - src/api/kpi_routes.py: REST API endpoints for KPI data: * GET /api/kpi/latest - Latest KPI snapshot * GET /api/kpi/series - KPI time series with date filtering * POST /api/kpi/recompute - Admin-only sync recompute trigger * POST /api/kpi/recompute-async - Admin-only async recompute - src/api/__init__.py: API module exports for router registration - tests/test_kpi_computer.py: Comprehensive unit tests covering: * KPIEvent and KPIState dataclasses * Event normalization and operation detection * Amount extraction and project/contributor parsing * State application for deposits, withdrawals, and corrections * Convenience functions and API integration - KPI_COMPUTATION_GUIDE.md: Complete documentation covering: * Architecture overview and key concepts * Usage examples and API reference * Correction handling and safety features * Troubleshooting and performance guidance MODIFIED FILES: - src/api/server.py: Registered kpi_router and updated root endpoint documentation with new KPI endpoints - src/db/models.py: Added KPI_EVENT_TYPES and KPI_OPERATIONS constants for event classification - src/security.py: Enhanced with admin token verification support: * verify_admin_token() dependency for FastAPI routes * require_admin_token() decorator for admin-only endpoints * get_current_user() for JWT user extraction * Support for both simple admin token and JWT-based auth - src/utils/logger.py: Refactored to support both JSON and text logging: * Added json_format parameter to setup_logger() * Preserved JSON logging with correlation ID for production * Added simple text logging for development * Backward compatibility aliases for existing code ACCEPTANCE CRITERIA MET: ✅ Produces TVL and cumulative volume series from Crowdfund Vault events ✅ Handles corrections/replays safely with correction event support ✅ Exposes metrics to backend via storage (DailyOnchainKPISnapshot) and API ✅ Admin-only trigger endpoint for rebuilds (/api/kpi/recompute) ✅ Deduplication by event_id to avoid duplicate derived rows ✅ Progress visible in logs with status responses SECURITY: - Admin-only endpoints protected by verify_admin_token dependency - API key authentication preserved for all protected endpoints - Rate limiting maintained for all API routes TESTING: - Full test coverage for KPI computation logic - Mocked external dependencies for isolated unit tests - Integration-ready with existing test suite
…Operations (Pulsefy#743) This commit implements backfill + incremental ingestion of account operations from Stellar Horizon, with deduplication by operation ID and rate limit respect. NEW FILES: - src/ingestion/account_operation_ingestor.py: Core ingestion engine with: * Backfill from a starting ledger * Incremental ingestion from persistent cursor * Deduplication by operation ID * Rate limit respect with exponential backoff * Persistent cursor tracking via LedgerCursorStore * Idempotent processing with safe point rollback * Operation parsing for all Stellar operation types - src/api/account_operation_routes.py: REST API endpoints: * POST /api/account-operations/ingest - Trigger ingestion (backfill/incremental) * GET /api/account-operations/status - Get ingestion cursor status * POST /api/account-operations/reset-cursor - Reset ingestion cursor * GET /api/account-operations/operations - Query ingested operations - tests/test_account_operation_ingestor.py: Comprehensive unit tests: * AccountOperation and IngestionStats dataclasses * Operation parsing and normalization * Incremental ingestion flow * Backfill flow with ledger bounds * Cursor management and status * Rate limit handling and retries MODIFIED FILES: - src/api/__init__.py: Added account_operation_router to exports - src/api/server.py: Registered account_operation_router and updated root endpoint documentation with new account operation endpoints - src/ingestion/__init__.py: Added account operation ingestor exports ACCEPTANCE CRITERIA MET: ✅ Backfill from specified ledger ✅ Incremental ingestion from last processed cursor ✅ Deduplication keyed by operation ID ✅ Respects rate limits with exponential backoff ✅ Stores operations in raw_soroban_events and contract_events tables ✅ Exposes status and operations via API SECURITY: - Admin-only endpoints protected by verify_admin_token dependency - API key authentication preserved for all protected endpoints - Rate limiting maintained for all API routes DATA PERSISTENCE: - Operations stored in raw_soroban_events (generic event store) - Payment operations also stored in contract_events for KPI compatibility - Cursor state persisted in ledger_cursors table TESTING: - Full test coverage for ingestion logic - Mocked Horizon API calls for isolated testing - Integration-ready with existing test suite
…or chain-derived datasets (Pulsefy#857) This commit implements a safe, admin-only rebuild mechanism for derived backend read models when ingestion logic changes, with dataset/contract domain support, progress visibility, and duplicate avoidance. NEW FILES (Backend): - src/read-model-rebuild/read-model-rebuild.module.ts: Module configuration - src/read-model-rebuild/read-model-rebuild.controller.ts: API endpoints - src/read-model-rebuild/read-model-rebuild.service.ts: Core rebuild orchestration - src/read-model-rebuild/read-model-rebuild.scheduler.ts: Scheduled cleanup & recovery - src/read-model-rebuild/entities/read-model-rebuild-job.entity.ts: Job entity with status tracking - src/read-model-rebuild/dto/rebuild-request.dto.ts: Request validation - src/read-model-rebuild/dto/rebuild-response.dto.ts: Response DTOs with progress - src/read-model-rebuild/index.ts: Module exports - src/database/migrations/1820000000000-CreateReadModelRebuildJobs.ts: Migration for rebuild jobs table NEW FILES (Data Processing): - src/api/rebuild_routes.py: Python endpoints for rebuild orchestration MODIFIED FILES: - apps/data-processing/src/api/server.py: Registered rebuild_router ACCEPTANCE CRITERIA MET: ✅ Admin-only trigger endpoint (POST /api/read-model/rebuild) with JWT + Roles guard ✅ Supports rebuild by dataset (kpi_snapshots, project_views, contract_events, daily_metrics, all) ✅ Supports rebuild by contract domain via contractId parameter ✅ Progress visible via GET /api/read-model/jobs/:jobId (status, items processed, estimated time) ✅ Outcome visible in logs and status responses ✅ Designed to avoid duplicate derived rows via: - Idempotency key support - Check for existing in-progress jobs - Force flag to override - Unique constraint on idempotency_key KEY FEATURES: - Job tracking with status: pending, in_progress, completed, failed, cancelled - Progress tracking: total_items, processed_items, failed_items - Estimated time remaining calculation - Audit logging via AdminAuditService - Distributed lock via JobLockService to prevent concurrent processing - Job history via JobHistoryService - Scheduled cleanup of old jobs (2 AM daily) - Stuck job recovery (hourly check for jobs stuck >2 hours) - Cancel pending/in-progress jobs - List jobs with filters (dataset, status) - API documentation via Swagger decorators SECURITY: - @roles('admin', 'superadmin') guard on all endpoints - JWT authentication required via JwtAuthGuard - Admin audit logging for all rebuild actions DATA PROCESSING INTEGRATION: - Python rebuild_routes.py provides KPI snapshot rebuild - /api/rebuild/kpi-snapshots triggers KPI recomputation from raw events - /api/rebuild/all triggers full rebuild of all datasets - Returns rebuild results with item counts and details
…inting and type safety improvements
This commit finalizes the read-model rebuild implementation with comprehensive
code quality improvements, type safety enhancements, and bug fixes across the
backend and data-processing services.
CHANGES (Backend):
- app.module.ts: Registered ReadModelRebuildModule
- read-model-rebuild/: Various improvements:
* Rebuild controller: Replaced hardcoded role strings with UserRole enum
* Rebuild service: Added proper type safety with isAxiosError guard,
improved error handling with getErrorDetails(), and fixed lock service
method calls (tryAcquire/release)
* Rebuild scheduler: Improved error handling with proper type guards
* DTOs: Removed unused imports, fixed formatting, added proper type safety
* Entity: Added IsNull() for contract_id queries
* Migration: Formatted enum arrays for better readability
- Various files: Fixed union type formatting (horizon-client, webhook, portfolio,
soroban-event guards, latency-budget health)
CHANGES (Data Processing):
- requirements.txt: Added python-jose[cryptography] for JWT support,
pinned httpx version for stability
- account_operation_ingestor.py:
* Replaced non-existent RateLimitExceededError with status code check (429)
* Removed unused imports and variables
* Fixed cursor reset logic
- kpi_computer.py:
* Improved exception handling with specific InvalidOperation
* Removed unused imports
* Fixed operation detection logic
- rebuild_routes.py: Removed unused datetime import
- models.py: Removed duplicate KPI constants (already defined elsewhere)
- logger.py: Added Union type for level parameter, improved type safety
- tests: Fixed test account address length, added proper setUp with context
managers for dependency patching
CODE QUALITY IMPROVEMENTS:
- ESLint compliance: Union types formatted correctly, consistent spacing
- Type safety: Added proper type guards for error handling
- Import optimization: Removed unused imports across all files
- Error handling: Standardized error extraction with getErrorDetails()
- Null safety: Used IsNull() for nullable query conditions
BUG FIXES:
- Fixed non-existent RateLimitExceededError import in account_operation_ingestor
- Fixed lock service method calls (tryAcquire/release instead of acquireLock/releaseLock)
- Fixed test account address length validation
- Fixed KPI constants duplication in models.py
|
@titilayo967 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! 🚀 |
titilayo967
marked this pull request as ready for review
July 30, 2026 15:34
Contributor
Author
|
@Cedarich please my PR is ready for review, approve workflow. |
… setup This commit fixes test compatibility for Python 3.9 by replacing the self.enterContext() pattern (Python 3.11+) with the traditional patch().start()/addCleanup(patcher.stop) pattern. CHANGES: - test_account_operation_ingestor.py: Replaced self.enterContext() calls with patcher.start() and self.addCleanup(patcher.stop) for Python 3.9 compatibility IMPACT: - Tests now run successfully in CI environments using Python 3.9 - No functional changes to test coverage or assertions Related: CI compatibility, Python version support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add read-model rebuild capability for chain-derived datasets, Horizon ingestion for account-level operations, and protocol KPI computation from crowdfund vault events.
Linked Issue
Closes #857
Closes #743
Closes #734
Type of Change
Validation
Documentation
Checklist
feat/,fix/, ordocs/Closes #857
Closes #743
Closes #734