The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/features/alerting/unified/utils/diff.ts

37 lines
1.0 KiB

import { chain, identity } from 'lodash';
import { jsonDiff } from 'app/features/dashboard-scene/settings/version-history/utils';
export type Diff = {
added: number;
removed: number;
};
export function computeVersionDiff<T extends Object>(
json1: T,
json2: T,
normalizeFunction: (item: T) => Object = identity
): Diff {
const cleanedJson1 = normalizeFunction(json1);
const cleanedJson2 = normalizeFunction(json2);
const diff = jsonDiff(cleanedJson1, cleanedJson2);
const added = chain(diff)
.values()
.flatMap()
.filter((operation) => operation.op === 'add' || operation.op === 'replace' || operation.op === 'move')
.sumBy((operation) => operation.endLineNumber - operation.startLineNumber + 1)
.value();
const removed = chain(diff)
.values()
.flatMap()
.filter((operation) => operation.op === 'remove' || operation.op === 'replace')
.sumBy((operation) => operation.endLineNumber - operation.startLineNumber + 1)
.value();
return {
added,
removed,
};
}