Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: count lifecycle more accurately #8816

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dbInit, {
} from '../../../../test/e2e/helpers/database-init';
import getLogger from '../../../../test/fixtures/no-logger';
import { ProjectLifecycleSummaryReadModel } from './project-lifecycle-summary-read-model';
import type { IFeatureToggleStore, StageName } from '../../../types';
import type { StageName } from '../../../types';
import { randomId } from '../../../util';

let db: ITestDb;
Expand All @@ -14,7 +14,7 @@ beforeAll(async () => {
db = await dbInit('project_lifecycle_summary_read_model_serial', getLogger);
readModel = new ProjectLifecycleSummaryReadModel(
db.rawDatabase,
{} as unknown as IFeatureToggleStore,
db.stores.featureToggleStore,
);
});

Expand Down Expand Up @@ -211,15 +211,15 @@ describe('count current flags in each stage', () => {
const flags = [
{
name: randomId(),
stages: ['initial', 'pre-live', 'live', 'archived'],
stages: ['initial', 'live'],
},
{
name: randomId(),
stages: ['initial', 'archived'],
stages: ['initial'],
},
{
name: randomId(),
stages: ['initial', 'pre-live', 'live', 'archived'],
stages: ['initial', 'pre-live', 'live', 'completed'],
},
{ name: randomId(), stages: ['initial', 'pre-live', 'live'] },
];
Expand All @@ -230,13 +230,24 @@ describe('count current flags in each stage', () => {
createdByUserId: 1,
});

for (const stage of stages) {
const time = Date.now();
for (const [index, stage] of stages.entries()) {
await db.stores.featureLifecycleStore.insert([
{
feature: flag.name,
stage: stage as StageName,
},
]);

await db
.rawDatabase('feature_lifecycles')
.where({
feature: flag.name,
stage: stage,
})
.update({
created_at: addMinutes(time, index),
});
}
}

Expand Down Expand Up @@ -266,11 +277,60 @@ describe('count current flags in each stage', () => {
const result = await readModel.getCurrentFlagsInEachStage(project.id);

expect(result).toMatchObject({
initial: 4,
'pre-live': 3,
live: 3,
completed: 0,
archived: 3,
initial: 1,
'pre-live': 0,
live: 2,
completed: 1,
archived: 0,
});
});

test('if a flag is archived, but does not have the corresponding lifecycle stage, we still count it as archived and exclude it from other stages', async () => {
const project = await db.stores.projectStore.create({
name: 'project',
id: randomId(),
});

const flag = await db.stores.featureToggleStore.create(project.id, {
name: randomId(),
createdByUserId: 1,
});

await db.stores.featureLifecycleStore.insert([
{
feature: flag.name,
stage: 'initial',
},
]);

await db.stores.featureToggleStore.archive(flag.name);

const result = await readModel.getCurrentFlagsInEachStage(project.id);

expect(result).toMatchObject({
initial: 0,
archived: 1,
});
});

test('the archived count is based on the features table (source of truth), not the lifecycle table', async () => {
const project = await db.stores.projectStore.create({
name: 'project',
id: randomId(),
});

const flag = await db.stores.featureToggleStore.create(project.id, {
name: randomId(),
createdByUserId: 1,
});

await db.stores.featureToggleStore.archive(flag.name);

const result = await readModel.getCurrentFlagsInEachStage(project.id);

expect(result).toMatchObject({
initial: 0,
archived: 1,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,37 @@ export class ProjectLifecycleSummaryReadModel
}

async getCurrentFlagsInEachStage(projectId: string): Promise<FlagsInStage> {
const query = this.db('feature_lifecycles as fl')
const query = this.db
.with('latest_stage', (qb) => {
qb.select('fl.feature')
.max('fl.created_at as max_created_at')
.from('feature_lifecycles as fl')
.groupBy('fl.feature');
})
.from('latest_stage as ls')
.innerJoin('feature_lifecycles as fl', (qb) => {
qb.on('ls.feature', '=', 'fl.feature').andOn(
'ls.max_created_at',
'=',
'fl.created_at',
);
})
.innerJoin('features as f', 'fl.feature', 'f.name')
.where('f.project', projectId)
.whereNot('fl.stage', 'archived')
.whereNull('f.archived_at')
.select('fl.stage')
.count('fl.feature as flag_count')
.groupBy('fl.stage');

const result = await query;

return result.reduce(
const archivedCount = await this.featureToggleStore.count({
project: projectId,
archived: true,
});

const lifecycleStages = result.reduce(
(acc, row) => {
acc[row.stage] = Number(row.flag_count);
return acc;
Expand All @@ -100,9 +121,12 @@ export class ProjectLifecycleSummaryReadModel
'pre-live': 0,
live: 0,
completed: 0,
archived: 0,
},
) as FlagsInStage;
return {
...lifecycleStages,
archived: archivedCount,
};
}

async getArchivedFlagsLast30Days(projectId: string): Promise<number> {
Expand Down
Loading