From 5004bd00aef5080d4905e9a5cb2ef73be0f991a0 Mon Sep 17 00:00:00 2001 From: William Guss Date: Tue, 6 Aug 2024 13:59:31 -0700 Subject: [PATCH] charts --- .../src/components/LMPDetailsSidePanel.js | 152 +++------- ell-studio/src/components/MetricChart.js | 259 ++++++++++++++++++ 2 files changed, 292 insertions(+), 119 deletions(-) create mode 100644 ell-studio/src/components/MetricChart.js diff --git a/ell-studio/src/components/LMPDetailsSidePanel.js b/ell-studio/src/components/LMPDetailsSidePanel.js index 77141730..bc8ace73 100644 --- a/ell-studio/src/components/LMPDetailsSidePanel.js +++ b/ell-studio/src/components/LMPDetailsSidePanel.js @@ -3,10 +3,10 @@ import { Link } from 'react-router-dom'; import { FiClock, FiTag, FiGitCommit, FiZap, FiHash, FiCalendar } from 'react-icons/fi'; import { getTimeAgo } from '../utils/lmpUtils'; import VersionBadge from './VersionBadge'; -import { AreaChart, Area, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid } from 'recharts'; -import { format } from 'date-fns'; +import MetricChart from './MetricChart'; import { useInvocationsFromLMP } from '../hooks/useBackend'; import { LMPCardTitle } from './depgraph/LMPCardTitle'; +import { subDays, format, addDays, endOfMonth } from 'date-fns'; function StatItem({ icon: Icon, label, value }) { return ( @@ -18,68 +18,26 @@ function StatItem({ icon: Icon, label, value }) { } function LMPDetailsSidePanel({ lmp, uses, versionHistory }) { - const [timeScale, setTimeScale] = useState('1h'); - const [movingAverage, setMovingAverage] = useState(1); - const { data: invocations } = useInvocationsFromLMP(lmp.name, lmp.lmp_id); + const { data: invocations } = useInvocationsFromLMP(lmp.name, lmp.lmp_id, 0, 100); const chartData = useMemo(() => { - if (!invocations) return []; - - const now = new Date(); - const timeScaleDays = { - '15m': 15 / (24 * 60), '1h': 1 / 24, '6h': 6 / 24, '1d': 1, '1w': 7, '1m': 30, '3m': 90, '1y': 365 - }[timeScale]; - const startDate = new Date(now.getTime() - timeScaleDays * 24 * 60 * 60 * 1000); - - const filteredInvocations = invocations.filter(inv => new Date(inv.created_at) >= startDate); - - // Group invocations by time intervals - const intervalMs = timeScaleDays * 24 * 60 * 60 * 1000 / 20; // Divide the time range into 20 intervals - const groupedData = filteredInvocations.reduce((acc, inv) => { - const date = new Date(inv.created_at); - const intervalIndex = Math.floor((date - startDate) / intervalMs); - const intervalDate = new Date(startDate.getTime() + intervalIndex * intervalMs); - const key = intervalDate.toISOString(); - acc[key] = (acc[key] || 0) + 1; - return acc; - }, {}); - - const sortedData = Object.entries(groupedData) - .map(([date, count]) => ({ date, count })) + if (!invocations || invocations.length === 0) return []; + + return invocations + .map(inv => ({ + date: new Date(inv.created_at), + count: 1, + latency: inv.latency_ms + })) .sort((a, b) => new Date(a.date) - new Date(b.date)); + }, [invocations]); - // Calculate moving average - return sortedData.map((item, index, array) => { - const start = Math.max(0, index - movingAverage + 1); - const windowSlice = array.slice(start, index + 1); - const avg = windowSlice.reduce((sum, i) => sum + i.count, 0) / windowSlice.length; - return { ...item, movingAvg: avg }; - }); - }, [invocations, timeScale, movingAverage]); - - const formatXAxis = (tickItem) => { - const date = new Date(tickItem); - switch(timeScale) { - case '15m': - case '1h': - case '6h': - return format(date, 'HH:mm'); - case '1d': - return format(date, 'HH:mm'); - case '1w': - return format(date, 'EEE'); - case '1m': - case '3m': - return format(date, 'MMM d'); - case '1y': - return format(date, 'MMM'); - default: - return format(date, 'MMM d'); - } - }; - - const totalInvocations = invocations?.length || 0; - const avgLatency = invocations?.reduce((sum, inv) => sum + inv.latency_ms, 0) / totalInvocations || 0; + const totalInvocations = useMemo(() => invocations?.length || 0, [invocations]); + const avgLatency = useMemo(() => { + if (!invocations || invocations.length === 0) return 0; + const sum = invocations.reduce((acc, inv) => acc + inv.latency_ms, 0); + return sum / invocations.length; + }, [invocations]); return (