'use client' import { useState } from 'react' import { useQuery } from '@tanstack/react-query' import { monitorAPI } from '@/lib/api' import { DashboardLayout } from '@/components/layout/dashboard-layout' import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card' import { Button } from '@/components/ui/button' type TimeRange = '7d' | '30d' | '90d' | 'all' export default function AnalyticsPage() { const [timeRange, setTimeRange] = useState('30d') const { data, isLoading } = useQuery({ queryKey: ['monitors'], queryFn: async () => { const response = await monitorAPI.list() return response.monitors }, }) if (isLoading) { return (

Loading...

) } const monitors = data || [] const totalMonitors = monitors.length const activeMonitors = monitors.filter((m: any) => m.status === 'active').length const errorMonitors = monitors.filter((m: any) => m.status === 'error').length const avgFrequency = totalMonitors > 0 ? Math.round(monitors.reduce((sum: number, m: any) => sum + m.frequency, 0) / totalMonitors) : 0 // Calculate additional stats const pausedMonitors = monitors.filter((m: any) => m.status === 'paused').length const recentChanges = monitors.filter((m: any) => { if (!m.last_change_at) return false const changeDate = new Date(m.last_change_at) const daysAgo = timeRange === '7d' ? 7 : timeRange === '30d' ? 30 : timeRange === '90d' ? 90 : 365 const cutoff = new Date(Date.now() - daysAgo * 24 * 60 * 60 * 1000) return changeDate >= cutoff }).length return ( {/* Time Range Selector */}
{(['7d', '30d', '90d', 'all'] as const).map((range) => ( ))}
{/* Stats Overview */}

Total Monitors

{totalMonitors}

Uptime Rate

{totalMonitors > 0 ? Math.round((activeMonitors / totalMonitors) * 100) : 0}%

Error Rate

{totalMonitors > 0 ? Math.round((errorMonitors / totalMonitors) * 100) : 0}%

Avg. Frequency

{avgFrequency} min

{/* Charts Placeholder */}
Monitor Status Distribution
{activeMonitors} Active
Active ({activeMonitors})
Error ({errorMonitors})
Check Frequency Distribution
{[ { label: '5 min', count: monitors.filter((m: any) => m.frequency === 5).length }, { label: '30 min', count: monitors.filter((m: any) => m.frequency === 30).length }, { label: '1 hour', count: monitors.filter((m: any) => m.frequency === 60).length }, { label: '6 hours', count: monitors.filter((m: any) => m.frequency === 360).length }, { label: '24 hours', count: monitors.filter((m: any) => m.frequency === 1440).length }, ].map((item) => (
{item.label}
0 ? (item.count / totalMonitors) * 100 : 0}%` }} />
{item.count}
))}
) }