Skip to content

Commit

Permalink
Fetch and store precomputed flags
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerank committed Nov 18, 2024
1 parent 97b3531 commit 6605107
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/api-endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { BASE_URL as DEFAULT_BASE_URL, UFC_ENDPOINT, BANDIT_ENDPOINT } from './constants';
import {
BASE_URL as DEFAULT_BASE_URL,
UFC_ENDPOINT,
BANDIT_ENDPOINT,
PRECOMPUTED_FLAGS_ENDPOINT,
} from './constants';
import { IQueryParams } from './http-client';

interface IApiEndpointsParams {
Expand Down Expand Up @@ -27,4 +32,8 @@ export default class ApiEndpoints {
banditParametersEndpoint(): URL {
return this.endpoint(BANDIT_ENDPOINT);
}

precomputedFlagsEndpoint(): URL {
return this.endpoint(PRECOMPUTED_FLAGS_ENDPOINT);
}
}
24 changes: 22 additions & 2 deletions src/configuration-requestor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { IConfigurationStore } from './configuration-store/configuration-store';
import { IHttpClient } from './http-client';
import { BanditVariation, BanditParameters, Flag, Environment } from './interfaces';
import {
BanditVariation,
BanditParameters,
Flag,
Environment,
PrecomputedFlag,
} from './interfaces';

type Entry = Flag | BanditVariation[] | BanditParameters;
type Entry = Flag | BanditVariation[] | BanditParameters | PrecomputedFlag;

// Requests AND stores flag configurations
export default class ConfigurationRequestor {
Expand All @@ -12,6 +18,7 @@ export default class ConfigurationRequestor {
private readonly banditVariationConfigurationStore: IConfigurationStore<
BanditVariation[]
> | null,
private readonly precomputedFlagStore: IConfigurationStore<PrecomputedFlag>,
private readonly banditModelConfigurationStore: IConfigurationStore<BanditParameters> | null,
) {}

Expand Down Expand Up @@ -91,4 +98,17 @@ export default class ConfigurationRequestor {
});
return banditVariationsByFlagKey;
}

async fetchAndStorePrecomputedFlags(): Promise<void> {
const precomputedResponse = await this.httpClient.getPrecomputedFlags();
if (!precomputedResponse?.flags) {
return;
}

await this.hydrateConfigurationStore(this.precomputedFlagStore, {
entries: precomputedResponse.flags,
environment: precomputedResponse.environment,
createdAt: precomputedResponse.createdAt,
});
}
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const DEFAULT_POLL_CONFIG_REQUEST_RETRIES = 7;
export const BASE_URL = 'https://fscdn.eppo.cloud/api';
export const UFC_ENDPOINT = '/flag-config/v1/config';
export const BANDIT_ENDPOINT = '/flag-config/v1/bandits';
export const PRECOMPUTED_FLAGS_ENDPOINT = '/flag-config/v1/precomputed';
export const SESSION_ASSIGNMENT_CONFIG_LOADED = 'eppo-session-assignment-config-loaded';
export const NULL_SENTINEL = 'EPPO_NULL';
// number of logging events that may be queued while waiting for initialization
Expand Down
6 changes: 6 additions & 0 deletions src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface IPrecomputedFlagsResponse {
export interface IHttpClient {
getUniversalFlagConfiguration(): Promise<IUniversalFlagConfigResponse | undefined>;
getBanditParameters(): Promise<IBanditParametersResponse | undefined>;
getPrecomputedFlags(): Promise<IPrecomputedFlagsResponse | undefined>;
rawGet<T>(url: URL): Promise<T | undefined>;
}

Expand All @@ -61,6 +62,11 @@ export default class FetchHttpClient implements IHttpClient {
return await this.rawGet<IBanditParametersResponse>(url);
}

async getPrecomputedFlags(): Promise<IPrecomputedFlagsResponse | undefined> {
const url = this.apiEndpoints.precomputedFlagsEndpoint();
return await this.rawGet<IPrecomputedFlagsResponse>(url);
}

async rawGet<T>(url: URL): Promise<T | undefined> {
try {
// Canonical implementation of abortable fetch for interrupting when request takes longer than desired.
Expand Down

0 comments on commit 6605107

Please sign in to comment.