Peer Mentor Stats Aggregator
Component Detail
Description
Computes aggregated activity statistics for a peer mentor required by badge criteria evaluation, including total assignment counts, streak lengths, training completions, and organisational thresholds. Queries Supabase efficiently to avoid full-table scans.
peer-mentor-stats-aggregator
Summaries
The Peer Mentor Stats Aggregator ensures that recognition decisions — badges and tier awards — are grounded in accurate, up-to-date activity data. Without reliable aggregation, the credibility of the entire recognition programme is at risk: incorrectly awarded or withheld badges erode trust with both mentors and coordinators. This shared component reduces the cost of building multiple features that all require activity statistics by providing a single, efficient data source. Its query optimisation against Supabase directly protects platform performance and reduces database infrastructure costs as the user base scales.
As a shared component, this aggregator is a dependency for badge evaluation and potentially tier eligibility, making it a high-priority deliverable. Medium complexity is driven by the need for efficient Supabase query design — naive implementations risk full-table scans that would degrade performance at scale. Streak calculation logic in particular requires careful specification, as definitions of 'consecutive activity' vary. Testing must cover edge cases: zero-activity users, streak resets, org-scoped vs global counts.
Because multiple features depend on this, bugs here have wide blast radius; thorough integration tests against representative data volumes are essential before downstream features are unblocked.
This aggregator queries Supabase via badge-repository, consolidating activity counts, streak lengths, training completions, and org-scoped assignment counts into a PeerMentorStats object. Streak calculation requires ordered activity records and must define a clear window (e.g., weekly) to determine consecutive qualifying periods. getAssignmentCountForOrg adds an organisationId filter and must use indexed columns to avoid scans. As a shared component, it is likely called from both the badge evaluation engine and tier eligibility checks — results should be considered for short-lived caching if called repeatedly within a single evaluation cycle.
All query methods should be backend-only and async.
Responsibilities
- Aggregate total activity counts per peer mentor
- Calculate streak lengths for consecutive activity
- Count training and certification completions
- Surface threshold counts for honorar-relevant milestones
Interfaces
getStats(String peerMentorId, String organisationId) -> PeerMentorStats
getActivityCount(String peerMentorId) -> int
getCurrentStreak(String peerMentorId) -> int
getTrainingCompletionCount(String peerMentorId) -> int
getAssignmentCountForOrg(String peerMentorId, String organisationId) -> int
Relationships
Related Data Entities (7)
Data entities managed by this component