1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/domain/project-health/project-health.ts

61 lines
1.8 KiB
TypeScript
Raw Normal View History

import { hoursToMilliseconds } from 'date-fns';
import type { IProjectHealthReport } from 'lib/types';
import type { IFeatureType } from 'lib/types/stores/feature-type-store';
2023-02-02 12:01:16 +01:00
type IPartialFeatures = Array<{
stale?: boolean;
createdAt?: Date;
type?: string;
}>;
const getPotentiallyStaleCount = (
2023-02-02 12:01:16 +01:00
features: IPartialFeatures,
featureTypes: IFeatureType[],
) => {
const today = new Date().valueOf();
return features.filter((feature) => {
const diff = today - feature.createdAt?.valueOf();
const featureTypeExpectedLifetime = featureTypes.find(
(t) => t.id === feature.type,
)?.lifetimeDays;
return (
!feature.stale &&
2023-02-02 12:01:16 +01:00
featureTypeExpectedLifetime !== null &&
diff >= featureTypeExpectedLifetime * hoursToMilliseconds(24)
);
}).length;
};
export const calculateProjectHealth = (
2023-02-02 12:01:16 +01:00
features: IPartialFeatures,
featureTypes: IFeatureType[],
): Pick<
IProjectHealthReport,
'staleCount' | 'potentiallyStaleCount' | 'activeCount'
> => ({
potentiallyStaleCount: getPotentiallyStaleCount(features, featureTypes),
activeCount: features.filter((f) => !f.stale).length,
staleCount: features.filter((f) => f.stale).length,
});
2023-02-02 12:01:16 +01:00
export const calculateHealthRating = (
features: IPartialFeatures,
featureTypes: IFeatureType[],
): number => {
const { potentiallyStaleCount, activeCount, staleCount } =
calculateProjectHealth(features, featureTypes);
const toggleCount = activeCount + staleCount;
const startPercentage = 100;
const stalePercentage = (staleCount / toggleCount) * 100 || 0;
const potentiallyStalePercentage =
(potentiallyStaleCount / toggleCount) * 100 || 0;
const rating = Math.round(
startPercentage - stalePercentage - potentiallyStalePercentage,
);
return rating;
};