Skip to content

feat: implement OHLC aggregation service and fix repository bugs#268

Merged
Sadeequ merged 2 commits into
StellarFlow-Network:mainfrom
nuhumusamagaji:feature/issue-208-ohlc-aggregation-service
Apr 29, 2026
Merged

feat: implement OHLC aggregation service and fix repository bugs#268
Sadeequ merged 2 commits into
StellarFlow-Network:mainfrom
nuhumusamagaji:feature/issue-208-ohlc-aggregation-service

Conversation

@nuhumusamagaji
Copy link
Copy Markdown

This PR implements the Analytics & OHLC Aggregation Service (Issue #208) to provide pre-aggregated time-series data for charting. It also includes a comprehensive stabilization of the repository, fixing several critical bugs related to duplicated code, ESM compatibility, and broken authentication middleware that were preventing the application from building and testing correctly.

🚀 Key Features

1. OHLC Aggregation Service

  • Prisma Schema Update: Added the OhlcCandle model with a composite unique constraint on [currency, granularity, openTime] to ensure data integrity.
  • Background Worker: Implemented PriceAggregatorService, a robust worker that calculates MINUTE, HOUR, and DAY candles from raw PriceHistory ticks.
  • Gap-Filling Strategy: Includes a look-back mechanism (configurable via LOOKBACK_WINDOWS) to capture late-arriving data and ensure no gaps in the charts.
  • Efficient Upserts: Uses atomic upsert operations to handle retries and overlapping runs without duplicating data.

2. Analytics API Endpoints

  • GET /api/v1/analytics/ohlc: Efficiently retrieves aggregated candles with support for currency filtering, granularity selection, and time-range queries.
  • GET /api/v1/analytics/status: Provides real-time visibility into the health and status of the aggregation worker.
  • Swagger Documentation: Full OpenAPI annotations for all new endpoints.

3. Repository Stabilization & Bug Fixes

  • Duplicate Code Resolution: Removed accidental file concatenations in src/routes/marketRates.ts and src/middleware/apiKeyMiddleware.ts.
  • Authentication Overhaul: Unified the apiKeyAuth middleware, resolving 503 Service Unavailable errors by properly integrating with the shared Prisma instance.
  • ESM & Node Compatibility:
    • Fixed __dirname usage in src/utils/winstonLogger.ts for ESM compatibility.
    • Resolved missing exports in src/services/webhook.ts and missing imports in src/services/marketRate/marketRateService.ts.
    • Fixed duplicate Horizon and default export declarations in stellarProvider.ts.
  • Database Alignment: Switched Prisma provider to postgresql to match the application's native type requirements.

🧪 Testing & Verification

  • New Test Suite: Added test/priceAggregator.jest.test.ts covering:
    • Unit logic for aggregation calculations.
    • Integration tests for API endpoints using a mocked server environment.
    • Validation of authentication and scope-based access.
  • Result: All 15/15 tests passing in an ESM-enabled Jest environment.

🛠 How to Test

  1. Prisma: Run npx prisma generate to update the local client.
  2. Database: Ensure DATABASE_URL is set to a PostgreSQL instance.
  3. Run Tests: npm run test:jest (or node --experimental-vm-modules ./node_modules/jest/bin/jest.js test/priceAggregator.jest.test.ts).
  4. API: Verify the endpoints:
    • GET http://localhost:PORT/api/v1/analytics/ohlc?currency=NGN&granularity=HOUR

📦 Dependencies Added/Updated

  • None (Utilized existing @prisma/client, express, and @stellar/stellar-sdk).

Closes: #208


- Added OhlcCandle model to Prisma schema\n- Implemented PriceAggregatorService background worker\n- Added /api/v1/analytics endpoints\n- Fixed numerous bugs (duplications, missing exports, ESM issues)\n- Added comprehensive Jest integration tests
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 29, 2026

@nuhumusamagaji 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

@Sadeequ Sadeequ merged commit 828ecfe into StellarFlow-Network:main Apr 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.

Analytics | Time-Series Data Aggregation Service

3 participants