mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-24 01:18:01 +02:00
Creates the impressions summary chart <img width="1358" alt="Screenshot 2024-02-21 at 13 25 05" src="https://github.com/Unleash/unleash/assets/104830839/ddf15637-34de-4883-9ef7-517e37eab331"> Closes # [1-2060](https://linear.app/unleash/issue/1-2060/impressions-summary-widget-frontend) --------- Signed-off-by: andreas-unleash <andreas@getunleash.ai> Co-authored-by: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com>
59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { getProjectColor } from './executive-dashboard-utils';
|
|
import { useTheme } from '@mui/material';
|
|
import {
|
|
ExecutiveSummarySchema,
|
|
ExecutiveSummarySchemaMetricsSummaryTrendsItem,
|
|
} from '../../openapi';
|
|
|
|
type MetricsSummaryTrends = ExecutiveSummarySchema['metricsSummaryTrends'];
|
|
|
|
export const useMetricsSummary = (
|
|
metricsSummaryTrends: MetricsSummaryTrends,
|
|
field: 'total' | 'totalYes' | 'totalNo' | 'totalApps',
|
|
) => {
|
|
const theme = useTheme();
|
|
|
|
const data = useMemo(() => {
|
|
const groupedFlagTrends = metricsSummaryTrends.reduce<
|
|
Record<string, ExecutiveSummarySchemaMetricsSummaryTrendsItem[]>
|
|
>((groups, item) => {
|
|
if (!groups[item.project]) {
|
|
groups[item.project] = [];
|
|
}
|
|
groups[item.project].push(item);
|
|
return groups;
|
|
}, {});
|
|
|
|
const datasets = Object.entries(groupedFlagTrends).map(
|
|
([project, metricsSummaryTrends]) => {
|
|
const color = getProjectColor(project);
|
|
return {
|
|
label: project,
|
|
data: metricsSummaryTrends.map((item) => {
|
|
if (field !== 'total') {
|
|
return item[field] || 0;
|
|
}
|
|
return item.totalYes + item.totalNo || 0;
|
|
}),
|
|
borderColor: color,
|
|
backgroundColor: color,
|
|
fill: false,
|
|
};
|
|
},
|
|
);
|
|
|
|
const objectKeys = Object.keys(groupedFlagTrends);
|
|
|
|
const firstElementSummary = groupedFlagTrends[objectKeys[0]] || [];
|
|
const firstElementsDates = firstElementSummary.map((item) => item.date);
|
|
|
|
return {
|
|
labels: firstElementsDates,
|
|
datasets,
|
|
};
|
|
}, [theme, metricsSummaryTrends]);
|
|
|
|
return data;
|
|
};
|