From beb57396ce2059b33e58ecfd5ca1ee899291ff9d Mon Sep 17 00:00:00 2001 From: sonenko Date: Wed, 5 Aug 2015 19:36:59 +0300 Subject: [PATCH 1/5] add ability to display avg aggregation in total for dataTable --- src/plugins/table_vis/public/table_vis.html | 4 +++- src/plugins/table_vis/public/table_vis.js | 4 +++- .../table_vis/public/table_vis_params.html | 16 +++++++++++++ src/ui/public/agg_table/agg_table.html | 4 +++- src/ui/public/agg_table/agg_table.js | 24 ++++++++++++++++++- src/ui/public/agg_table/agg_table_group.html | 24 +++++++++++++++---- src/ui/public/agg_table/agg_table_group.js | 4 +++- .../paginated_table/paginated_table.html | 5 ++++ .../public/paginated_table/paginated_table.js | 4 +++- 9 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/plugins/table_vis/public/table_vis.html b/src/plugins/table_vis/public/table_vis.html index 33fdf9cf5e8f1..8e9b1f1f9660d 100644 --- a/src/plugins/table_vis/public/table_vis.html +++ b/src/plugins/table_vis/public/table_vis.html @@ -9,7 +9,9 @@

No results found

group="tableGroups" export-title="vis.title" per-page="vis.params.perPage" - sort="sort"> + sort="sort" + show-total="vis.params.showTotal" + total-func="vis.params.totalFunc"> diff --git a/src/plugins/table_vis/public/table_vis.js b/src/plugins/table_vis/public/table_vis.js index 91a6d00da2668..85ede8dbd9bb5 100644 --- a/src/plugins/table_vis/public/table_vis.js +++ b/src/plugins/table_vis/public/table_vis.js @@ -42,7 +42,9 @@ function TableVisTypeProvider(Private) { sort: { columnIndex: null, direction: null - } + }, + showTotal: false, + totalFunc: 'sum' }, editor: '' }, diff --git a/src/plugins/table_vis/public/table_vis_params.html b/src/plugins/table_vis/public/table_vis_params.html index 9c846808e065a..8f67b65647f03 100644 --- a/src/plugins/table_vis/public/table_vis_params.html +++ b/src/plugins/table_vis/public/table_vis_params.html @@ -23,3 +23,19 @@ Calculate metrics for every bucket/level + +
+ +
+ +
+ + +
\ No newline at end of file diff --git a/src/ui/public/agg_table/agg_table.html b/src/ui/public/agg_table/agg_table.html index 6e1a940fc0c4f..266d975589384 100644 --- a/src/ui/public/agg_table/agg_table.html +++ b/src/ui/public/agg_table/agg_table.html @@ -3,7 +3,9 @@ rows="rows" columns="formattedColumns" per-page="perPage" - sort="sort"> + sort="sort" + show-total="showTotal" + totalFunc="totalFunc">
Export:   diff --git a/src/ui/public/agg_table/agg_table.js b/src/ui/public/agg_table/agg_table.js index 1165de1244be9..fd2034a2d4f36 100644 --- a/src/ui/public/agg_table/agg_table.js +++ b/src/ui/public/agg_table/agg_table.js @@ -16,7 +16,9 @@ uiModules table: '=', perPage: '=?', sort: '=?', - exportTitle: '=?' + exportTitle: '=?', + showTotal: '=', + totalFunc: '=' }, controllerAs: 'aggTable', compile: function ($el) { @@ -93,6 +95,26 @@ uiModules formattedColumn.class = 'visualize-table-right'; } + let isFirstValueNumeric = + (table.rows && table.rows[0] && table.rows[0][i] && _.isNumber(table.rows[0][i].value)) === true; + + if ((!!field && field.type === 'number') || isFirstValueNumeric) { + function sum() { + return _.reduce(table.rows, function (prev, curr, n, all) {return prev + curr[i].value; }, 0); + } + + switch ($scope.totalFunc) { + case 'sum': + formattedColumn.total = sum(); + break; + case 'avg': + formattedColumn.total = sum() / table.rows.length; + break; + default: + break; + } + } + return formattedColumn; }); }); diff --git a/src/ui/public/agg_table/agg_table_group.html b/src/ui/public/agg_table/agg_table_group.html index 5a8defc96d26b..2c176524cd6a5 100644 --- a/src/ui/public/agg_table/agg_table_group.html +++ b/src/ui/public/agg_table/agg_table_group.html @@ -9,13 +9,21 @@ - + + sort="sort" + show-total="showTotal" + total-func="totalFunc"> @@ -33,13 +41,21 @@ - + + sort="sort" + show-total="showTotal" + total-func="totalFunc"> diff --git a/src/ui/public/agg_table/agg_table_group.js b/src/ui/public/agg_table/agg_table_group.js index cfbf903299c9b..2cd1031182ba4 100644 --- a/src/ui/public/agg_table/agg_table_group.js +++ b/src/ui/public/agg_table/agg_table_group.js @@ -13,7 +13,9 @@ uiModules group: '=', perPage: '=?', sort: '=?', - exportTitle: '=?' + exportTitle: '=?', + showTotal: '=', + totalFunc: '=' }, compile: function ($el) { // Use the compile function from the RecursionHelper, diff --git a/src/ui/public/paginated_table/paginated_table.html b/src/ui/public/paginated_table/paginated_table.html index d40155c0d1578..1d64e70180ce6 100644 --- a/src/ui/public/paginated_table/paginated_table.html +++ b/src/ui/public/paginated_table/paginated_table.html @@ -27,6 +27,11 @@ + + + {{col.total | number}} + +
diff --git a/src/ui/public/paginated_table/paginated_table.js b/src/ui/public/paginated_table/paginated_table.js index 5f00db096f1c5..1fbef21b8479b 100644 --- a/src/ui/public/paginated_table/paginated_table.js +++ b/src/ui/public/paginated_table/paginated_table.js @@ -17,7 +17,9 @@ uiModules perPage: '=?', sortHandler: '=?', sort: '=?', - showSelector: '=?' + showSelector: '=?', + showTotal: '=', + totalFunc: '=' }, controllerAs: 'paginatedTable', controller: function ($scope) { From 02af709179872f698b236ab7d27254d121cba895 Mon Sep 17 00:00:00 2001 From: sonenko Date: Fri, 7 Aug 2015 11:33:07 +0300 Subject: [PATCH 2/5] remove redundant parts --- src/ui/public/agg_table/agg_table.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ui/public/agg_table/agg_table.js b/src/ui/public/agg_table/agg_table.js index fd2034a2d4f36..6648f451a4bc8 100644 --- a/src/ui/public/agg_table/agg_table.js +++ b/src/ui/public/agg_table/agg_table.js @@ -65,10 +65,10 @@ uiModules return escape(col.title); })); - return csvRows.map(function (row) { - return row.join(self.csv.separator) + '\r\n'; - }).join(''); - }; + return csvRows.map(function (row) { + return row.join(self.csv.separator) + '\r\n'; + }).join(''); + }; $scope.$watch('table', function () { let table = $scope.table; @@ -96,9 +96,9 @@ uiModules } let isFirstValueNumeric = - (table.rows && table.rows[0] && table.rows[0][i] && _.isNumber(table.rows[0][i].value)) === true; + table.rows && table.rows[0] && table.rows[0][i] && _.isNumber(table.rows[0][i].value); - if ((!!field && field.type === 'number') || isFirstValueNumeric) { + if ((field && field.type === 'number') || isFirstValueNumeric) { function sum() { return _.reduce(table.rows, function (prev, curr, n, all) {return prev + curr[i].value; }, 0); } From 2169efb92ab7d0f4e01930d2b825ad3e283fe330 Mon Sep 17 00:00:00 2001 From: sonenko Date: Tue, 25 Aug 2015 23:38:53 +0300 Subject: [PATCH 3/5] re #4527 add min, max, and count aggregation to display in total for dataTable visualization --- src/plugins/table_vis/public/table_vis_params.html | 2 +- src/ui/public/agg_table/agg_table.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/plugins/table_vis/public/table_vis_params.html b/src/plugins/table_vis/public/table_vis_params.html index 8f67b65647f03..a7cc4130b7a46 100644 --- a/src/plugins/table_vis/public/table_vis_params.html +++ b/src/plugins/table_vis/public/table_vis_params.html @@ -36,6 +36,6 @@ \ No newline at end of file diff --git a/src/ui/public/agg_table/agg_table.js b/src/ui/public/agg_table/agg_table.js index 6648f451a4bc8..408e1cb07e055 100644 --- a/src/ui/public/agg_table/agg_table.js +++ b/src/ui/public/agg_table/agg_table.js @@ -110,6 +110,15 @@ uiModules case 'avg': formattedColumn.total = sum() / table.rows.length; break; + case 'min': + formattedColumn.total = _.chain(table.rows).map(i).map('value').min().value(); + break; + case 'max': + formattedColumn.total = _.chain(table.rows).map(i).map('value').max().value(); + break; + case 'count': + formattedColumn.total = table.rows.length; + break; default: break; } From 60e18b3de6dcd8d6c59e5aa9d5507b11a2be720a Mon Sep 17 00:00:00 2001 From: sonenko Date: Tue, 14 Jun 2016 00:47:38 +0300 Subject: [PATCH 4/5] fix style tests --- src/ui/public/agg_table/agg_table.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ui/public/agg_table/agg_table.js b/src/ui/public/agg_table/agg_table.js index 408e1cb07e055..b8cfe720217ee 100644 --- a/src/ui/public/agg_table/agg_table.js +++ b/src/ui/public/agg_table/agg_table.js @@ -65,10 +65,10 @@ uiModules return escape(col.title); })); - return csvRows.map(function (row) { - return row.join(self.csv.separator) + '\r\n'; - }).join(''); - }; + return csvRows.map(function (row) { + return row.join(self.csv.separator) + '\r\n'; + }).join(''); + }; $scope.$watch('table', function () { let table = $scope.table; From 9d61c8f2075b0d54ced0b158236764d9a214d4cf Mon Sep 17 00:00:00 2001 From: sonenko Date: Tue, 14 Jun 2016 18:13:36 +0300 Subject: [PATCH 5/5] refactor total aggregations for table after review --- src/plugins/table_vis/public/table_vis_params.html | 4 ++-- src/plugins/table_vis/public/table_vis_params.js | 2 ++ src/ui/public/agg_table/agg_table.js | 14 +++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/plugins/table_vis/public/table_vis_params.html b/src/plugins/table_vis/public/table_vis_params.html index a7cc4130b7a46..16c24dc67cf62 100644 --- a/src/plugins/table_vis/public/table_vis_params.html +++ b/src/plugins/table_vis/public/table_vis_params.html @@ -36,6 +36,6 @@ - \ No newline at end of file + diff --git a/src/plugins/table_vis/public/table_vis_params.js b/src/plugins/table_vis/public/table_vis_params.js index 3b1e703ede435..5bd00795dae6d 100644 --- a/src/plugins/table_vis/public/table_vis_params.js +++ b/src/plugins/table_vis/public/table_vis_params.js @@ -8,6 +8,8 @@ uiModules.get('kibana/table_vis') restrict: 'E', template: tableVisParamsTemplate, link: function ($scope) { + $scope.totalAggregations = ['sum', 'avg', 'min', 'max', 'count']; + $scope.$watchMulti([ 'vis.params.showPartialRows', 'vis.params.showMeticsAtAllLevels' diff --git a/src/ui/public/agg_table/agg_table.js b/src/ui/public/agg_table/agg_table.js index b8cfe720217ee..a6853254ef8ec 100644 --- a/src/ui/public/agg_table/agg_table.js +++ b/src/ui/public/agg_table/agg_table.js @@ -95,20 +95,20 @@ uiModules formattedColumn.class = 'visualize-table-right'; } - let isFirstValueNumeric = - table.rows && table.rows[0] && table.rows[0][i] && _.isNumber(table.rows[0][i].value); + const isFieldNumeric = (field && field.type === 'number'); + const isFirstValueNumeric = _.isNumber(_.get(table, `rows[0][${i}].value`)); - if ((field && field.type === 'number') || isFirstValueNumeric) { - function sum() { - return _.reduce(table.rows, function (prev, curr, n, all) {return prev + curr[i].value; }, 0); + if (isFieldNumeric || isFirstValueNumeric) { + function sum(tableRows) { + return _.reduce(tableRows, function (prev, curr, n, all) {return prev + curr[i].value; }, 0); } switch ($scope.totalFunc) { case 'sum': - formattedColumn.total = sum(); + formattedColumn.total = sum(table.rows); break; case 'avg': - formattedColumn.total = sum() / table.rows.length; + formattedColumn.total = sum(table.rows) / table.rows.length; break; case 'min': formattedColumn.total = _.chain(table.rows).map(i).map('value').min().value();