diff options
| author | Fuwn <[email protected]> | 2026-01-20 16:49:50 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-20 16:49:50 -0800 |
| commit | 31cc0a33a378dbc8cb0e3c70b40d6570f3bed2e4 (patch) | |
| tree | 6de6f3354af8095be83740b3b24ca1490892a999 /internal/storage/sqlite.go | |
| parent | perf: Optimize API refresh to single /api/page request (diff) | |
| download | kaze-31cc0a33a378dbc8cb0e3c70b40d6570f3bed2e4.tar.xz kaze-31cc0a33a378dbc8cb0e3c70b40d6570f3bed2e4.zip | |
feat: Add new API endpoints (health, summary, uptime, incidents)
New endpoints:
- GET /api/health - Simple health check, always public (for load balancers)
- GET /api/summary - Lightweight status overview (counts + overall status)
- GET /api/uptime/{name}?period=1h|24h|7d|30d|90d - Historical uptime stats
- GET /api/incidents?filter=all|active|resolved|scheduled - List incidents
All new endpoints (except /api/health) follow api.access rules.
Diffstat (limited to 'internal/storage/sqlite.go')
| -rw-r--r-- | internal/storage/sqlite.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/storage/sqlite.go b/internal/storage/sqlite.go index e441fe7..3d89cb1 100644 --- a/internal/storage/sqlite.go +++ b/internal/storage/sqlite.go @@ -496,6 +496,43 @@ func (s *Storage) GetUptimeHistory(ctx context.Context, monitorName string, days return result, nil } +// UptimeStats contains uptime statistics for a period +type UptimeStats struct { + UptimePercent float64 + TotalChecks int64 + SuccessChecks int64 + FailedChecks int64 +} + +// GetUptimeStats returns uptime statistics for a monitor over a given duration +func (s *Storage) GetUptimeStats(ctx context.Context, monitorName string, duration time.Duration) (*UptimeStats, error) { + cutoff := time.Now().Add(-duration) + + var totalChecks, successChecks int64 + err := s.db.QueryRowContext(ctx, ` + SELECT + COUNT(*) as total, + SUM(CASE WHEN status = 'up' THEN 1 ELSE 0 END) as success + FROM check_results + WHERE monitor_name = ? AND timestamp >= ? + `, monitorName, cutoff).Scan(&totalChecks, &successChecks) + if err != nil { + return nil, fmt.Errorf("failed to query uptime stats: %w", err) + } + + stats := &UptimeStats{ + TotalChecks: totalChecks, + SuccessChecks: successChecks, + FailedChecks: totalChecks - successChecks, + } + + if totalChecks > 0 { + stats.UptimePercent = float64(successChecks) / float64(totalChecks) * 100 + } + + return stats, nil +} + // PingResult represents a single ping for the history display type PingResult struct { Status string // up, down, degraded |