@@ -12,6 +12,8 @@ type Entry = Flag | BanditVariation[] | BanditParameters;
12
12
13
13
// Requests AND stores flag configurations
14
14
export default class ConfigurationRequestor {
15
+ private banditModelVersions : string [ ] = [ ] ;
16
+
15
17
constructor (
16
18
private readonly httpClient : IHttpClient ,
17
19
private readonly flagConfigurationStore : IConfigurationStore < Flag > ,
@@ -33,40 +35,48 @@ export default class ConfigurationRequestor {
33
35
createdAt : configResponse . createdAt ,
34
36
} ) ;
35
37
36
- const flagsHaveBandits = Object . keys ( configResponse . bandits ?? { } ) . length > 0 ;
38
+ const flagsHaveBandits = Object . keys ( configResponse . banditReferences ?? { } ) . length > 0 ;
37
39
const banditStoresProvided = Boolean (
38
40
this . banditVariationConfigurationStore && this . banditModelConfigurationStore ,
39
41
) ;
40
42
if ( flagsHaveBandits && banditStoresProvided ) {
41
43
// Map bandit flag associations by flag key for quick lookup (instead of bandit key as provided by the UFC)
42
- const banditVariations = this . indexBanditVariationsByFlagKey ( configResponse . bandits ) ;
44
+ const banditVariations = this . indexBanditVariationsByFlagKey ( configResponse . banditReferences ) ;
43
45
44
46
await this . hydrateConfigurationStore ( this . banditVariationConfigurationStore , {
45
47
entries : banditVariations ,
46
48
environment : configResponse . environment ,
47
49
createdAt : configResponse . createdAt ,
48
50
} ) ;
49
51
50
- if ( this . requiresBanditModelConfigurationStoreUpdate ( configResponse . banditReferences ) ) {
52
+ if ( ! this . banditModelConfigurationStore ) {
53
+ throw new Error ( 'Bandit parameters fetched but no bandit configuration store provided' ) ;
54
+ }
55
+ if (
56
+ this . requiresBanditModelConfigurationStoreUpdate (
57
+ this . banditModelVersions ,
58
+ configResponse . banditReferences ,
59
+ )
60
+ ) {
51
61
const banditResponse = await this . httpClient . getBanditParameters ( ) ;
52
62
if ( banditResponse ?. bandits ) {
53
- if ( ! this . banditModelConfigurationStore ) {
54
- throw new Error ( 'Bandit parameters fetched but no bandit configuration store provided' ) ;
55
- }
56
-
57
63
await this . hydrateConfigurationStore ( this . banditModelConfigurationStore , {
58
64
entries : banditResponse . bandits ,
59
65
environment : configResponse . environment ,
60
66
createdAt : configResponse . createdAt ,
61
67
} ) ;
68
+
69
+ this . setBanditModelVersions (
70
+ this . getLoadedBanditModelVersionsFromStore ( this . banditModelConfigurationStore ) ,
71
+ ) ;
62
72
}
63
73
}
64
74
}
65
75
}
66
76
67
- private getLoadedBanditModelVersions (
77
+ private getLoadedBanditModelVersionsFromStore (
68
78
banditModelConfigurationStore : IConfigurationStore < BanditParameters > | null ,
69
- ) {
79
+ ) : string [ ] {
70
80
if ( banditModelConfigurationStore === null ) {
71
81
return [ ] ;
72
82
}
@@ -75,22 +85,20 @@ export default class ConfigurationRequestor {
75
85
) ;
76
86
}
77
87
88
+ private setBanditModelVersions ( modelVersions : string [ ] ) {
89
+ this . banditModelVersions = modelVersions ;
90
+ }
91
+
78
92
private requiresBanditModelConfigurationStoreUpdate (
93
+ currentBanditModelVersions : string [ ] ,
79
94
banditReferences : Record < string , BanditReference > ,
80
95
) : boolean {
81
- if ( ! this . banditModelConfigurationStore ) {
82
- throw new Error ( 'Bandit parameters fetched but no bandit configuration store provided' ) ;
83
- }
84
96
const referencedModelVersions = Object . values ( banditReferences ) . map (
85
97
( banditReference : BanditReference ) => banditReference . modelVersion ,
86
98
) ;
87
99
88
- const banditModelVersionsInStore = this . getLoadedBanditModelVersions (
89
- this . banditModelConfigurationStore ,
90
- ) ;
91
-
92
100
referencedModelVersions . forEach ( ( modelVersion ) => {
93
- if ( ! banditModelVersionsInStore . includes ( modelVersion ) ) {
101
+ if ( ! currentBanditModelVersions . includes ( modelVersion ) ) {
94
102
return false ;
95
103
}
96
104
} ) ;
@@ -117,11 +125,11 @@ export default class ConfigurationRequestor {
117
125
}
118
126
119
127
private indexBanditVariationsByFlagKey (
120
- banditVariationsByBanditKey : Record < string , BanditVariation [ ] > ,
128
+ banditVariationsByBanditKey : Record < string , BanditReference > ,
121
129
) : Record < string , BanditVariation [ ] > {
122
130
const banditVariationsByFlagKey : Record < string , BanditVariation [ ] > = { } ;
123
- Object . values ( banditVariationsByBanditKey ) . forEach ( ( banditVariations ) => {
124
- banditVariations . forEach ( ( banditVariation ) => {
131
+ Object . values ( banditVariationsByBanditKey ) . forEach ( ( banditReference ) => {
132
+ banditReference . flagVariations . forEach ( ( banditVariation ) => {
125
133
let banditVariations = banditVariationsByFlagKey [ banditVariation . flagKey ] ;
126
134
if ( ! banditVariations ) {
127
135
banditVariations = [ ] ;
0 commit comments