Skip to content

Commit

Permalink
export documents in discover tab in CSV format
Browse files Browse the repository at this point in the history
  • Loading branch information
tongwang authored and fbaligand committed Jun 8, 2018
1 parent 80f9878 commit 4888a41
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/ui/public/doc_table/doc_table.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
<div
class="doc-table-container"
ng-if="hits.length"
ng-class="{ loading: searchSource.activeFetchCount > 0 }"
>
ng-class="{ loading: searchSource.activeFetchCount > 0 }">

<div class="export">
<small>Export:</small>&nbsp;&nbsp;
<a class="small" ng-click="exportAsCsv(false)">
Raw <i aria-hidden="true" class="fa fa-download"></i>
</a>&nbsp;&nbsp;&nbsp;
<a class="small" ng-click="exportAsCsv(true)">
Formatted <i aria-hidden="true" class="fa fa-download"></i>
</a>
</div>

<div ng-if="!infiniteScroll">
<div class="kuiBar docTableBar">
<div class="kuiBarSection">
Expand Down Expand Up @@ -109,4 +119,4 @@
<div ng-if="hits != null && !hits.length" class="table-vis-error">
<h2><i class="fa fa-meh-o"></i></h2>
<h4>No results found</h4>
</div>
</div>
77 changes: 76 additions & 1 deletion src/ui/public/doc_table/doc_table.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import _ from 'lodash';
import html from 'ui/doc_table/doc_table.html';
import { getSort } from 'ui/doc_table/lib/get_sort';
import { saveAs } from '@elastic/filesaver';
import 'ui/doc_table/doc_table.less';
import 'ui/directives/truncated';
import 'ui/directives/infinite_scroll';
import 'ui/doc_table/components/table_header';
import 'ui/doc_table/components/table_row';
import { uiModules } from 'ui/modules';
import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';


import { getLimitedSearchResultsMessage } from './doc_table_strings';

uiModules.get('kibana')
.directive('docTable', function (config, Notifier, getAppState, pagerFactory, $filter) {
.directive('docTable', function (config, Notifier, getAppState, pagerFactory, $filter, Private) {
const fieldFormats = Private(RegistryFieldFormatsProvider);
return {
restrict: 'E',
template: html,
Expand Down Expand Up @@ -140,6 +144,77 @@ uiModules.get('kibana')
$scope.shouldShowLimitedResultsWarning = () => (
!$scope.pager.hasNextPage && $scope.pager.totalItems < $scope.totalHitCount
);

$scope.exportAsCsv = function (formatted) {
var csv = {
separator: config.get('csv:separator'),
quoteValues: config.get('csv:quoteValues')
};

var rows = $scope.hits;
var columns = $scope.columns;
if ($scope.indexPattern.timeFieldName) {
columns = [$scope.indexPattern.timeFieldName].concat(columns);
}
var nonAlphaNumRE = /[^a-zA-Z0-9]/;
var allDoubleQuoteRE = /"/g;

function escape(val) {
if (_.isObject(val)) val = val.valueOf();
val = String(val);
if (csv.quoteValues && nonAlphaNumRE.test(val)) {
val = '"' + val.replace(allDoubleQuoteRE, '""') + '"';
}
return val;
}

function formatField(value, name) {
var field = $scope.indexPattern.fields.byName[name];
if (!field) return value;
var defaultFormat = fieldFormats.getDefaultType(field.type);
var formatter = (field && field.format) ? field.format : defaultFormat;

return formatter.convert(value);
}

function formatRow(row) {
$scope.indexPattern.flattenHit(row);
row.$$_formatted = row.$$_formatted || _.mapValues(row.$$_flattened, formatField);
return row.$$_formatted;
}

// get column values for each row
var csvRows = rows.map(function (row, i) {
return columns.map(function (column, j) {
var val;

if (formatted) {
val = (row.$$_formatted || formatRow(row))[column];
} else {
val = (row.$$_flattened || formatRow(row))[column];
}

val = (val == null) ? '' : val;

return val;
});
});

// escape each cell in each row
csvRows = csvRows.map(function (row, i) {
return row.map(escape);
});

// add the columns to the rows
csvRows.unshift(columns.map(escape));

var data = csvRows.map(function (row) {
return row.join(csv.separator) + '\r\n';
}).join('');

saveAs(new Blob([data], { type: 'text/plain' }), 'export.csv');
};

}
};
});
4 changes: 4 additions & 0 deletions src/ui/public/doc_table/doc_table.less
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ doc-table {
z-index: 20;
opacity: @loading-opacity;
}

.export {
margin: 10px;
}
}

/**
Expand Down

0 comments on commit 4888a41

Please sign in to comment.