// src/components/ui/AgentTelemetryTab.tsx
"use client";
import { useTimeline } from "@/lib/api-with-context";
import { Loader2 } from "lucide-react";
import {
ResponsiveContainer,
AreaChart,
Area,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend
} from "recharts";
interface Props {
agentUuid: string;
}
export default function AgentTelemetryTab({ agentUuid }: Props) {
const { data, isLoading } = useTimeline(60, agentUuid);
if (isLoading) {
return (
);
}
if (!data || data.length === 0) {
return (
No telemetry data recorded for this agent in the last 24 hours.
);
}
// Format time labels for XAxis
const chartData = data.map(pt => {
const date = new Date(pt.fetched_at);
const dateStr = date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit' });
const timeStr = date.toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit' });
const xAxisLabel = `${dateStr} ${timeStr}`;
const rawFullLabel = date.toLocaleString('en-GB', {
day: '2-digit',
month: 'long',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false
});
const fullLabel = rawFullLabel.replace(' at ', ', ') + ' WIB';
return {
...pt,
xAxisLabel,
fullLabel,
flows: pt.total_flows ?? 0,
peak: pt.peak_flow_rate ?? 0,
drops: pt.packet_drops ?? 0,
cpu: pt.cpu_usage ?? 0,
memory: pt.memory_usage ?? 0,
queue: pt.queue_depth ?? 0
};
});
return (
{/* Flow Rates Chart */}
Flow Rates & Telemetry Peak
{
const matched = chartData.find(item => item.xAxisLabel === value);
return matched ? matched.fullLabel : value;
}}
contentStyle={{ backgroundColor: '#0f172a', borderColor: '#334155', borderRadius: '12px' }}
labelStyle={{ color: '#94a3b8', fontWeight: 'bold' }}
/>
{/* Packet Drops Chart */}
Packet Drop Rate Trend
{
const matched = chartData.find(item => item.xAxisLabel === value);
return matched ? matched.fullLabel : value;
}}
contentStyle={{ backgroundColor: '#0f172a', borderColor: '#334155', borderRadius: '12px' }}
labelStyle={{ color: '#94a3b8', fontWeight: 'bold' }}
/>
{/* Agent Resource Telemetry (CPU & Memory) */}
Agent Resource Telemetry (CPU & RAM)
{
const matched = chartData.find(item => item.xAxisLabel === value);
return matched ? matched.fullLabel : value;
}}
contentStyle={{ backgroundColor: '#0f172a', borderColor: '#334155', borderRadius: '12px' }}
labelStyle={{ color: '#94a3b8', fontWeight: 'bold' }}
/>
{/* Network Queue Depth */}
Network Packet Queue Depth Trend
{
const matched = chartData.find(item => item.xAxisLabel === value);
return matched ? matched.fullLabel : value;
}}
contentStyle={{ backgroundColor: '#0f172a', borderColor: '#334155', borderRadius: '12px' }}
labelStyle={{ color: '#94a3b8', fontWeight: 'bold' }}
/>
);
}