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

1-3085: count flags in each stage #8699

Merged
merged 1 commit into from
Nov 8, 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 @@ -197,3 +197,79 @@ describe('Average time calculation', () => {
});
});
});

describe('count current flags in each stage', () => {
test('it counts the number of flags in each stage for the given project', async () => {
const project = await db.stores.projectStore.create({
name: 'project',
id: randomId(),
});

const flags = [
{
name: randomId(),
stages: ['initial', 'pre-live', 'live', 'archived'],
},
{
name: randomId(),
stages: ['initial', 'archived'],
},
{
name: randomId(),
stages: ['initial', 'pre-live', 'live', 'archived'],
},
{ name: randomId(), stages: ['initial', 'pre-live', 'live'] },
];

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

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

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

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

const readModel = new ProjectLifecycleSummaryReadModel(db.rawDatabase);

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

expect(result).toMatchObject({
initial: 4,
'pre-live': 3,
live: 3,
completed: 0,
archived: 3,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,28 @@ export class ProjectLifecycleSummaryReadModel
}

async getCurrentFlagsInEachStage(projectId: string) {
return 0;
const query = this.db('feature_lifecycles as fl')
.innerJoin('features as f', 'fl.feature', 'f.name')
.where('f.project', projectId)
.select('fl.stage')
.count('fl.feature as flag_count')
.groupBy('fl.stage');

const result = await query;

return result.reduce(
(acc, row) => {
acc[row.stage] = Number(row.flag_count);
return acc;
},
{
initial: 0,
'pre-live': 0,
live: 0,
completed: 0,
archived: 0,
},
);
}

async getArchivedFlagsOverLastMonth(projectId: string) {
Expand Down
Loading