Skip to content

Commit 882894c

Browse files
committed
refactor: update ErrorRateChart to handle missing data and improve rate calculation logic
1 parent dc6de35 commit 882894c

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/client/components/aiGateway/AIGatewayAnalytics.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -690,18 +690,26 @@ const ErrorRateChart: React.FC<ErrorRateChartProps> = React.memo((props) => {
690690
});
691691

692692
const arr = Array.from(dateMap.entries()).map(
693-
([date, { success, failed }]) => {
694-
const total = success + failed;
695-
const rate = total > 0 ? (failed / total) * 100 : 0;
696-
return { date, value: Math.max(0, Math.min(100, rate)) };
697-
}
693+
([date, { success, failed }]) => ({ date, success, failed })
698694
);
699695

700696
if (arr.length === 0) {
701697
return [];
702698
}
703699

704-
return getDateArray(arr, time.startAt, time.endAt, time.unit);
700+
// Fill missing date buckets with success=0/failed=0,
701+
// then compute rate. Buckets with no traffic at all (total === 0)
702+
// are emitted as null so the line is left blank instead of pinned to 0.
703+
return getDateArray(arr, time.startAt, time.endAt, time.unit).map(
704+
({ date, success, failed }) => {
705+
const total = success + failed;
706+
if (total === 0) {
707+
return { date, value: null as unknown as number };
708+
}
709+
const rate = (failed / total) * 100;
710+
return { date, value: Math.max(0, Math.min(100, rate)) };
711+
}
712+
);
705713
}, [rawData, time.startAt, time.endAt, time.unit]);
706714

707715
const chartConfig: ChartConfig = useMemo(

src/client/components/chart/TimeEventChart.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ export const TimeEventChart: React.FC<TimeEventChartProps> = React.memo(
8686
Object.keys(chartConfig)
8787
);
8888

89+
// Use unique gradient id per chart instance to avoid SVG id collisions
90+
// when multiple charts coexist on the same page (e.g. all using `value` key).
91+
const reactId = React.useId();
92+
const gradientIdPrefix = `gradient-${reactId.replace(/:/g, '')}`;
93+
8994
const stacked = chartType === 'stack';
9095

9196
// Render pie chart
@@ -137,7 +142,7 @@ export const TimeEventChart: React.FC<TimeEventChartProps> = React.memo(
137142
return (
138143
<linearGradient
139144
key={key}
140-
id={`color-${key}`}
145+
id={`${gradientIdPrefix}-${key}`}
141146
x1="0"
142147
y1="0"
143148
x2="0"
@@ -258,7 +263,9 @@ export const TimeEventChart: React.FC<TimeEventChartProps> = React.memo(
258263
stackId={stacked ? '1' : undefined}
259264
stroke={color}
260265
fillOpacity={chartType === 'line' ? 0 : 1}
261-
fill={drawGradientArea ? `url(#color-${key})` : color}
266+
fill={
267+
drawGradientArea ? `url(#${gradientIdPrefix}-${key})` : color
268+
}
262269
strokeWidth={2}
263270
strokeDasharray={getStrokeDasharray(key)}
264271
onAnimationEnd={handleAnimationEnd}

0 commit comments

Comments
 (0)