From aa8a8c84f69d67b86042c561151571391dcb662d Mon Sep 17 00:00:00 2001 From: Jos Ahrens Date: Wed, 11 Dec 2024 13:32:18 +0100 Subject: [PATCH] fix: division by zero in language stats Signed-off-by: Jos Ahrens --- workspaces/copilot/.changeset/six-years-serve.md | 5 +++++ workspaces/copilot/plugins/copilot/src/utils/index.ts | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 workspaces/copilot/.changeset/six-years-serve.md diff --git a/workspaces/copilot/.changeset/six-years-serve.md b/workspaces/copilot/.changeset/six-years-serve.md new file mode 100644 index 0000000000..4a0a3e1952 --- /dev/null +++ b/workspaces/copilot/.changeset/six-years-serve.md @@ -0,0 +1,5 @@ +--- +'@backstage-community/plugin-copilot': minor +--- + +Fix division by zero in language stats diff --git a/workspaces/copilot/plugins/copilot/src/utils/index.ts b/workspaces/copilot/plugins/copilot/src/utils/index.ts index 8520b5ea61..3e3b10089f 100644 --- a/workspaces/copilot/plugins/copilot/src/utils/index.ts +++ b/workspaces/copilot/plugins/copilot/src/utils/index.ts @@ -45,14 +45,19 @@ export function getLanguageStats(metricsArray: Metric[]): LanguageStats[] { if (existingStats) { existingStats.totalSuggestions += item.suggestions_count; existingStats.totalAcceptances += item.acceptances_count; - existingStats.acceptanceRate = - existingStats.totalAcceptances / existingStats.totalSuggestions; + if (existingStats.totalSuggestions > 0) { + existingStats.acceptanceRate = + existingStats.totalAcceptances / existingStats.totalSuggestions; + } } else { languageStatsMap.set(item.language, { language: item.language, totalSuggestions: item.suggestions_count, totalAcceptances: item.acceptances_count, - acceptanceRate: item.acceptances_count / item.suggestions_count, + acceptanceRate: + item.suggestions_count > 0 + ? item.acceptances_count / item.suggestions_count + : 0, }); } });