-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
165 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { cloneDeep } from 'lodash'; | ||
import { stateSchemaByVersion } from './task_state'; | ||
|
||
describe('telemetry task state', () => { | ||
describe('v1', () => { | ||
const v1 = stateSchemaByVersion[1]; | ||
it('should work on empty object when running the up migration', () => { | ||
const result = v1.up({}); | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"runs": 0, | ||
"telemetry": undefined, | ||
} | ||
`); | ||
}); | ||
|
||
it(`shouldn't overwrite properties when running the up migration`, () => { | ||
const state = { | ||
runs: 1, | ||
telemetry: { | ||
panels: { | ||
total: 2, | ||
by_reference: 3, | ||
by_value: 4, | ||
by_type: { | ||
foo: 5, | ||
}, | ||
}, | ||
controls: { | ||
total: 6, | ||
chaining_system: { foo: 7 }, | ||
label_position: { foo: 8 }, | ||
ignore_settings: { foo: 9 }, | ||
by_type: { foo: 10 }, | ||
}, | ||
}, | ||
}; | ||
const result = v1.up(cloneDeep(state)); | ||
expect(result).toEqual(state); | ||
}); | ||
|
||
it(`should migrate the old default state that didn't match the schema`, () => { | ||
const result = v1.up({ byDate: {}, suggestionsByDate: {}, saved: {}, runs: 0 }); | ||
expect(result).toMatchInlineSnapshot(` | ||
Object { | ||
"runs": 0, | ||
"telemetry": undefined, | ||
} | ||
`); | ||
}); | ||
|
||
it('should drop unknown properties when running the up migration', () => { | ||
const state = { foo: true }; | ||
const result = v1.up(state); | ||
expect(result).not.toHaveProperty('foo'); | ||
}); | ||
}); | ||
}); |
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,85 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { schema, type TypeOf } from '@kbn/config-schema'; | ||
|
||
/** | ||
* WARNING: Do not modify the existing versioned schema(s) below, instead define a new version (ex: 2, 3, 4). | ||
* This is required to support zero-downtime upgrades and rollbacks. See https://github.com/elastic/kibana/issues/155764. | ||
* | ||
* As you add a new schema version, don't forget to change latestTaskStateSchema variable to reference the latest schema. | ||
* For example, changing stateSchemaByVersion[1].schema to stateSchemaByVersion[2].schema. | ||
*/ | ||
export const stateSchemaByVersion = { | ||
1: { | ||
// A task that was created < 8.10 will go through this "up" migration | ||
// to ensure it matches the v1 schema. | ||
up: (state: Record<string, any>) => ({ | ||
runs: typeof state.runs === 'number' ? state.runs : 0, | ||
telemetry: state.telemetry | ||
? { | ||
panels: { | ||
total: state.telemetry.panels?.total || 0, | ||
by_reference: state.telemetry.panels?.by_reference || 0, | ||
by_value: state.telemetry.panels?.by_value || 0, | ||
by_type: state.telemetry.panels?.by_type || {}, | ||
}, | ||
controls: { | ||
total: state.telemetry.controls?.total || 0, | ||
chaining_system: state.telemetry.controls?.chaining_system || {}, | ||
label_position: state.telemetry.controls?.label_position || {}, | ||
ignore_settings: state.telemetry.controls?.ignore_settings || {}, | ||
by_type: state.telemetry.controls?.by_type || {}, | ||
}, | ||
} | ||
: undefined, | ||
}), | ||
schema: schema.object({ | ||
runs: schema.number(), | ||
telemetry: schema.maybe( | ||
schema.object({ | ||
panels: schema.object({ | ||
total: schema.number(), | ||
by_reference: schema.number(), | ||
by_value: schema.number(), | ||
by_type: schema.recordOf( | ||
schema.string(), | ||
schema.object({ | ||
total: schema.number(), | ||
by_reference: schema.number(), | ||
by_value: schema.number(), | ||
details: schema.recordOf(schema.string(), schema.number()), | ||
}) | ||
), | ||
}), | ||
controls: schema.object({ | ||
total: schema.number(), | ||
chaining_system: schema.recordOf(schema.string(), schema.number()), | ||
label_position: schema.recordOf(schema.string(), schema.number()), | ||
ignore_settings: schema.recordOf(schema.string(), schema.number()), | ||
by_type: schema.recordOf( | ||
schema.string(), | ||
schema.object({ | ||
total: schema.number(), | ||
details: schema.recordOf(schema.string(), schema.number()), | ||
}) | ||
), | ||
}), | ||
}) | ||
), | ||
}), | ||
}, | ||
}; | ||
|
||
const latestTaskStateSchema = stateSchemaByVersion[1].schema; | ||
export type LatestTaskStateSchema = TypeOf<typeof latestTaskStateSchema>; | ||
|
||
export const emptyState: LatestTaskStateSchema = { | ||
runs: 0, | ||
telemetry: undefined, | ||
}; |