mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-19 01:17:18 +02:00
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.
This commit is contained in:
parent
50ddb365b9
commit
7d65615335
@ -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",
|
||||
|
@ -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;
|
||||
}
|
@ -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;
|
||||
|
@ -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,
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user