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

Factor out column manipulation in the doc table #10681

Conversation

weltenwort
Copy link
Member

@weltenwort weltenwort commented Mar 3, 2017

This refactoring effort turns the implicit manipulation of the set of
columns that was scattered throughout the doc table and the field
chooser into explicit function calls. This brings with it the following
improvements:

  • The column manipulation code is not duplicated (DRY)
  • The controller stays in control (IOC)
  • If the required functions are not provided by the controller,
    manipulation of the columns is disabled.

Additionally, the discover_field now uses a properly isolated scope
instead of accessing inherited properties of the scope.

As a side benefit, this enables us to restrict the ability of the user to change the columns in saved search panels on dashboards. See #9523 for a (currently unresolved) discussion on that topic.

@weltenwort weltenwort self-assigned this Mar 3, 2017
@weltenwort weltenwort mentioned this pull request Mar 3, 2017
3 tasks
@weltenwort weltenwort force-pushed the refactoring/discover-table-dry-column-change branch from d577e31 to aa68edb Compare March 3, 2017 15:51
@weltenwort weltenwort force-pushed the refactoring/discover-table-dry-column-change branch 4 times, most recently from 7eeabcd to d9131a3 Compare March 28, 2017 15:30
@weltenwort weltenwort requested review from lukasolson and Bargs March 28, 2017 18:58
Copy link
Contributor

@stacey-gammon stacey-gammon left a comment

Choose a reason for hiding this comment

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

This looks awesome Felix! Much improved.

},
controller: function ($scope) {
$scope.mapping = $scope.indexPattern.fields.byName;
$scope.flattened = $scope.indexPattern.flattenHit($scope.hit);
$scope.formatted = $scope.indexPattern.formatHit($scope.hit);
$scope.fields = _.keys($scope.flattened).sort();

$scope.toggleColumn = function (fieldName) {
_.toggleInOut($scope.columns, fieldName);
$scope.canToggleColumn = function canToggleColumn() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is called per row so could maybe pull out the calculation so it doesn't run every time?

const canToggleColumns = _.isFunction($scope.onAddColumn) && _.isFunction($scope.onRemoveColumn);
$scope.canToggleColumns = () => canToggleColumns;

Though it's probably like a nanosecond extra for those _.isFunction calculations and negligible in the long run, so feel free to ignore. :)

Though I do think I'd call it canToggleColumns which makes it clearer it's either on or off for all.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point about the plural, thanks!

To get the precomputed value correct I would have to set up a watch (because $scope.onAddColumn and $scope.onRemoveColumn could change over time), which would probably already negate some of the benefit. I'll take a look at the profiler to see if it's worth it 😉

};

$scope.removeColumn = function removeColumn(columnName) {
$scope.savedObj.searchSource.get('index').popularizeField(columnName, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. Is this new? A quick test seems to indicate that previously adjusting the columns via dashboard didn't affect popularity score. Makes sense to add it here, though.

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, this was previously not consistently triggered for the different ways to add columns. I actually didn't notice, but I guess we could keep it.

Copy link
Member

Choose a reason for hiding this comment

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

I would think it would make sense to add it here as well.

Copy link
Member

@lukasolson lukasolson left a comment

Choose a reason for hiding this comment

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

I left a few tiny nitpicks below, but really this LGTM. Thanks for taking this on!

link: function ($scope, $elem) {
let detailsElem;
let detailScope = $scope.$new();
let detailScope = null;
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to initialize this to null rather than just leaving uninitialized?

Copy link
Member Author

Choose a reason for hiding this comment

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

no, just habit from languages where this could blow up 😉 thanks for pointing it out

@@ -155,7 +152,7 @@ app.directive('discFieldChooser', function ($location, globalState, config, $rou
$scope.indexPattern.popularizeField(fieldName, 1);
};

$scope.vizLocation = function (field) {
function getVizualizeUrl(field) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Misspelling of "visualize"

Copy link
Member Author

Choose a reason for hiding this comment

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

zZzZz, thanks - that's the downside of auto-completion

ng-if="canMoveColumnRight(name)"
tooltip-append-to-body="1"
tooltip="Move column to the right"
></i>
Copy link
Member

Choose a reason for hiding this comment

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

Soooo much more readable. Thanks 👍

@weltenwort weltenwort force-pushed the refactoring/discover-table-dry-column-change branch from d9131a3 to 68a50ee Compare March 31, 2017 11:04
@weltenwort
Copy link
Member Author

jenkins, test this (and stop failing arbitrarily)

@weltenwort weltenwort force-pushed the refactoring/discover-table-dry-column-change branch from f57d78d to 1850e2c Compare April 3, 2017 08:53
This refactoring effort turns the implicit manipulation of the set of
columns that was scattered throughout the doc table and the field
chooser into explicit function calls. This brings with it the following
improvements:

* The column manipulation code is not duplicated (DRY)
* The controller stays in control (IOC)
* If the required functions are not provided by the controller,
  manipulation of the columns is disabled.

Additionally, the `discover_field` now uses a properly isolated scope
instead of accessing inherited properties of the scope.
@weltenwort weltenwort force-pushed the refactoring/discover-table-dry-column-change branch from 1850e2c to 7f2bda8 Compare April 3, 2017 15:23
@weltenwort weltenwort merged commit 18e0252 into elastic:master Apr 3, 2017
weltenwort added a commit to weltenwort/kibana that referenced this pull request Apr 3, 2017
* Factor out column manipulation in the doc table

This refactoring effort turns the implicit manipulation of the set of
columns that was scattered throughout the doc table and the field
chooser into explicit function calls. This brings with it the following
improvements:

* The column manipulation code is not duplicated (DRY)
* The controller stays in control (IOC)
* If the required functions are not provided by the controller,
  manipulation of the columns is disabled.

Additionally, the `discover_field` now uses a properly isolated scope
instead of accessing inherited properties of the scope.

* Make filter addition and removal in tests more reliable

* Change function name to plural, move up ng-if

* Remove inconsistent variable initialization

* Fix function name typo

* Save the state in the action instead of a $watch
weltenwort added a commit that referenced this pull request Apr 3, 2017
Backports PR #10681

* Factor out column manipulation in the doc table

This refactoring effort turns the implicit manipulation of the set of
columns that was scattered throughout the doc table and the field
chooser into explicit function calls. This brings with it the following
improvements:

* The column manipulation code is not duplicated (DRY)
* The controller stays in control (IOC)
* If the required functions are not provided by the controller,
  manipulation of the columns is disabled.

Additionally, the `discover_field` now uses a properly isolated scope
instead of accessing inherited properties of the scope.

* Make filter addition and removal in tests more reliable

* Change function name to plural, move up ng-if

* Remove inconsistent variable initialization

* Fix function name typo

* Save the state in the action instead of a $watch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants