From a59a0313628a845306e144e17b452e6b9d5510f4 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Wed, 20 Nov 2024 13:59:44 +0100 Subject: [PATCH] chore: minor cleanup of project health and status (#8806) This PR: - conditionally deprecates the project health report endpoint. We only use this for technical debt dashboard that we're removing. Now it's deprecated once you turn the simplifiy flag on. - extracts the calculate project health function into the project health functions file in the appropriate domain folder. That same function is now shared by the project health service and the project status service. For the last point, it's a little outside of how we normally do things, because it takes its stores as arguments, but it slots in well in that file. An option would be to make a project health read model and then wire that up in a couple places. It's more code, but probably closer to how we do things in general. That said, I wanted to suggest this because it's quick and easy (why do much work when little work do trick?). --- .../domain/project-health/project-health.ts | 27 +++++++++++++++++-- .../project-status/project-status-service.ts | 18 ++++--------- .../routes/admin-api/project/health-report.ts | 3 +++ src/lib/services/project-health-service.ts | 19 +++++-------- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/lib/domain/project-health/project-health.ts b/src/lib/domain/project-health/project-health.ts index 248719e9e3..07a7b6be5b 100644 --- a/src/lib/domain/project-health/project-health.ts +++ b/src/lib/domain/project-health/project-health.ts @@ -1,6 +1,13 @@ import { hoursToMilliseconds } from 'date-fns'; -import type { IProjectHealthReport } from '../../types'; -import type { IFeatureType } from '../../types/stores/feature-type-store'; +import type { + IFeatureToggleStore, + IProject, + IProjectHealthReport, +} from '../../types'; +import type { + IFeatureType, + IFeatureTypeStore, +} from '../../types/stores/feature-type-store'; type IPartialFeatures = Array<{ stale?: boolean; @@ -58,3 +65,19 @@ export const calculateHealthRating = ( return rating; }; + +export const calculateProjectHealthRating = + ( + featureTypeStore: IFeatureTypeStore, + featureToggleStore: IFeatureToggleStore, + ) => + async ({ id }: Pick): Promise => { + const featureTypes = await featureTypeStore.getAll(); + + const toggles = await featureToggleStore.getAll({ + project: id, + archived: false, + }); + + return calculateHealthRating(toggles, featureTypes); + }; diff --git a/src/lib/features/project-status/project-status-service.ts b/src/lib/features/project-status/project-status-service.ts index 856673ae7a..8345abe69d 100644 --- a/src/lib/features/project-status/project-status-service.ts +++ b/src/lib/features/project-status/project-status-service.ts @@ -1,4 +1,4 @@ -import { calculateHealthRating } from '../../domain/project-health/project-health'; +import { calculateProjectHealthRating } from '../../domain/project-health/project-health'; import type { ProjectStatusSchema } from '../../openapi'; import type { IApiTokenStore, @@ -52,17 +52,6 @@ export class ProjectStatusService { this.featureToggleStore = featureToggleStore; } - private async calculateHealthRating(projectId: string): Promise { - const featureTypes = await this.featureTypeStore.getAll(); - - const toggles = await this.featureToggleStore.getAll({ - project: projectId, - archived: false, - }); - - return calculateHealthRating(toggles, featureTypes); - } - async getProjectStatus(projectId: string): Promise { const [ members, @@ -77,7 +66,10 @@ export class ProjectStatusService { this.apiTokenStore.countProjectTokens(projectId), this.segmentStore.getProjectSegmentCount(projectId), this.eventStore.getProjectRecentEventActivity(projectId), - this.calculateHealthRating(projectId), + calculateProjectHealthRating( + this.featureTypeStore, + this.featureToggleStore, + )({ id: projectId }), this.projectLifecycleSummaryReadModel.getProjectLifecycleSummary( projectId, ), diff --git a/src/lib/routes/admin-api/project/health-report.ts b/src/lib/routes/admin-api/project/health-report.ts index cf5d405602..77740cbae3 100644 --- a/src/lib/routes/admin-api/project/health-report.ts +++ b/src/lib/routes/admin-api/project/health-report.ts @@ -42,6 +42,9 @@ export default class ProjectHealthReport extends Controller { middleware: [ openApiService.validPath({ tags: ['Projects'], + deprecated: config.flagResolver.isEnabled( + 'simplifyProjectOverview', + ), operationId: 'getProjectHealthReport', summary: 'Get a health report for a project.', description: diff --git a/src/lib/services/project-health-service.ts b/src/lib/services/project-health-service.ts index 1b45fbeaaa..729da8d1e2 100644 --- a/src/lib/services/project-health-service.ts +++ b/src/lib/services/project-health-service.ts @@ -7,8 +7,8 @@ import type { IFeatureTypeStore } from '../types/stores/feature-type-store'; import type { IProjectStore } from '../features/project/project-store-type'; import type ProjectService from '../features/project/project-service'; import { - calculateHealthRating, calculateProjectHealth, + calculateProjectHealthRating, } from '../domain/project-health/project-health'; export default class ProjectHealthService { @@ -22,6 +22,8 @@ export default class ProjectHealthService { private projectService: ProjectService; + calculateHealthRating: (project: IProject) => Promise; + constructor( { projectStore, @@ -40,6 +42,10 @@ export default class ProjectHealthService { this.featureToggleStore = featureToggleStore; this.projectService = projectService; + this.calculateHealthRating = calculateProjectHealthRating( + this.featureTypeStore, + this.featureToggleStore, + ); } async getProjectHealthReport( @@ -64,17 +70,6 @@ export default class ProjectHealthService { }; } - async calculateHealthRating(project: IProject): Promise { - const featureTypes = await this.featureTypeStore.getAll(); - - const toggles = await this.featureToggleStore.getAll({ - project: project.id, - archived: false, - }); - - return calculateHealthRating(toggles, featureTypes); - } - async setHealthRating(): Promise { const projects = await this.projectStore.getAll();