Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: count number of combinations from playground (#4077)
This PR adds plausible tracking of the number of feature combinations that we get from the advanced playground API. The event type has been added to plausible Relates to #3545
- Loading branch information
1 parent
89cf16f
commit 12c0073
Showing
6 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...component/playground/Playground/AdvancedPlaygroundResultsTable/combinationCounter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { countCombinations } from './combinationCounter'; | ||
import { | ||
AdvancedPlaygroundEnvironmentFeatureSchema, | ||
AdvancedPlaygroundFeatureSchema, | ||
} from 'openapi'; | ||
// @ts-expect-error no types available | ||
import cartesian from 'cartesian'; | ||
|
||
const generateFeature = ( | ||
context: Record<string, string> | ||
): AdvancedPlaygroundEnvironmentFeatureSchema => ({ | ||
isEnabled: false, | ||
isEnabledInCurrentEnvironment: true, | ||
variant: { | ||
name: 'disabled', | ||
enabled: false, | ||
}, | ||
context: { | ||
appName: 'playground', | ||
}, | ||
variants: [], | ||
name: 'default', | ||
environment: 'development', | ||
projectId: 'default', | ||
strategies: { | ||
result: false, | ||
data: [ | ||
{ | ||
name: 'default', | ||
id: '7b233aae-cbc4-45ea-ace2-4c78c8e7e760', | ||
disabled: false, | ||
parameters: {}, | ||
result: { | ||
enabled: false, | ||
evaluationStatus: 'complete' as 'complete', | ||
}, | ||
constraints: [ | ||
{ | ||
inverted: false, | ||
values: ['k'], | ||
operator: 'IN', | ||
contextName: 'appName', | ||
caseInsensitive: false, | ||
result: false, | ||
}, | ||
], | ||
segments: [], | ||
links: { | ||
edit: '/projects/default/features/default/strategies/edit?environmentId=development&strategyId=7b233aae-cbc4-45ea-ace2-4c78c8e7e760', | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
const generateInput = ( | ||
featureCount: number, | ||
environments: string[], | ||
contextValues: { [key: string]: string[] } | ||
): AdvancedPlaygroundFeatureSchema[] => { | ||
const cartesianContext = cartesian(contextValues); | ||
|
||
return Array.from(Array(featureCount)).map((_, i) => ({ | ||
name: `feature-${i}`, | ||
projectId: 'default', | ||
environments: Object.fromEntries( | ||
environments.map(env => [ | ||
env, | ||
cartesianContext.map(generateFeature), | ||
]) | ||
), | ||
})); | ||
}; | ||
|
||
it('counts the correct number of combinations', () => { | ||
const assertCount = ( | ||
numberOfFeatures: number, | ||
envs: string[], | ||
context: { [k: string]: string[] } | ||
) => { | ||
const totalCombinations = | ||
numberOfFeatures * | ||
envs.length * | ||
Object.values(context) | ||
.map(contextValues => contextValues.length) | ||
.reduce((total, n) => total + n); | ||
const input = generateInput(numberOfFeatures, envs, context); | ||
expect(countCombinations(input)).toEqual(totalCombinations); | ||
}; | ||
|
||
assertCount(1, ['development'], { x: ['2'] }); | ||
assertCount(10, ['development', 'production'], { | ||
x: ['1', '2'], | ||
y: ['x', 'abc'], | ||
}); | ||
assertCount(5, ['development'], { x: ['1', '2'] }); | ||
}); |
12 changes: 12 additions & 0 deletions
12
.../src/component/playground/Playground/AdvancedPlaygroundResultsTable/combinationCounter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AdvancedPlaygroundFeatureSchema } from 'openapi'; | ||
|
||
export const countCombinations = ( | ||
features: AdvancedPlaygroundFeatureSchema[] | ||
) => | ||
features.reduce( | ||
(total, feature) => | ||
total + | ||
Object.values(feature.environments).flatMap(env => Object.keys(env)) | ||
.length, | ||
0 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters