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

[Lens] CSV Export for Lens #83430

Merged
merged 29 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
078e554
:sparkles: First draft of csv export in Lens
dej611 Nov 16, 2020
5d789d6
:alembic: Create action stub
dej611 Nov 16, 2020
965bb69
:truck: + :white_check_mark: Move export csv to data plugins + basic …
dej611 Nov 17, 2020
456d14a
:lipstick: Enable/disable rather than show/hide button
dej611 Nov 17, 2020
8f917b1
:bug: Fix tests
dej611 Nov 17, 2020
851b98f
:speech_balloon: Fix i18n checks + :packages: Update API docs
dej611 Nov 17, 2020
08d75d0
:fire: removed embeddable action for now
dej611 Nov 17, 2020
12ac1e0
Merge remote-tracking branch 'upstream/master' into feature/lens/csv-…
dej611 Nov 17, 2020
88eefc6
:bug: Forgotten type
dej611 Nov 18, 2020
ce68a93
Merge remote-tracking branch 'upstream/master' into feature/lens/csv-…
dej611 Nov 18, 2020
88c2a8d
:lipstick: Move the cancel button on the right
dej611 Nov 18, 2020
27678f4
:bug: Avoid to save the activeData content
dej611 Nov 18, 2020
52f606f
:bug: Fix tests
dej611 Nov 18, 2020
237664e
Merge branch 'master' into feature/lens/csv-export
kibanamachine Nov 19, 2020
4de7ae8
:sparkles: split the current plugin in two distinct areas
dej611 Nov 19, 2020
6e88319
Merge branch 'feature/lens/csv-export' of github.com:dej611/kibana in…
dej611 Nov 19, 2020
f851e69
:memo: Update API documentation
dej611 Nov 20, 2020
3dad985
:ok_hand: Integrated feedback from review
dej611 Nov 20, 2020
5749f32
:label: fix type issue
dej611 Nov 20, 2020
4674a83
:white_check_mark: Add download unit tests
dej611 Nov 20, 2020
3da4fab
Merge branch 'master' into feature/lens/csv-export
kibanamachine Nov 23, 2020
4cada29
:ok_hand: Integrate feedback
dej611 Nov 23, 2020
1fbdeef
Merge branch 'feature/lens/csv-export' of github.com:dej611/kibana in…
dej611 Nov 23, 2020
084a22f
:memo: Update API doc
dej611 Nov 23, 2020
cb173b0
Merge remote-tracking branch 'upstream/master' into feature/lens/csv-…
dej611 Nov 23, 2020
ca868b3
:recycle: Refactor export plugin as per feedback received
dej611 Nov 23, 2020
0e7770c
:memo: Update API doc
dej611 Nov 23, 2020
80eac9f
:ok_hand: use named exports
dej611 Nov 23, 2020
c3941a1
:white_check_mark: Add basic functional test
dej611 Nov 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [exporters](./kibana-plugin-plugins-data-public.exporters.md)

## exporters variable

<b>Signature:</b>

```typescript
exporters: {
datatableToCSV: typeof datatableToCSV;
CSV_MIME_TYPE: string;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
| [esFilters](./kibana-plugin-plugins-data-public.esfilters.md) | |
| [esKuery](./kibana-plugin-plugins-data-public.eskuery.md) | |
| [esQuery](./kibana-plugin-plugins-data-public.esquery.md) | |
| [exporters](./kibana-plugin-plugins-data-public.exporters.md) | |
| [extractSearchSourceReferences](./kibana-plugin-plugins-data-public.extractsearchsourcereferences.md) | |
| [fieldFormats](./kibana-plugin-plugins-data-public.fieldformats.md) | |
| [fieldList](./kibana-plugin-plugins-data-public.fieldlist.md) | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) &gt; [exporters](./kibana-plugin-plugins-data-server.exporters.md)

## exporters variable

<b>Signature:</b>

```typescript
exporters: {
datatableToCSV: typeof datatableToCSV;
CSV_MIME_TYPE: string;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
| [esFilters](./kibana-plugin-plugins-data-server.esfilters.md) | |
| [esKuery](./kibana-plugin-plugins-data-server.eskuery.md) | |
| [esQuery](./kibana-plugin-plugins-data-server.esquery.md) | |
| [exporters](./kibana-plugin-plugins-data-server.exporters.md) | |
| [fieldFormats](./kibana-plugin-plugins-data-server.fieldformats.md) | |
| [indexPatterns](./kibana-plugin-plugins-data-server.indexpatterns.md) | |
| [mergeCapabilitiesWithFields](./kibana-plugin-plugins-data-server.mergecapabilitieswithfields.md) | |
Expand Down
85 changes: 85 additions & 0 deletions src/plugins/data/common/exports/export_csv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Datatable } from 'src/plugins/expressions';
import { FieldFormat } from '../../common/field_formats';
import { datatableToCSV } from './export_csv';

function getDefaultOptions() {
const formatFactory = jest.fn();
formatFactory.mockReturnValue({ convert: (v: unknown) => `Formatted_${v}` } as FieldFormat);
return {
csvSeparator: ',',
quoteValues: true,
formatFactory,
};
}

function getDataTable({ multipleColumns }: { multipleColumns?: boolean } = {}): Datatable {
const layer1: Datatable = {
type: 'datatable',
columns: [{ id: 'col1', name: 'columnOne', meta: { type: 'string' } }],
rows: [{ col1: 'value' }],
};
if (multipleColumns) {
layer1.columns.push({ id: 'col2', name: 'columnTwo', meta: { type: 'number' } });
layer1.rows[0].col2 = 5;
}
return layer1;
}

describe('CSV exporter', () => {
test('should not break with empty data', () => {
expect(
datatableToCSV({ type: 'datatable', columns: [], rows: [] }, getDefaultOptions())
).toMatch('');
});

test('should export formatted values by default', () => {
expect(datatableToCSV(getDataTable(), getDefaultOptions())).toMatch(
'columnOne\r\n"Formatted_value"\r\n'
);
});

test('should not quote values when requested', () => {
return expect(
datatableToCSV(getDataTable(), { ...getDefaultOptions(), quoteValues: false })
).toMatch('columnOne\r\nFormatted_value\r\n');
});

test('should use raw values when requested', () => {
expect(datatableToCSV(getDataTable(), { ...getDefaultOptions(), raw: true })).toMatch(
'columnOne\r\nvalue\r\n'
);
});

test('should use separator for multiple columns', () => {
expect(datatableToCSV(getDataTable({ multipleColumns: true }), getDefaultOptions())).toMatch(
'columnOne,columnTwo\r\n"Formatted_value","Formatted_5"\r\n'
);
});

test('should escape values', () => {
const datatable = getDataTable();
datatable.rows[0].col1 = '"value"';
expect(datatableToCSV(datatable, getDefaultOptions())).toMatch(
'columnOne\r\n"Formatted_""value"""\r\n'
);
});
});
82 changes: 82 additions & 0 deletions src/plugins/data/common/exports/export_csv.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

// Inspired by the inspector CSV exporter

import { FormatFactory } from 'src/plugins/data/common/field_formats/utils';
import { Datatable } from 'src/plugins/expressions';

const LINE_FEED_CHARACTER = '\r\n';
const nonAlphaNumRE = /[^a-zA-Z0-9]/;
const allDoubleQuoteRE = /"/g;
export const CSV_MIME_TYPE = 'text/plain;charset=utf-8';

// TODO: enhance this later on
function escape(val: object | string, quoteValues: boolean) {
if (val != null && typeof val === 'object') {
val = val.valueOf();
}

val = String(val);

if (quoteValues && nonAlphaNumRE.test(val)) {
val = `"${val.replace(allDoubleQuoteRE, '""')}"`;
}

return val;
}

interface CSVOptions {
csvSeparator: string;
quoteValues: boolean;
formatFactory: FormatFactory;
raw?: boolean;
}

export function datatableToCSV(
{ columns, rows }: Datatable,
{ csvSeparator, quoteValues, formatFactory, raw }: CSVOptions
) {
// Build the header row by its names
const header = columns.map((col) => escape(col.name, quoteValues));

const formatters = columns.reduce<Record<string, ReturnType<FormatFactory>>>(
(memo, { id, meta }) => {
memo[id] = formatFactory(meta?.params);
return memo;
},
{}
);

// Convert the array of row objects to an array of row arrays
const csvRows = rows.map((row) => {
return columns.map((column) =>
escape(raw ? row[column.id] : formatters[column.id].convert(row[column.id]), quoteValues)
);
});

if (header.length === 0) {
return '';
}

return (
[header, ...csvRows].map((row) => row.join(csvSeparator)).join(LINE_FEED_CHARACTER) +
LINE_FEED_CHARACTER
); // Add \r\n after last line
}
20 changes: 20 additions & 0 deletions src/plugins/data/common/exports/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { datatableToCSV, CSV_MIME_TYPE } from './export_csv';
1 change: 1 addition & 0 deletions src/plugins/data/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from './query';
export * from './search';
export * from './types';
export * from './utils';
export * from './exports';

/**
* Use data plugin interface instead
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ export {
FieldFormat,
} from '../common';

/**
* Exporters (CSV)
*/

import { datatableToCSV, CSV_MIME_TYPE } from '../common';
export const exporters = {
datatableToCSV,
CSV_MIME_TYPE,
};

/*
* Index patterns:
*/
Expand Down
55 changes: 33 additions & 22 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { CoreSetup } from 'src/core/public';
import { CoreSetup as CoreSetup_2 } from 'kibana/public';
import { CoreStart } from 'kibana/public';
import { CoreStart as CoreStart_2 } from 'src/core/public';
import { Datatable as Datatable_2 } from 'src/plugins/expressions/common';
import { Datatable as Datatable_2 } from 'src/plugins/expressions';
import { Datatable as Datatable_3 } from 'src/plugins/expressions/common';
import { DatatableColumn as DatatableColumn_2 } from 'src/plugins/expressions';
import { Ensure } from '@kbn/utility-types';
import { EnvironmentMode } from '@kbn/config';
Expand All @@ -35,6 +36,7 @@ import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import { ExpressionFunctionDefinition as ExpressionFunctionDefinition_2 } from 'src/plugins/expressions/public';
import { ExpressionsSetup } from 'src/plugins/expressions/public';
import { ExpressionValueBoxed } from 'src/plugins/expressions/common';
import { FormatFactory as FormatFactory_2 } from 'src/plugins/data/common/field_formats/utils';
import { History } from 'history';
import { Href } from 'history';
import { IconType } from '@elastic/eui';
Expand Down Expand Up @@ -672,6 +674,14 @@ export type ExistsFilter = Filter & {
exists?: FilterExistsProperty;
};

// Warning: (ae-missing-release-tag) "exporters" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export const exporters: {
datatableToCSV: typeof datatableToCSV;
CSV_MIME_TYPE: string;
};

// Warning: (ae-missing-release-tag) "ExpressionFunctionKibana" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
Expand Down Expand Up @@ -2392,27 +2402,28 @@ export const UI_SETTINGS: {
// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "UrlFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "StringFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:178:26 - (ae-forgotten-export) The symbol "TruncateFormat" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "validateIndexPattern" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "getFromSavedObject" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:236:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:393:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:393:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:393:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:393:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:395:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:396:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:407:1 - (ae-forgotten-export) The symbol "Ipv4Address" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:408:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:412:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:413:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:416:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:417:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:420:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:220:23 - (ae-forgotten-export) The symbol "datatableToCSV" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:246:27 - (ae-forgotten-export) The symbol "isFilterable" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:246:27 - (ae-forgotten-export) The symbol "isNestedField" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:246:27 - (ae-forgotten-export) The symbol "validateIndexPattern" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:246:27 - (ae-forgotten-export) The symbol "getFromSavedObject" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:246:27 - (ae-forgotten-export) The symbol "flattenHitWrapper" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:246:27 - (ae-forgotten-export) The symbol "formatHitProvider" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:403:20 - (ae-forgotten-export) The symbol "getRequestInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:403:20 - (ae-forgotten-export) The symbol "getResponseInspectorStats" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:403:20 - (ae-forgotten-export) The symbol "tabifyAggResponse" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:403:20 - (ae-forgotten-export) The symbol "tabifyGetColumns" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "CidrMask" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "dateHistogramInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:415:1 - (ae-forgotten-export) The symbol "InvalidEsCalendarIntervalError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:416:1 - (ae-forgotten-export) The symbol "InvalidEsIntervalFormatError" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:417:1 - (ae-forgotten-export) The symbol "Ipv4Address" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:418:1 - (ae-forgotten-export) The symbol "isDateHistogramBucketAggConfig" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:422:1 - (ae-forgotten-export) The symbol "isValidEsInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:423:1 - (ae-forgotten-export) The symbol "isValidInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:426:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:427:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:430:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:45:5 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ export const esFilters = {
isFilterDisabled,
};

/**
* Exporters (CSV)
*/

import { datatableToCSV, CSV_MIME_TYPE } from '../common';
export const exporters = {
datatableToCSV,
CSV_MIME_TYPE,
};

/*
* esQuery and esKuery:
*/
Expand Down
Loading