mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 01:15:46 +08:00

* Add alertingRuleVersionHistory feature toggle * WIP: Add version history tab * revert temp change in index.ts * wip2 * --wip-- * sync code with the BE changes in the endpoint * add translations * Add translations * use ff only for restore feature * WIP: Add tracking, make version required, and start mapping dif results Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com> * Tweak more translations and improve types * Add button to show/hide JSON diff * update type for top level rule fields * Create types * Make updated_by/version properties optional * Update mocks to remove updated by and version * add comments to restore code * rename fetature flag, as we use this one only for the restore feature * Update version history to handle special cases * Add diff numbers * Fix conflicts * Move generic computeVersionDiff to a utils file * Update DOM structure of version summary and tidy up types * Add tests for version comparison logic * Lint fix utils file * Rename props and add docs * Change to EmptyState and log when no versions * Remove CreatedBy component and simplify * Add missing i18n for version history * add test for computeVersionDiff * update test * fix number diff order and add a test * fix prettier * fix prettier * Add promise resolve back in * Rename to humanReadableDiff and tweak translation * Show tab for recording rules as well * Split components out to separate files * Add optional interval seconds * Update i18n * Remove commented code * Remove value * Remove unneeded version * Consistent rendering of updated by * Mode parseVersionInfo to a separate pure function * update invalidate/provide tags for getAlertVersionHistory * Use checkedVersions state only in the parent component * update getSpecialUidMap name and create an interface * Fix prettier * update tab description * use set instead of map for checkedVersions --------- Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
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,
|
|
};
|
|
}
|