-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
useAvgTimeToProduction.ts
48 lines (40 loc) · 1.54 KB
/
useAvgTimeToProduction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { useMemo } from 'react';
import type {
ExecutiveSummarySchema,
ExecutiveSummarySchemaProjectFlagTrendsItem,
} from 'openapi';
import type { GroupedDataByProject } from './useGroupedProjectTrends';
const validTrend = (trend: ExecutiveSummarySchemaProjectFlagTrendsItem) =>
Boolean(trend) && Boolean(trend.timeToProduction);
export const useAvgTimeToProduction = (
projectsData: GroupedDataByProject<
ExecutiveSummarySchema['projectFlagTrends']
>,
) =>
useMemo(() => {
let totalProjects = Object.keys(projectsData).length;
if (totalProjects === 0) {
return 0;
}
const totalAvgTimeToProduction = Object.entries(projectsData).reduce(
(acc, [_, trends]) => {
const latestTrend = trends.reduce(
(latest, current) =>
new Date(latest.date) < new Date(current.date)
? current
: latest,
trends[0],
);
// If there's no valid latest trend, this project won't contribute to the average
if (!validTrend(latestTrend)) {
totalProjects--;
return acc;
}
const timeToProduction = latestTrend.timeToProduction || 0;
return acc + timeToProduction;
},
0,
);
const overallAverage = totalAvgTimeToProduction / totalProjects;
return overallAverage;
}, [projectsData]);