Skip to content

Commit

Permalink
feat: diff for delta
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaanus committed Jan 2, 2025
1 parent e0b4e25 commit e53993e
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
CLIENT_METRICS_NAMEPREFIX,
CLIENT_METRICS_TAGS,
} from '../../internals';
import isEqual from 'lodash.isequal';
import { diff } from 'json-diff';

const version = 2;

Expand Down Expand Up @@ -180,10 +182,39 @@ export default class FeatureController extends Controller {
featuresSize + segmentsSize,
);

await this.clientFeatureToggleService.getClientDelta(
undefined,
query!,
const delta =
await this.clientFeatureToggleService.getClientDelta(
undefined,
query!,
);

const sortedToggles = features.sort((a, b) =>
a.name.localeCompare(b.name),
);
const sortedNewToggles = delta?.updated.sort((a, b) =>
a.name.localeCompare(b.name),
);

const deepEqualIgnoreOrder = (obj1, obj2) => {
const sortedObj1 = JSON.parse(
JSON.stringify(obj1, Object.keys(obj1).sort()),
);
const sortedObj2 = JSON.parse(
JSON.stringify(obj2, Object.keys(obj2).sort()),
);
return isEqual(sortedObj1, sortedObj2);
};

if (!deepEqualIgnoreOrder(sortedToggles, sortedNewToggles)) {
this.logger.warn(
`old features and new features are different. Old count ${
features.length
}, new count ${delta?.updated.length}, query ${JSON.stringify(query)},
diff ${JSON.stringify(
diff(sortedToggles, sortedNewToggles),
)}`,
);
}
this.storeFootprint();
} catch (e) {
this.logger.error('Delta diff failed', e);
Expand Down

0 comments on commit e53993e

Please sign in to comment.