Skip to content

Commit

Permalink
Refactor, allow copying in all formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Wheating committed Sep 12, 2023
1 parent b5e4678 commit 8015b5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
35 changes: 13 additions & 22 deletions web-console/src/utils/download.ts
Original file line number Diff line number Diff line change
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,24 +99,19 @@ export function downloadQueryResults(
return JSONBig.stringify(outputObject);
});
}

const lineBreak = '\n';
downloadFile(lines.join(lineBreak), format, filename);
return lines.join('\n');
}

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);
});
export function downloadQueryResults(
queryResult: QueryResult,
filename: string,
format: string,
): void {
const resultString: string = queryResultsToString(queryResult, format);
downloadFile(resultString, format, filename);
}

const lineBreak = '\n';
copyAndAlert(lines.join(lineBreak), 'Query results copied to clipboard');
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,7 +24,7 @@ import React, { useState } from 'react';

import type { Execution } from '../../../druid-models';
import {
copyJSONResultsToClipboard,
copyQueryResultsToClipboard,
downloadQueryResults,
formatDurationHybrid,
formatInteger,
Expand Down Expand Up @@ -69,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 @@ -98,14 +102,14 @@ export const ExecutionSummaryPanel = React.memo(function ExecutionSummaryPanel(
className="download-button"
content={
<Menu>
<MenuDivider title="Save Query Results:" />
<MenuDivider title="Download results as..." />
<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)}
/>
<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

0 comments on commit 8015b5e

Please sign in to comment.