aboutsummaryrefslogtreecommitdiff
path: root/internal/storage/sqlite.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/storage/sqlite.go')
-rw-r--r--internal/storage/sqlite.go37
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