forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Table Visualization]restore export csv feature to table vis
* add addtional action in toolbar to allow export data to csv. there are two types of csv, raw and formatted. raw is the original data and formatted is to show formatted Date and percentage data when needed. * when table is not saved, export csv file will be named as unsaved-raw.csv if choose raw. when table is saved with a filename, it will be saved as [filename]-[raw/formatted].csv Partically resolved: opensearch-project#2379 Signed-off-by: Anan Zhuang <ananzh@amazon.com>
- Loading branch information
Showing
6 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"opensearchDashboardsUtils", | ||
"opensearchDashboardsReact", | ||
"charts", | ||
"share", | ||
"visDefaultEditor" | ||
] | ||
} |
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
57 changes: 57 additions & 0 deletions
57
src/plugins/vis_type_table/public/components/table_vis_control.tsx
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,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import { EuiPopover, EuiButtonEmpty, EuiContextMenuPanel, EuiContextMenuItem } from '@elastic/eui'; | ||
import { OpenSearchDashboardsDatatableRow } from 'src/plugins/expressions'; | ||
import { CoreStart } from 'opensearch-dashboards/public'; | ||
import { exportAsCsv } from '../utils/convert_to_csv_data'; | ||
import { FormattedColumn } from '../types'; | ||
import { useOpenSearchDashboards } from '../../../../../src/plugins/opensearch_dashboards_react/public'; | ||
|
||
interface TableVisControlProps { | ||
filename?: string; | ||
rows: OpenSearchDashboardsDatatableRow[]; | ||
columns: FormattedColumn[]; | ||
} | ||
|
||
export const TableVisControl = (props: TableVisControlProps) => { | ||
const { | ||
services: { uiSettings }, | ||
} = useOpenSearchDashboards<CoreStart>(); | ||
const [isPopoverOpen, setPopover] = useState(false); | ||
|
||
return ( | ||
<EuiPopover | ||
id="dataTableExportData" | ||
button={ | ||
<EuiButtonEmpty size="xs" iconType="download" onClick={() => setPopover((open) => !open)}> | ||
Export | ||
</EuiButtonEmpty> | ||
} | ||
isOpen={isPopoverOpen} | ||
closePopover={() => setPopover(false)} | ||
panelPaddingSize="none" | ||
> | ||
<EuiContextMenuPanel | ||
size="s" | ||
items={[ | ||
<EuiContextMenuItem | ||
key="rawCsv" | ||
onClick={() => exportAsCsv(false, { ...props, uiSettings })} | ||
> | ||
Raw | ||
</EuiContextMenuItem>, | ||
<EuiContextMenuItem | ||
key="formattedCsv" | ||
onClick={() => exportAsCsv(true, { ...props, uiSettings })} | ||
> | ||
Formatted | ||
</EuiContextMenuItem>, | ||
]} | ||
/> | ||
</EuiPopover> | ||
); | ||
}; |
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
85 changes: 85 additions & 0 deletions
85
src/plugins/vis_type_table/public/utils/convert_to_csv_data.ts
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 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Any modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
/* | ||
* 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 { isObject } from 'lodash'; | ||
// @ts-ignore | ||
import { saveAs } from '@elastic/filesaver'; | ||
import { CoreStart } from 'opensearch-dashboards/public'; | ||
import { CSV_SEPARATOR_SETTING, CSV_QUOTE_VALUES_SETTING } from '../../../share/public'; | ||
import { OpenSearchDashboardsDatatable } from '../../../expressions/public'; | ||
import { FormattedColumn } from '../types'; | ||
|
||
const nonAlphaNumRE = /[^a-zA-Z0-9]/; | ||
const allDoubleQuoteRE = /"/g; | ||
|
||
interface CSVDataProps { | ||
filename?: string; | ||
rows: OpenSearchDashboardsDatatable['rows']; | ||
columns: FormattedColumn[]; | ||
uiSettings: CoreStart['uiSettings']; | ||
} | ||
|
||
const toCsv = function (formatted: boolean, { rows, columns, uiSettings }: CSVDataProps) { | ||
const separator = uiSettings.get(CSV_SEPARATOR_SETTING); | ||
const quoteValues = uiSettings.get(CSV_QUOTE_VALUES_SETTING); | ||
|
||
function escape(val: any) { | ||
if (!formatted && isObject(val)) val = val.valueOf(); | ||
val = String(val); | ||
if (quoteValues && nonAlphaNumRE.test(val)) { | ||
val = '"' + val.replace(allDoubleQuoteRE, '""') + '"'; | ||
} | ||
return val; | ||
} | ||
|
||
let csvRows: string[][] = []; | ||
for (const row of rows) { | ||
const rowArray = []; | ||
for (const col of columns) { | ||
const value = row[col.id]; | ||
const formattedValue = | ||
formatted && col.formatter ? escape(col.formatter.convert(value)) : escape(value); | ||
rowArray.push(formattedValue); | ||
} | ||
csvRows = [...csvRows, rowArray]; | ||
} | ||
|
||
// add the columns to the rows | ||
csvRows.unshift(columns.map((col) => escape(col.title))); | ||
|
||
return csvRows.map((row) => row.join(separator) + '\r\n').join(''); | ||
}; | ||
|
||
export const exportAsCsv = function (formatted: boolean, csvData: CSVDataProps) { | ||
const csv = new Blob([toCsv(formatted, csvData)], { type: 'text/csv;charset=utf-8' }); | ||
const type = formatted ? 'formatted' : 'raw'; | ||
if (csvData.filename) saveAs(csv, `${csvData.filename}-${type}.csv`); | ||
else saveAs(csv, `unsaved-${type}.csv`); | ||
}; |