Skip to content

Commit

Permalink
Merge pull request elastic#4570 from sonenko/master
Browse files Browse the repository at this point in the history
show total in DataTable

Former-commit-id: c6f5963
  • Loading branch information
panda01 authored Jun 21, 2016
2 parents 479a82a + 9d61c8f commit c48964e
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/plugins/table_vis/public/table_vis.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ <h4>No results found</h4>
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">
</kbn-agg-table-group>
</div>
</div>
4 changes: 3 additions & 1 deletion src/plugins/table_vis/public/table_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function TableVisTypeProvider(Private) {
sort: {
columnIndex: null,
direction: null
}
},
showTotal: false,
totalFunc: 'sum'
},
editor: '<table-vis-params></table-vis-params>'
},
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/table_vis/public/table_vis_params.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@
Calculate metrics for every bucket/level
</label>
</div>

<div class="checkbox">
<label>
<input type="checkbox" ng-model="vis.params.showTotal">
Show total
</label>
</div>

<div>
<label>Total function</label>
<select ng-disabled="!vis.params.showTotal"
class="form-control"
ng-model="vis.params.totalFunc"
ng-options="x for x in totalAggregations">
</select>
</div>
2 changes: 2 additions & 0 deletions src/plugins/table_vis/public/table_vis_params.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion src/ui/public/agg_table/agg_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
rows="rows"
columns="formattedColumns"
per-page="perPage"
sort="sort">
sort="sort"
show-total="showTotal"
totalFunc="totalFunc">

<div class="agg-table-controls">
<small>Export:</small>&nbsp;&nbsp;
Expand Down
33 changes: 32 additions & 1 deletion src/ui/public/agg_table/agg_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ uiModules
table: '=',
perPage: '=?',
sort: '=?',
exportTitle: '=?'
exportTitle: '=?',
showTotal: '=',
totalFunc: '='
},
controllerAs: 'aggTable',
compile: function ($el) {
Expand Down Expand Up @@ -93,6 +95,35 @@ uiModules
formattedColumn.class = 'visualize-table-right';
}

const isFieldNumeric = (field && field.type === 'number');
const isFirstValueNumeric = _.isNumber(_.get(table, `rows[0][${i}].value`));

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(table.rows);
break;
case 'avg':
formattedColumn.total = sum(table.rows) / 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;
}
}

return formattedColumn;
});
});
Expand Down
24 changes: 20 additions & 4 deletions src/ui/public/agg_table/agg_table_group.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
<tbody ng-repeat-end>
<tr>
<td>
<kbn-agg-table-group ng-if="table.tables" group="table" per-page="perPage" sort="sort"></kbn-agg-table-group>
<kbn-agg-table-group
ng-if="table.tables"
group="table"
per-page="perPage"
sort="sort"
show-total="showTotal"
total-func="totalFunc"></kbn-agg-table-group>
<kbn-agg-table
ng-if="table.rows"
table="table"
export-title="exportTitle"
per-page="perPage"
sort="sort">
sort="sort"
show-total="showTotal"
total-func="totalFunc">
</kbn-agg-table>
</td>
</tr>
Expand All @@ -33,13 +41,21 @@
<tbody>
<tr>
<td ng-repeat="table in columns">
<kbn-agg-table-group ng-if="table.tables" group="table" per-page="perPage" sort="sort"></kbn-agg-table-group>
<kbn-agg-table-group
ng-if="table.tables"
group="table"
per-page="perPage"
sort="sort"
show-total="showTotal"
total-func="totalFunc"></kbn-agg-table-group>
<kbn-agg-table
ng-if="table.rows"
table="table"
export-title="exportTitle"
per-page="perPage"
sort="sort">
sort="sort"
show-total="showTotal"
total-func="totalFunc">
</kbn-agg-table>
</td>
</tr>
Expand Down
4 changes: 3 additions & 1 deletion src/ui/public/agg_table/agg_table_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ uiModules
group: '=',
perPage: '=?',
sort: '=?',
exportTitle: '=?'
exportTitle: '=?',
showTotal: '=',
totalFunc: '='
},
compile: function ($el) {
// Use the compile function from the RecursionHelper,
Expand Down
5 changes: 5 additions & 0 deletions src/ui/public/paginated_table/paginated_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
</tr>
</thead>
<tbody kbn-rows="page" kbn-rows-min="perPage"></tbody>
<tfoot ng-if="showTotal">
<tr>
<th ng-repeat="col in columns" class="numeric-value">{{col.total | number}}</th>
</tr>
</tfoot>
</table>
</div>

Expand Down
4 changes: 3 additions & 1 deletion src/ui/public/paginated_table/paginated_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ uiModules
perPage: '=?',
sortHandler: '=?',
sort: '=?',
showSelector: '=?'
showSelector: '=?',
showTotal: '=',
totalFunc: '='
},
controllerAs: 'paginatedTable',
controller: function ($scope) {
Expand Down

0 comments on commit c48964e

Please sign in to comment.