Skip to content

Sort output config fields stably#4746

Merged
edmundoa merged 1 commit into
Graylog2:masterfrom
Al2Klimov:bugfix/config-sort-stable
Apr 25, 2018
Merged

Sort output config fields stably#4746
edmundoa merged 1 commit into
Graylog2:masterfrom
Al2Klimov:bugfix/config-sort-stable

Conversation

@Al2Klimov
Copy link
Copy Markdown
Contributor

Description

fixes #4745

Motivation and Context

#4745

How Has This Been Tested?

Not at all. Unfortunately I couldn't build Graylog independent of my change.

Screenshots (if appropriate):

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 20, 2018

CLA assistant check
All committers have signed the CLA.

@Al2Klimov
Copy link
Copy Markdown
Contributor Author

PS: I (and @N-o-X and @bobapple) really would like you to include this in v2.4.x.

@Al2Klimov
Copy link
Copy Markdown
Contributor Author

Unfortunately I seem not to have access to your Jenkins so I can't even figure out my mistake.

@Al2Klimov
Copy link
Copy Markdown
Contributor Author

As discussed w/ @jalogisch (offline):

  • Command executed by Jenkins (effectively): node node_modules/.bin/eslint -c packages/graylog-web-plugin/.eslintrc -f checkstyle src/components/configurationforms/ConfigurationForm.jsx
  • I fixed my code style errors, but there are some more (of others)
  • I'll open a dedicated PR for 2.4.x

@Al2Klimov Al2Klimov mentioned this pull request Apr 23, 2018
9 tasks
@edmundoa edmundoa self-requested a review April 25, 2018 09:11
@edmundoa edmundoa self-assigned this Apr 25, 2018
Copy link
Copy Markdown
Contributor

@edmundoa edmundoa left a comment

Choose a reason for hiding this comment

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

Other than the inline comments, I'm not sure how reliable this approach is across browsers.

On the backend, we use a LinkedHashMap that will guarantee order, but on the frontend we store the results in an Object, and Object properties are not guaranteed to be ordered, even if most browsers do.

If we want to guarantee to keep fields in order, we should use another strategy, for instance sorting by optionality and then by key name. That will at least guarantee that all fields are in the same order in all browsers, as long as key names are not changed.


const configFieldKeys = $.map(this.state.configFields, (v, k) => k).sort(this._sortByOptionality);
const configFieldKeys = $.map(this.state.configFields, (v, k) => k)
.map((v, i) => ({ key: v, pos: i }))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know you didn't wrote that line, but I would vote for replacing the $.map(this.state.configFields, (v, k) => k) with the much clearer, in my opinion, Object.keys(this.state.configFields).

Please also use a clearer name for v. I suggest key instead. In the end we minify the code, so better make it easier to understand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


_sortByOptionality(x1, x2) {
return (this.state.configFields[x1].is_optional - this.state.configFields[x2].is_optional);
return this.state.configFields[x1.key].is_optional - this.state.configFields[x2.key].is_optional || x1.pos - x2.pos;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should rewrite this in a more explicit way.
Subtracting two booleans is already hard to understand without deep understanding of JS internals, and using the or operator to do another comparison when the previous result was 0 only makes it harder to understand.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@edmundoa
Copy link
Copy Markdown
Contributor

I got carried on by the review and I forgot to mention: thank you very much for your contribution! Let's work through the code and get this fix for good 🙂

@Al2Klimov
Copy link
Copy Markdown
Contributor Author

Hello @edmundoa and thank you for responding!

we should use another strategy, for instance sorting (...) by key name.

This would be a breaking change for our output plugin and for all the others.
So far plugin authors registered the config options in the desired order (e.g. host, user, password, ...), but this would (1) shuffle all options (of all plugins) (2) during a patchlevel release.

Besides that.. AFAIK no one complained about that.

So I won't change that – at least not in this PR as it doesn't solve the problem.

But you can expect the other changes soon.

Best,
AK

@edmundoa
Copy link
Copy Markdown
Contributor

@Al2Klimov sounds reasonable to me. For what I read, most browsers respect the Object property order, even if the standard does not require that. The way I see it, at least the situation will be better than it was before for most people.

To further guarantee that the inputs will appear in the order specified by the author (even if it is implicitly), we would need to explicitly export the order of each key in the API, allowing to read that property from the JS code and sorting by it. That change would be fine for 3.0, but it would be a no-go for 2.4.x.

@Al2Klimov
Copy link
Copy Markdown
Contributor Author

@edmundoa Btw @N-o-X and me wondered why you even sort by optionality. If I as a plugin author want the options to be sorted this way, I register them according to this. But if I'd like a custom order.. I have no chance.

@edmundoa
Copy link
Copy Markdown
Contributor

@Al2Klimov to be honest, I don't know the reason for it and it probably happened organically. I guess the reason was to let users see the mandatory fields they had to fill out first, in case they didn't pay much attention to the form. Also sometimes configuration fields are put together from different classes and maybe the sorting looked somehow "wrong".

Feel free to open an issue for that so we can discuss it and maybe also change it.

Copy link
Copy Markdown
Contributor

@edmundoa edmundoa left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@edmundoa edmundoa merged commit 730eaa0 into Graylog2:master Apr 25, 2018
@edmundoa
Copy link
Copy Markdown
Contributor

Thank you for your contribution @Al2Klimov! Do you want to update the 2.4 branch with the changes or should I?

@Al2Klimov Al2Klimov deleted the bugfix/config-sort-stable branch April 25, 2018 11:49
@Al2Klimov
Copy link
Copy Markdown
Contributor Author

I'll do.

edmundoa pushed a commit that referenced this pull request Apr 25, 2018
@edmundoa edmundoa added this to the 3.0.0 milestone Apr 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output config options ordered randomly

3 participants