unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
37 lines • 1.8 kB
JavaScript
import { hoursToMilliseconds } from 'date-fns';
const getPotentiallyStaleCount = (features, featureTypes) => {
const today = Date.now();
return features.filter((feature) => {
const diff = feature.createdAt
? today - feature.createdAt.valueOf()
: 0;
const featureTypeExpectedLifetime = featureTypes.find((t) => t.id === feature.type)?.lifetimeDays;
return (!feature.stale &&
featureTypeExpectedLifetime !== null &&
featureTypeExpectedLifetime !== undefined &&
diff >= featureTypeExpectedLifetime * hoursToMilliseconds(24));
}).length;
};
export const calculateProjectHealth = (features, featureTypes) => ({
potentiallyStaleCount: getPotentiallyStaleCount(features, featureTypes),
activeCount: features.filter((f) => !f.stale).length,
staleCount: features.filter((f) => f.stale).length,
});
export const calculateHealthRating = (features, featureTypes) => {
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;
};
export const calculateProjectHealthRating = (featureTypeStore, featureToggleStore) => async ({ id }) => {
const featureTypes = await featureTypeStore.getAll();
const toggles = await featureToggleStore.getAll({
project: id,
archived: false,
});
return calculateHealthRating(toggles, featureTypes);
};
//# sourceMappingURL=project-health.js.map