Skip to content

Commit e381963

Browse files
polling bandits only when needed
1 parent 83025f3 commit e381963

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/configuration-requestor.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Entry = Flag | BanditVariation[] | BanditParameters;
1212

1313
// Requests AND stores flag configurations
1414
export default class ConfigurationRequestor {
15+
private banditModelVersions: string[] = [];
16+
1517
constructor(
1618
private readonly httpClient: IHttpClient,
1719
private readonly flagConfigurationStore: IConfigurationStore<Flag>,
@@ -33,40 +35,48 @@ export default class ConfigurationRequestor {
3335
createdAt: configResponse.createdAt,
3436
});
3537

36-
const flagsHaveBandits = Object.keys(configResponse.bandits ?? {}).length > 0;
38+
const flagsHaveBandits = Object.keys(configResponse.banditReferences ?? {}).length > 0;
3739
const banditStoresProvided = Boolean(
3840
this.banditVariationConfigurationStore && this.banditModelConfigurationStore,
3941
);
4042
if (flagsHaveBandits && banditStoresProvided) {
4143
// 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);
4345

4446
await this.hydrateConfigurationStore(this.banditVariationConfigurationStore, {
4547
entries: banditVariations,
4648
environment: configResponse.environment,
4749
createdAt: configResponse.createdAt,
4850
});
4951

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+
) {
5161
const banditResponse = await this.httpClient.getBanditParameters();
5262
if (banditResponse?.bandits) {
53-
if (!this.banditModelConfigurationStore) {
54-
throw new Error('Bandit parameters fetched but no bandit configuration store provided');
55-
}
56-
5763
await this.hydrateConfigurationStore(this.banditModelConfigurationStore, {
5864
entries: banditResponse.bandits,
5965
environment: configResponse.environment,
6066
createdAt: configResponse.createdAt,
6167
});
68+
69+
this.setBanditModelVersions(
70+
this.getLoadedBanditModelVersionsFromStore(this.banditModelConfigurationStore),
71+
);
6272
}
6373
}
6474
}
6575
}
6676

67-
private getLoadedBanditModelVersions(
77+
private getLoadedBanditModelVersionsFromStore(
6878
banditModelConfigurationStore: IConfigurationStore<BanditParameters> | null,
69-
) {
79+
): string[] {
7080
if (banditModelConfigurationStore === null) {
7181
return [];
7282
}
@@ -75,22 +85,20 @@ export default class ConfigurationRequestor {
7585
);
7686
}
7787

88+
private setBanditModelVersions(modelVersions: string[]) {
89+
this.banditModelVersions = modelVersions;
90+
}
91+
7892
private requiresBanditModelConfigurationStoreUpdate(
93+
currentBanditModelVersions: string[],
7994
banditReferences: Record<string, BanditReference>,
8095
): boolean {
81-
if (!this.banditModelConfigurationStore) {
82-
throw new Error('Bandit parameters fetched but no bandit configuration store provided');
83-
}
8496
const referencedModelVersions = Object.values(banditReferences).map(
8597
(banditReference: BanditReference) => banditReference.modelVersion,
8698
);
8799

88-
const banditModelVersionsInStore = this.getLoadedBanditModelVersions(
89-
this.banditModelConfigurationStore,
90-
);
91-
92100
referencedModelVersions.forEach((modelVersion) => {
93-
if (!banditModelVersionsInStore.includes(modelVersion)) {
101+
if (!currentBanditModelVersions.includes(modelVersion)) {
94102
return false;
95103
}
96104
});
@@ -117,11 +125,11 @@ export default class ConfigurationRequestor {
117125
}
118126

119127
private indexBanditVariationsByFlagKey(
120-
banditVariationsByBanditKey: Record<string, BanditVariation[]>,
128+
banditVariationsByBanditKey: Record<string, BanditReference>,
121129
): Record<string, BanditVariation[]> {
122130
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) => {
125133
let banditVariations = banditVariationsByFlagKey[banditVariation.flagKey];
126134
if (!banditVariations) {
127135
banditVariations = [];

src/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export interface BanditVariation {
101101

102102
export interface BanditReference {
103103
modelVersion: string;
104-
flagVariation: BanditVariation[];
104+
flagVariations: BanditVariation[];
105105
}
106106

107107
export interface BanditParameters {

0 commit comments

Comments
 (0)