Skip to content

[FEATURE] Analytics: Custom metrics and KPI definition engine (#74) #413

Description

@KevinMB0220

Description

Roadmap Phase 4 → Analytics & Monitoring → #74 Create custom metrics and KPIs has no tracking issue. It is the remaining gap in a section whose other three items have shipped: #339 (analytics dashboard, roadmap #71), #340 (real-time performance monitoring, #72) and #341 (configurable alerting, #73).

Those three cover fixed, developer-defined metrics. #74 is about letting an operator define a metric without a code change — pick a source, an aggregation, a window and a dimension, name it, and have it appear in the dashboard and be alertable.

Background

What exists today:

  • packages/api/rest/src/services/monitoring/transaction-monitoring.service.ts, monitoring-alert.service.ts, alert-evaluator.ts, transaction-rule-engine.ts, and a channel dispatcher with webhook and notification channels
  • packages/api/rest/src/routes/monitoring/transactions.ts and alerts.ts

alert-evaluator.ts and transaction-rule-engine.ts already evaluate conditions over transaction data, which is the closest existing analogue and the natural thing to generalize rather than duplicate.

What is missing is the definition layer: every metric is currently hardcoded, so adding one means editing a service, opening a PR and redeploying.

Requirements

Metric definition model

  • A metric_definitions table and a typed model: id, userId/orgId, name, description, source, aggregation, window, dimensions, filters, unit, enabled
  • Sources drawn from what the DevKit already records: transactions, wallet events, DeFi positions, oracle prices, automation executions, API request logs
  • Aggregations: count, sum, avg, min, max, p50/p95/p99, rate, distinct_count
  • Rolling windows (5m, 1h, 24h, 7d, 30d) and calendar windows (day/week/month to date)
  • Dimensions/group-by: asset, protocol, network, wallet, user, team, endpoint
  • Derived metrics — a definition may reference other definitions in an arithmetic expression (e.g. failed_tx / total_tx). Detect and reject cycles

Evaluation

  • A MetricsService that compiles a definition into a parameterized Supabase query — never string-concatenated SQL
  • Reuse the caching layer from [FEATURE] Core: Advanced caching strategies with TTL and invalidation (#77) #343 with a TTL derived from the window; a 30d metric must not be recomputed per request
  • Scheduled materialization for expensive definitions, with on-demand evaluation for cheap ones
  • Backfill: computing a newly created definition over existing history

KPI targets

  • Attach a target/threshold and direction (higher-is-better / lower-is-better) to a definition
  • Expose current value, target, delta vs. previous period, and trend

API

  • POST/GET/PATCH/DELETE /api/v1/metrics/definitions — full CRUD, scoped to the owner
  • GET /api/v1/metrics/:id/value — current value, optional dimension filters
  • GET /api/v1/metrics/:id/series — time series for charting, with explicit bucket size
  • POST /api/v1/metrics/preview — evaluate an unsaved definition, so the UI can validate before saving

Integration

Guardrails

Acceptance Criteria

  • An operator can define "average swap size in USD, per protocol, 24h rolling" through the API with no code change, and read its value
  • A derived metric referencing two other definitions evaluates correctly; a cyclic definition is rejected at save time
  • An alert rule can target a custom metric and fires through the existing channels
  • Repeated reads inside the TTL are served from cache
  • A definition with an unsupported source or aggregation is rejected with a clear validation error
  • A user cannot read or modify another user's definitions
  • preview evaluates without persisting
  • Tests cover each aggregation, each window type, dimension grouping and cycle detection

References

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

    APIsDificulty: HardDripsIssue tracked in the Drips campaignStellar WaveIssues in the Stellar wave programcoreenhancementNew feature or requestphase-4Phase 4: Enterprise & Scale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions