From a89f05181d6c0f0c06771c46ebb860667dd673bd Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Thu, 15 Aug 2024 11:32:46 +0200 Subject: [PATCH] fix: exclude archived features in segments count (#7886) --- .../segment/admin-segment.e2e.test.ts | 24 ++++++++++++++--- src/lib/features/segment/segment-store.ts | 26 +++++++++++-------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/lib/features/segment/admin-segment.e2e.test.ts b/src/lib/features/segment/admin-segment.e2e.test.ts index 8db2d61174..07c6b9023d 100644 --- a/src/lib/features/segment/admin-segment.e2e.test.ts +++ b/src/lib/features/segment/admin-segment.e2e.test.ts @@ -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', () => { diff --git a/src/lib/features/segment/segment-store.ts b/src/lib/features/segment/segment-store.ts index 91ff8234f5..04ad651e7c 100644 --- a/src/lib/features/segment/segment-store.ts +++ b/src/lib/features/segment/segment-store.ts @@ -16,6 +16,7 @@ import { isDefined } from '../../util'; const T = { segments: 'segments', featureStrategies: 'feature_strategies', + features: 'features', featureStrategySegment: 'feature_strategy_segment', }; @@ -123,17 +124,15 @@ export default class SegmentStore implements ISegmentStore { private async getAllWithoutChangeRequestUsageData(): Promise { 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( + `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, @@ -145,6 +144,11 @@ export default class SegmentStore implements ISegmentStore { `${T.featureStrategies}.id`, `${T.featureStrategySegment}.feature_strategy_id`, ) + .leftJoin( + T.features, + `${T.featureStrategies}.feature_name`, + `${T.features}.name`, + ) .groupBy(this.prefixColumns()) .orderBy('name', 'asc');