-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
5c7a300
commit a5974ac
Showing
24 changed files
with
536 additions
and
60 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
.../development/plugins/data/public/kibana-plugin-plugins-data-public.exporters.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [exporters](./kibana-plugin-plugins-data-public.exporters.md) | ||
|
||
## exporters variable | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
exporters: { | ||
datatableToCSV: typeof datatableToCSV; | ||
CSV_MIME_TYPE: string; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
.../development/plugins/data/server/kibana-plugin-plugins-data-server.exporters.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [exporters](./kibana-plugin-plugins-data-server.exporters.md) | ||
|
||
## exporters variable | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
exporters: { | ||
datatableToCSV: typeof datatableToCSV; | ||
CSV_MIME_TYPE: string; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.