Skip to content

Commit

Permalink
chore: minor cleanup of project health and status (#8806)
Browse files Browse the repository at this point in the history
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?).
  • Loading branch information
thomasheartman authored Nov 20, 2024
1 parent 61df153 commit a59a031
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
27 changes: 25 additions & 2 deletions src/lib/domain/project-health/project-health.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,3 +65,19 @@ export const calculateHealthRating = (

return rating;
};

export const calculateProjectHealthRating =
(
featureTypeStore: IFeatureTypeStore,
featureToggleStore: IFeatureToggleStore,
) =>
async ({ id }: Pick<IProject, 'id'>): Promise<number> => {
const featureTypes = await featureTypeStore.getAll();

const toggles = await featureToggleStore.getAll({
project: id,
archived: false,
});

return calculateHealthRating(toggles, featureTypes);
};
18 changes: 5 additions & 13 deletions src/lib/features/project-status/project-status-service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -52,17 +52,6 @@ export class ProjectStatusService {
this.featureToggleStore = featureToggleStore;
}

private async calculateHealthRating(projectId: string): Promise<number> {
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<ProjectStatusSchema> {
const [
members,
Expand All @@ -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,
),
Expand Down
3 changes: 3 additions & 0 deletions src/lib/routes/admin-api/project/health-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 7 additions & 12 deletions src/lib/services/project-health-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -22,6 +22,8 @@ export default class ProjectHealthService {

private projectService: ProjectService;

calculateHealthRating: (project: IProject) => Promise<number>;

constructor(
{
projectStore,
Expand All @@ -40,6 +42,10 @@ export default class ProjectHealthService {
this.featureToggleStore = featureToggleStore;

this.projectService = projectService;
this.calculateHealthRating = calculateProjectHealthRating(
this.featureTypeStore,
this.featureToggleStore,
);
}

async getProjectHealthReport(
Expand All @@ -64,17 +70,6 @@ export default class ProjectHealthService {
};
}

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();

Expand Down

0 comments on commit a59a031

Please sign in to comment.