1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

feat: simplified insights data fetching (#6342)

Simplify insights data fetching - let charts pick data instead of
preparing labels in hook.
This commit is contained in:
Tymoteusz Czech 2024-02-27 09:20:45 +01:00 committed by GitHub
parent 7cebf7b8fe
commit ed44de6c94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 27 deletions

View File

@ -11,7 +11,18 @@ interface IFlagsProjectChartProps {
export const FlagsProjectChart: VFC<IFlagsProjectChartProps> = ({ export const FlagsProjectChart: VFC<IFlagsProjectChartProps> = ({
projectFlagTrends, projectFlagTrends,
}) => { }) => {
const data = useProjectChartData(projectFlagTrends, 'total'); const data = useProjectChartData(projectFlagTrends);
return <LineChart data={data} isLocalTooltip />; return (
<LineChart
data={data}
isLocalTooltip
overrideOptions={{
parsing: {
yAxisKey: 'total',
xAxisKey: 'date',
},
}}
/>
);
}; };

View File

@ -10,8 +10,8 @@ import {
Chart, Chart,
Filler, Filler,
type ChartData, type ChartData,
type ScatterDataPoint,
TooltipModel, TooltipModel,
ChartOptions,
} from 'chart.js'; } from 'chart.js';
import { Line } from 'react-chartjs-2'; import { Line } from 'react-chartjs-2';
import 'chartjs-adapter-date-fns'; import 'chartjs-adapter-date-fns';
@ -223,28 +223,38 @@ const customHighlightPlugin = {
}; };
const LineChartComponent: VFC<{ const LineChartComponent: VFC<{
data: ChartData<'line', (number | ScatterDataPoint | null)[], unknown>; data: ChartData<'line', unknown>;
aspectRatio?: number; aspectRatio?: number;
cover?: ReactNode; cover?: ReactNode;
isLocalTooltip?: boolean; isLocalTooltip?: boolean;
overrideOptions?: ChartOptions<'line'>;
TooltipComponent?: ({ TooltipComponent?: ({
tooltip, tooltip,
}: { tooltip: TooltipState | null }) => ReturnType<VFC>; }: { tooltip: TooltipState | null }) => ReturnType<VFC>;
}> = ({ data, aspectRatio, cover, isLocalTooltip, TooltipComponent }) => { }> = ({
data,
aspectRatio,
cover,
isLocalTooltip,
overrideOptions,
TooltipComponent,
}) => {
const theme = useTheme(); const theme = useTheme();
const { locationSettings } = useLocationSettings(); const { locationSettings } = useLocationSettings();
const [tooltip, setTooltip] = useState<null | TooltipState>(null); const [tooltip, setTooltip] = useState<null | TooltipState>(null);
const options = useMemo( const options = useMemo(
() => () => ({
createOptions( ...createOptions(
theme, theme,
locationSettings, locationSettings,
setTooltip, setTooltip,
Boolean(cover), Boolean(cover),
isLocalTooltip, isLocalTooltip,
), ),
[theme, locationSettings], ...overrideOptions,
}),
[theme, locationSettings, overrideOptions],
); );
return ( return (

View File

@ -74,13 +74,19 @@ const TooltipComponent: VFC<{ tooltip: TooltipState | null }> = ({
export const ProjectHealthChart: VFC<IFlagsProjectChartProps> = ({ export const ProjectHealthChart: VFC<IFlagsProjectChartProps> = ({
projectFlagTrends, projectFlagTrends,
}) => { }) => {
const data = useProjectChartData(projectFlagTrends, 'health'); const data = useProjectChartData(projectFlagTrends);
return ( return (
<LineChart <LineChart
data={data} data={data}
isLocalTooltip isLocalTooltip
TooltipComponent={TooltipComponent} TooltipComponent={TooltipComponent}
overrideOptions={{
parsing: {
yAxisKey: 'health',
xAxisKey: 'date',
},
}}
/> />
); );
}; };

View File

@ -11,7 +11,18 @@ interface IFlagsProjectChartProps {
export const TimeToProductionChart: VFC<IFlagsProjectChartProps> = ({ export const TimeToProductionChart: VFC<IFlagsProjectChartProps> = ({
projectFlagTrends, projectFlagTrends,
}) => { }) => {
const data = useProjectChartData(projectFlagTrends, 'timeToProduction'); const data = useProjectChartData(projectFlagTrends);
return <LineChart data={data} isLocalTooltip />; return (
<LineChart
data={data}
isLocalTooltip
overrideOptions={{
parsing: {
yAxisKey: 'timeToProduction',
xAxisKey: 'date',
},
}}
/>
);
}; };

View File

@ -8,10 +8,7 @@ import { useTheme } from '@mui/material';
type ProjectFlagTrends = ExecutiveSummarySchema['projectFlagTrends']; type ProjectFlagTrends = ExecutiveSummarySchema['projectFlagTrends'];
export const useProjectChartData = ( export const useProjectChartData = (projectFlagTrends: ProjectFlagTrends) => {
projectFlagTrends: ProjectFlagTrends,
field: 'timeToProduction' | 'total' | 'health',
) => {
const theme = useTheme(); const theme = useTheme();
const data = useMemo(() => { const data = useMemo(() => {
@ -30,9 +27,7 @@ export const useProjectChartData = (
const color = getProjectColor(project); const color = getProjectColor(project);
return { return {
label: project, label: project,
data: trends.map((item) => { data: trends,
return item[field] || 0;
}),
borderColor: color, borderColor: color,
backgroundColor: color, backgroundColor: color,
fill: false, fill: false,
@ -40,15 +35,7 @@ export const useProjectChartData = (
}, },
); );
const objectKeys = Object.keys(groupedFlagTrends); return { datasets };
const firstElementTrends = groupedFlagTrends[objectKeys[0]] || [];
const firstElementsDates = firstElementTrends.map((item) => item.date);
return {
labels: firstElementsDates,
datasets,
};
}, [theme, projectFlagTrends]); }, [theme, projectFlagTrends]);
return data; return data;