From 7d656153359d8829cba5e4026b863fe6cd3955e3 Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Mon, 30 Oct 2023 13:30:21 +0100 Subject: [PATCH] refactor: remove deep diff (#5217) For a while we ran a diffing algorithm in production to verify that the results of the refactor did not differ from the previous results. As the experiment has run it's course and new attributes have been added on top of the new flow, this will remove the logging and associated code. --- package.json | 1 - src/lib/features/feature-toggle/deep-diff.ts | 67 ---------------- .../feature-toggle/feature-toggle-service.ts | 34 -------- .../feature-toggle/tests/deep-diff.test.ts | 79 ------------------- 4 files changed, 181 deletions(-) delete mode 100644 src/lib/features/feature-toggle/deep-diff.ts delete mode 100644 src/lib/features/feature-toggle/tests/deep-diff.test.ts diff --git a/package.json b/package.json index 6e4e2b7b7197..af88746cd7ca 100644 --- a/package.json +++ b/package.json @@ -122,7 +122,6 @@ "knex": "^2.5.1", "lodash.get": "^4.4.2", "lodash.groupby": "^4.6.0", - "lodash.isequal": "^4.5.0", "lodash.sortby": "^4.7.0", "log4js": "^6.0.0", "make-fetch-happen": "^11.0.0", diff --git a/src/lib/features/feature-toggle/deep-diff.ts b/src/lib/features/feature-toggle/deep-diff.ts deleted file mode 100644 index 9a80c8d09507..000000000000 --- a/src/lib/features/feature-toggle/deep-diff.ts +++ /dev/null @@ -1,67 +0,0 @@ -import sortBy from 'lodash.sortby'; - -interface Difference { - index: (string | number)[]; - reason: string; - valueA: any; - valueB: any; -} - -export function deepDiff(arr1: any[], arr2: any[]): Difference[] | null { - const diff: Difference[] = []; - - function compare(a: any, b: any, parentIndex: (string | number)[]): void { - if (Array.isArray(a) && Array.isArray(b)) { - if (a.length !== b.length) { - diff.push({ - index: parentIndex, - reason: 'Different lengths', - valueA: a, - valueB: b, - }); - } else { - const sortedA = sortBy(a, 'name'); - const sortedB = sortBy(b, 'name'); - - for (let i = 0; i < sortedA.length; i++) { - compare(sortedA[i], sortedB[i], parentIndex.concat(i)); - } - } - } else if ( - typeof a === 'object' && - a !== null && - typeof b === 'object' && - b !== null - ) { - const keysA = Object.keys(a); - const keysB = Object.keys(b); - - if ( - keysA.length !== keysB.length || - !keysA.every((key) => keysB.includes(key)) - ) { - diff.push({ - index: parentIndex, - reason: 'Different keys', - valueA: a, - valueB: b, - }); - } else { - for (const key of keysA) { - compare(a[key], b[key], parentIndex.concat(key)); - } - } - } else if (a !== b) { - diff.push({ - index: parentIndex, - reason: 'Different values', - valueA: a, - valueB: b, - }); - } - } - - compare(arr1, arr2, []); - - return diff.length > 0 ? diff : null; -} diff --git a/src/lib/features/feature-toggle/feature-toggle-service.ts b/src/lib/features/feature-toggle/feature-toggle-service.ts index 6f8a6208ed56..ecebff77fe8b 100644 --- a/src/lib/features/feature-toggle/feature-toggle-service.ts +++ b/src/lib/features/feature-toggle/feature-toggle-service.ts @@ -102,8 +102,6 @@ import { IPrivateProjectChecker } from '../private-project/privateProjectChecker import { IDependentFeaturesReadModel } from '../dependent-features/dependent-features-read-model-type'; import EventService from '../../services/event-service'; import { DependentFeaturesService } from '../dependent-features/dependent-features-service'; -import isEqual from 'lodash.isequal'; -import { deepDiff } from './deep-diff'; interface IFeatureContext { featureName: string; @@ -1058,22 +1056,6 @@ class FeatureToggleService { await this.featureToggleStore.getPlaygroundFeatures(query), ]); - const equal = isEqual( - featuresFromClientStore, - featuresFromFeatureToggleStore, - ); - - if (!equal) { - const difference = deepDiff( - featuresFromClientStore, - featuresFromFeatureToggleStore, - ); - this.logger.warn( - 'getPlaygroundFeatures: features from client-feature-toggle-store is not equal to features from feature-toggle-store', - difference, - ); - } - const features = this.flagResolver.isEnabled('separateAdminClientApi') ? featuresFromFeatureToggleStore : featuresFromClientStore; @@ -1109,22 +1091,6 @@ class FeatureToggleService { ), ]); - const equal = isEqual( - featuresFromClientStore, - featuresFromFeatureToggleStore, - ); - - if (!equal) { - const difference = deepDiff( - featuresFromClientStore, - featuresFromFeatureToggleStore, - ); - this.logger.warn( - 'getFeatureToggles: features from client-feature-toggle-store is not equal to features from feature-toggle-store diff', - difference, - ); - } - const features = this.flagResolver.isEnabled('separateAdminClientApi') ? featuresFromFeatureToggleStore : featuresFromClientStore; diff --git a/src/lib/features/feature-toggle/tests/deep-diff.test.ts b/src/lib/features/feature-toggle/tests/deep-diff.test.ts deleted file mode 100644 index 228a095b39fa..000000000000 --- a/src/lib/features/feature-toggle/tests/deep-diff.test.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { deepDiff } from '../deep-diff'; // Import the deepDiff function - -describe('deepDiff', () => { - test('should sort arrays by name before comparing', () => { - // Define two arrays that are identical except for the order of elements - const array1 = [ - { name: 'b', value: 2 }, - { name: 'a', value: 1 }, - ]; - const array2 = [ - { name: 'a', value: 1 }, - { name: 'b', value: 2 }, - ]; - - // If the function correctly sorts before comparing, there should be no differences - const result = deepDiff(array1, array2); - - // Assert that there is no difference - expect(result).toBeNull(); - }); - - it('should return null for equal arrays', () => { - const arr1 = [1, 2, 3]; - const arr2 = [1, 2, 3]; - expect(deepDiff(arr1, arr2)).toBe(null); - }); - - it('should find differences in arrays with different lengths', () => { - const arr1 = [1, 2, 3]; - const arr2 = [1, 2, 3, 4]; - expect(deepDiff(arr1, arr2)).toEqual([ - { - index: [], - reason: 'Different lengths', - valueA: arr1, - valueB: arr2, - }, - ]); - }); - - it('should find differences in arrays with different values', () => { - const arr1 = [1, 2, 3]; - const arr2 = [1, 4, 3]; - expect(deepDiff(arr1, arr2)).toEqual([ - { - index: [1], - reason: 'Different values', - valueA: 2, - valueB: 4, - }, - ]); - }); - - it('should find differences in arrays with different keys in objects', () => { - const arr1 = [{ a: 1 }, { b: 2 }]; - const arr2 = [{ a: 1 }, { c: 2 }]; - expect(deepDiff(arr1, arr2)).toEqual([ - { - index: [1], - reason: 'Different keys', - valueA: { b: 2 }, - valueB: { c: 2 }, - }, - ]); - }); - - it('should handle nested differences in objects', () => { - const arr1 = [{ a: { b: 1 } }, { c: { d: 2 } }]; - const arr2 = [{ a: { b: 1 } }, { c: { d: 3 } }]; - expect(deepDiff(arr1, arr2)).toEqual([ - { - index: [1, 'c', 'd'], - reason: 'Different values', - valueA: 2, - valueB: 3, - }, - ]); - }); -});