feat(rest-api-application): migrate stats context to contract-first architecture - #1954
Merged
josecelano merged 6 commits intoJun 27, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Migrates the REST API stats context to the contract-first layered architecture (protocol → application → runtime-adapter → axum transport), moving aggregation out of rest-api-core into a dedicated runtime adapter and rewiring Axum handlers to call a single application-layer use-case service.
Changes:
- Added
Stats/LabeledStatsDTOs torest-api-protocoland introducedtorrust-metricsforMetricCollection. - Added
StatsQueryPort+StatsApiServiceinrest-api-application, and implementedTrackerStatsAdapterinrest-api-runtime-adapterto own aggregation. - Rewired Axum stats/metrics handlers to use
Arc<StatsApiService>state; updated dependency-layer deny rules and supporting docs.
Reviewed changes
Copilot reviewed 23 out of 24 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| project-words.txt | Adds new dictionary words and reorders entries to keep cspell dictionary tidy. |
| packages/rest-api-runtime-adapter/src/adapters/stats.rs | Introduces TrackerStatsAdapter implementing StatsQueryPort and performing stats aggregation/metric merges. |
| packages/rest-api-runtime-adapter/src/adapters/mod.rs | Exposes the new stats adapter module. |
| packages/rest-api-runtime-adapter/Cargo.toml | Adds runtime-adapter dependencies required for stats aggregation (http-core, udp-*, swarm-*, tokio, torrust-metrics). |
| packages/rest-api-protocol/src/v1/context/stats/resources/stats.rs | Defines contract DTOs for /api/v1/stats and /api/v1/metrics. |
| packages/rest-api-protocol/src/v1/context/stats/resources/mod.rs | Registers the stats resources module. |
| packages/rest-api-protocol/src/v1/context/stats/mod.rs | Adds the stats context module in the protocol layer. |
| packages/rest-api-protocol/src/v1/context/mod.rs | Registers the new stats context. |
| packages/rest-api-protocol/Cargo.toml | Adds torrust-metrics dependency to support MetricCollection in protocol DTOs. |
| packages/rest-api-application/src/use_cases/stats.rs | Adds the StatsApiService application use-case that delegates to the port. |
| packages/rest-api-application/src/use_cases/mod.rs | Registers the new stats use-case module. |
| packages/rest-api-application/src/ports/stats.rs | Introduces StatsQueryPort defining the application boundary for stats queries. |
| packages/rest-api-application/src/ports/mod.rs | Registers the new stats port module. |
| packages/axum-rest-api-server/tests/server/v1/contract/context/stats.rs | Updates tests to import Stats from rest-api-protocol (contract-first). |
| packages/axum-rest-api-server/tests/server/v1/asserts.rs | Updates asserts to use protocol Stats type. |
| packages/axum-rest-api-server/src/v1/routes.rs | Wires TrackerStatsAdapter + StatsApiService and injects into stats routes. |
| packages/axum-rest-api-server/src/v1/context/stats/routes.rs | Switches stats routing state from tuple repositories to Arc<StatsApiService>. |
| packages/axum-rest-api-server/src/v1/context/stats/responses.rs | Keeps Prometheus serialization in Axum, now using protocol DTOs directly. |
| packages/axum-rest-api-server/src/v1/context/stats/resources.rs | Removes Axum-local stats DTOs (now owned by protocol). |
| packages/axum-rest-api-server/src/v1/context/stats/mod.rs | Removes resources module export (DTOs moved to protocol). |
| packages/axum-rest-api-server/src/v1/context/stats/handlers.rs | Refactors handlers to call StatsApiService instead of rest-api-core aggregation. |
| docs/issues/open/1942-1938-si-4-migrate-stats-context.md | Updates SI-4 spec with applied aggregation strategy and task statuses. |
| deny.toml | Allows rest-api-runtime-adapter to wrap udp-server, http-core, and udp-core per layer rules. |
| Cargo.lock | Locks new dependency graph including torrust-metrics and tokio for runtime-adapter. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9 tasks
…rchitecture - Add Stats (28-field) and LabeledStats DTOs to rest-api-protocol - Add torrust-metrics dependency to rest-api-protocol for MetricCollection - Define StatsQueryPort trait in rest-api-application - Implement StatsApiService use-case in rest-api-application - Implement TrackerStatsAdapter in rest-api-runtime-adapter (moved aggregation logic from rest-api-core - Option 3) - Rewire Axum handlers to dispatch through StatsApiService - Remove 7+ tuple state wiring from handlers and routes - Keep Prometheus serialization in Axum responses.rs (Option A) - Update issue spec with Option 3 documentation - Update deny.toml with new adapter dependency edges
…s adapter - Remove dead code in get_labeled_stats (unused prefetch lines) - Remove unnecessary #[allow(deprecated)] directive - Remove unused ban_service field and import - Fix inconsistent expect panic messages (remove 'msg: ' prefix) - Remove unused tokio dependency from adapter Cargo.toml - Fix duplicate deps in adapter Cargo.toml - Fix clippy struct_field_names lint on adapter - Update spec verification checklist to reflect pre-commit status
josecelano
force-pushed
the
1942-migrate-stats-context
branch
from
June 26, 2026 21:07
eb45e77 to
791e3f9
Compare
- Added new ClippyFixer agent to automate clippy warning fixes - Added fix-clippy-warnings skill with proper clippy handling guidelines - Updated run-linters skill to reference the new clippy fix skill - All changes follow repository rules about preferring fixes over allowances - All clippy warnings in SI-4 were properly fixed with as_chunks instead of #[allow]
Member
Author
|
ACK bb10343 |
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.
Description
Implements SI-4: Migrate
statscontext to contract-first architecture (sub-issue of #1938).Changes
rest-api-protocol: Addedv1::context::stats/withStats(28 fields) andLabeledStatsDTOs. Addedtorrust-metricsdep forMetricCollection.rest-api-application: DefinedStatsQueryPorttrait andStatsApiServiceuse-case.rest-api-runtime-adapter: ImplementedTrackerStatsAdapter— aggregation logic moved here fromrest-api-core(Option 3). Added deps:http-core,udp-core,udp-server,swarm-coordination-registry.axum-rest-api-server: Rewired handlers to singleArc<StatsApiService>instead of 7-element tuple state. Prometheus serialization stays in Axum (Option A).deny.toml: Updated wrapper lists forudp-server,http-core,udp-coreto allow the adapter.Architecture flow
Aggregation Strategy
Three options were considered (see spec for details). Option 3 (applied): move aggregation from
rest-api-coreinto the adapter, advancing SI-5's deprecation goal.Verification
StatsandLabeledStatsDTOs defined in protocolStatsQueryPorttrait defined in applicationStatsApiServiceuse-case implementedTrackerStatsAdapterimplemented (aggregation moved from rest-api-core)