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

[Discover] Add container for data state (migrated from useSavedSearch hook) #148614

Merged
merged 21 commits into from
Jan 23, 2023

Conversation

kertal
Copy link
Member

@kertal kertal commented Jan 10, 2023

Summary

This PR migrates the hook responsible for data fetching in Discover main (use_saved_search.ts) to be part of DiscoverStateContainer as DataStateContainer.

Testing

There shall be no regression, everything should work like before

Reviewing

The majority of files were changed because import paths changed. Sadly GitHub doesn't diff the main change:

The logic of

was migrated to

This is part of #142131 and code was extracted of the POC #140765. There will be follow up cleanups once the next part of the POC are migrated.

Checklist

@kertal kertal changed the title [Discover] Add discover data store [Discover] Add data store Jan 10, 2023
@kertal kertal self-assigned this Jan 10, 2023
@kertal kertal added Feature:Discover Discover Application Team:DataDiscovery Discover, search (e.g. data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. labels Jan 10, 2023
@kertal
Copy link
Member Author

kertal commented Jan 16, 2023

@elasticmachine merge upstream

Comment on lines -65 to -69
* Skip initial fetch when discover:searchOnPageLoad is disabled.
*/
if (initialFetchStatus === FetchStatus.UNINITIALIZED) {
fetch$ = fetch$.pipe(skip(1));
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed and made more implicit, not to skip a fetch in the given condition, but to fetch when it's needed and not to initially fetch when discover:searchOnPageLoad is disabled

Comment on lines +102 to +103
? columns.map((field) => ({ field, include_unmapped: 'true' }))
: [{ field: '*', include_unmapped: 'true' }];
Copy link
Member Author

Choose a reason for hiding this comment

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

Without this change, the CSV export broke.
Why? [{ field: '*', include_unmapped: 'true' }] is necessary to export all the fields given useFieldsApi is true (Which is the default)
Bevor the refactoring of this PR this was added by the code executed in the hook. So it was added before , and at a different part of the code, which made it hard to track & debug

This is now slightly better, more decoupled, independent from previous searchSource mutations

@kertal kertal changed the title [Discover] Add data store [Discover] Add container for data state (migrated from useSavedSearch hook) Jan 17, 2023
@kertal kertal added v8.7.0 chore release_note:skip Skip the PR/issue when compiling release notes labels Jan 17, 2023
@kertal kertal marked this pull request as ready for review January 17, 2023 08:23
@kertal kertal requested a review from a team as a code owner January 17, 2023 08:23
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-data-discovery (Team:DataDiscovery)

@kertal
Copy link
Member Author

kertal commented Jan 17, 2023

@elasticmachine merge upstream

Copy link
Contributor

@mattkime mattkime left a comment

Choose a reason for hiding this comment

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

A few minor requests and a couple of items to discuss before I give this my 👍

/**
* Observable triggering fetching data from ES
*/
refetch$: DataRefetch$;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is a Subject being used? Is something happening that couldn't be expressed in terms of a function?

Copy link
Member Author

Choose a reason for hiding this comment

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

We have several observables that can trigger a fetch, and this is one of them:

don't think we can change this so easily, but there's already a fetch function exported that's using the observable internally:

I think just this function could be used in the UI. so the Subject would be a internal implementation detail

/**
* Start subscribing to other observables that trigger data fetches
*/
subscribe: () => () => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this called externally? Is there a reason to instantiate this object without subscribing?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, we currently initialize the discover state container twice, also legacy behavior I'll change in a follow up, it was part of the POC:

first:

const { appState } = getDiscoverStateContainer({

second:

const stateContainer = useMemo(() => {
const container = getDiscoverStateContainer({
history,
savedSearch,
services,

furthermore it makes sense, since we are subscribing to more store / services / container to orchestrate subscribing.

/**
* Observable emitting when a next fetch is triggered
*/
fetch$: DataFetch$;
Copy link
Contributor

Choose a reason for hiding this comment

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

Its difficult to tell the difference between fetch$ and refetch$ based on name, type, and description. As best I can tell refetch$ is called by external consumers but not fetch$, and external consumers might subscribe to fetch$ but probably not refetch$

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, fetch$ was just recently exposed for the histogram, so not exposing refetch$ (but just a fetch function which is more implicit) might make this clearer

update: can't do this, because we need refetch$ here:

const refetch = stateContainer?.dataState.refetch$.subscribe(() => {
)

So I think improving the description would be the way to go ... or naming
refetch$ -> startFetch$ , triggerFetch$
open for suggestions 🙇

Copy link
Contributor

@mattkime mattkime left a comment

Choose a reason for hiding this comment

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

Changes look good to me and work well.

@kertal
Copy link
Member Author

kertal commented Jan 22, 2023

@elasticmachine merge upstream

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
discover 443 441 -2

Async chunks

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

id before after diff
discover 395.8KB 392.2KB -3.6KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
discover 28.1KB 28.1KB +14.0B

History

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

cc @kertal

@kertal kertal merged commit 0755f6d into elastic:main Jan 23, 2023
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting chore Feature:Discover Discover Application release_note:skip Skip the PR/issue when compiling release notes Team:DataDiscovery Discover, search (e.g. data plugin and KQL), data views, saved searches. For ES|QL, use Team:ES|QL. v8.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants