Skip to content

feat: add per-period uptime statistics#3389

Open
gorkem-bwl wants to merge 4 commits intodevelopfrom
feat/per-period-uptime-stats
Open

feat: add per-period uptime statistics#3389
gorkem-bwl wants to merge 4 commits intodevelopfrom
feat/per-period-uptime-stats

Conversation

@gorkem-bwl
Copy link
Contributor

@gorkem-bwl gorkem-bwl commented Mar 6, 2026

Summary

Closes #3364

  • Adds an UptimeStatsByPeriod component on the uptime details page showing uptime percentages for 4 periods (2h, 24h, 7d, 30d)
  • Reuses the existing /monitors/uptime/details/:monitorId endpoint — no new aggregation pipeline added
  • Percentages are color-coded: green (≥99%), yellow (≥95%), red (<95%)
  • Fixes z.email()z.string().email() Zod compatibility issue across all validation files

Test plan

  • Navigate to an uptime monitor's details page and verify the period stats row appears between stat boxes and time range selector
  • Verify all 4 periods (2h, 24h, 7d, 30d) display correct uptime percentages
  • Verify color coding: monitors with high uptime show green, degraded show yellow/red
  • Verify stats refresh automatically (60s interval)
  • Verify server starts without Zod errors

## Changes
- Add GET /monitors/uptime/stats/:monitorId endpoint
- Single MongoDB $facet aggregation computes uptime % for all periods (24h, 7d, 30d, 1y, all) in one query
- Add UptimeStatsByPeriod component displayed between stat boxes and time range selector on monitor detail page
- Color-coded percentages: green (>=99%), yellow (>=95%), red (<95%)
- Fix z.email() -> z.string().email() in shared validation (Zod compatibility)
## Changes
- Replace $facet with single $group using conditional sums for better
  efficiency (single scan instead of 5x)
- Move getColor to MonitorUtils.ts as getUptimeColor for reuse
- Add refreshInterval to SWR call to prevent stale data
- Fix remaining z.email() calls to z.string().email() across validation files

## Benefits
- Aggregation now scans documents once instead of five times
- Reduced memory usage for monitors with large check histories
- Consistent color utility location alongside other threshold functions
Remove the dedicated /monitors/uptime/stats/:monitorId endpoint and
its aggregation pipeline. Instead, reuse the existing details endpoint
by fetching /monitors/uptime/details/:monitorId for each of the 4
time ranges (2h, 24h, 7d, 30d) and displaying the uptimePercentage
from each response.

This avoids adding a new expensive aggregation that scans all checks
and instead leverages the existing time-scoped pipeline.
@gorkem-bwl gorkem-bwl reopened this Mar 6, 2026
- Use StatBox component for uptime period cards (fixes unused 'periods' error in previous push)
- Fix prettier formatting in authValidation.ts
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.

[Feature] - Get per period uptime statistics

2 participants