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: exclude archived features in segments count #7886

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 20 additions & 4 deletions src/lib/features/segment/admin-segment.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,16 +602,32 @@ test('Should show usage in features and projects', async () => {
);
const [feature] = await fetchFeatures();
const [strategy] = await fetchFeatureStrategies(feature.name);
//@ts-ignore
await addSegmentsToStrategy([segment.id], strategy.id);

const segments = await fetchSegments();
expect(segments).toMatchObject([
const unusedSegments = await fetchSegments();
expect(unusedSegments).toMatchObject([
{
usedInFeatures: 0,
usedInProjects: 0,
},
]);

await addSegmentsToStrategy([segment.id], strategy.id);
const usedSegments = await fetchSegments();
expect(usedSegments).toMatchObject([
{
usedInFeatures: 1,
usedInProjects: 1,
},
]);

await app.archiveFeature(feature.name, feature.project);
const segmentsWithArchivedFeatures = await fetchSegments();
expect(segmentsWithArchivedFeatures).toMatchObject([
{
usedInFeatures: 0,
usedInProjects: 0,
},
]);
});

describe('detect strategy usage in change requests', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/features/segment/segment-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('usage counting', () => {
await db.rawDatabase.table('change_requests').delete();
});

test('segment usage in active CRs is counted iff we ask for it', async () => {
test('segment usage in active CRs is counted if we ask for it', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't actually a typo, but shorthand for "if and only if", but maybe I should've written it out? 😅

const CR_ID = 54321;

const flag1 = await db.stores.featureToggleStore.create('default', {
Expand Down
26 changes: 15 additions & 11 deletions src/lib/features/segment/segment-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { isDefined } from '../../util';
const T = {
segments: 'segments',
featureStrategies: 'feature_strategies',
features: 'features',
featureStrategySegment: 'feature_strategy_segment',
};

Expand Down Expand Up @@ -123,17 +124,15 @@ export default class SegmentStore implements ISegmentStore {

private async getAllWithoutChangeRequestUsageData(): Promise<ISegment[]> {
const rows: ISegmentRow[] = await this.db
.select(
this.prefixColumns(),
'used_in_projects',
'used_in_features',
)
.countDistinct(
`${T.featureStrategies}.project_name AS used_in_projects`,
)
.countDistinct(
`${T.featureStrategies}.feature_name AS used_in_features`,
)
.select([
...this.prefixColumns(),
this.db.raw(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The COUNT functions are now wrapped with CASE statements. These CASE statements ensure that only non-archived features (where features.archived_at is NULL) are counted. If a feature is archived, the CASE returns NULL, which does not affect the count, effectively making it 0

`count(distinct case when ${T.features}.archived_at is null then ${T.featureStrategies}.project_name end) as used_in_projects`,
),
this.db.raw(
`count(distinct case when ${T.features}.archived_at is null then ${T.featureStrategies}.feature_name end) as used_in_features`,
),
])
.from(T.segments)
.leftJoin(
T.featureStrategySegment,
Expand All @@ -145,6 +144,11 @@ export default class SegmentStore implements ISegmentStore {
`${T.featureStrategies}.id`,
`${T.featureStrategySegment}.feature_strategy_id`,
)
.leftJoin(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a new join to get feature archived info

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tried the where clause just before groupBy instead of rewriting counts but it didn't work.

T.features,
`${T.featureStrategies}.feature_name`,
`${T.features}.name`,
)
.groupBy(this.prefixColumns())
.orderBy('name', 'asc');

Expand Down
Loading