Skip to content
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

[App Search] Schema: Set up server routes, grand foray into shared/connected Kea logic #99548

Merged
merged 11 commits into from
May 11, 2021

Conversation

cee-chen
Copy link
Contributor

@cee-chen cee-chen commented May 6, 2021

Summary

Whoof, a lot going on here! There (currently) isn't any changed UI/UX to test this with, but if you go to the schema pages of an engine or meta engine you can see the XHR call getting made on page load and inspect the API response.

This PR is also a semi-experimental foray into sharing some base values/actions between 2 separate-but-similar logic files. Indexed vs Meta Engines is generally a good candidate for this and there's some other meta engine logic where I might attempt something similar later if this is well-received by y'all.

Strongly recommend following along by commit!

Checklist

cee-chen added 6 commits May 6, 2021 09:15
- values & actions shared between both default source engine & meta engine pages
- significantly different actions (no updating) & API response from source engines, hence the separate files

+ fix typing issue - without Partial<>, all 4 enum types are expected instead of 1-4
- for some reason this causes an error in a separate a util file, not sure why (Typescript issue?)
@cee-chen cee-chen added release_note:skip Skip the PR/issue when compiling release notes v7.14.0 auto-backport Deprecated - use backport:version if exact versions are needed labels May 6, 2021
@cee-chen cee-chen requested review from JasonStoltz and a team May 6, 2021 20:49
Comment on lines 13 to 25
interface MetaEngineSchemaValues extends SchemaBaseValues {
fields: MetaEngineSchemaApiResponse['fields'];
conflictingFields: MetaEngineSchemaApiResponse['conflictingFields'];
conflictingFieldsCount: number;
hasConflicts: boolean;
}

interface MetaEngineSchemaActions extends SchemaBaseActions {
loadMetaEngineSchema(): void;
onMetaEngineSchemaLoad(response: MetaEngineSchemaApiResponse): MetaEngineSchemaApiResponse;
}

export const MetaEngineSchemaLogic = kea<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully comparing the two different logic files between source engines & meta engines helps illustrate why I chose to separate them - they're all in 1 file right now in the standalone UI and it's pretty hard to grok as a result. Hoping this is at least tidier.

It's honestly weird because the Meta Engine view, aside from the schema data, is almost completely different from the default/indexed schema view - it has no actual actions to take for example.

Some day I'll have a breakthrough genius idea on how to better organize our meta engine vs indexed engine logic/views/etc., but that day is not today 🙃

@@ -31,7 +31,7 @@ export type Schema = Record<string, SchemaType>;

// This is a mapping of schema field types ("text", "number", "geolocation", "date")
// to the names of source engines which utilize that type
export type SchemaConflictFieldTypes = Record<SchemaType, string[]>;
export type SchemaConflictFieldTypes = Partial<Record<SchemaType, string[]>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I didn't run into a type error with this before now, but without the Partial<> Typescript yells if you have just e.g. { text: ['some-engine'], number: ['another-engine'] } - it wants all the keys from SchemaType otherwise 🤷

Comment on lines +27 to +33
mostRecentIndexJob: {
percentageComplete: 100,
numDocumentsWithErrors: 10,
activeReindexJobId: 'some-id',
isActive: false,
hasErrors: true,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI that a large majority of this data we're no longer using after 7.12/the document storage refactor - percentageComplete will always come back as 100 and isActive as false. I still kept it in here as an example, but def would be nice to clean up our APIs some day

Comment on lines 52 to 60
/**
* Unfortunately, we can't mount({ schema: ... }) & have to use an action to set schema
* because of the separate connected logic file - our LogicMounter test helper sets context
* for only the currently tested file
*/
const mountAndSetSchema = ({ schema, ...values }: { schema: Schema; [key: string]: unknown }) => {
mount(values);
SchemaLogic.actions.setSchema(schema);
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to flag this as one very unfortunate hiccup in the connect approach to sharing state... our LogicMounter test helper automatically uses the current path to populate values, so we can't populate connected logics' values. So doing this:

mount({ schema: {} }); // belongs to SchemaBaseLogic, not SchemaLogic

does nothing and doesn't actually correctly set the value. I used this as a workaround in this file for now, but not sure if we should try to come up with a better/more elegant solution or just want for .extend() type support 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting. How would this look if we bypassed the helper? 🤔

Copy link
Contributor Author

@cee-chen cee-chen May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume something like this:

resetContext({
  defaults: {
    enterprise_search: {
      app_search: {
        some_base_logic: {
          someSharedValue: 'hello',
        },
        some_extended_logic: {
          someSpecificValue: 'world',
        },
      },
    },
  },
});

Looking at it that way, I can definitely see the potential of extending our test mount helper to mount with separate files, but it would be fairly tricky and be a more complex API/usage than our current helper

Copy link
Member

@JasonStoltz JasonStoltz May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh ok, that clears it up for me, thanks for posting that.

Comment on lines 56 to 59
connect: {
values: [SchemaBaseLogic, ['dataLoading', 'schema']],
actions: [SchemaBaseLogic, ['loadSchema', 'setSchema']],
},
Copy link
Contributor Author

@cee-chen cee-chen May 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW though, I do actually kind of like that shared values/actions are so explicitly listed rather than .extend() being somewhat magical / invisible & having to dive into another file to see what's shared. Tradeoffs everywhere I guess!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the same way. 💯

@cee-chen
Copy link
Contributor Author

cee-chen commented May 6, 2021

@elasticmachine merge upstream

Copy link
Member

@JasonStoltz JasonStoltz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the base logic, it keeps things DRY without adding a ton of overhead.

Just a few questions and comments.

Copy link
Contributor Author

@cee-chen cee-chen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonStoltz check it out!!! 82a711c

-31 lines! This is so much simpler/cleaner haha. Thanks for showing me the light!! It somehow never occurred to me the shared action itself could continue to be extended via reducers 🤦‍♀️ Seriously so cool.

@@ -23,6 +23,9 @@ describe('SchemaBaseLogic', () => {
some_text_field: SchemaType.Text,
some_number_field: SchemaType.Number,
};
const MOCK_RESPONSE = {
schema: MOCK_SCHEMA,
} as any;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm being a bit lazy and using any in this test file to avoid the whole SchemaApiResponse | MetaEngineSchemaApiResponse typing shenanigans. I could cast as unknown as SchemaApiResponse if folks prefer me to stop taking shortcuts 😅

Comment on lines +24 to +26
onSchemaLoad(
response: SchemaApiResponse | MetaEngineSchemaApiResponse
): SchemaApiResponse | MetaEngineSchemaApiResponse;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing comments continued - this is mostly here as dev documentation. I override/extend the onSchemaLoad action typing in each individual extended logic file (see below comments).

@@ -35,7 +35,6 @@ interface SchemaValues extends SchemaBaseValues {
}

interface SchemaActions extends SchemaBaseActions {
loadIndexedEngineSchema(): void;
onSchemaLoad(response: SchemaApiResponse): SchemaApiResponse;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment - this fixes the onSchemaLoad type differences between source & meta engines

@@ -18,8 +18,7 @@ interface MetaEngineSchemaValues extends SchemaBaseValues {
}

interface MetaEngineSchemaActions extends SchemaBaseActions {
loadMetaEngineSchema(): void;
onMetaEngineSchemaLoad(response: MetaEngineSchemaApiResponse): MetaEngineSchemaApiResponse;
onSchemaLoad(response: MetaEngineSchemaApiResponse): MetaEngineSchemaApiResponse;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above comment - this fixes the onSchemaLoad type differences between source & meta engines

actions: {
loadMetaEngineSchema: true,
onMetaEngineSchemaLoad: (response) => response,
actions: [SchemaBaseLogic, ['loadSchema', 'onSchemaLoad']],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cleaned up setSchema from this list since meta engines isn't using that action (bad copypasta on my part)

loadMetaEngineSchema: () => {
actions.loadSchema(actions.onMetaEngineSchemaLoad);
},
}),
Copy link
Contributor Author

@cee-chen cee-chen May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh I forgot to include this comment in the batch just now, but just wanted to reiterate that it's SUPER cool I was able to drop the actions and listeners block for MetaEngineSchemaLogic totally. Thanks again Jason, you're a genius! 🧠

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
enterpriseSearch 1368 1371 +3

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
enterpriseSearch 2.0MB 2.0MB +4.9KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Member

@JasonStoltz JasonStoltz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Glad that worked out for you.

@cee-chen cee-chen merged commit c28213b into elastic:master May 11, 2021
kibanamachine added a commit to kibanamachine/kibana that referenced this pull request May 11, 2021
…nnected Kea logic (elastic#99548)

* Set up server API routes

* Set up types

* Set up shared/base Schema logic file

- values & actions shared between both default source engine & meta engine pages

* Add default/indexed engine SchemaLogic

* Add MetaEnginesSchemaLogic

- significantly different actions (no updating) & API response from source engines, hence the separate files

+ fix typing issue - without Partial<>, all 4 enum types are expected instead of 1-4
- for some reason this causes an error in a separate a util file, not sure why (Typescript issue?)

* Update Schema & MetaEngineSchema views with loaders

* PR feedback: comment nit

* PR feedback: Remove unnecessary async/awaits

* PR feedback: Simplify loadSchema to be shared by base logic

Much clean, such simple

Co-authored-by: Kibana Machine <[email protected]>
@kibanamachine
Copy link
Contributor

💚 Backport successful

Status Branch Result
7.x

This backport PR will be merged automatically after passing CI.

@cee-chen cee-chen deleted the schema-2 branch May 11, 2021 18:52
kibanamachine added a commit that referenced this pull request May 11, 2021
…nnected Kea logic (#99548) (#99833)

* Set up server API routes

* Set up types

* Set up shared/base Schema logic file

- values & actions shared between both default source engine & meta engine pages

* Add default/indexed engine SchemaLogic

* Add MetaEnginesSchemaLogic

- significantly different actions (no updating) & API response from source engines, hence the separate files

+ fix typing issue - without Partial<>, all 4 enum types are expected instead of 1-4
- for some reason this causes an error in a separate a util file, not sure why (Typescript issue?)

* Update Schema & MetaEngineSchema views with loaders

* PR feedback: comment nit

* PR feedback: Remove unnecessary async/awaits

* PR feedback: Simplify loadSchema to be shared by base logic

Much clean, such simple

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Constance <[email protected]>
@cee-chen cee-chen mentioned this pull request May 12, 2021
29 tasks
cee-chen added a commit to cee-chen/kibana that referenced this pull request May 12, 2021
cee-chen pushed a commit that referenced this pull request May 12, 2021
* Add schema add field modal to page

* Add SchemaCallouts component

* Add empty state

* Add SchemaTable component

- Main update functionality is covered by the shared SchemaFieldTypeSelect component

+ Readd 'Recently added' i18n strings removed in https://github.com/elastic/kibana/pull/98955/files/d93e31dd415ba61354ab3714cd2728d60efa6ddc#r624038044

squash with table

* Add final update types button behavior

+ expand tests more explicitly & cleanly

* Fix i18n

just throw my body in the trash

* Update x-pack/plugins/enterprise_search/public/applications/app_search/components/schema/views/schema.test.tsx

Co-authored-by: Jason Stoltzfus <[email protected]>

* [Misc] Add missing SchemaBaseLogic state check

- Missed this in #99548

* Update x-pack/plugins/enterprise_search/public/applications/app_search/components/schema/components/empty_state.tsx

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Jason Stoltzfus <[email protected]>
kibanamachine added a commit to kibanamachine/kibana that referenced this pull request May 12, 2021
* Add schema add field modal to page

* Add SchemaCallouts component

* Add empty state

* Add SchemaTable component

- Main update functionality is covered by the shared SchemaFieldTypeSelect component

+ Readd 'Recently added' i18n strings removed in https://github.com/elastic/kibana/pull/98955/files/d93e31dd415ba61354ab3714cd2728d60efa6ddc#r624038044

squash with table

* Add final update types button behavior

+ expand tests more explicitly & cleanly

* Fix i18n

just throw my body in the trash

* Update x-pack/plugins/enterprise_search/public/applications/app_search/components/schema/views/schema.test.tsx

Co-authored-by: Jason Stoltzfus <[email protected]>

* [Misc] Add missing SchemaBaseLogic state check

- Missed this in elastic#99548

* Update x-pack/plugins/enterprise_search/public/applications/app_search/components/schema/components/empty_state.tsx

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Jason Stoltzfus <[email protected]>
kibanamachine added a commit that referenced this pull request May 13, 2021
* Add schema add field modal to page

* Add SchemaCallouts component

* Add empty state

* Add SchemaTable component

- Main update functionality is covered by the shared SchemaFieldTypeSelect component

+ Readd 'Recently added' i18n strings removed in https://github.com/elastic/kibana/pull/98955/files/d93e31dd415ba61354ab3714cd2728d60efa6ddc#r624038044

squash with table

* Add final update types button behavior

+ expand tests more explicitly & cleanly

* Fix i18n

just throw my body in the trash

* Update x-pack/plugins/enterprise_search/public/applications/app_search/components/schema/views/schema.test.tsx

Co-authored-by: Jason Stoltzfus <[email protected]>

* [Misc] Add missing SchemaBaseLogic state check

- Missed this in #99548

* Update x-pack/plugins/enterprise_search/public/applications/app_search/components/schema/components/empty_state.tsx

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Jason Stoltzfus <[email protected]>

Co-authored-by: Constance <[email protected]>
Co-authored-by: Jason Stoltzfus <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed release_note:skip Skip the PR/issue when compiling release notes v7.14.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants