|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { FeatureValue } from '../client/codewhispereruserclient' |
| 7 | +import { codeWhispererClient as client } from '../client/codewhisperer' |
| 8 | +import { AuthUtil } from '../util/authUtil' |
| 9 | +import { getLogger } from '../../shared/logger' |
| 10 | + |
| 11 | +export class FeatureContext { |
| 12 | + constructor(public name: string, public variation: string, public value: FeatureValue) {} |
| 13 | +} |
| 14 | + |
| 15 | +const testFeatureName = 'testFeature' |
| 16 | +const featureConfigPollIntervalInMs = 30 * 60 * 1000 // 30 mins |
| 17 | + |
| 18 | +// TODO: add real feature later |
| 19 | +export const featureDefinitions = new Map([ |
| 20 | + [testFeatureName, new FeatureContext(testFeatureName, 'CONTROL', { stringValue: 'testValue' })], |
| 21 | +]) |
| 22 | + |
| 23 | +export class FeatureConfigProvider { |
| 24 | + private featureConfigs = new Map<string, FeatureContext>() |
| 25 | + |
| 26 | + static #instance: FeatureConfigProvider |
| 27 | + |
| 28 | + constructor() { |
| 29 | + this.fetchFeatureConfigs() |
| 30 | + |
| 31 | + setInterval(this.fetchFeatureConfigs.bind(this), featureConfigPollIntervalInMs) |
| 32 | + } |
| 33 | + |
| 34 | + public static get instance() { |
| 35 | + return (this.#instance ??= new this()) |
| 36 | + } |
| 37 | + |
| 38 | + async fetchFeatureConfigs(): Promise<void> { |
| 39 | + if (AuthUtil.instance.isConnectionExpired()) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + getLogger().debug('CodeWhisperer: Fetching feature configs') |
| 44 | + try { |
| 45 | + const response = await client.listFeatureEvaluations() |
| 46 | + |
| 47 | + // Overwrite feature configs from server response |
| 48 | + response.featureEvaluations.forEach(evaluation => { |
| 49 | + this.featureConfigs.set( |
| 50 | + evaluation.feature, |
| 51 | + new FeatureContext(evaluation.feature, evaluation.variation, evaluation.value) |
| 52 | + ) |
| 53 | + }) |
| 54 | + } catch (e) { |
| 55 | + getLogger().debug('CodeWhisperer: Error when fetching feature configs', e) |
| 56 | + } |
| 57 | + getLogger().debug(`CodeWhisperer: Current feature configs: ${this.getFeatureConfigsTelemetry()}`) |
| 58 | + } |
| 59 | + |
| 60 | + // Sample format: "{testFeature: CONTROL}"" |
| 61 | + getFeatureConfigsTelemetry(): string { |
| 62 | + return `{${Array.from(this.featureConfigs.entries()) |
| 63 | + .map(([name, context]) => `${name}: ${context.variation}`) |
| 64 | + .join(', ')}}` |
| 65 | + } |
| 66 | + |
| 67 | + // TODO: for all feature variations, define a contract that can be enforced upon the implementation of |
| 68 | + // the business logic. |
| 69 | + // When we align on a new feature config, client-side will implement specific business logic to utilize |
| 70 | + // these values by: |
| 71 | + // 1) Add an entry in featureDefinitions, which is <feature_name> to <feature_context>. |
| 72 | + // 2) Add a function with name `getXXX`, where XXX refers to the feature name. |
| 73 | + // 3) Specify the return type: One of the return type string/boolean/Long/Double should be used here. |
| 74 | + // 4) Specify the key for the `getFeatureValueForKey` helper function which is the feature name. |
| 75 | + // 5) Specify the corresponding type value getter for the `FeatureValue` class. For example, |
| 76 | + // if the return type is Long, then the corresponding type value getter is `longValue()`. |
| 77 | + // 6) Add a test case for this feature. |
| 78 | + // 7) In case `getXXX()` returns undefined, it should be treated as a default/control group. |
| 79 | + getTestFeature(): string | undefined { |
| 80 | + return this.getFeatureValueForKey(testFeatureName).stringValue |
| 81 | + } |
| 82 | + |
| 83 | + // Get the feature value for the given key. |
| 84 | + // In case of a misconfiguration, it will return a default feature value of Boolean true. |
| 85 | + private getFeatureValueForKey(name: string): FeatureValue { |
| 86 | + return this.featureConfigs.get(name)?.value ?? featureDefinitions.get(name)?.value ?? { boolValue: true } |
| 87 | + } |
| 88 | +} |
0 commit comments