-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expressions/migrations2 #81281
Expressions/migrations2 #81281
Conversation
4fcc5f1
to
15d9305
Compare
@@ -257,6 +257,21 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u | |||
return telemetryData; | |||
} | |||
|
|||
migrate(ast: SerializableState, version: string) { | |||
return this.walkAst(cloneDeep(ast) as ExpressionAstExpression, (fn, link) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we know the input is ExpressionAstExpression as we don't allow migrating that structure but just the actual functions within
@@ -257,6 +257,21 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u | |||
return telemetryData; | |||
} | |||
|
|||
migrate(ast: SerializableState, version: string) { | |||
return this.walkAst(cloneDeep(ast) as ExpressionAstExpression, (fn, link) => { | |||
link = fn.migrations[version](link) as ExpressionAstFunction; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and we expect the migration to return ExpressionAstFunction. thats not the general case with migrations, we might be working with unknown states where the only thing we know is that its serializable. When we do have more knowledge we'll need to do the casting.
ps: open for suggestions on how to better type safe this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the only other option that comes to mind is instead casting fn.migrations[version] as MigrateFunction<SerializableState, ExpressionAstFunction>
, but that doesn't really make a difference here in terms of type safety, so I think we can go with the existing implementation for now
@@ -20,6 +20,7 @@ | |||
import { i18n } from '@kbn/i18n'; | |||
import { ExpressionFunctionDefinition } from '../types'; | |||
import { ExpressionValueSearchContext } from '../../expression_types'; | |||
import { MigrateFunction } from '../../../../kibana_utils/common/persistable_state'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all changes to this file will be reverted, its just an example
@@ -44,6 +62,10 @@ export const kibana: ExpressionFunctionKibana = { | |||
|
|||
args: {}, | |||
|
|||
migrations: { | |||
'7.10.1': (MIGRATE7_10_1 as any) as MigrateFunction, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the part i really don't like. any suggestions ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't TS like this? Looks as if it should work without casting to me, but need to test it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe part of the issue is that in expression_function.ts
you are re-defining the migration type instead of reusing MigrateFunction
? (I assume that's what's being used behind the scenes here?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem is that MigrateFunction there is <SerializableSTate,SerializableState> where MIGRATE7_10_1 actually work with well defined types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem is that MigrateFunction there is <SerializableSTate,SerializableState> where MIGRATE7_10_1 actually work with well defined types
Okay, after looking into this more, there are actually a couple problems. First, migrations
isn't in ExpressionFunctionDefinition
at all, which is masked by the casting.
More problematic is that ExpressionFunctionDefinition
would need to provide a generic type param so you can specify the actual type(s) of your migrations, that way TS doesn't complain because the functions you provide are too specific.
The bummer is that this makes things more verbose and makes ExpressionFunctionDefinition
even more complex, but usage would essentially look like:
interface Migrations extends Record<string, any> {
'7.10.1': MigrateFunction<MYSTATE_7_10_0, MYSTATE_7_10_1>;
}
export type ExpressionFunctionKibana = ExpressionFunctionDefinition<
'kibana',
ExpressionValueSearchContext | null,
object,
ExpressionValueSearchContext,
ExecutionContext,
Migrations
>;
And then ExpressionFunctionDefinition
would need to take a generic param along the lines of:
Migrations extends Record<string, MigrateFunction> = Record<string, MigrateFunction>
I tested this and it worked for me locally, but LMK what you think. Either way if we are adding migrations
to the functions, we should update ExpressionFunctionDefinition
Pinging @elastic/kibana-app-arch (Team:AppArch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some initial comments. I think I might be able to provide some more productive suggestions once I actually test this locally and look at the TS errors, but ran out of time to do that. Thought I'd post what I had so far anyway.
@@ -257,6 +257,21 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u | |||
return telemetryData; | |||
} | |||
|
|||
migrate(ast: SerializableState, version: string) { | |||
return this.walkAst(cloneDeep(ast) as ExpressionAstExpression, (fn, link) => { | |||
link = fn.migrations[version](link) as ExpressionAstFunction; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the only other option that comes to mind is instead casting fn.migrations[version] as MigrateFunction<SerializableState, ExpressionAstFunction>
, but that doesn't really make a difference here in terms of type safety, so I think we can go with the existing implementation for now
* @param state | ||
* @param version | ||
*/ | ||
migrateToLatest?: (state: SerializableState, version: string) => P; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused about why migrateToLatest
requires a version
at all? Shouldn't that just be something that the function knows about internally? Or maybe version
is used differently here from migrate
and that's what I am getting mixed up about
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this is the version of your state, where in migrate() its the version of migration you want to run. i will make that clear in the jsdocs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay - yeah, I think the fact that version
means two things is just a naming difficulty then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comments clear up most of the confusion, though I think something like toVersion and fromVersion would be even more intuitive.
* @param state | ||
* @param version | ||
*/ | ||
migrate?: (state: SerializableState, version: string) => SerializableState; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it would help clean up some of the casting if we provided a FromVersion
and ToVersion
here as well (as in MigrateFunction
). Then in executor you wouldn't need to cast to ExpressionAstFunction
and could instead provide it in the generic type params.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we should keep this one as it is, as we want to keep types for old versions of state internal (nobody should be messing with that) and migrate really doesn't know anything more about the state coming in apart that it needs to be serializable. (nor does the consumer)
@@ -44,6 +62,10 @@ export const kibana: ExpressionFunctionKibana = { | |||
|
|||
args: {}, | |||
|
|||
migrations: { | |||
'7.10.1': (MIGRATE7_10_1 as any) as MigrateFunction, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't TS like this? Looks as if it should work without casting to me, but need to test it
@@ -44,6 +62,10 @@ export const kibana: ExpressionFunctionKibana = { | |||
|
|||
args: {}, | |||
|
|||
migrations: { | |||
'7.10.1': (MIGRATE7_10_1 as any) as MigrateFunction, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe part of the issue is that in expression_function.ts
you are re-defining the migration type instead of reusing MigrateFunction
? (I assume that's what's being used behind the scenes here?)
also can we ignore the metrics failled test ? i think its lefit that the bundle grew 27kb as i used |
Yeah, that's a bit annoying. I guess you could try to minimize the bundle increase by manually implementing |
# Conflicts: # src/plugins/expressions/common/executor/executor.ts # src/plugins/expressions/common/expression_functions/specs/kibana.ts # src/plugins/expressions/common/service/expressions_services.ts
fba0d44
to
134e82c
Compare
💚 Build SucceededMetrics [docs]page load bundle size
History
To update your PR or re-run it, just comment with: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm on board with this in the current state, and as discussed offline we can come back and do a TS refactor once we see how it works using this in the wild. The casting of the actual migrations isn't ideal, but I also think it's something that would be easy enough to change in a follow up once we have a better strategy for how to approach it.
* master: (71 commits) [Chrome] Extension to append an element to the last breadcrumb (elastic#82015) [Monitoring] Thread pool rejections alert (elastic#79433) [Actions] Fix actionType type on registerType function (elastic#82125) [Security Solution] Modal for saving timeline (elastic#81802) add tests for index pattern switching (elastic#81987) TS project references for share plugin (elastic#82051) [Graph] Fix problem with duplicate ids (elastic#82109) skip 'returns a single bucket if array has 1'. related elastic#81460 Add a link to documentation in the alerts and actions management UI (elastic#81909) [Fleet] fix duplicate ingest pipeline refs (elastic#82078) Context menu trigger for URL Drilldown (elastic#81158) SO management: fix legacy import index pattern selection being reset when switching page (elastic#81621) Fixed dead links (elastic#78696) [Search] Add "restore" to session service (elastic#81924) fix Lens heading structure (elastic#81752) [ML] Data Frame Analytics: Fix feature importance cell value and decision path chart (elastic#82011) Remove legacy app arch items from codeowners. (elastic#82084) [TSVB] Renamed 'positive rate' to 'counter rate' (elastic#80939) Expressions/migrations2 (elastic#81281) [Telemetry] [Schema] remove number type and support all es number types (elastic#81774) ...
Summary
adds migrate and migrateToLatest functions to persistableState type and expression service
Checklist
Delete any items that are not applicable to this PR.
For maintainers