Skip to content

Commit

Permalink
Add option to copy query results to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWheating authored and Sam Wheating committed Sep 5, 2023
1 parent 8088a76 commit b5e4678
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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 {
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:" />
<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

0 comments on commit b5e4678

Please sign in to comment.