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

Validate Painless Lab index field #60841

Merged
merged 9 commits into from
Mar 23, 2020

Conversation

cjcenizal
Copy link
Contributor

@cjcenizal cjcenizal commented Mar 21, 2020

Fixes #60020

This required significant refactoring of the store's shape. To make this refactor easier, I also tried to simplify the way types and constants are organized. An additional improvement I made was to clarify the labeling of various form fields.

image

@cjcenizal cjcenizal force-pushed the painless/validate-index branch from 0a84482 to 5aadb77 Compare March 21, 2020 00:48
@cjcenizal cjcenizal requested a review from jloleysens March 21, 2020 00:48
@cjcenizal cjcenizal added painless painless release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more labels Mar 21, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/es-ui (Team:Elasticsearch UI)


const updateState = (getNextState: (s: Store) => Partial<Store>): void => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jloleysens Was there a particular reason to accept a callback here instead of the state change?

Copy link
Contributor

@jloleysens jloleysens Mar 23, 2020

Choose a reason for hiding this comment

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

The primary reason was to provide callers of updateState with a reference to the previous state so they can make decisions about it if needed, e.g., updateState(s => s.something ? ... : ...) and so that it is a bit more convenient to work with as the store grows in complexity (otherwise we have to fetch the store from const { store } = useAppContext() and use it when we update something and the update it again in future if the shape changes. So it is primarily a mechanism of optimisation.

This followed the useState pattern, so I am happy to have it accept both a callback and an object 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, thanks for explaining! I follow your reasoning until this part:

otherwise we have to fetch the store from const { store } = useAppContext() and use it when we update something and then update it again in the future if the shape changes.

This is where I get lost. Is there an example of this in the codebase? Philosophically I would suggest waiting until this becomes a problem before introducing a solution for it.

services: {
http: HttpSetup;
};
links: Links;
}

const initialPayload = {
Copy link
Contributor Author

@cjcenizal cjcenizal Mar 21, 2020

Choose a reason for hiding this comment

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

@jloleysens What do you think of bringing this into this file? It's not consumed elsewhere so I think it creates noise to extract it. If we want to break this file's code up into multiple files, I'd prefer to convert it into module (a directory with an index file) to make it clear what's intended for consumption elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, I am happy with this change. I had the store.ts file separate so that if we choose to use a reducer in future we can just add the reducer to the store file.

Copy link
Contributor

Choose a reason for hiding this comment

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

I do think if we start to expand more on validators it would be nice to have a separate file for these. Ideally I like to keep the context file as minimal as possible so that it only has the concern setting up the context. Where, at the moment, it houses store setup and validation logic - perhaps worth creating the folder structure you were speaking about. That way future engineers know where to put things?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I'll create a folder now. In terms of validation, I would expect us to try using the form hook lib if we want to add more rules in the future.

Copy link
Contributor

@jloleysens jloleysens left a comment

Choose a reason for hiding this comment

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

@cjcenizal thanks for adding some validation to the store! I think it's a pattern we can build upon in future. I did not test this locally.

I left one comment regarding state init that I think needs be addressed before merging though!

(Approving to not block because the change is minor, incl. the "yay" param)

services: {
http: HttpSetup;
};
links: Links;
}

const initialPayload = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, I am happy with this change. I had the store.ts file separate so that if we choose to use a reducer in future we can just add the reducer to the store file.

context: painlessContextOptions[0].value,
code: exampleScript,
parameters: `{
"string-parameter": "yay",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we clean these up?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or are they intended as the parameter defaults? "yay" seems a bit too informal to me 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha yes I intended them to be defaults. I'll try to find something appropriately solemn. :)

services: {
http: HttpSetup;
};
links: Links;
}

const initialPayload = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I do think if we start to expand more on validators it would be nice to have a separate file for these. Ideally I like to keep the context file as minimal as possible so that it only has the concern setting up the context. Where, at the moment, it houses store setup and validation logic - perhaps worth creating the folder structure you were speaking about. That way future engineers know where to put things?

...initialState,
const PAINLESS_LAB_KEY = 'painlessLabState';

const defaultPayload = {
Copy link
Contributor

@jloleysens jloleysens Mar 23, 2020

Choose a reason for hiding this comment

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

This should be inside of a getter so that we have this pattern: useState(() => ...). In this implementation we are calling JSON.parse and localStorage.getItem for every state update. This post explains the lazy init idea: https://dmitripavlutin.com/react-usestate-hook-guide/#3-lazy-initialization-of-state

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for sharing that link! I was completely overlooking that performance hit and the link opened my eyes. Will fix.


const updateState = (getNextState: (s: Store) => Partial<Store>): void => {
Copy link
Contributor

@jloleysens jloleysens Mar 23, 2020

Choose a reason for hiding this comment

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

The primary reason was to provide callers of updateState with a reference to the previous state so they can make decisions about it if needed, e.g., updateState(s => s.something ? ... : ...) and so that it is a bit more convenient to work with as the store grows in complexity (otherwise we have to fetch the store from const { store } = useAppContext() and use it when we update something and the update it again in future if the shape changes. So it is primarily a mechanism of optimisation.

This followed the useState pattern, so I am happy to have it accept both a callback and an object 👍

…meters to provide an example to users.

- Move PAINLESS_LAB_KEY constant into context file, since that's the only place it's being used.
- Remove common directory, move constants and types files to root.
- Move initialState into context file, where it's being used.
- This required a refactor to the shape of our store.
@cjcenizal cjcenizal force-pushed the painless/validate-index branch from 5aadb77 to 87a7f58 Compare March 23, 2020 20:05
@cjcenizal
Copy link
Contributor Author

Thanks for the review @jloleysens! I've addressed all of your feedback except for your suggestion to allow updatePayload to accept both a payload object and a callback. I'd be happy to implement this in a separate PR if you feel strongly about it.

@cjcenizal cjcenizal merged commit d0d3036 into elastic:app/painless Mar 23, 2020
@cjcenizal cjcenizal deleted the painless/validate-index branch March 23, 2020 20:24
@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

  • 💚 Build #35138 succeeded 5aadb7764d1abe72e5363ffb0a184a96a63080b1

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

cjcenizal added a commit that referenced this pull request Mar 23, 2020
* Create Painless Playground app (#54578)

* Replace heart script with smiley face script. (#57755)

* Rename Painless Playground -> Painless Lab. (#57545)

* Fix i18n namespace.

* Improve smiley face proportions.
- Add def keyword to Painless spec.
- Temporarily fix broken highlighting.
- Add small padding to main controls.

* [Painless Lab] Minor Fixes (#58135)

* Code restructure, improve types, add plugin id, introduced hook

Moved the code execution hook to a custom hook outside of main,
also chaining off promise to avoid lower level handling of
sequencing.

* Re-instated formatting code

To improve DX the execution error response from the painless API
was massaged to a more reader friendly state, only giving non-repeating
information.

Currently it is hard to determine the line and character information from
the painless endpoint. If the user wishes to see this raw information it
will be available in the API response flyout.

* Remove leading new line in default script

* Remove registration of feature flag

* Fix types

* Restore previous auto-submit request behaviour

* Remove use of null and remove old comment

Stick with "undefined" as the designation for something not existing.

* [Painless Lab] NP migration (#59794)

* Fix sample document editor.

* [Painless Lab] Fix float -> integer coercion bug (#60201)

* Clarify data and persistence flow. Fix floating point precision bug.
* Send a string to API and ES client instead of an object.

* Rename helpers lib to format. Add tests for formatRequestPayload.

* Add query parameter to score context (#60414)

* Fix typo and i18n

* Make state init lazy

Otherwise we are needlessly reading and JSON.parse'ing on every
state update

* Support the query parameter in requests to Painless

* Fix borked i18n

* Fix i18n

* Another i18n issue

* [Painless] Minor state update model refactor (#60532)

* Fix typo and i18n

* Make state init lazy

Otherwise we are needlessly reading and JSON.parse'ing on every
state update

* Support the query parameter in requests to Painless

* WiP on state refactor

* Some cleanup after manual testing

* Fix types and i18n

* Fix i18n in context_tab

* i18n

* [Painless] Language Service (#60612)

* Added language service

* Use the correct monaco instance and add wordwise operations

* Remove plugin context initializer for now

* [Painless] Replace hard-coded links (#60603)

* Replace hard-coded links

Also remove all props from Main component

* Pass the new links object to the request flyout too

* Link directly to painless execute API's contexts

* Remove responsive stacking from tabs with icons in them.

* Resize Painless Lab bottom bar to accommodate nav drawer width (#60833)

* Validate Painless Lab index field (#60841)

* Make JSON format of parameters field more prominent. Set default parameters to provide an example to users.
* Set default document to provide an example to users.
* Simplify context's updateState interface.
* Refactor store and context file organization.
  - Remove common directory, move constants and types files to root.
  - Move initialState into context file, where it's being used.
* Add validation for index input.
* Create context directory.

* Fix bottom bar z-index.

* Position flyout help link so it's bottom-aligned with the title and farther from the close button.

Co-authored-by: Matthias Wilhelm <[email protected]>
Co-authored-by: Jean-Louis Leysens <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Alison Goryachev <[email protected]>
@cjcenizal cjcenizal added the Feature:Painless Lab Dev tool for learning Painless label Mar 23, 2020
cjcenizal added a commit that referenced this pull request Mar 24, 2020
* Create Painless Playground app (#54578)

* Replace heart script with smiley face script. (#57755)

* Rename Painless Playground -> Painless Lab. (#57545)

* Fix i18n namespace.

* Improve smiley face proportions.
- Add def keyword to Painless spec.
- Temporarily fix broken highlighting.
- Add small padding to main controls.

* [Painless Lab] Minor Fixes (#58135)

* Code restructure, improve types, add plugin id, introduced hook

Moved the code execution hook to a custom hook outside of main,
also chaining off promise to avoid lower level handling of
sequencing.

* Re-instated formatting code

To improve DX the execution error response from the painless API
was massaged to a more reader friendly state, only giving non-repeating
information.

Currently it is hard to determine the line and character information from
the painless endpoint. If the user wishes to see this raw information it
will be available in the API response flyout.

* Remove leading new line in default script

* Remove registration of feature flag

* Fix types

* Restore previous auto-submit request behaviour

* Remove use of null and remove old comment

Stick with "undefined" as the designation for something not existing.

* [Painless Lab] NP migration (#59794)

* Fix sample document editor.

* [Painless Lab] Fix float -> integer coercion bug (#60201)

* Clarify data and persistence flow. Fix floating point precision bug.
* Send a string to API and ES client instead of an object.

* Rename helpers lib to format. Add tests for formatRequestPayload.

* Add query parameter to score context (#60414)

* Fix typo and i18n

* Make state init lazy

Otherwise we are needlessly reading and JSON.parse'ing on every
state update

* Support the query parameter in requests to Painless

* Fix borked i18n

* Fix i18n

* Another i18n issue

* [Painless] Minor state update model refactor (#60532)

* Fix typo and i18n

* Make state init lazy

Otherwise we are needlessly reading and JSON.parse'ing on every
state update

* Support the query parameter in requests to Painless

* WiP on state refactor

* Some cleanup after manual testing

* Fix types and i18n

* Fix i18n in context_tab

* i18n

* [Painless] Language Service (#60612)

* Added language service

* Use the correct monaco instance and add wordwise operations

* Remove plugin context initializer for now

* [Painless] Replace hard-coded links (#60603)

* Replace hard-coded links

Also remove all props from Main component

* Pass the new links object to the request flyout too

* Link directly to painless execute API's contexts

* Remove responsive stacking from tabs with icons in them.

* Resize Painless Lab bottom bar to accommodate nav drawer width (#60833)

* Validate Painless Lab index field (#60841)

* Make JSON format of parameters field more prominent. Set default parameters to provide an example to users.
* Set default document to provide an example to users.
* Simplify context's updateState interface.
* Refactor store and context file organization.
  - Remove common directory, move constants and types files to root.
  - Move initialState into context file, where it's being used.
* Add validation for index input.
* Create context directory.

* Fix bottom bar z-index.

* Position flyout help link so it's bottom-aligned with the title and farther from the close button.

Co-authored-by: Matthias Wilhelm <[email protected]>
Co-authored-by: Jean-Louis Leysens <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Alison Goryachev <[email protected]>

Co-authored-by: Matthias Wilhelm <[email protected]>
Co-authored-by: Jean-Louis Leysens <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Alison Goryachev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature:Dev Tools Feature:Painless Lab Dev tool for learning Painless painless painless release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants