Skip to content

Commit

Permalink
1-3121: fix wrong counting for unhealthy flags (#8772)
Browse files Browse the repository at this point in the history
This PR fixes the counting of unhealthy flags for the project status
page. The issue was that we were looking for `archived = false`, but we
don't set that flag in the db anymore. Instead, we set the `archived_at`
date, which should be null if the flag is unarchived.
  • Loading branch information
thomasheartman authored Nov 20, 2024
1 parent ec44c5b commit 20749cf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export class ProjectStaleFlagsReadModel implements IProjectStaleFlagsReadModel {
async getStaleFlagCountForProject(projectId: string): Promise<number> {
const result = await this.db('features')
.count()
.where({ project: projectId, archived: false })
.whereNull('archived_at')
.where({ project: projectId })
.where((builder) =>
builder
.orWhere({ stale: true })
Expand Down
64 changes: 37 additions & 27 deletions src/lib/features/project-status/projects-status.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,36 +264,46 @@ test('project status includes stale flags', async () => {
{} as IUser,
{} as IAuditUser,
);
const createFlagInState = async (
name: string,
state?: Object,
projectId?: string,
) => {
await app.createFeature(name, projectId);
if (state) {
await db.rawDatabase('features').update(state).where({ name });
}
};

await createFlagInState('stale-flag', { stale: true });
await createFlagInState('potentially-stale-flag', {
potentially_stale: true,
});
await createFlagInState('potentially-stale-and-stale-flag', {
potentially_stale: true,
stale: true,
});
await createFlagInState('non-stale-flag');
await createFlagInState('archived-stale-flag', {
archived: true,
stale: true,
});
await createFlagInState(
'stale-other-project',
{ stale: true },
otherProject.id,
function cartesianProduct(...arrays: any[][]): any[][] {
return arrays.reduce(
(acc, array) => {
return acc.flatMap((accItem) =>
array.map((item) => [...accItem, item]),
);
},
[[]] as any[][],
);
}

// of all 16 (2^4) permutations, only 3 are unhealthy flags in a given project.
const combinations = cartesianProduct(
[false, true], // stale
[false, true], // potentially stale
[false, true], // archived
['default', otherProject.id], // project
);

for (const [stale, potentiallyStale, archived, project] of combinations) {
const name = `flag-${project}-stale-${stale}-potentially-stale-${potentiallyStale}-archived-${archived}`;
await app.createFeature(
{
name,
stale,
},
project,
);
if (potentiallyStale) {
await db
.rawDatabase('features')
.update('potentially_stale', true)
.where({ name });
}
if (archived) {
await app.archiveFeature(name, project);
}
}

const { body } = await app.request
.get('/api/admin/projects/default/status')
.expect('Content-Type', /json/)
Expand Down

0 comments on commit 20749cf

Please sign in to comment.