@@ -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 > ,
@@ -34,13 +36,13 @@ export default class ConfigurationRequestor {
34
36
format : configResponse . format ,
35
37
} ) ;
36
38
37
- const flagsHaveBandits = Object . keys ( configResponse . bandits ?? { } ) . length > 0 ;
39
+ const flagsHaveBandits = Object . keys ( configResponse . banditReferences ?? { } ) . length > 0 ;
38
40
const banditStoresProvided = Boolean (
39
41
this . banditVariationConfigurationStore && this . banditModelConfigurationStore ,
40
42
) ;
41
43
if ( flagsHaveBandits && banditStoresProvided ) {
42
44
// Map bandit flag associations by flag key for quick lookup (instead of bandit key as provided by the UFC)
43
- const banditVariations = this . indexBanditVariationsByFlagKey ( configResponse . bandits ) ;
45
+ const banditVariations = this . indexBanditVariationsByFlagKey ( configResponse . banditReferences ) ;
44
46
45
47
await hydrateConfigurationStore ( this . banditVariationConfigurationStore , {
46
48
entries : banditVariations ,
@@ -49,26 +51,34 @@ export default class ConfigurationRequestor {
49
51
format : configResponse . format ,
50
52
} ) ;
51
53
52
- if ( this . requiresBanditModelConfigurationStoreUpdate ( configResponse . banditReferences ) ) {
54
+ if ( ! this . banditModelConfigurationStore ) {
55
+ throw new Error ( 'Bandit parameters fetched but no bandit configuration store provided' ) ;
56
+ }
57
+ if (
58
+ this . requiresBanditModelConfigurationStoreUpdate (
59
+ this . banditModelVersions ,
60
+ configResponse . banditReferences ,
61
+ )
62
+ ) {
53
63
const banditResponse = await this . httpClient . getBanditParameters ( ) ;
54
64
if ( banditResponse ?. bandits ) {
55
- if ( ! this . banditModelConfigurationStore ) {
56
- throw new Error ( 'Bandit parameters fetched but no bandit configuration store provided' ) ;
57
- }
58
-
59
65
await hydrateConfigurationStore ( this . banditModelConfigurationStore , {
60
66
entries : banditResponse . bandits ,
61
67
environment : configResponse . environment ,
62
68
createdAt : configResponse . createdAt ,
63
69
format : configResponse . format , } ) ;
70
+
71
+ this . setBanditModelVersions (
72
+ this . getLoadedBanditModelVersionsFromStore ( this . banditModelConfigurationStore ) ,
73
+ ) ;
64
74
}
65
75
}
66
76
}
67
77
}
68
78
69
- private getLoadedBanditModelVersions (
79
+ private getLoadedBanditModelVersionsFromStore (
70
80
banditModelConfigurationStore : IConfigurationStore < BanditParameters > | null ,
71
- ) {
81
+ ) : string [ ] {
72
82
if ( banditModelConfigurationStore === null ) {
73
83
return [ ] ;
74
84
}
@@ -77,22 +87,20 @@ export default class ConfigurationRequestor {
77
87
) ;
78
88
}
79
89
90
+ private setBanditModelVersions ( modelVersions : string [ ] ) {
91
+ this . banditModelVersions = modelVersions ;
92
+ }
93
+
80
94
private requiresBanditModelConfigurationStoreUpdate (
95
+ currentBanditModelVersions : string [ ] ,
81
96
banditReferences : Record < string , BanditReference > ,
82
97
) : boolean {
83
- if ( ! this . banditModelConfigurationStore ) {
84
- throw new Error ( 'Bandit parameters fetched but no bandit configuration store provided' ) ;
85
- }
86
98
const referencedModelVersions = Object . values ( banditReferences ) . map (
87
99
( banditReference : BanditReference ) => banditReference . modelVersion ,
88
100
) ;
89
101
90
- const banditModelVersionsInStore = this . getLoadedBanditModelVersions (
91
- this . banditModelConfigurationStore ,
92
- ) ;
93
-
94
102
referencedModelVersions . forEach ( ( modelVersion ) => {
95
- if ( ! banditModelVersionsInStore . includes ( modelVersion ) ) {
103
+ if ( ! currentBanditModelVersions . includes ( modelVersion ) ) {
96
104
return false ;
97
105
}
98
106
} ) ;
@@ -101,11 +109,11 @@ export default class ConfigurationRequestor {
101
109
}
102
110
103
111
private indexBanditVariationsByFlagKey (
104
- banditVariationsByBanditKey : Record < string , BanditVariation [ ] > ,
112
+ banditVariationsByBanditKey : Record < string , BanditReference > ,
105
113
) : Record < string , BanditVariation [ ] > {
106
114
const banditVariationsByFlagKey : Record < string , BanditVariation [ ] > = { } ;
107
- Object . values ( banditVariationsByBanditKey ) . forEach ( ( banditVariations ) => {
108
- banditVariations . forEach ( ( banditVariation ) => {
115
+ Object . values ( banditVariationsByBanditKey ) . forEach ( ( banditReference ) => {
116
+ banditReference . flagVariations . forEach ( ( banditVariation ) => {
109
117
let banditVariations = banditVariationsByFlagKey [ banditVariation . flagKey ] ;
110
118
if ( ! banditVariations ) {
111
119
banditVariations = [ ] ;
0 commit comments