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

Change query processing wait time to make response quick #7320

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
72 changes: 41 additions & 31 deletions client/app/services/query-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const logger = debug("redash:services:QueryResult");
const filterTypes = ["filter", "multi-filter", "multiFilter"];

function defer() {
const result = { onStatusChange: status => {} };
const result = { onStatusChange: (status) => {} };
result.promise = new Promise((resolve, reject) => {
result.resolve = resolve;
result.reject = reject;
Expand Down Expand Up @@ -40,13 +40,13 @@ function getColumnNameWithoutType(column) {
}

function getColumnFriendlyName(column) {
return getColumnNameWithoutType(column).replace(/(?:^|\s)\S/g, a => a.toUpperCase());
return getColumnNameWithoutType(column).replace(/(?:^|\s)\S/g, (a) => a.toUpperCase());
}

const createOrSaveUrl = data => (data.id ? `api/query_results/${data.id}` : "api/query_results");
const createOrSaveUrl = (data) => (data.id ? `api/query_results/${data.id}` : "api/query_results");
const QueryResultResource = {
get: ({ id }) => axios.get(`api/query_results/${id}`),
post: data => axios.post(createOrSaveUrl(data), data),
post: (data) => axios.post(createOrSaveUrl(data), data),
};

export const ExecutionStatus = {
Expand Down Expand Up @@ -97,11 +97,11 @@ function handleErrorResponse(queryResult, error) {
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
return new Promise((resolve) => setTimeout(resolve, ms));
}

export function fetchDataFromJob(jobId, interval = 1000) {
return axios.get(`api/jobs/${jobId}`).then(data => {
return axios.get(`api/jobs/${jobId}`).then((data) => {
const status = statuses[data.job.status];
if (status === ExecutionStatus.WAITING || status === ExecutionStatus.PROCESSING) {
return sleep(interval).then(() => fetchDataFromJob(data.job.id));
Expand Down Expand Up @@ -146,7 +146,7 @@ class QueryResult {
// TODO: we should stop manipulating incoming data, and switch to relaying
// on the column type set by the backend. This logic is prone to errors,
// and better be removed. Kept for now, for backward compatability.
each(this.query_result.data.rows, row => {
each(this.query_result.data.rows, (row) => {
forOwn(row, (v, k) => {
let newType = null;
if (isNumber(v)) {
Expand All @@ -173,7 +173,7 @@ class QueryResult {
});
});

each(this.query_result.data.columns, column => {
each(this.query_result.data.columns, (column) => {
column.name = "" + column.name;
if (columnTypes[column.name]) {
if (column.type == null || column.type === "string") {
Expand Down Expand Up @@ -265,14 +265,14 @@ class QueryResult {

getColumnNames() {
if (this.columnNames === undefined && this.query_result.data) {
this.columnNames = this.query_result.data.columns.map(v => v.name);
this.columnNames = this.query_result.data.columns.map((v) => v.name);
}

return this.columnNames;
}

getColumnFriendlyNames() {
return this.getColumnNames().map(col => getColumnFriendlyName(col));
return this.getColumnNames().map((col) => getColumnFriendlyName(col));
}

getTruncated() {
Expand All @@ -286,7 +286,7 @@ class QueryResult {

const filters = [];

this.getColumns().forEach(col => {
this.getColumns().forEach((col) => {
const name = col.name;
const type = name.split("::")[1] || name.split("__")[1];
if (includes(filterTypes, type)) {
Expand All @@ -302,8 +302,8 @@ class QueryResult {
}
}, this);

this.getRawData().forEach(row => {
filters.forEach(filter => {
this.getRawData().forEach((row) => {
filters.forEach((filter) => {
filter.values.push(row[filter.name]);
if (filter.values.length === 1) {
if (filter.multiple) {
Expand All @@ -315,8 +315,8 @@ class QueryResult {
});
});

filters.forEach(filter => {
filter.values = uniqBy(filter.values, v => {
filters.forEach((filter) => {
filter.values = uniqBy(filter.values, (v) => {
if (moment.isMoment(v)) {
return v.unix();
}
Expand Down Expand Up @@ -345,12 +345,12 @@ class QueryResult {

axios
.get(`api/queries/${queryId}/results/${id}.json`)
.then(response => {
.then((response) => {
// Success handler
queryResult.isLoadingResult = false;
queryResult.update(response);
})
.catch(error => {
.catch((error) => {
// Error handler
queryResult.isLoadingResult = false;
handleErrorResponse(queryResult, error);
Expand All @@ -362,10 +362,10 @@ class QueryResult {
loadLatestCachedResult(queryId, parameters) {
axios
.post(`api/queries/${queryId}/results`, { queryId, parameters })
.then(response => {
.then((response) => {
this.update(response);
})
.catch(error => {
.catch((error) => {
handleErrorResponse(this, error);
});
}
Expand All @@ -375,11 +375,11 @@ class QueryResult {
this.deferred.onStatusChange(ExecutionStatus.LOADING_RESULT);

QueryResultResource.get({ id: this.job.query_result_id })
.then(response => {
.then((response) => {
this.update(response);
this.isLoadingResult = false;
})
.catch(error => {
.catch((error) => {
if (tryCount === undefined) {
tryCount = 0;
}
Expand All @@ -394,9 +394,12 @@ class QueryResult {
});
this.isLoadingResult = false;
} else {
setTimeout(() => {
this.loadResult(tryCount + 1);
}, 1000 * Math.pow(2, tryCount));
setTimeout(
() => {
this.loadResult(tryCount + 1);
},
1000 * Math.pow(2, tryCount)
);
}
});
}
Expand All @@ -410,19 +413,26 @@ class QueryResult {
: axios.get(`api/queries/${query}/jobs/${this.job.id}`);

request
.then(jobResponse => {
.then((jobResponse) => {
this.update(jobResponse);

if (this.getStatus() === "processing" && this.job.query_result_id && this.job.query_result_id !== "None") {
loadResult();
} else if (this.getStatus() !== "failed") {
const waitTime = tryNumber > 10 ? 3000 : 500;
let waitTime;
if (tryNumber <= 10) {
waitTime = 500;
} else if (tryNumber <= 50) {
waitTime = 1000;
} else {
waitTime = 3000;
}
setTimeout(() => {
this.refreshStatus(query, parameters, tryNumber + 1);
}, waitTime);
}
})
.catch(error => {
.catch((error) => {
logger("Connection error", error);
// TODO: use QueryResultError, or better yet: exception/reject of promise.
this.update({
Expand Down Expand Up @@ -451,14 +461,14 @@ class QueryResult {

axios
.post(`api/queries/${id}/results`, { id, parameters, apply_auto_limit: applyAutoLimit, max_age: maxAge })
.then(response => {
.then((response) => {
queryResult.update(response);

if ("job" in response) {
queryResult.refreshStatus(id, parameters);
}
})
.catch(error => {
.catch((error) => {
handleErrorResponse(queryResult, error);
});

Expand All @@ -481,14 +491,14 @@ class QueryResult {
}

QueryResultResource.post(params)
.then(response => {
.then((response) => {
queryResult.update(response);

if ("job" in response) {
queryResult.refreshStatus(query, parameters);
}
})
.catch(error => {
.catch((error) => {
handleErrorResponse(queryResult, error);
});

Expand Down
Loading