Skip to content

feat: add comprehensive contract sustainability metrics#452

Merged
Xhristin3 merged 3 commits intorinafcode:mainfrom
Mrchinedum:feat/sustainability-metrics
Apr 28, 2026
Merged

feat: add comprehensive contract sustainability metrics#452
Xhristin3 merged 3 commits intorinafcode:mainfrom
Mrchinedum:feat/sustainability-metrics

Conversation

@Mrchinedum
Copy link
Copy Markdown
Contributor

fixed #390

feat: add comprehensive contract sustainability metrics

Sustainability metrics were not tracked anywhere in the contract.
This change introduces a dedicated module to define KPIs, record
resource usage, monitor efficiency, and report platform health.

What changed

contracts/teachlink/src/sustainability.rs (new)

  • SustainabilityManager with full CRUD over a SustainabilityMetrics
    snapshot stored in contract instance storage
  • record_invocation(storage_write) — increments total_invocations and
    optionally total_storage_writes on every tracked call
  • record_event / record_rewards / record_content_minted /
    record_active_user — granular hooks for each KPI dimension
  • update_efficiency(successful_ops, total_ops) — computes
    efficiency_score in basis points (0-10 000)
  • get_metrics() — returns the full SustainabilityMetrics snapshot
  • health_score() — composite 0-100 score:
    50 % efficiency score
    25 % content creation (capped at 1 000 tokens = 100 %)
    25 % user adoption (capped at 1 000 users = 100 %)
  • Unit tests: record_invocation, update_efficiency, health_score_full,
    health_score_zero_ops

contracts/teachlink/src/types.rs

  • SustainabilityMetrics contracttype struct:
    total_invocations, total_storage_writes, total_events_emitted,
    total_rewards_distributed, total_content_minted,
    total_active_users, efficiency_score, last_updated

contracts/teachlink/src/storage.rs

  • SUSTAINABILITY_METRICS Symbol key ("sust_met")

contracts/teachlink/src/events.rs

  • SustainabilityMetricsUpdatedEvent emitted on invocation recording
    and efficiency updates (fields: total_invocations,
    total_storage_writes, total_events_emitted, efficiency_score,
    updated_at)

contracts/teachlink/src/lib.rs

  • mod sustainability declared
  • SustainabilityMetrics added to pub use types
  • Four public contract entry points:
    get_sustainability_metrics → report metrics
    get_sustainability_health_score → report health score
    update_sustainability_efficiency → monitor efficiency
    record_sustainability_invocation → track resource usage

Acceptance criteria met

✓ Sustainability KPIs defined (efficiency, resource usage, adoption,
content creation, rewards flow)
✓ Resource usage tracked (invocations, storage writes, events emitted)
✓ Efficiency monitored (basis-point score, updated via public entry point)
✓ Metrics reported (snapshot query + composite health score)

- Add SustainabilityMetrics type with KPIs: invocations, storage
  writes, events emitted, rewards distributed, content minted,
  active users, and efficiency score
- Add SUSTAINABILITY_METRICS storage key
- Add SustainabilityMetricsUpdatedEvent
- Add SustainabilityManager with record, query, and health score logic
- Expose 4 public contract entry points in lib.rs
- Include unit tests for core metric tracking and health scoring
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 26, 2026

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

Mrchinedum and others added 2 commits April 26, 2026 16:48
- Add 8 sustainability Prometheus gauges to MetricsService
  (invocations, storage writes, events emitted, rewards distributed,
  content minted, active users, efficiency score, health score)
- Add updateSustainabilityMetrics() to push gauges on each query
- Add getSustainabilitySnapshot() to DashboardService: computes
  real-time KPIs (efficiency, health, dispute rate, reward claim rate)
  and pushes them to Prometheus
- Add GET /analytics/sustainability endpoint in ReportingController
- Add teachlink-sustainability alert group to prometheus/alerts.yml
  with 5 rules: low efficiency, critical efficiency, low health score,
  high error rate, no new transactions
- Create teachlink-monitoring-dashboard.json (691 lines, 20 panels):
  - Row 1: Real-Time Platform Health (6 stat panels)
  - Row 2: Historical Trends (4 time-series panels)
  - Row 3: Alert Management (firing alerts, active count, critical count)
  - Row 4: Platform Insights (cache ratio, latency percentiles,
    dependency health, indexer progress, HTTP status breakdown)
@Xhristin3 Xhristin3 merged commit ebc3e05 into rinafcode:main Apr 28, 2026
1 of 3 checks passed
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 comprehensive contract sustainability metrics

2 participants