From 6e76ef481f5664d223cccfa607108e7db87fa5b4 Mon Sep 17 00:00:00 2001 From: Stephen Royal Date: Tue, 5 Dec 2023 11:30:00 -0800 Subject: [PATCH] Add Full Deltas Logging --- src/StatsigNetwork.ts | 6 ++++++ src/StatsigStore.ts | 31 +++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/StatsigNetwork.ts b/src/StatsigNetwork.ts index 4144668..feedb0e 100644 --- a/src/StatsigNetwork.ts +++ b/src/StatsigNetwork.ts @@ -69,6 +69,8 @@ export default class StatsigNetwork { previousDerivedFields?: Record; hadBadDeltaChecksum?: boolean; badChecksum?: string; + badMergedConfigs?: Record; + badFullResponse?: Record; }): PromiseWithTimeout> { const { user, @@ -79,6 +81,8 @@ export default class StatsigNetwork { previousDerivedFields, hadBadDeltaChecksum, badChecksum, + badMergedConfigs, + badFullResponse, } = args; const input = { user, @@ -90,6 +94,8 @@ export default class StatsigNetwork { previousDerivedFields: previousDerivedFields, hadBadDeltaChecksum: hadBadDeltaChecksum, badChecksum: badChecksum, + badMergedConfigs: badMergedConfigs, + badFullResponse: badFullResponse, }; return this.postWithTimeout(StatsigEndpoint.Initialize, input, { diff --git a/src/StatsigStore.ts b/src/StatsigStore.ts index 1802498..902f49b 100644 --- a/src/StatsigStore.ts +++ b/src/StatsigStore.ts @@ -70,6 +70,11 @@ type APIInitializeDataWithDeltas = APIInitializeData & { deleted_layers?: string[]; is_delta?: boolean; checksum?: string; + deltas_full_response?: { + dynamic_configs: Record; + feature_gates: Record; + layer_configs: Record; + }; }; type APIInitializeDataWithPrefetchedUsers = APIInitializeData & { @@ -477,11 +482,12 @@ export default class StatsigStore { mergedValues[requestedUserCacheKey.v1]; removeDeletedKeysFromUserValues(initResponse, userValues); const expectedFullHash = initResponse.checksum; - const currentFullHash = djb2HashForObject({ + const mergedConfigs = { feature_gates: userValues.feature_gates, dynamic_configs: userValues.dynamic_configs, layer_configs: userValues.layer_configs, - }); + }; + const currentFullHash = djb2HashForObject(mergedConfigs); if (expectedFullHash && expectedFullHash !== currentFullHash) { hasBadHash = true; badChecksum = currentFullHash; @@ -492,6 +498,23 @@ export default class StatsigStore { } if (hasBadHash || hashChanged) { + if (initResponse.deltas_full_response != null) { + // retry + this.refetchAndSaveValues( + user, + prefetchUsers, + undefined, + badChecksum, + hasBadHash, + mergedConfigs, + initResponse.deltas_full_response, + ).catch((reason) => + this.sdkInternal + .getErrorBoundary() + .logError('refetchAndSaveValues', reason), + ); + return; + } // retry this.refetchAndSaveValues( user, @@ -525,6 +548,8 @@ export default class StatsigStore { timeout: number = this.sdkInternal.getOptions().getInitTimeoutMs(), badChecksum?: string, hadBadChecksum?: boolean, + badMergedConfigs?: Record, + badFullResponse?: Record, ): Promise { const sinceTime = this.getLastUpdateTime(user); const previousDerivedFields = this.getPreviousDerivedFields(user); @@ -540,6 +565,8 @@ export default class StatsigStore { previousDerivedFields, hadBadDeltaChecksum: hadBadChecksum, badChecksum, + badMergedConfigs, + badFullResponse, }) .then((json) => { if (json?.has_updates) {