2021-07-07 10:46:50 +02:00
|
|
|
import { IUnleashStores } from '../types/stores';
|
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import { Logger } from '../logger';
|
2023-01-31 13:08:20 +01:00
|
|
|
import type { IProject, IProjectHealthReport } from '../types/model';
|
|
|
|
import type { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
|
|
|
|
import type {
|
|
|
|
IFeatureType,
|
|
|
|
IFeatureTypeStore,
|
|
|
|
} from '../types/stores/feature-type-store';
|
|
|
|
import type { IProjectStore } from '../types/stores/project-store';
|
2023-01-18 13:22:58 +01:00
|
|
|
import ProjectService from './project-service';
|
2023-01-31 13:08:20 +01:00
|
|
|
import {
|
|
|
|
calculateProjectHealth,
|
2023-02-02 12:01:16 +01:00
|
|
|
calculateHealthRating,
|
|
|
|
} from '../domain/project-health/project-health';
|
2021-07-07 10:46:50 +02:00
|
|
|
|
|
|
|
export default class ProjectHealthService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private projectStore: IProjectStore;
|
2021-07-07 10:46:50 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private featureTypeStore: IFeatureTypeStore;
|
2021-07-07 10:46:50 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
private featureToggleStore: IFeatureToggleStore;
|
2021-07-07 10:46:50 +02:00
|
|
|
|
2023-01-31 13:08:20 +01:00
|
|
|
private featureTypes: IFeatureType[];
|
2021-07-07 10:46:50 +02:00
|
|
|
|
2023-01-18 13:22:58 +01:00
|
|
|
private projectService: ProjectService;
|
2022-11-30 12:41:53 +01:00
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
constructor(
|
|
|
|
{
|
|
|
|
projectStore,
|
|
|
|
featureTypeStore,
|
|
|
|
featureToggleStore,
|
|
|
|
}: Pick<
|
2021-08-12 15:04:37 +02:00
|
|
|
IUnleashStores,
|
|
|
|
'projectStore' | 'featureTypeStore' | 'featureToggleStore'
|
2021-07-07 10:46:50 +02:00
|
|
|
>,
|
|
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
2023-01-18 13:22:58 +01:00
|
|
|
projectService: ProjectService,
|
2021-07-07 10:46:50 +02:00
|
|
|
) {
|
|
|
|
this.logger = getLogger('services/project-health-service.ts');
|
|
|
|
this.projectStore = projectStore;
|
|
|
|
this.featureTypeStore = featureTypeStore;
|
|
|
|
this.featureToggleStore = featureToggleStore;
|
2023-01-31 13:08:20 +01:00
|
|
|
this.featureTypes = [];
|
2022-11-30 12:41:53 +01:00
|
|
|
|
2023-01-18 13:22:58 +01:00
|
|
|
this.projectService = projectService;
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getProjectHealthReport(
|
|
|
|
projectId: string,
|
|
|
|
): Promise<IProjectHealthReport> {
|
2023-01-31 13:08:20 +01:00
|
|
|
if (this.featureTypes.length === 0) {
|
|
|
|
this.featureTypes = await this.featureTypeStore.getAll();
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:22:58 +01:00
|
|
|
const overview = await this.projectService.getProjectOverview(
|
2022-11-30 12:41:53 +01:00
|
|
|
projectId,
|
|
|
|
false,
|
|
|
|
undefined,
|
|
|
|
);
|
2023-01-31 13:08:20 +01:00
|
|
|
|
|
|
|
const healthRating = calculateProjectHealth(
|
|
|
|
overview.features,
|
|
|
|
this.featureTypes,
|
|
|
|
);
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
return {
|
|
|
|
...overview,
|
2023-01-31 13:08:20 +01:00
|
|
|
...healthRating,
|
2021-07-07 10:46:50 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-31 13:08:20 +01:00
|
|
|
async calculateHealthRating(project: IProject): Promise<number> {
|
|
|
|
if (this.featureTypes.length === 0) {
|
|
|
|
this.featureTypes = await this.featureTypeStore.getAll();
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 10:23:57 +02:00
|
|
|
const toggles = await this.featureToggleStore.getAll({
|
2021-07-07 10:46:50 +02:00
|
|
|
project: project.id,
|
2021-11-25 10:09:23 +01:00
|
|
|
archived: false,
|
2021-07-07 10:46:50 +02:00
|
|
|
});
|
|
|
|
|
2023-02-02 12:01:16 +01:00
|
|
|
return calculateHealthRating(toggles, this.featureTypes);
|
2021-07-07 10:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async setHealthRating(): Promise<void> {
|
|
|
|
const projects = await this.projectStore.getAll();
|
|
|
|
|
|
|
|
await Promise.all(
|
2021-08-12 15:04:37 +02:00
|
|
|
projects.map(async (project) => {
|
2021-07-07 10:46:50 +02:00
|
|
|
const newHealth = await this.calculateHealthRating(project);
|
|
|
|
await this.projectStore.updateHealth({
|
|
|
|
id: project.id,
|
|
|
|
health: newHealth,
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|