forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule model versions in alerting (elastic#171927)
Towards: elastic#166967 This PR adds `ruleModelVersion` to saved object registry and `latestRuleVersion` to ruleTypeRegistry. These new assets will be used in a follow-on PR for skipping the rule task executions when there is version mismatch. POC for the issue: elastic#167128 --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
45e88fe
commit 59eb614
Showing
12 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
x-pack/plugins/alerting/server/saved_objects/rule_model_versions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
CustomSavedObjectsModelVersionMap, | ||
getLatestRuleVersion, | ||
getMinimumCompatibleVersion, | ||
} from './rule_model_versions'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { RawRule } from '../types'; | ||
|
||
describe('rule model versions', () => { | ||
const ruleModelVersions: CustomSavedObjectsModelVersionMap = { | ||
'1': { | ||
changes: [], | ||
schemas: { | ||
create: schema.object({ | ||
name: schema.string(), | ||
}), | ||
}, | ||
isCompatibleWithPreviousVersion: (rawRule) => true, | ||
}, | ||
'2': { | ||
changes: [], | ||
schemas: { | ||
create: schema.object({ | ||
name: schema.string(), | ||
}), | ||
}, | ||
isCompatibleWithPreviousVersion: (rawRule) => false, | ||
}, | ||
'3': { | ||
changes: [], | ||
schemas: { | ||
create: schema.object({ | ||
name: schema.string(), | ||
}), | ||
}, | ||
isCompatibleWithPreviousVersion: (rawRule) => rawRule.name === 'test', | ||
}, | ||
'4': { | ||
changes: [], | ||
schemas: { | ||
create: schema.object({ | ||
name: schema.string(), | ||
}), | ||
}, | ||
isCompatibleWithPreviousVersion: (rawRule) => rawRule.name === 'test', | ||
}, | ||
}; | ||
|
||
const rawRule = { name: 'test' } as RawRule; | ||
const mismatchingRawRule = { enabled: true } as RawRule; | ||
|
||
describe('getMinimumCompatibleVersion', () => { | ||
it('should return the minimum compatible version for the matching rawRule', () => { | ||
expect(getMinimumCompatibleVersion(ruleModelVersions, 1, rawRule)).toBe(1); | ||
expect(getMinimumCompatibleVersion(ruleModelVersions, 2, rawRule)).toBe(2); | ||
expect(getMinimumCompatibleVersion(ruleModelVersions, 3, rawRule)).toBe(2); | ||
expect(getMinimumCompatibleVersion(ruleModelVersions, 4, rawRule)).toBe(2); | ||
}); | ||
it('should return the minimum compatible version for the mismatching rawRule', () => { | ||
expect(getMinimumCompatibleVersion(ruleModelVersions, 3, mismatchingRawRule)).toBe(3); | ||
expect(getMinimumCompatibleVersion(ruleModelVersions, 4, mismatchingRawRule)).toBe(4); | ||
}); | ||
}); | ||
|
||
describe('getLatestRuleVersion', () => { | ||
it('should return the latest rule model version', () => { | ||
expect(getLatestRuleVersion()).toBe(1); | ||
}); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
x-pack/plugins/alerting/server/saved_objects/rule_model_versions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
SavedObjectsModelVersion, | ||
SavedObjectsModelVersionMap, | ||
} from '@kbn/core-saved-objects-server'; | ||
import { RawRule } from '../types'; | ||
import { rawRuleSchemaV1 } from './schemas/raw_rule'; | ||
|
||
interface CustomSavedObjectsModelVersion extends SavedObjectsModelVersion { | ||
isCompatibleWithPreviousVersion: (param: RawRule) => boolean; | ||
} | ||
|
||
export interface CustomSavedObjectsModelVersionMap extends SavedObjectsModelVersionMap { | ||
[modelVersion: string]: CustomSavedObjectsModelVersion; | ||
} | ||
|
||
export const ruleModelVersions: CustomSavedObjectsModelVersionMap = { | ||
'1': { | ||
changes: [], | ||
schemas: { | ||
create: rawRuleSchemaV1, | ||
}, | ||
isCompatibleWithPreviousVersion: (rawRule) => true, | ||
}, | ||
}; | ||
|
||
export const getLatestRuleVersion = () => Math.max(...Object.keys(ruleModelVersions).map(Number)); | ||
|
||
export function getMinimumCompatibleVersion( | ||
modelVersions: CustomSavedObjectsModelVersionMap, | ||
version: number, | ||
rawRule: RawRule | ||
): number { | ||
if (version === 1) { | ||
return 1; | ||
} | ||
|
||
if (modelVersions[version].isCompatibleWithPreviousVersion(rawRule)) { | ||
return getMinimumCompatibleVersion(modelVersions, version - 1, rawRule); | ||
} | ||
|
||
return version; | ||
} |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/alerting/server/saved_objects/schemas/raw_rule/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { rawRuleSchema as rawRuleSchemaV1 } from './v1'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters