Skip to content

Commit 333d730

Browse files
polling bandits only when needed
1 parent 8c02dd3 commit 333d730

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

src/configuration-requestor.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,57 @@ export default class ConfigurationRequestor {
4242
format: configResponse.format,
4343
});
4444

45-
// TODO: different polling intervals for bandit parameters
46-
const banditResponse = await this.httpClient.getBanditParameters();
47-
if (banditResponse?.bandits) {
48-
if (!this.banditModelConfigurationStore) {
49-
throw new Error('Bandit parameters fetched but no bandit configuration store provided');
50-
}
45+
if (this.requiresBanditModelConfigurationStoreUpdate(configResponse.banditReferences)) {
46+
const banditResponse = await this.httpClient.getBanditParameters();
47+
if (banditResponse?.bandits) {
48+
if (!this.banditModelConfigurationStore) {
49+
throw new Error('Bandit parameters fetched but no bandit configuration store provided');
50+
}
5151

52-
await hydrateConfigurationStore(this.banditModelConfigurationStore, {
53-
entries: banditResponse.bandits,
54-
environment: configResponse.environment,
55-
createdAt: configResponse.createdAt,
56-
format: configResponse.format,
57-
});
52+
await hydrateConfigurationStore(this.banditModelConfigurationStore, {
53+
entries: banditResponse.bandits,
54+
environment: configResponse.environment,
55+
createdAt: configResponse.createdAt,
56+
format: configResponse.format,});
57+
}
5858
}
5959
}
6060
}
6161

62+
private getLoadedBanditModelVersions(
63+
banditModelConfigurationStore: IConfigurationStore<BanditParameters> | null,
64+
) {
65+
if (banditModelConfigurationStore === null) {
66+
return [];
67+
}
68+
return Object.values(banditModelConfigurationStore.entries()).map(
69+
(banditParam: BanditParameters) => banditParam.modelVersion,
70+
);
71+
}
72+
73+
private requiresBanditModelConfigurationStoreUpdate(
74+
banditReferences: Record<string, BanditReference>,
75+
): boolean {
76+
if (!this.banditModelConfigurationStore) {
77+
throw new Error('Bandit parameters fetched but no bandit configuration store provided');
78+
}
79+
const referencedModelVersions = Object.values(banditReferences).map(
80+
(banditReference: BanditReference) => banditReference.modelVersion
81+
);
82+
83+
const banditModelVersionsInStore = this.getLoadedBanditModelVersions(
84+
this.banditModelConfigurationStore,
85+
);
86+
87+
referencedModelVersions.forEach((modelVersion) => {
88+
if (!banditModelVersionsInStore.includes(modelVersion)) {
89+
return false;
90+
}
91+
});
92+
93+
return true;
94+
}
95+
6296
private indexBanditVariationsByFlagKey(
6397
banditVariationsByBanditKey: Record<string, BanditVariation[]>,
6498
): Record<string, BanditVariation[]> {

src/http-client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ApiEndpoints from './api-endpoints';
22
import {
33
BanditParameters,
4+
BanditReference,
45
BanditVariation,
56
Environment,
67
Flag,
@@ -36,6 +37,7 @@ export interface IUniversalFlagConfigResponse {
3637
environment: Environment;
3738
flags: Record<string, Flag>;
3839
bandits: Record<string, BanditVariation[]>;
40+
banditReferences: Record<string, BanditReference>;
3941
}
4042

4143
export interface IBanditParametersResponse {

src/interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ export interface BanditVariation {
101101
variationValue: string;
102102
}
103103

104+
export interface BanditReference {
105+
modelVersion: string;
106+
flagVariation: BanditVariation[];
107+
}
108+
104109
export interface BanditParameters {
105110
banditKey: string;
106111
modelName: string;

0 commit comments

Comments
 (0)