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

[Logs UI] Refactor log entry data fetching to hooks #51526

Merged
merged 25 commits into from
Dec 9, 2019

Conversation

Zacqary
Copy link
Contributor

@Zacqary Zacqary commented Nov 22, 2019

Summary

Closes #50395

Refactors the log entry data fetching to use React hooks without Redux. Creates temporary bridges to Apollo and Redux as we continue to work on removing them from other parts of the data layer.

Update: Refactored to use encapsulated hooks similar to how other features are already implemented

Data store hooks are moved into a folder called /store/v2, which includes several different hooks for different parts of the application state, and a context provider component to make them accessible to components.

It's ultimately a similar structure to the Redux data model but with significantly reduced complexity.

This is a departure from the way other data-fetching hooks have been implemented so far. I went with this approach because of how tightly-coupled the Log Entries, Log Position, and Log Filter states are. Since we plan to refactor all three of them in rapid succession, it didn't seem like I'd be able to make the Log Entries state self-contained in the same way Highlights, Summary, and Analysis are. The Highlights state also tightly depends on these three states, so I'm wondering if we want to reevaluate its implementation.

Details:

  • Reducers are replaced by some useState hooks. Data fetching uses a useReducer hook with a Redux-like pattern to track its loading/loaded/error logic, but action types are placed in a single enum and don't need to be exported across multiple files.
  • A change in part of the app state updates the context, triggering re-renders of any components that include the useStore hook. We can further optimize this to only re-render components that depend on updated parts of the state.
  • Epics are replaced by useEffects which watch for dependency changes rather than observable streams of actions.
  • Data fetching is triggered by changes to the logPosition and logFilter states, so the top-level Store component also connects these bits of the state to the logEntries state. This uses a selector to only pull certain parts of the other states that it needs. We can still make use of the reselect library to build selectors, as it can select from any plain object and not just a Redux store.

I worry that the last item may be creating a circular dependency structure, but that might not actually be all that different from epics depending on action types from other sections of the state and vice-versa.

Known Issues

sourceId can only be default in this PR. The sourceId is determined through a lot of weird layers of context and I feel like it requires its own issue to refactor. I may need some help figuring that one out.

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

For maintainers

@Zacqary Zacqary added v8.0.0 Feature:Logs UI Logs UI feature Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services release_note:skip Skip the PR/issue when compiling release notes v7.6.0 labels Nov 22, 2019
@Zacqary Zacqary requested a review from a team November 22, 2019 23:00
@elasticmachine
Copy link
Contributor

Pinging @elastic/logs-metrics-ui (Team:logs-metrics-ui)

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@Zacqary Zacqary marked this pull request as ready for review November 26, 2019 23:33
@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@Zacqary
Copy link
Contributor Author

Zacqary commented Nov 27, 2019

retest

@elasticmachine
Copy link
Contributor

💔 Build Failed

@afgomez afgomez self-requested a review November 28, 2019 09:29
Copy link
Contributor

@afgomez afgomez left a comment

Choose a reason for hiding this comment

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

This is a quick first pass. I'll download the branch and work my way through the code to understand everything better.

Looking good!

Comment on lines 14 to 15
// Bridges Redux container state with Hooks state. Once state is moved fully from
// Redux to Hooks this can be removed.
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this can go away now 🎉

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 quite yet, after #50397 and #50398 these can all go away


const [prevParams, cachePrevParams] = useState(params);

const runFetchNewEntriesRequest = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Bear in mind that I'm a hooks newbie.

I see the body of the useFetchEntriesEffect defines a bunch of functions like this one. Shouldn't these be wrapped in a useCallback hook?

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 think this seemed like an unnecessary optimization because I believe the effect only ever re-runs when params change, and all of these functions depend on params. I can wrap them in useCallbacks just in case there's something I'm missing, though.

Copy link
Contributor

@afgomez afgomez left a comment

Choose a reason for hiding this comment

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

Please take a look at my comments above.

Thanks for tackling this <3

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💔 Build Failed

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Copy link
Contributor

@afgomez afgomez left a comment

Choose a reason for hiding this comment

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

Code wise I don't have any more comments. Thanks for addressing the ones I left!
If @weltenwort doesn't have any more comments we can go ahead

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

History

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

@Zacqary Zacqary merged commit 21f9ab2 into elastic:master Dec 9, 2019
@Zacqary Zacqary deleted the 50395-refactor-log-entries branch December 9, 2019 23:25
Zacqary added a commit to Zacqary/kibana that referenced this pull request Dec 9, 2019
* Get initialinitial log fetch working with v2 store

* Replicate shouldLoadAroundPosition logic within hooks

* Reload entries on filter change

* Add scroll to load additional entries functionality

* Cleanup types types and remove state/remote folder

* Typescript cleanup

* Remove extraneous console.log

* Fix typecheck

* Add action to load new entries manually

* Typecheck fix

* Move v2 store stuff into logs containers

* Typecheck fix

* More typecheck fix

* Remove filterQuery from log highlights redux bridge

* Rename LogEntriesDependencies to LogEntriesFetchParams

* Fix endless reloading bug

* Fix duplicate entry rendering

* Make sourceId into a dynamic parameter

* Fix bug in pagesAfterEnd not being reported causing endless reload

* Fix bugs with live streaming
Zacqary added a commit that referenced this pull request Dec 12, 2019
* Get initialinitial log fetch working with v2 store

* Replicate shouldLoadAroundPosition logic within hooks

* Reload entries on filter change

* Add scroll to load additional entries functionality

* Cleanup types types and remove state/remote folder

* Typescript cleanup

* Remove extraneous console.log

* Fix typecheck

* Add action to load new entries manually

* Typecheck fix

* Move v2 store stuff into logs containers

* Typecheck fix

* More typecheck fix

* Remove filterQuery from log highlights redux bridge

* Rename LogEntriesDependencies to LogEntriesFetchParams

* Fix endless reloading bug

* Fix duplicate entry rendering

* Make sourceId into a dynamic parameter

* Fix bug in pagesAfterEnd not being reported causing endless reload

* Fix bugs with live streaming
timductive pushed a commit to timductive/kibana that referenced this pull request Dec 16, 2019
* Get initialinitial log fetch working with v2 store

* Replicate shouldLoadAroundPosition logic within hooks

* Reload entries on filter change

* Add scroll to load additional entries functionality

* Cleanup types types and remove state/remote folder

* Typescript cleanup

* Remove extraneous console.log

* Fix typecheck

* Add action to load new entries manually

* Typecheck fix

* Move v2 store stuff into logs containers

* Typecheck fix

* More typecheck fix

* Remove filterQuery from log highlights redux bridge

* Rename LogEntriesDependencies to LogEntriesFetchParams

* Fix endless reloading bug

* Fix duplicate entry rendering

* Make sourceId into a dynamic parameter

* Fix bug in pagesAfterEnd not being reported causing endless reload

* Fix bugs with live streaming
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Logs UI Logs UI feature release_note:skip Skip the PR/issue when compiling release notes Team:Infra Monitoring UI - DEPRECATED DEPRECATED - Label for the Infra Monitoring UI team. Use Team:obs-ux-infra_services v7.6.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Logs UI] Refactor log entry data fetching to hooks
3 participants