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 1 commit
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
19 changes: 18 additions & 1 deletion 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 @@ -107,3 +107,20 @@ export function downloadQueryResults(
const lineBreak = '\n';
downloadFile(lines.join(lineBreak), format, filename);
}

export function copyJSONResultsToClipboard(queryResult: QueryResult): void {
SamWheating marked this conversation as resolved.
Show resolved Hide resolved
let lines: string[] = [];
lines = queryResult.rows.map(r => {
const outputObject: Record<string, any> = {};
for (let k = 0; k < r.length; k++) {
const newName = queryResult.header[k];
if (newName) {
outputObject[newName.name] = r[k];
}
}
return JSONBig.stringify(outputObject);
});

const lineBreak = '\n';
copyAndAlert(lines.join(lineBreak), '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 {
copyJSONResultsToClipboard,
downloadQueryResults,
formatDurationHybrid,
formatInteger,
Expand Down Expand Up @@ -97,10 +98,14 @@ export const ExecutionSummaryPanel = React.memo(function ExecutionSummaryPanel(
className="download-button"
content={
<Menu>
<MenuDivider title="Download results as..." />
<MenuDivider title="Save Query Results:" />
SamWheating marked this conversation as resolved.
Show resolved Hide resolved
<MenuItem text="CSV" onClick={() => handleDownload('csv')} />
<MenuItem text="TSV" onClick={() => handleDownload('tsv')} />
<MenuItem text="JSON (new line delimited)" onClick={() => handleDownload('json')} />
<MenuItem
text="JSON (copy to clipboard)"
onClick={() => copyJSONResultsToClipboard(queryResult)}
/>
</Menu>
}
position={Position.BOTTOM_RIGHT}
Expand Down