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

Add option to copy query results directly to clipboard #14889

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 16 additions & 8 deletions web-console/src/utils/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { QueryResult } from '@druid-toolkit/query';
import FileSaver from 'file-saver';
import * as JSONBig from 'json-bigint-native';

import { stringifyValue } from './general';
import { copyAndAlert, stringifyValue } from './general';

export function downloadUrl(url: string, filename: string) {
// Create a link and set the URL using `createObjectURL`
Expand Down Expand Up @@ -74,11 +74,7 @@ export function downloadFile(text: string, type: string, filename: string): void
FileSaver.saveAs(blob, filename);
}

export function downloadQueryResults(
queryResult: QueryResult,
filename: string,
format: string,
): void {
function queryResultsToString(queryResult: QueryResult, format: string): string {
let lines: string[] = [];
let separator = '';

Expand All @@ -103,7 +99,19 @@ export function downloadQueryResults(
return JSONBig.stringify(outputObject);
});
}
return lines.join('\n');
}

export function downloadQueryResults(
queryResult: QueryResult,
filename: string,
format: string,
): void {
const resultString: string = queryResultsToString(queryResult, format);
downloadFile(resultString, format, filename);
}

const lineBreak = '\n';
downloadFile(lines.join(lineBreak), format, filename);
export function copyQueryResultsToClipboard(queryResult: QueryResult, format: string): void {
const resultString: string = queryResultsToString(queryResult, format);
copyAndAlert(resultString, 'Query results copied to clipboard');
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import React, { useState } from 'react';

import type { Execution } from '../../../druid-models';
import {
copyQueryResultsToClipboard,
downloadQueryResults,
formatDurationHybrid,
formatInteger,
Expand Down Expand Up @@ -68,6 +69,10 @@ export const ExecutionSummaryPanel = React.memo(function ExecutionSummaryPanel(
downloadQueryResults(queryResult, `results-${execution.id}.${format}`, format);
};

const handleCopy = (format: string) => {
copyQueryResultsToClipboard(queryResult, format);
};

buttons.push(
<Button
key="results"
Expand Down Expand Up @@ -101,6 +106,10 @@ export const ExecutionSummaryPanel = React.memo(function ExecutionSummaryPanel(
<MenuItem text="CSV" onClick={() => handleDownload('csv')} />
<MenuItem text="TSV" onClick={() => handleDownload('tsv')} />
<MenuItem text="JSON (new line delimited)" onClick={() => handleDownload('json')} />
<MenuDivider title="Copy to clipboard as..." />
<MenuItem text="CSV" onClick={() => handleCopy('csv')} />
<MenuItem text="TSV" onClick={() => handleCopy('tsv')} />
<MenuItem text="JSON (new line delimited)" onClick={() => handleCopy('json')} />
</Menu>
}
position={Position.BOTTOM_RIGHT}
Expand Down