1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00
unleash.unleash/src/lib/services/project-health-service.ts
andreas-unleash b07c032d56
fix: update potentially-stale status dynamically (#4905)
Fixes 2 bugs:

- project-health-service keeping the feature types as an instance
variable and only updating it once was preventing real calculation to
happen if the lifetime value changed for a feature toggle type
- the ui was reading from a predefined map for the lifetime values so
they would never reflect the BE change

Closes #
[SR-66](https://linear.app/unleash/issue/SR-66/slack-question-around-potentially-stale-and-its-uses)

<img width="1680" alt="Screenshot 2023-10-02 at 14 37 17"
src="https://github.com/Unleash/unleash/assets/104830839/7bee8d4a-9054-4214-a1a2-11ad8169c3d5">
<img width="1660" alt="Screenshot 2023-10-02 at 14 37 06"
src="https://github.com/Unleash/unleash/assets/104830839/23bf55c7-a380-4423-a732-205ad81d5c3c">

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
2023-10-04 12:47:16 +03:00

92 lines
2.7 KiB
TypeScript

import { IUnleashStores } from '../types/stores';
import { IUnleashConfig } from '../types/option';
import { Logger } from '../logger';
import type { IProject, IProjectHealthReport } from '../types/model';
import type { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
import type { IFeatureTypeStore } from '../types/stores/feature-type-store';
import type { IProjectStore } from '../types/stores/project-store';
import ProjectService from './project-service';
import {
calculateHealthRating,
calculateProjectHealth,
} from '../domain/project-health/project-health';
export default class ProjectHealthService {
private logger: Logger;
private projectStore: IProjectStore;
private featureTypeStore: IFeatureTypeStore;
private featureToggleStore: IFeatureToggleStore;
private projectService: ProjectService;
constructor(
{
projectStore,
featureTypeStore,
featureToggleStore,
}: Pick<
IUnleashStores,
'projectStore' | 'featureTypeStore' | 'featureToggleStore'
>,
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
projectService: ProjectService,
) {
this.logger = getLogger('services/project-health-service.ts');
this.projectStore = projectStore;
this.featureTypeStore = featureTypeStore;
this.featureToggleStore = featureToggleStore;
this.projectService = projectService;
}
async getProjectHealthReport(
projectId: string,
): Promise<IProjectHealthReport> {
const featureTypes = await this.featureTypeStore.getAll();
const overview = await this.projectService.getProjectOverview(
projectId,
false,
undefined,
);
const healthRating = calculateProjectHealth(
overview.features,
featureTypes,
);
return {
...overview,
...healthRating,
};
}
async calculateHealthRating(project: IProject): Promise<number> {
const featureTypes = await this.featureTypeStore.getAll();
const toggles = await this.featureToggleStore.getAll({
project: project.id,
archived: false,
});
return calculateHealthRating(toggles, featureTypes);
}
async setHealthRating(): Promise<void> {
const projects = await this.projectStore.getAll();
await Promise.all(
projects.map(async (project) => {
const newHealth = await this.calculateHealthRating(project);
await this.projectStore.updateHealth({
id: project.id,
health: newHealth,
});
}),
);
}
}