Skip to content

feat: Add download csv functionality to search tables #939

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
3 changes: 1 addition & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"react-bootstrap": "^2.4.0",
"react-bootstrap-range-slider": "^3.0.8",
"react-copy-to-clipboard": "^5.1.0",
"react-csv": "^2.2.2",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hasn't been updated since 2020 and has several open issues with comma delimiters

"react-dom": "18.3.1",
"react-error-boundary": "^3.1.4",
"react-grid-layout": "^1.3.4",
Expand All @@ -83,6 +82,7 @@
"react-json-tree": "^0.17.0",
"react-markdown": "^8.0.4",
"react-modern-drawer": "^1.2.0",
"react-papaparse": "^4.4.0",
"react-query": "^3.39.3",
"react-select": "^5.7.0",
"react-sortable-hoc": "^2.0.0",
Expand Down Expand Up @@ -125,7 +125,6 @@
"@types/pluralize": "^0.0.29",
"@types/react": "18.3.1",
"@types/react-copy-to-clipboard": "^5.0.2",
"@types/react-csv": "^1.1.3",
"@types/react-dom": "18.3.1",
"@types/react-grid-layout": "^1.3.2",
"@types/react-syntax-highlighter": "^13.5.2",
Expand Down
49 changes: 16 additions & 33 deletions packages/app/src/HDXMultiSeriesTableChart.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { memo, useCallback, useMemo, useRef } from 'react';
import Link from 'next/link';
import { CSVLink } from 'react-csv';
import { Flex, Text } from '@mantine/core';
import {
flexRender,
Expand All @@ -13,29 +12,12 @@ import {
import { ColumnDef } from '@tanstack/react-table';
import { useVirtualizer } from '@tanstack/react-virtual';

import { CsvExportButton } from './components/CsvExportButton';
import { useCsvExport } from './hooks/useCsvExport';
import { UNDEFINED_WIDTH } from './tableUtils';
import type { NumberFormat } from './types';
import { formatNumber } from './utils';

export const generateCsvData = (
data: any[],
columns: {
dataKey: string;
displayName: string;
sortOrder?: 'asc' | 'desc';
numberFormat?: NumberFormat;
columnWidthPercent?: number;
}[],
groupColumnName?: string,
) => {
return data.map(row => ({
...(groupColumnName != null ? { [groupColumnName]: row.group } : {}),
...Object.fromEntries(
columns.map(({ displayName, dataKey }) => [displayName, row[dataKey]]),
),
}));
};

export const Table = ({
data,
groupColumnName,
Expand Down Expand Up @@ -177,9 +159,14 @@ export const Table = ({
[items, rowVirtualizer.options.scrollMargin, totalSize],
);

const csvData = useMemo(() => {
return generateCsvData(data, columns, groupColumnName);
}, [data, columns, groupColumnName]);
const { csvData } = useCsvExport(
data,
columns.map(col => ({
dataKey: col.dataKey,
displayName: col.displayName,
})),
{ groupColumnName },
);

return (
<div
Expand Down Expand Up @@ -257,18 +244,14 @@ export const Table = ({
)}
{headerIndex === headerGroup.headers.length - 1 && (
<div className="d-flex align-items-center">
<CSVLink
<CsvExportButton
data={csvData}
filename={`HyperDX_table_results`}
filename="HyperDX_table_results"
className="fs-8 text-muted-hover ms-2"
title="Download table as CSV"
>
<div
className="fs-8 text-muted-hover ms-2"
role="button"
title="Download table as CSV"
>
<i className="bi bi-download" />
</div>
</CSVLink>
<i className="bi bi-download" />
</CsvExportButton>
</div>
)}
</Flex>
Expand Down
93 changes: 93 additions & 0 deletions packages/app/src/components/CsvExportButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React from 'react';
import { useCSVDownloader } from 'react-papaparse';

interface CsvExportButtonProps {
data: Record<string, any>[];
filename: string;
children: React.ReactNode;
className?: string;
title?: string;
disabled?: boolean;
onExportStart?: () => void;
onExportComplete?: () => void;
onExportError?: (error: Error) => void;
}

export const CsvExportButton: React.FC<CsvExportButtonProps> = ({
data,
filename,
children,
className,
title,
disabled = false,
onExportStart,
onExportComplete,
onExportError,
...props
}) => {
const { CSVDownloader } = useCSVDownloader();

const handleClick = () => {
try {
if (data.length === 0) {
onExportError?.(new Error('No data to export'));
return;
}

onExportStart?.();
onExportComplete?.();
} catch (error) {
onExportError?.(
error instanceof Error ? error : new Error('Export failed'),
);
}
};

if (disabled || data.length === 0) {
return (
<div
className={className}
title={disabled ? 'Export disabled' : 'No data to export'}
style={{ opacity: 0.5, cursor: 'not-allowed' }}
{...props}
>
{children}
</div>
);
}

return (
<div
className={className}
role="button"
title={title}
onClick={handleClick}
{...props}
>
<CSVDownloader
data={data}
filename={filename}
config={{
quotes: true,
quoteChar: '"',
escapeChar: '"',
delimiter: ',',
header: true,
}}
style={{
color: 'inherit',
textDecoration: 'none',
background: 'none',
border: 'none',
padding: 0,
cursor: 'pointer',
display: 'block',
width: '100%',
height: '100%',
}}
>
{children}
</CSVDownloader>
</div>
);
};
20 changes: 19 additions & 1 deletion packages/app/src/components/DBRowTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import cx from 'classnames';
import { isString } from 'lodash';
import curry from 'lodash/curry';
import { Button, Modal } from 'react-bootstrap';
import { CSVLink } from 'react-csv';
import { useHotkeys } from 'react-hotkeys-hook';
import {
Bar,
Expand Down Expand Up @@ -43,6 +42,7 @@ import {
} from '@tanstack/react-table';
import { useVirtualizer } from '@tanstack/react-virtual';

import { useCsvExport } from '@/hooks/useCsvExport';
import { useTableMetadata } from '@/hooks/useMetadata';
import useOffsetPaginatedQuery from '@/hooks/useOffsetPaginatedQuery';
import { useGroupedPatterns } from '@/hooks/usePatterns';
Expand All @@ -60,9 +60,11 @@ import {
} from '@/utils';

import { SQLPreview } from './ChartSQLPreview';
import { CsvExportButton } from './CsvExportButton';
import LogLevel from './LogLevel';

import styles from '../../styles/LogTable.module.scss';

type Row = Record<string, any> & { duration: number };
type AccessorFn = (row: Row, column: string) => any;

Expand Down Expand Up @@ -315,6 +317,14 @@ export const RawLogTable = memo(
return inferLogLevelColumn(dedupedRows);
}, [dedupedRows]);

const { csvData, maxRows, isLimited } = useCsvExport(
dedupedRows,
displayedColumns.map(col => ({
dataKey: col,
displayName: columnNameMap?.[col] ?? col,
})),
);

const columns = useMemo<ColumnDef<any>[]>(
() => [
{
Expand Down Expand Up @@ -671,6 +681,14 @@ export const RawLogTable = memo(
<i className="bi bi-arrow-clockwise" />
</div>
)}
<CsvExportButton
data={csvData}
filename={`hyperdx_search_results_${new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19)}`}
className="fs-6 text-muted-hover ms-2"
title={`Download table as CSV (max ${maxRows.toLocaleString()} rows)${isLimited ? ' - data truncated' : ''}`}
>
<i className="bi bi-download" />
</CsvExportButton>
{onSettingsClick != null && (
<div
className="fs-8 text-muted-hover ms-2"
Expand Down
Loading