import {useMemo, useState} from 'react'
import {Activity, RefreshCw} from 'lucide-react'
import {Link, Navigate, useParams} from 'react-router-dom'
import {useCloudDiagnosticsQuery} from '@/api/queries/cloudQueries'
import {formatDateTime, formatRelativeTime} from '@/lib/format'
import type {CloudProvider, CloudServiceStatus, EmulatedServiceStatus, RuntimeDiagnostics} from '@/types/cloud'
type ServiceFilter = 'all' | 'not-running' | 'with-page' | 'without-page'
const SERVICE_FILTERS: Array<{id: ServiceFilter; label: string}> = [
{id: 'all', label: 'All'},
{id: 'not-running', label: 'Not running'},
{id: 'with-page', label: 'Has console page'},
{id: 'without-page', label: 'No console page'},
]
export function DiagnosticsPage() {
const params = useParams()
const cloud = normalizeCloud(params.cloud)
const query = useCloudDiagnosticsQuery(cloud ?? 'aws')
if (!cloud) return
const report = query.data
return (
<>
Diagnostics
{report ? `Checked ${formatRelativeTime(report.checkedAt) ?? 'just now'}` : 'Runtime, services and console health'}
{query.isLoading &&
Running diagnostics
}
{query.error &&
Could not load diagnostics: {query.error.message}
}
{report &&
}
>
)
}
function DiagnosticsReport({report}: {report: RuntimeDiagnostics}) {
const emulated = report.emulatedServices
const notRunning = emulated?.filter((service) => service.state !== 'running').length ?? 0
const covered = emulated?.filter((service) => service.consolePages.length > 0).length ?? 0
const reachable = report.runtime.status === 'reachable'
return (
<>
Runtime
{reachable ? 'Reachable' : 'Unavailable'}
{report.runtime.endpoint ?? 'No endpoint'}{report.runtime.latencyMs !== null ? ` · ${report.runtime.latencyMs} ms` : ''}
Version
{report.runtime.version ?? 'Unknown'}
{report.runtime.edition ? `${report.runtime.edition} edition` : 'Not reported by the runtime'}
0 ? 'pending' : ''}`}>
Emulated services
{emulated ? emulated.length : '—'}
{emulated ? (notRunning > 0 ? `${notRunning} not running` : 'All running') : 'Not reported by the runtime'}
Console coverage
{emulated ? `${covered} / ${emulated.length}` : `${report.consoleServices.length}`}
{emulated ? 'Emulated services with a console page' : 'Console services'}
{report.runtime.error && {report.runtime.error}
}
{emulated && }
>
)
}
function ConsoleServicesTable({services}: {services: CloudServiceStatus[]}) {
const [showResponding, setShowResponding] = useState(false)
const failing = services.filter((service) => service.runtime !== 'reachable')
const responding = services.filter((service) => service.runtime === 'reachable')
const ordered = showResponding ? [...failing, ...responding] : failing
return (
{services.length === 0 && No console services are registered for this cloud.
}
{ordered.length > 0 &&
| Service | Status | Latency | Detail |
{ordered.map((service) => (
{service.service} |
{statusLabel(service.runtime)} |
{service.latencyMs === null ? '—' : `${service.latencyMs} ms`} |
{service.error ? {service.error}{service.errorCode ? ` (${service.errorCode})` : ''} : —} |
))}
}
)
}
function EmulatedServices({services}: {services: EmulatedServiceStatus[]}) {
const [filter, setFilter] = useState('all')
const [search, setSearch] = useState('')
const visible = useMemo(() => {
const term = search.trim().toLowerCase()
return services.filter((service) => {
if (term && !service.name.includes(term) && !service.consolePages.some((page) => page.displayName.toLowerCase().includes(term))) return false
if (filter === 'not-running') return service.state !== 'running'
if (filter === 'with-page') return service.consolePages.length > 0
if (filter === 'without-page') return service.consolePages.length === 0
return true
})
}, [services, filter, search])
return (
{visible.length === 0
? No emulated services match this filter.
: {visible.map((service) => (
-
{service.name}
{service.state !== 'running' && {service.state}}
{service.consolePages.map((page) => {page.displayName})}
))}
}
)
}
function ContainersTable({report}: {report: RuntimeDiagnostics}) {
const containers = report.containers
return (
Runtime containers
Containers started for EC2 instances
{containers === null && This runtime does not publish container diagnostics.
}
{containers?.length === 0 && No EC2 instance containers are running.
}
{containers && containers.length > 0 &&
| Instance | Container | SSH |
{containers.map((container) => (
{container.resourceId} |
{container.status ?? '—'} |
{container.sshPort ? {container.sshHost ?? 'localhost'}:{container.sshPort} : '—'} |
))}
}
)
}
function ConsoleProcess({report}: {report: RuntimeDiagnostics}) {
return (
- Uptime
- {formatUptime(report.console.uptimeSeconds)}
- Started
- {formatDateTime(report.console.startedAt) ?? report.console.startedAt}
- Sign-in
- {report.console.authMode === 'oidc' ? 'OIDC single sign-on' : 'Local administrator'}
- Active sessions
- {report.console.activeSessions}
)
}
function formatUptime(totalSeconds: number): string {
const days = Math.floor(totalSeconds / 86_400)
const hours = Math.floor((totalSeconds % 86_400) / 3_600)
const minutes = Math.floor((totalSeconds % 3_600) / 60)
if (days > 0) return `${days} d ${hours} h`
if (hours > 0) return `${hours} h ${minutes} min`
if (minutes > 0) return `${minutes} min`
return `${totalSeconds} s`
}
function statusClass(runtime: CloudServiceStatus['runtime']): string {
if (runtime === 'reachable') return 'healthy'
if (runtime === 'coming_soon') return 'degraded'
return 'unavailable'
}
function statusLabel(runtime: CloudServiceStatus['runtime']): string {
if (runtime === 'reachable') return 'Reachable'
if (runtime === 'coming_soon') return 'Not registered'
return 'Unavailable'
}
function normalizeCloud(value?: string): CloudProvider | null {
return value === 'aws' || value === 'azure' || value === 'gcp' ? value : null
}