From 791dd3831689f16dbd95098c7df4374bba5c976b Mon Sep 17 00:00:00 2001 From: Chloe Date: Fri, 1 May 2020 21:30:42 -0700 Subject: [PATCH] Remove JSON formatter for csv and txt responses from the sql plugin; added asynchronous method for download functionality (#50) --- .../QueryResults/QueryResultsBody.tsx | 28 +-- .../__snapshots__/QueryResults.test.tsx.snap | 84 ++++++++- .../QueryResultsBody.test.tsx.snap | 161 +++++++++++++++++- server/services/QueryService.ts | 17 +- 4 files changed, 258 insertions(+), 32 deletions(-) diff --git a/public/components/QueryResults/QueryResultsBody.tsx b/public/components/QueryResults/QueryResultsBody.tsx index e3df8c0..46d39d7 100644 --- a/public/components/QueryResults/QueryResultsBody.tsx +++ b/public/components/QueryResults/QueryResultsBody.tsx @@ -175,34 +175,42 @@ class QueryResultsBody extends React.Component { + const jsonObject = JSON.parse(this.props.queryResultsJSON); + const data = JSON.stringify(jsonObject, undefined, 4); + onDownloadFile(data, "json", this.props.selectedTabName + ".json"); + }, 2000); }; onDownloadJDBC = (): void => { if (!this.props.queryResultsJDBC) { this.props.getJdbc(this.props.queries); } - const jsonObject = JSON.parse(this.props.queryResultsJDBC); - const data = JSON.stringify(jsonObject, undefined, 4); - onDownloadFile(data, "json", this.props.selectedTabName + ".json"); + setTimeout(() => { + const jsonObject = JSON.parse(this.props.queryResultsJDBC); + const data = JSON.stringify(jsonObject, undefined, 4); + onDownloadFile(data, "json", this.props.selectedTabName + ".json"); + }, 2000); }; onDownloadCSV = (): void => { if (!this.props.queryResultsCSV) { this.props.getCsv(this.props.queries); } - const data = this.props.queryResultsCSV; - onDownloadFile(data, "csv", this.props.selectedTabName + ".csv"); + setTimeout(() => { + const data = this.props.queryResultsCSV; + onDownloadFile(data, "csv", this.props.selectedTabName + ".csv"); + }, 2000); }; onDownloadText = (): void => { if (!this.props.queryResultsTEXT) { this.props.getText(this.props.queries); } - const data = this.props.queryResultsTEXT; - onDownloadFile(data, "plain", this.props.selectedTabName + ".txt"); + setTimeout(() => { + const data = this.props.queryResultsTEXT; + onDownloadFile(data, "plain", this.props.selectedTabName + ".txt"); + }, 2000); }; // Actions for Downloads Button diff --git a/public/components/QueryResults/__snapshots__/QueryResults.test.tsx.snap b/public/components/QueryResults/__snapshots__/QueryResults.test.tsx.snap index 56dcca8..4ec5bda 100644 --- a/public/components/QueryResults/__snapshots__/QueryResults.test.tsx.snap +++ b/public/components/QueryResults/__snapshots__/QueryResults.test.tsx.snap @@ -645,7 +645,7 @@ exports[` spec renders the component to test tabs down class="table-header" > - spec renders the component to test tabs down - + spec renders the component to test tabs down > id @@ -720,6 +721,7 @@ exports[` spec renders the component to test tabs down > category @@ -749,6 +751,7 @@ exports[` spec renders the component to test tabs down > currency @@ -778,6 +781,7 @@ exports[` spec renders the component to test tabs down > customer_first_name @@ -807,6 +811,7 @@ exports[` spec renders the component to test tabs down > customer_full_name @@ -836,6 +841,7 @@ exports[` spec renders the component to test tabs down > customer_gender @@ -865,6 +871,7 @@ exports[` spec renders the component to test tabs down > customer_id @@ -894,6 +901,7 @@ exports[` spec renders the component to test tabs down > customer_last_name @@ -923,6 +931,7 @@ exports[` spec renders the component to test tabs down > customer_phone @@ -952,6 +961,7 @@ exports[` spec renders the component to test tabs down > day_of_week @@ -981,6 +991,7 @@ exports[` spec renders the component to test tabs down > day_of_week_i @@ -1010,6 +1021,7 @@ exports[` spec renders the component to test tabs down > email @@ -1039,6 +1051,7 @@ exports[` spec renders the component to test tabs down > manufacturer @@ -1068,6 +1081,7 @@ exports[` spec renders the component to test tabs down > order_date @@ -1097,6 +1111,7 @@ exports[` spec renders the component to test tabs down > order_id @@ -1126,6 +1141,7 @@ exports[` spec renders the component to test tabs down > products @@ -1155,6 +1171,7 @@ exports[` spec renders the component to test tabs down > sku @@ -1184,6 +1201,7 @@ exports[` spec renders the component to test tabs down > taxful_total_price @@ -1213,6 +1231,7 @@ exports[` spec renders the component to test tabs down > taxless_total_price @@ -1242,6 +1261,7 @@ exports[` spec renders the component to test tabs down > total_quantity @@ -1271,6 +1291,7 @@ exports[` spec renders the component to test tabs down > total_unique_products @@ -1300,6 +1321,7 @@ exports[` spec renders the component to test tabs down > type @@ -1329,6 +1351,7 @@ exports[` spec renders the component to test tabs down > user @@ -1358,6 +1381,7 @@ exports[` spec renders the component to test tabs down > geoip @@ -5721,7 +5745,7 @@ exports[` spec renders the component to test tabs right class="table-header" > - spec renders the component to test tabs right - + spec renders the component to test tabs right > id @@ -5796,6 +5821,7 @@ exports[` spec renders the component to test tabs right > category @@ -5825,6 +5851,7 @@ exports[` spec renders the component to test tabs right > currency @@ -5854,6 +5881,7 @@ exports[` spec renders the component to test tabs right > customer_first_name @@ -5883,6 +5911,7 @@ exports[` spec renders the component to test tabs right > customer_full_name @@ -5912,6 +5941,7 @@ exports[` spec renders the component to test tabs right > customer_gender @@ -5941,6 +5971,7 @@ exports[` spec renders the component to test tabs right > customer_id @@ -5970,6 +6001,7 @@ exports[` spec renders the component to test tabs right > customer_last_name @@ -5999,6 +6031,7 @@ exports[` spec renders the component to test tabs right > customer_phone @@ -6028,6 +6061,7 @@ exports[` spec renders the component to test tabs right > day_of_week @@ -6057,6 +6091,7 @@ exports[` spec renders the component to test tabs right > day_of_week_i @@ -6086,6 +6121,7 @@ exports[` spec renders the component to test tabs right > email @@ -6115,6 +6151,7 @@ exports[` spec renders the component to test tabs right > manufacturer @@ -6144,6 +6181,7 @@ exports[` spec renders the component to test tabs right > order_date @@ -6173,6 +6211,7 @@ exports[` spec renders the component to test tabs right > order_id @@ -6202,6 +6241,7 @@ exports[` spec renders the component to test tabs right > products @@ -6231,6 +6271,7 @@ exports[` spec renders the component to test tabs right > sku @@ -6260,6 +6301,7 @@ exports[` spec renders the component to test tabs right > taxful_total_price @@ -6289,6 +6331,7 @@ exports[` spec renders the component to test tabs right > taxless_total_price @@ -6318,6 +6361,7 @@ exports[` spec renders the component to test tabs right > total_quantity @@ -6347,6 +6391,7 @@ exports[` spec renders the component to test tabs right > total_unique_products @@ -6376,6 +6421,7 @@ exports[` spec renders the component to test tabs right > type @@ -6405,6 +6451,7 @@ exports[` spec renders the component to test tabs right > user @@ -6434,6 +6481,7 @@ exports[` spec renders the component to test tabs right > geoip @@ -10797,7 +10845,7 @@ exports[` spec renders the component with mock query re class="table-header" > - spec renders the component with mock query re - + spec renders the component with mock query re > id @@ -10872,6 +10921,7 @@ exports[` spec renders the component with mock query re > category @@ -10901,6 +10951,7 @@ exports[` spec renders the component with mock query re > currency @@ -10930,6 +10981,7 @@ exports[` spec renders the component with mock query re > customer_first_name @@ -10959,6 +11011,7 @@ exports[` spec renders the component with mock query re > customer_full_name @@ -10988,6 +11041,7 @@ exports[` spec renders the component with mock query re > customer_gender @@ -11017,6 +11071,7 @@ exports[` spec renders the component with mock query re > customer_id @@ -11046,6 +11101,7 @@ exports[` spec renders the component with mock query re > customer_last_name @@ -11075,6 +11131,7 @@ exports[` spec renders the component with mock query re > customer_phone @@ -11104,6 +11161,7 @@ exports[` spec renders the component with mock query re > day_of_week @@ -11133,6 +11191,7 @@ exports[` spec renders the component with mock query re > day_of_week_i @@ -11162,6 +11221,7 @@ exports[` spec renders the component with mock query re > email @@ -11191,6 +11251,7 @@ exports[` spec renders the component with mock query re > manufacturer @@ -11220,6 +11281,7 @@ exports[` spec renders the component with mock query re > order_date @@ -11249,6 +11311,7 @@ exports[` spec renders the component with mock query re > order_id @@ -11278,6 +11341,7 @@ exports[` spec renders the component with mock query re > products @@ -11307,6 +11371,7 @@ exports[` spec renders the component with mock query re > sku @@ -11336,6 +11401,7 @@ exports[` spec renders the component with mock query re > taxful_total_price @@ -11365,6 +11431,7 @@ exports[` spec renders the component with mock query re > taxless_total_price @@ -11394,6 +11461,7 @@ exports[` spec renders the component with mock query re > total_quantity @@ -11423,6 +11491,7 @@ exports[` spec renders the component with mock query re > total_unique_products @@ -11452,6 +11521,7 @@ exports[` spec renders the component with mock query re > type @@ -11481,6 +11551,7 @@ exports[` spec renders the component with mock query re > user @@ -11510,6 +11581,7 @@ exports[` spec renders the component with mock query re > geoip diff --git a/public/components/QueryResults/__snapshots__/QueryResultsBody.test.tsx.snap b/public/components/QueryResults/__snapshots__/QueryResultsBody.test.tsx.snap index 3bec6fb..315ecdc 100644 --- a/public/components/QueryResults/__snapshots__/QueryResultsBody.test.tsx.snap +++ b/public/components/QueryResults/__snapshots__/QueryResultsBody.test.tsx.snap @@ -129,7 +129,28 @@ exports[` spec renders component with mock QueryResults data
- +
+ + +
@@ -165,7 +186,7 @@ exports[` spec renders component with mock QueryResults data class="table-header" > - spec renders component with mock QueryResults data - + spec renders component with mock QueryResults data > id @@ -239,6 +261,7 @@ exports[` spec renders component with mock QueryResults data > category @@ -268,6 +291,7 @@ exports[` spec renders component with mock QueryResults data > currency @@ -297,6 +321,7 @@ exports[` spec renders component with mock QueryResults data > customer_first_name @@ -326,6 +351,7 @@ exports[` spec renders component with mock QueryResults data > customer_full_name @@ -355,6 +381,7 @@ exports[` spec renders component with mock QueryResults data > customer_gender @@ -384,6 +411,7 @@ exports[` spec renders component with mock QueryResults data > customer_id @@ -413,6 +441,7 @@ exports[` spec renders component with mock QueryResults data > customer_last_name @@ -442,6 +471,7 @@ exports[` spec renders component with mock QueryResults data > customer_phone @@ -471,6 +501,7 @@ exports[` spec renders component with mock QueryResults data > day_of_week @@ -500,6 +531,7 @@ exports[` spec renders component with mock QueryResults data > day_of_week_i @@ -529,6 +561,7 @@ exports[` spec renders component with mock QueryResults data > email @@ -558,6 +591,7 @@ exports[` spec renders component with mock QueryResults data > manufacturer @@ -587,6 +621,7 @@ exports[` spec renders component with mock QueryResults data > order_date @@ -616,6 +651,7 @@ exports[` spec renders component with mock QueryResults data > order_id @@ -645,6 +681,7 @@ exports[` spec renders component with mock QueryResults data > products @@ -674,6 +711,7 @@ exports[` spec renders component with mock QueryResults data > sku @@ -703,6 +741,7 @@ exports[` spec renders component with mock QueryResults data > taxful_total_price @@ -732,6 +771,7 @@ exports[` spec renders component with mock QueryResults data > taxless_total_price @@ -761,6 +801,7 @@ exports[` spec renders component with mock QueryResults data > total_quantity @@ -790,6 +831,7 @@ exports[` spec renders component with mock QueryResults data > total_unique_products @@ -819,6 +861,7 @@ exports[` spec renders component with mock QueryResults data > type @@ -848,6 +891,7 @@ exports[` spec renders component with mock QueryResults data > user @@ -877,6 +921,7 @@ exports[` spec renders component with mock QueryResults data > geoip @@ -5128,7 +5173,28 @@ exports[` spec renders component with mock QueryResults data
- +
+ + +
@@ -5213,7 +5279,7 @@ exports[` spec renders component with mock QueryResults data class="euiFieldSearch euiFieldSearch--fullWidth" placeholder="Search" type="search" - value="" + value="Test" />
spec renders component with mock QueryResults data EuiIconMock
+
+ +
@@ -5265,7 +5342,28 @@ exports[` spec renders component with mock QueryResults data
- +
+ + +
@@ -5301,7 +5399,7 @@ exports[` spec renders component with mock QueryResults data class="table-header" > - spec renders component with mock QueryResults data - + spec renders component with mock QueryResults data > id @@ -5375,6 +5474,7 @@ exports[` spec renders component with mock QueryResults data > category @@ -5404,6 +5504,7 @@ exports[` spec renders component with mock QueryResults data > currency @@ -5433,6 +5534,7 @@ exports[` spec renders component with mock QueryResults data > customer_first_name @@ -5462,6 +5564,7 @@ exports[` spec renders component with mock QueryResults data > customer_full_name @@ -5491,6 +5594,7 @@ exports[` spec renders component with mock QueryResults data > customer_gender @@ -5520,6 +5624,7 @@ exports[` spec renders component with mock QueryResults data > customer_id @@ -5549,6 +5654,7 @@ exports[` spec renders component with mock QueryResults data > customer_last_name @@ -5578,6 +5684,7 @@ exports[` spec renders component with mock QueryResults data > customer_phone @@ -5607,6 +5714,7 @@ exports[` spec renders component with mock QueryResults data > day_of_week @@ -5636,6 +5744,7 @@ exports[` spec renders component with mock QueryResults data > day_of_week_i @@ -5665,6 +5774,7 @@ exports[` spec renders component with mock QueryResults data > email @@ -5694,6 +5804,7 @@ exports[` spec renders component with mock QueryResults data > manufacturer @@ -5723,6 +5834,7 @@ exports[` spec renders component with mock QueryResults data > order_date @@ -5752,6 +5864,7 @@ exports[` spec renders component with mock QueryResults data > order_id @@ -5781,6 +5894,7 @@ exports[` spec renders component with mock QueryResults data > products @@ -5810,6 +5924,7 @@ exports[` spec renders component with mock QueryResults data > sku @@ -5839,6 +5954,7 @@ exports[` spec renders component with mock QueryResults data > taxful_total_price @@ -5868,6 +5984,7 @@ exports[` spec renders component with mock QueryResults data > taxless_total_price @@ -5897,6 +6014,7 @@ exports[` spec renders component with mock QueryResults data > total_quantity @@ -5926,6 +6044,7 @@ exports[` spec renders component with mock QueryResults data > total_unique_products @@ -5955,6 +6074,7 @@ exports[` spec renders component with mock QueryResults data > type @@ -5984,6 +6104,7 @@ exports[` spec renders component with mock QueryResults data > user @@ -6013,6 +6134,7 @@ exports[` spec renders component with mock QueryResults data > geoip @@ -10520,7 +10642,28 @@ exports[` spec renders component with mock QueryResults data
- +
+ + +
diff --git a/server/services/QueryService.ts b/server/services/QueryService.ts index 8be4a41..e94468e 100644 --- a/server/services/QueryService.ts +++ b/server/services/QueryService.ts @@ -17,6 +17,7 @@ import "core-js/stable"; import "regenerator-runtime/runtime"; import { Request, ResponseToolkit } from 'hapi-latest'; import { CLUSTER } from './utils/constants'; +import _ from "lodash"; export default class QueryService { private client: any; @@ -24,14 +25,16 @@ export default class QueryService { this.client = client; } - describeQueryInternal = async (request: Request, h: ResponseToolkit, format: string, err?: Error) => { + describeQueryInternal = async (request: Request, h: ResponseToolkit, format: string, responseFormat: string, err?: Error) => { try { const params = { body: JSON.stringify(request.payload), }; const { callWithRequest } = await this.client.getCluster(CLUSTER.SQL); const createResponse = await callWithRequest(request, format, params); - return h.response({ ok: true, resp: JSON.stringify(createResponse) }); + return h.response({ ok: true, resp: + _.isEqual(responseFormat, "json") ? JSON.stringify(createResponse) : createResponse + }); } catch (err) { console.log(err); } @@ -39,22 +42,22 @@ export default class QueryService { }; describeQuery = async (request: Request, h: ResponseToolkit, err?: Error) => { - return this.describeQueryInternal(request, h, "sql.query", err) + return this.describeQueryInternal(request, h, "sql.query", "json", err) }; describeQueryCsv = async (request: Request, h: ResponseToolkit, err?: Error) => { - return this.describeQueryInternal(request, h, "sql.getCsv", err) + return this.describeQueryInternal(request, h, "sql.getCsv", null, err) }; describeQueryJson = async (request: Request, h: ResponseToolkit, err?: Error) => { - return this.describeQueryInternal(request, h, "sql.getJson", err) + return this.describeQueryInternal(request, h, "sql.getJson", "json", err) }; describeQueryJdbc = async (request: Request, h: ResponseToolkit, err?: Error) => { - return this.describeQueryInternal(request, h, "sql.getJdbc", err) + return this.describeQueryInternal(request, h, "sql.getJdbc", "json", err) }; describeQueryText = async (request: Request, h: ResponseToolkit, err?: Error) => { - return this.describeQueryInternal(request, h, "sql.getText", err) + return this.describeQueryInternal(request, h, "sql.getText", null, err) }; }