Skip to content

Commit b95e3df

Browse files
polling bandits only when needed
1 parent e50d8fd commit b95e3df

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

src/configuration-requestor.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,57 @@ export default class ConfigurationRequestor {
4141
createdAt: configResponse.createdAt,
4242
});
4343

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

51-
await this.hydrateConfigurationStore(this.banditModelConfigurationStore, {
52-
entries: banditResponse.bandits,
53-
environment: configResponse.environment,
54-
createdAt: configResponse.createdAt,
55-
});
51+
await this.hydrateConfigurationStore(this.banditModelConfigurationStore, {
52+
entries: banditResponse.bandits,
53+
environment: configResponse.environment,
54+
createdAt: configResponse.createdAt,
55+
});
56+
}
5657
}
5758
}
5859
}
5960

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

src/http-client.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import ApiEndpoints from './api-endpoints';
2-
import { BanditParameters, BanditVariation, Environment, Flag } from './interfaces';
2+
import {
3+
BanditParameters,
4+
BanditReference,
5+
BanditVariation,
6+
Environment,
7+
Flag,
8+
} from './interfaces';
39

410
export interface IQueryParams {
511
apiKey: string;
@@ -21,6 +27,7 @@ export interface IUniversalFlagConfigResponse {
2127
environment: Environment;
2228
flags: Record<string, Flag>;
2329
bandits: Record<string, BanditVariation[]>;
30+
banditReferences: Record<string, BanditReference>;
2431
}
2532

2633
export interface IBanditParametersResponse {

src/interfaces.ts

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

102+
export interface BanditReference {
103+
modelVersion: string;
104+
flagVariation: BanditVariation[];
105+
}
106+
102107
export interface BanditParameters {
103108
banditKey: string;
104109
modelName: string;

0 commit comments

Comments
 (0)