Skip to content

Commit

Permalink
fix: make TTP stat show last week calculation (#6766)
Browse files Browse the repository at this point in the history
Currently the median time to production stat is showing the aggregated
median across all dates.

This pr changes the calculation to only use the last week summary like
the rest of the stat widgets.
<img width="1665" alt="Screenshot 2024-04-03 at 11 25 31"
src="https://github.com/Unleash/unleash/assets/104830839/c6869b48-99bd-4f5b-a25e-7e0e3a2dc9ef">

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
  • Loading branch information
andreas-unleash committed Apr 3, 2024
1 parent fe6aaf7 commit 717e541
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 115 deletions.
5 changes: 2 additions & 3 deletions frontend/src/component/insights/InsightsCharts.tsx
Expand Up @@ -40,8 +40,8 @@ interface IChartsProps {
averageUsers: number;
averageHealth?: string;
flagsPerUser?: string;
medianTimeToProduction?: number;
};
medianTimeToProduction: number;
loading: boolean;
projects: string[];
}
Expand Down Expand Up @@ -71,7 +71,6 @@ export const InsightsCharts: VFC<IChartsProps> = ({
userTrends,
groupedProjectsData,
flagTrends,
medianTimeToProduction,
groupedMetricsData,
environmentTypeTrends,
loading,
Expand Down Expand Up @@ -167,7 +166,7 @@ export const InsightsCharts: VFC<IChartsProps> = ({
</ChartWidget>
<Widget {...chartInfo.medianTimeToProduction}>
<TimeToProduction
daysToProduction={medianTimeToProduction}
daysToProduction={summary.medianTimeToProduction}
/>
</Widget>
<ChartWidget
Expand Down
Expand Up @@ -16,6 +16,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 0,
users: 1,
date: '',
timeToProduction: 4,
},
{
week: '2024-01',
Expand All @@ -26,6 +27,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 0,
users: 2,
date: '',
timeToProduction: 5,
},
{
week: '2024-02',
Expand All @@ -36,6 +38,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 1,
users: 1,
date: '',
timeToProduction: 4,
},
{
week: '2024-02',
Expand All @@ -46,6 +49,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 0,
users: 3,
date: '',
timeToProduction: 2,
},
],
{ total: 1 } as unknown as InstanceInsightsSchemaUsers,
Expand All @@ -60,6 +64,7 @@ describe('useFilteredFlagTrends', () => {
averageUsers: 2,
averageHealth: '79',
flagsPerUser: '14.00',
medianTimeToProduction: 3,
});
});

Expand All @@ -76,6 +81,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 0,
users: 0,
date: '',
timeToProduction: 4,
},
],
{ total: 1 } as unknown as InstanceInsightsSchemaUsers,
Expand All @@ -90,6 +96,7 @@ describe('useFilteredFlagTrends', () => {
averageUsers: 0,
averageHealth: '100',
flagsPerUser: '5.00',
medianTimeToProduction: 4,
});
});

Expand All @@ -106,6 +113,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 0,
users: 0,
date: '',
timeToProduction: 2,
},
{
week: '2024-01',
Expand All @@ -116,6 +124,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 0,
users: 3,
date: '',
timeToProduction: 5,
},
],
{ total: 1 } as unknown as InstanceInsightsSchemaUsers,
Expand All @@ -130,6 +139,7 @@ describe('useFilteredFlagTrends', () => {
averageUsers: 1.5,
averageHealth: '100',
flagsPerUser: '10.00',
medianTimeToProduction: 3.5,
});
});

Expand All @@ -146,6 +156,7 @@ describe('useFilteredFlagTrends', () => {
potentiallyStale: 0,
users: 0,
date: '',
timeToProduction: 0,
},
],
{ total: 1 } as unknown as InstanceInsightsSchemaUsers,
Expand All @@ -160,6 +171,7 @@ describe('useFilteredFlagTrends', () => {
averageUsers: 0,
averageHealth: undefined,
flagsPerUser: '0.00',
medianTimeToProduction: 0,
});
});
});
19 changes: 19 additions & 0 deletions frontend/src/component/insights/hooks/useFilteredFlagsSummary.ts
Expand Up @@ -4,6 +4,10 @@ import type {
InstanceInsightsSchemaUsers,
} from 'openapi';

const validTimeToProduction = (
item: InstanceInsightsSchemaProjectFlagTrendsItem,
) => Boolean(item) && typeof item.timeToProduction === 'number';

// NOTE: should we move project filtering to the backend?
export const useFilteredFlagsSummary = (
filteredProjectFlagTrends: InstanceInsightsSchemaProjectFlagTrendsItem[],
Expand Down Expand Up @@ -42,6 +46,20 @@ export const useFilteredFlagsSummary = (
},
);

const timesToProduction: number[] = lastWeekSummary
.filter(validTimeToProduction)
.map((item) => item.timeToProduction!); // Non-null assertion is safe due to filter

// Calculate median timeToProduction for lastWeekSummary
timesToProduction.sort((a, b) => a - b);
const midIndex = Math.floor(timesToProduction.length / 2);
const medianTimeToProduction =
timesToProduction.length % 2 === 0
? (timesToProduction[midIndex - 1] +
timesToProduction[midIndex]) /
2
: timesToProduction[midIndex];

const flagsPerUserCalculation = sum.total / users.total;
const flagsPerUser = Number.isNaN(flagsPerUserCalculation)
? 'N/A'
Expand All @@ -54,5 +72,6 @@ export const useFilteredFlagsSummary = (
averageHealth: sum.total
? ((sum.active / (sum.total || 1)) * 100).toFixed(0)
: undefined,
medianTimeToProduction,
};
}, [filteredProjectFlagTrends]);
6 changes: 0 additions & 6 deletions frontend/src/component/insights/hooks/useInsightsData.ts
Expand Up @@ -3,7 +3,6 @@ import type { InstanceInsightsSchema } from 'openapi';
import { useFilteredTrends } from './useFilteredTrends';
import { useGroupedProjectTrends } from './useGroupedProjectTrends';
import { useFilteredFlagsSummary } from './useFilteredFlagsSummary';
import { useMedianTimeToProduction } from './useMedianTimeToProduction';

export const useInsightsData = (
executiveDashboardData: InstanceInsightsSchema,
Expand All @@ -27,9 +26,6 @@ export const useInsightsData = (
executiveDashboardData.users,
);

const medianTimeToProduction =
useMedianTimeToProduction(groupedProjectsData);

return useMemo(
() => ({
...executiveDashboardData,
Expand All @@ -40,7 +36,6 @@ export const useInsightsData = (
users: executiveDashboardData.users,
environmentTypeTrends: executiveDashboardData.environmentTypeTrends,
summary,
medianTimeToProduction,
}),
[
executiveDashboardData,
Expand All @@ -50,7 +45,6 @@ export const useInsightsData = (
metricsData,
groupedMetricsData,
summary,
medianTimeToProduction,
],
);
};

This file was deleted.

43 changes: 0 additions & 43 deletions frontend/src/component/insights/hooks/useMedianTimeToProduction.ts

This file was deleted.

0 comments on commit 717e541

Please sign in to comment.