import {
AreaChart,
Area,
LineChart,
Line,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
ResponsiveContainer,
} from 'recharts';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { format } from 'date-fns';
import { formatCurrency, formatNumber } from './chart-utils';
interface TimeSeriesDataPoint {
timestamp: string;
[key: string]: string | number;
}
interface TimeSeriesChartProps {
data: TimeSeriesDataPoint[];
series: Array<{
key: string;
name: string;
color: string;
type?: 'line' | 'area';
}>;
title?: string;
description?: string;
yAxisFormatter?: (value: number) => string;
chartType?: 'line' | 'area';
}
// Format timestamp for display
function formatXAxisLabel(timestamp: string): string {
try {
const date = new Date(timestamp);
return format(date, 'MMM dd HH:mm');
} catch {
return timestamp;
}
}
// Tooltip component defined outside main component
interface TimeTooltipProps {
active?: boolean;
payload?: Array<{ name: string; value: number; color: string }>;
label?: string;
yAxisFormatter?: (value: number) => string;
}
function TimeTooltip({ active, payload, label, yAxisFormatter }: TimeTooltipProps) {
if (active && payload && payload.length && yAxisFormatter) {
return (
{label ? formatXAxisLabel(label) : ''}
{payload.map((entry: { name: string; value: number; color: string }) => (
{entry.name}: {yAxisFormatter(entry.value)}
))}
);
}
return null;
}
export function TimeSeriesChart({
data,
series,
title = 'Metrics Over Time',
description,
yAxisFormatter = formatNumber,
chartType = 'area',
}: TimeSeriesChartProps) {
const formatXAxis = (timestamp: string) => formatXAxisLabel(timestamp);
const ChartComponent = chartType === 'area' ? AreaChart : LineChart;
return (
{title}
{description && (
{description}
)}
{series.map((s) => (
))}
} />
{series.map((s) =>
chartType === 'area' ? (
) : (
)
)}
);
}
// Pre-configured chart for cost metrics
export function CostTimeSeriesChart({
data,
title = 'Cost Over Time',
description = 'Cumulative costs by service',
}: {
data: TimeSeriesDataPoint[];
title?: string;
description?: string;
}) {
const series = [
{ key: 'sqs_cost', name: 'SQS', color: '#FF9900', type: 'area' as const },
{ key: 'lambda_cost', name: 'Lambda', color: '#F97316', type: 'area' as const },
{ key: 'bedrock_cost', name: 'Bedrock', color: '#8B5CF6', type: 'area' as const },
];
return (
);
}
// Pre-configured chart for request metrics
export function RequestTimeSeriesChart({
data,
title = 'Requests Over Time',
description = 'Request volume trends',
}: {
data: TimeSeriesDataPoint[];
title?: string;
description?: string;
}) {
const series = [
{ key: 'requests', name: 'Requests', color: '#3B82F6', type: 'line' as const },
{ key: 'errors', name: 'Errors', color: '#EF4444', type: 'line' as const },
];
return (
);
}