1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00
unleash.unleash/src/lib/services/playground-service.ts
andreas-unleash d45f81ab02
fix: set feature.enabled to false when all strategies are deactivated (#3655)
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
set feature.enabled to false when all strategies are deactivated
## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
Co-authored-by: Prabodh Meshram <prabodh.meshram7@gmail.com>
2023-05-02 21:33:14 +03:00

107 lines
4.0 KiB
TypeScript

import FeatureToggleService from './feature-toggle-service';
import { SdkContextSchema } from 'lib/openapi/spec/sdk-context-schema';
import { IUnleashServices } from 'lib/types/services';
import { ALL } from '../../lib/types/models/api-token';
import { PlaygroundFeatureSchema } from 'lib/openapi/spec/playground-feature-schema';
import { Logger } from '../logger';
import { IUnleashConfig } from 'lib/types';
import { offlineUnleashClient } from '../util/offline-unleash-client';
import { FeatureInterface } from 'lib/util/feature-evaluator/feature';
import { FeatureStrategiesEvaluationResult } from 'lib/util/feature-evaluator/client';
import { ISegmentService } from 'lib/segments/segment-service-interface';
export class PlaygroundService {
private readonly logger: Logger;
private readonly featureToggleService: FeatureToggleService;
private readonly segmentService: ISegmentService;
constructor(
config: IUnleashConfig,
{
featureToggleServiceV2,
segmentService,
}: Pick<IUnleashServices, 'featureToggleServiceV2' | 'segmentService'>,
) {
this.logger = config.getLogger('services/playground-service.ts');
this.featureToggleService = featureToggleServiceV2;
this.segmentService = segmentService;
}
async evaluateQuery(
projects: typeof ALL | string[],
environment: string,
context: SdkContextSchema,
): Promise<PlaygroundFeatureSchema[]> {
const [features, segments] = await Promise.all([
this.featureToggleService.getClientFeatures(
{
project: projects === ALL ? undefined : projects,
environment,
},
true,
false,
),
this.segmentService.getActive(),
]);
const [head, ...rest] = features;
if (!head) {
return [];
} else {
const client = await offlineUnleashClient({
features: [head, ...rest],
context,
logError: this.logger.error,
segments,
});
const variantsMap = features.reduce((acc, feature) => {
acc[feature.name] = feature.variants;
return acc;
}, {});
const clientContext = {
...context,
currentTime: context.currentTime
? new Date(context.currentTime)
: undefined,
};
const output: PlaygroundFeatureSchema[] = await Promise.all(
client
.getFeatureToggleDefinitions()
.map(async (feature: FeatureInterface) => {
const strategyEvaluationResult: FeatureStrategiesEvaluationResult =
client.isEnabled(feature.name, clientContext);
const isEnabled =
strategyEvaluationResult.result === true &&
feature.enabled;
return {
isEnabled,
isEnabledInCurrentEnvironment: feature.enabled,
strategies: {
result: strategyEvaluationResult.result,
data: strategyEvaluationResult.strategies,
},
projectId:
await this.featureToggleService.getProjectId(
feature.name,
),
variant: client.getVariant(
feature.name,
clientContext,
),
name: feature.name,
variants: variantsMap[feature.name] || [],
};
}),
);
return output;
}
}
}