From cd2cee77385ecf79efd2f1aa21fab0dd43943264 Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Wed, 30 Jan 2019 15:19:11 +0200 Subject: [PATCH] use the textless endpoint (/api/queries/:id/results) for pristine queriest --- client/app/pages/queries/view.js | 6 ++--- client/app/services/query-result.js | 24 +++++++++++++++++ client/app/services/query.js | 40 ++++++++++++++++------------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/client/app/pages/queries/view.js b/client/app/pages/queries/view.js index 61ed3bd234..78fcd91cc3 100644 --- a/client/app/pages/queries/view.js +++ b/client/app/pages/queries/view.js @@ -26,7 +26,7 @@ function QueryViewCtrl( DataSource, Visualization, ) { - function getQueryResult(maxAge, selectedQueryText) { + function getQueryResult(maxAge, selectedQueryText, isDirty) { if (maxAge === undefined) { maxAge = $location.search().maxAge; } @@ -36,7 +36,7 @@ function QueryViewCtrl( } $scope.showLog = false; - $scope.queryResult = $scope.query.getQueryResult(maxAge, selectedQueryText); + $scope.queryResult = $scope.query.getQueryResult(maxAge, selectedQueryText, isDirty); } function getDataSourceId() { @@ -118,7 +118,7 @@ function QueryViewCtrl( return; } - getQueryResult(0, $scope.selectedQueryText); + getQueryResult(0, $scope.selectedQueryText, $scope.isDirty); $scope.lockButton(true); $scope.cancelling = false; Events.record('execute', 'query', $scope.query.id); diff --git a/client/app/services/query-result.js b/client/app/services/query-result.js index 1d096d5538..06fa301c9b 100644 --- a/client/app/services/query-result.js +++ b/client/app/services/query-result.js @@ -531,6 +531,30 @@ function QueryResultService($resource, $timeout, $q, QueryResultError) { return `${queryName.replace(/ /g, '_') + moment(this.getUpdatedAt()).format('_YYYY_MM_DD')}.${fileType}`; } + static getByQueryId(id, parameters, maxAge) { + const queryResult = new QueryResult(); + + $resource('api/queries/:id/results', { id: '@id' }, { post: { method: 'POST' } }).post( + { + id, + parameters, + max_age: maxAge, + }, + (response) => { + queryResult.update(response); + + if ('job' in response) { + queryResult.refreshStatus(id); + } + }, + (error) => { + handleErrorResponse(queryResult, error); + }, + ); + + return queryResult; + } + static get(dataSourceId, query, parameters, maxAge, queryId) { const queryResult = new QueryResult(); diff --git a/client/app/services/query.js b/client/app/services/query.js index 740e339b21..1db316a8b7 100644 --- a/client/app/services/query.js +++ b/client/app/services/query.js @@ -429,7 +429,7 @@ function QueryResource( return this.getParameters().isRequired(); }; - QueryService.prototype.getQueryResult = function getQueryResult(maxAge, selectedQueryText) { + QueryService.prototype.getQueryResult = function getQueryResult(maxAge, selectedQueryText, isDirty) { if (!this.query) { return new QueryResultError("Can't execute empty query."); } @@ -454,26 +454,30 @@ function QueryResource( }); } - if (parameters.isRequired()) { - // Need to clear latest results, to make sure we don't use results for different params. - this.latest_query_data = null; - this.latest_query_data_id = null; - } - - if (this.latest_query_data && maxAge !== 0) { - if (!this.queryResult) { - this.queryResult = new QueryResult({ - query_result: this.latest_query_data, - }); + if (isDirty) { + if (parameters.isRequired()) { + // Need to clear latest results, to make sure we don't use results for different params. + this.latest_query_data = null; + this.latest_query_data_id = null; } - } else if (this.latest_query_data_id && maxAge !== 0) { - if (!this.queryResult) { - this.queryResult = QueryResult.getById(this.latest_query_data_id); + + if (this.latest_query_data && maxAge !== 0) { + if (!this.queryResult) { + this.queryResult = new QueryResult({ + query_result: this.latest_query_data, + }); + } + } else if (this.latest_query_data_id && maxAge !== 0) { + if (!this.queryResult) { + this.queryResult = QueryResult.getById(this.latest_query_data_id); + } + } else if (this.data_source_id) { + this.queryResult = QueryResult.get(this.data_source_id, queryText, parameters.getValues(), maxAge, this.id); + } else { + return new QueryResultError('Please select data source to run this query.'); } - } else if (this.data_source_id) { - this.queryResult = QueryResult.get(this.data_source_id, queryText, parameters.getValues(), maxAge, this.id); } else { - return new QueryResultError('Please select data source to run this query.'); + this.queryResult = QueryResult.getByQueryId(this.id, parameters.getValues(), maxAge); } return this.queryResult;