|
1 | | -import { IConfigurationStore } from './configuration-store/configuration-store'; |
| 1 | +import { Configuration } from './configuration'; |
| 2 | +import { ConfigurationStore } from './configuration-store'; |
2 | 3 | import { IHttpClient } from './http-client'; |
3 | | -import { |
4 | | - ConfigStoreHydrationPacket, |
5 | | - IConfiguration, |
6 | | - StoreBackedConfiguration, |
7 | | -} from './i-configuration'; |
8 | | -import { BanditVariation, BanditParameters, Flag, BanditReference } from './interfaces'; |
| 4 | + |
| 5 | +export type ConfigurationRequestorOptions = { |
| 6 | + wantsBandits?: boolean; |
| 7 | +}; |
9 | 8 |
|
10 | 9 | // Requests AND stores flag configurations |
11 | 10 | export default class ConfigurationRequestor { |
12 | | - private banditModelVersions: string[] = []; |
13 | | - private readonly configuration: StoreBackedConfiguration; |
| 11 | + private readonly options: ConfigurationRequestorOptions; |
14 | 12 |
|
15 | 13 | constructor( |
16 | 14 | private readonly httpClient: IHttpClient, |
17 | | - private readonly flagConfigurationStore: IConfigurationStore<Flag>, |
18 | | - private readonly banditVariationConfigurationStore: IConfigurationStore< |
19 | | - BanditVariation[] |
20 | | - > | null, |
21 | | - private readonly banditModelConfigurationStore: IConfigurationStore<BanditParameters> | null, |
| 15 | + private readonly configurationStore: ConfigurationStore, |
| 16 | + options: Partial<ConfigurationRequestorOptions> = {}, |
22 | 17 | ) { |
23 | | - this.configuration = new StoreBackedConfiguration( |
24 | | - this.flagConfigurationStore, |
25 | | - this.banditVariationConfigurationStore, |
26 | | - this.banditModelConfigurationStore, |
27 | | - ); |
28 | | - } |
29 | | - |
30 | | - public isFlagConfigExpired(): Promise<boolean> { |
31 | | - return this.flagConfigurationStore.isExpired(); |
32 | | - } |
33 | | - |
34 | | - public getConfiguration(): IConfiguration { |
35 | | - return this.configuration; |
| 18 | + this.options = { |
| 19 | + wantsBandits: true, |
| 20 | + ...options, |
| 21 | + }; |
36 | 22 | } |
37 | 23 |
|
38 | | - async fetchAndStoreConfigurations(): Promise<void> { |
| 24 | + async fetchConfiguration(): Promise<Configuration | null> { |
39 | 25 | const configResponse = await this.httpClient.getUniversalFlagConfiguration(); |
40 | | - if (!configResponse?.flags) { |
41 | | - return; |
42 | | - } |
43 | | - |
44 | | - const flagResponsePacket: ConfigStoreHydrationPacket<Flag> = { |
45 | | - entries: configResponse.flags, |
46 | | - environment: configResponse.environment, |
47 | | - createdAt: configResponse.createdAt, |
48 | | - format: configResponse.format, |
49 | | - }; |
50 | | - |
51 | | - let banditVariationPacket: ConfigStoreHydrationPacket<BanditVariation[]> | undefined; |
52 | | - let banditModelPacket: ConfigStoreHydrationPacket<BanditParameters> | undefined; |
53 | | - const flagsHaveBandits = Object.keys(configResponse.banditReferences ?? {}).length > 0; |
54 | | - const banditStoresProvided = Boolean( |
55 | | - this.banditVariationConfigurationStore && this.banditModelConfigurationStore, |
56 | | - ); |
57 | | - if (flagsHaveBandits && banditStoresProvided) { |
58 | | - // Map bandit flag associations by flag key for quick lookup (instead of bandit key as provided by the UFC) |
59 | | - const banditVariations = this.indexBanditVariationsByFlagKey(configResponse.banditReferences); |
60 | | - |
61 | | - banditVariationPacket = { |
62 | | - entries: banditVariations, |
63 | | - environment: configResponse.environment, |
64 | | - createdAt: configResponse.createdAt, |
65 | | - format: configResponse.format, |
66 | | - }; |
67 | | - |
68 | | - if ( |
69 | | - this.requiresBanditModelConfigurationStoreUpdate( |
70 | | - this.banditModelVersions, |
71 | | - configResponse.banditReferences, |
72 | | - ) |
73 | | - ) { |
74 | | - const banditResponse = await this.httpClient.getBanditParameters(); |
75 | | - if (banditResponse?.bandits) { |
76 | | - banditModelPacket = { |
77 | | - entries: banditResponse.bandits, |
78 | | - environment: configResponse.environment, |
79 | | - createdAt: configResponse.createdAt, |
80 | | - format: configResponse.format, |
81 | | - }; |
82 | | - |
83 | | - this.banditModelVersions = this.getLoadedBanditModelVersions(banditResponse.bandits); |
84 | | - } |
85 | | - } |
| 26 | + if (!configResponse?.response.flags) { |
| 27 | + return null; |
86 | 28 | } |
87 | 29 |
|
88 | | - if ( |
89 | | - await this.configuration.hydrateConfigurationStores( |
90 | | - flagResponsePacket, |
91 | | - banditVariationPacket, |
92 | | - banditModelPacket, |
93 | | - ) |
94 | | - ) { |
95 | | - // TODO: Notify that config updated. |
96 | | - } |
97 | | - } |
| 30 | + const needsBandits = |
| 31 | + this.options.wantsBandits && |
| 32 | + Object.keys(configResponse.response.banditReferences ?? {}).length > 0; |
98 | 33 |
|
99 | | - private getLoadedBanditModelVersions(entries: Record<string, BanditParameters>): string[] { |
100 | | - return Object.values(entries).map((banditParam: BanditParameters) => banditParam.modelVersion); |
101 | | - } |
| 34 | + const banditsConfig = needsBandits ? await this.httpClient.getBanditParameters() : undefined; |
102 | 35 |
|
103 | | - private requiresBanditModelConfigurationStoreUpdate( |
104 | | - currentBanditModelVersions: string[], |
105 | | - banditReferences: Record<string, BanditReference>, |
106 | | - ): boolean { |
107 | | - const referencedModelVersions = Object.values(banditReferences).map( |
108 | | - (banditReference: BanditReference) => banditReference.modelVersion, |
109 | | - ); |
110 | | - |
111 | | - return !referencedModelVersions.every((modelVersion) => |
112 | | - currentBanditModelVersions.includes(modelVersion), |
113 | | - ); |
| 36 | + return Configuration.fromResponses({ |
| 37 | + flags: configResponse, |
| 38 | + bandits: banditsConfig, |
| 39 | + }); |
114 | 40 | } |
115 | 41 |
|
116 | | - private indexBanditVariationsByFlagKey( |
117 | | - banditVariationsByBanditKey: Record<string, BanditReference>, |
118 | | - ): Record<string, BanditVariation[]> { |
119 | | - const banditVariationsByFlagKey: Record<string, BanditVariation[]> = {}; |
120 | | - Object.values(banditVariationsByBanditKey).forEach((banditReference) => { |
121 | | - banditReference.flagVariations.forEach((banditVariation) => { |
122 | | - let banditVariations = banditVariationsByFlagKey[banditVariation.flagKey]; |
123 | | - if (!banditVariations) { |
124 | | - banditVariations = []; |
125 | | - banditVariationsByFlagKey[banditVariation.flagKey] = banditVariations; |
126 | | - } |
127 | | - banditVariations.push(banditVariation); |
128 | | - }); |
129 | | - }); |
130 | | - return banditVariationsByFlagKey; |
| 42 | + async fetchAndStoreConfigurations(): Promise<void> { |
| 43 | + const configuration = await this.fetchConfiguration(); |
| 44 | + if (configuration) { |
| 45 | + this.configurationStore.setConfiguration(configuration); |
| 46 | + } |
131 | 47 | } |
132 | 48 | } |
0 commit comments