Skip to content

Commit

Permalink
Fix: Wrong formatted CSV when splitting tables
Browse files Browse the repository at this point in the history
  • Loading branch information
stratoula committed May 25, 2020
1 parent d0ecdd9 commit ac428ef
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
23 changes: 16 additions & 7 deletions src/plugins/vis_type_table/public/agg_table/agg_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function KbnAggTable(config, RecursionHelper) {
self._saveAs(csv, self.csv.filename);
};

self.toCsv = function(formatted) {
self.toCsv = function (formatted) {
const rows = formatted ? $scope.rows : $scope.table.rows;
const columns = formatted ? $scope.formattedColumns : $scope.table.columns;
const nonAlphaNumRE = /[^a-zA-Z0-9]/;
Expand All @@ -72,11 +72,14 @@ export function KbnAggTable(config, RecursionHelper) {
}

let csvRows = [];
rows.map(row => {
rows.map((row) => {
const rowArray = [];
Object.entries(row).map(([k, v]) => {
const column = columns.find(c => c.id === k);
const columnIdx = columns.findIndex(c => c.id === k);
const column = columns.find((c) => c.id === k);

if (!column) return;

const columnIdx = columns.findIndex((c) => c.id === k);
const formattedValue =
formatted && column ? escape(column.formatter.convert(v)) : escape(v);
rowArray.splice(columnIdx, 0, formattedValue);
Expand Down Expand Up @@ -116,16 +119,22 @@ export function KbnAggTable(config, RecursionHelper) {

if (typeof $scope.dimensions === 'undefined') return;

const { buckets, metrics, splitColumn } = $scope.dimensions;
const { buckets, metrics, splitColumn, splitRow } = $scope.dimensions;

$scope.formattedColumns = table.columns
.map(function (col, i) {
const isBucket = buckets.find((bucket) => bucket.accessor === i);
const isSplitColumn = splitColumn
? splitColumn.find((splitColumn) => splitColumn.accessor === i)
: undefined;
const isSplitRow = splitRow
? splitRow.find((splitRow) => splitRow.accessor === i)
: undefined;
const dimension =
isBucket || isSplitColumn || metrics.find((metric) => metric.accessor === i);
isBucket ||
isSplitColumn ||
isSplitRow ||
metrics.find((metric) => metric.accessor === i);

if (!dimension) return;

Expand Down Expand Up @@ -253,7 +262,7 @@ function addPercentageCol(columns, title, rows, insertAtIndex) {
id: newId,
formatter,
});
const newRows = rows.map(row => ({
const newRows = rows.map((row) => ({
[newId]: row[id] / sumTotal,
...row,
}));
Expand Down
72 changes: 36 additions & 36 deletions test/functional/apps/visualize/_data_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,26 +415,26 @@ export default function ({ getService, getPageObjects }) {
const data = await PageObjects.visChart.getTableVisContent();
expect(data).to.be.eql([
[
['CN', 'CN', '330'],
['CN', 'IN', '274'],
['CN', 'US', '140'],
['IN', 'CN', '286'],
['IN', 'IN', '281'],
['IN', 'US', '133'],
['US', 'CN', '135'],
['US', 'IN', '134'],
['US', 'US', '52'],
['jpg', 'CN', 'CN', '330'],
['jpg', 'CN', 'IN', '274'],
['jpg', 'CN', 'US', '140'],
['jpg', 'IN', 'CN', '286'],
['jpg', 'IN', 'IN', '281'],
['jpg', 'IN', 'US', '133'],
['jpg', 'US', 'CN', '135'],
['jpg', 'US', 'IN', '134'],
['jpg', 'US', 'US', '52'],
],
[
['CN', 'CN', '90'],
['CN', 'IN', '84'],
['CN', 'US', '27'],
['IN', 'CN', '69'],
['IN', 'IN', '58'],
['IN', 'US', '34'],
['US', 'IN', '36'],
['US', 'CN', '29'],
['US', 'US', '13'],
['css', 'CN', 'CN', '90'],
['css', 'CN', 'IN', '84'],
['css', 'CN', 'US', '27'],
['css', 'IN', 'CN', '69'],
['css', 'IN', 'IN', '58'],
['css', 'IN', 'US', '34'],
['css', 'US', 'IN', '36'],
['css', 'US', 'CN', '29'],
['css', 'US', 'US', '13'],
],
]);
});
Expand All @@ -446,26 +446,26 @@ export default function ({ getService, getPageObjects }) {
const data = await PageObjects.visChart.getTableVisContent();
expect(data).to.be.eql([
[
['CN', '1,718', 'CN', '330'],
['CN', '1,718', 'IN', '274'],
['CN', '1,718', 'US', '140'],
['IN', '1,511', 'CN', '286'],
['IN', '1,511', 'IN', '281'],
['IN', '1,511', 'US', '133'],
['US', '770', 'CN', '135'],
['US', '770', 'IN', '134'],
['US', '770', 'US', '52'],
['jpg', 'CN', '1,718', 'CN', '330'],
['jpg', 'CN', '1,718', 'IN', '274'],
['jpg', 'CN', '1,718', 'US', '140'],
['jpg', 'IN', '1,511', 'CN', '286'],
['jpg', 'IN', '1,511', 'IN', '281'],
['jpg', 'IN', '1,511', 'US', '133'],
['jpg', 'US', '770', 'CN', '135'],
['jpg', 'US', '770', 'IN', '134'],
['jpg', 'US', '770', 'US', '52'],
],
[
['CN', '422', 'CN', '90'],
['CN', '422', 'IN', '84'],
['CN', '422', 'US', '27'],
['IN', '346', 'CN', '69'],
['IN', '346', 'IN', '58'],
['IN', '346', 'US', '34'],
['US', '189', 'IN', '36'],
['US', '189', 'CN', '29'],
['US', '189', 'US', '13'],
['css', 'CN', '422', 'CN', '90'],
['css', 'CN', '422', 'IN', '84'],
['css', 'CN', '422', 'US', '27'],
['css', 'IN', '346', 'CN', '69'],
['css', 'IN', '346', 'IN', '58'],
['css', 'IN', '346', 'US', '34'],
['css', 'US', '189', 'IN', '36'],
['css', 'US', '189', 'CN', '29'],
['css', 'US', '189', 'US', '13'],
],
]);
});
Expand Down

0 comments on commit ac428ef

Please sign in to comment.