1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00
unleash.unleash/frontend/src/component/insights/componentsStat/CreationArchiveStats/calculateArchiveRatio.ts
Thomas Heartman 236addbe23
fix: show average ratio for the period of collected data (#10735)
Instead of showing the current ratio of archived to created flags, we
show you the average ratio for the selected period.

Before:
<img width="1153" height="391" alt="image"
src="https://github.com/user-attachments/assets/efe87982-544e-485a-aa4d-04faa7d552fc"
/>

After:
<img width="1143" height="385" alt="image"
src="https://github.com/user-attachments/assets/25f8ee4d-3eaa-4e01-a793-2cc0321a55ed"
/>
2025-10-06 13:05:21 +02:00

38 lines
1.4 KiB
TypeScript

import { calculateRatio } from 'component/insights/calculate-ratio/calculate-ratio';
import type { GroupedDataByProject } from 'component/insights/hooks/useGroupedProjectTrends';
import type { InstanceInsightsSchema } from 'openapi';
export const calculateArchiveRatio = (
groupedCreationArchiveData: GroupedDataByProject<
InstanceInsightsSchema['creationArchiveTrends']
>,
): string => {
const { totalCreated, totalArchived } = Object.values(
groupedCreationArchiveData,
).reduce(
(totalAcc, totalCurr) => {
const { created, archived } = totalCurr.reduce(
(acc, curr) => {
const createdSum = curr.createdFlags
? Object.values(curr.createdFlags).reduce(
(sum, count) => sum + count,
0,
)
: 0;
return {
created: acc.created + createdSum,
archived: acc.archived + (curr.archivedFlags ?? 0),
};
},
{ created: 0, archived: 0 },
);
return {
totalCreated: totalAcc.totalCreated + created,
totalArchived: totalAcc.totalArchived + archived,
};
},
{ totalCreated: 0, totalArchived: 0 },
);
return calculateRatio(totalArchived, totalCreated);
};