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

[ML] Explain log rate spikes: Move API stream demos to Kibana examples. #132590

Merged
merged 25 commits into from
May 24, 2022

Conversation

walterra
Copy link
Contributor

@walterra walterra commented May 20, 2022

Summary

Part of #136265.
Follow up to #132121.

To run Kibana with the described examples, use yarn start --run-examples.

This creates a response_stream plugin in the Kibana /examples section. The plugin demonstrates API endpoints that can stream data chunks with a single request with gzip/compression support. gzip-streams get decompressed natively by browsers. The plugin demonstrates two use cases to get started: Streaming a raw string as well as a more complex example that streams Redux-like actions to the client which update React state via useReducer().

Code in @kbn/aiops-utils contains helpers to set up a stream on the server side (streamFactory()) and consume it on the client side via a custom hook (useFetchStream()). The utilities make use of TS generics in a way that allows to have type safety for both request related options as well as the returned data.

No additional third party libraries are used in the helpers to make it work. On the server, they integrate with Hapi and use node's own gzip. On the client, the custom hook abstracts away the necessary logic to consume the stream, internally it makes use of a generator function and useReducer() to update React state.

On the server, the simpler stream to send a string is set up like this:

const { end, push, responseWithHeaders } = streamFactory(request.headers);

The request's headers get passed on to automatically identify if compression is supported by the client.

On the client, the custom hook is used like this:

const { error, start, cancel, data, isRunning } = useFetchStream<
    ApiSimpleStringStream, typeof basePath
>(`${basePath}/internal/response_stream/simple_string_stream`);

response_stream-0002

Checklist

For maintainers

@walterra walterra self-assigned this May 20, 2022
@walterra walterra changed the title Explain log rate spikes: Move demo to Kibana examples. [ML] Explain log rate spikes: Move demo to Kibana examples. May 20, 2022
@walterra walterra changed the title [ML] Explain log rate spikes: Move demo to Kibana examples. [ML] Explain log rate spikes: Move API stream demos to Kibana examples. May 20, 2022
@walterra walterra marked this pull request as ready for review May 23, 2022 12:20
@walterra walterra requested a review from a team as a code owner May 23, 2022 12:20
@walterra walterra added the :ml label May 23, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/ml-ui (:ml)

@walterra walterra added the release_note:skip Skip the PR/issue when compiling release notes label May 23, 2022
Copy link
Member

@mistic mistic left a comment

Choose a reason for hiding this comment

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

The changes related to a new package creation LGTM

@walterra
Copy link
Contributor Author

@qn895 addressed your comments, ready for another look!

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
aiops 78 96 +18
ml 1612 1610 -2
total +16

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/aiops-utils - 18 +18

Async chunks

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

id before after diff
aiops 219.0KB 275.6KB +56.6KB
ml 3.3MB 3.3MB -1.6KB
total +55.0KB

Page load bundle

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

id before after diff
aiops 3.9KB 3.6KB -308.0B
ml 40.4KB 40.3KB -122.0B
total -430.0B
Unknown metric groups

API count

id before after diff
@kbn/aiops-utils - 35 +35
aiops 12 11 -1
total +34

async chunk count

id before after diff
aiops 3 1 -2

ESLint disabled line counts

id before after diff
aiops 6 5 -1

Total ESLint disabled count

id before after diff
aiops 6 5 -1

History

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

cc @walterra

Copy link
Contributor

@peteharverson peteharverson left a comment

Choose a reason for hiding this comment

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

Tested locally and LGTM. Just one suggestion for the title.


developerExamples.register({
appId: 'response-stream',
title: 'response stream',
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd go with Response stream as the title for the example.

@qn895
Copy link
Member

qn895 commented May 24, 2022

Latest changes LGTM 🎉 Amazing work!!

@walterra walterra merged commit c968e50 into elastic:main May 24, 2022
@walterra walterra deleted the ml-aiops-examples branch May 24, 2022 14:59
@kibanamachine kibanamachine added backport:skip This commit does not require backporting labels May 25, 2022
walterra added a commit that referenced this pull request May 22, 2024
…amples. (#182690)

## Summary

Follow up to #132590.
Part of #181111.

This updates the developer examples for `@kbn/ml-response-stream` to
include a variant with a full Redux Toolkit setup. For this case, the
`@kbn/ml-response-stream` now includes a generic slice `streamSlice`
that can be used. This allows the actions created to be streamed via
NDJSON to be shared across server and client.

Functional tests for the examples were added too. To run these tests you
can use the following commands:

```
# Start the test server (can continue running)
node scripts/functional_tests_server.js --config test/examples/config.js
# Start a test run
node scripts/functional_test_runner.js --config test/examples/config.js
```

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
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 :ml release_note:skip Skip the PR/issue when compiling release notes v8.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants