Skip to content

Commit

Permalink
Query service
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldutra committed Dec 29, 2019
1 parent 9d38114 commit 7a9409f
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 330 deletions.
2 changes: 1 addition & 1 deletion client/app/components/EditParameterSettingsDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function EditParameterSettingsDialog(props) {
useEffect(() => {
const queryId = props.parameter.queryId;
if (queryId) {
Query.get({ id: queryId }, query => {
Query.get({ id: queryId }).then(query => {
setInitialQuery(query);
});
}
Expand Down
4 changes: 2 additions & 2 deletions client/app/components/QuerySelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ const { Option } = Select;
function search(term) {
// get recent
if (!term) {
return Query.recent().$promise.then(results => {
return Query.recent().then(results => {
const filteredResults = results.filter(item => !item.is_draft); // filter out draft
return Promise.resolve(filteredResults);
});
}

// search by query
return Query.query({ q: term }).$promise.then(({ results }) => Promise.resolve(results));
return Query.query({ q: term }).then(({ results }) => Promise.resolve(results));
}

export function QuerySelector(props) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useMemo, useCallback, useEffect } from "react";
import PropTypes from "prop-types";
import { isEmpty, template } from "lodash";
import { isEmpty, template, get } from "lodash";

import Dropdown from "antd/lib/dropdown";
import Icon from "antd/lib/icon";
Expand All @@ -18,8 +18,9 @@ export default function FavoritesDropdown({ fetch, urlTemplate }) {
const fetchItems = useCallback(
(showLoadingState = true) => {
setLoading(showLoadingState);
fetch()
.$promise.then(({ results }) => {
const request = fetch();
get(request, "$promise", request)
.then(({ results }) => {
setItems(results);
})
.finally(() => {
Expand Down
2 changes: 1 addition & 1 deletion client/app/components/dashboards/AddWidgetDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AddWidgetDialog extends React.Component {
});

if (selectedQuery) {
Query.get({ id: selectedQuery.id }, query => {
Query.get({ id: selectedQuery.id }).then(query => {
if (query) {
const existingParamNames = map(this.props.dashboard.getParametersDefs(), param => param.name);
this.setState({
Expand Down
5 changes: 3 additions & 2 deletions client/app/components/queries/VisualizationEmbed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import QueryResultsLink from "@/components/EditVisualizationButton/QueryResultsL
import VisualizationName from "@/visualizations/VisualizationName";
import { VisualizationRenderer } from "@/visualizations/VisualizationRenderer";
import { VisualizationType } from "@/visualizations";
import { Query } from "@/services/query";

import logoUrl from "@/assets/images/redash_icon_small.png";

Expand Down Expand Up @@ -201,10 +202,10 @@ export default function init(ngModule) {
return Auth.loadConfig();
}

function loadQuery($route, Auth, Query) {
function loadQuery($route, Auth) {
"ngInject";

return loadSession($route, Auth).then(() => Query.get({ id: $route.current.params.queryId }).$promise);
return loadSession($route, Auth).then(() => Query.get({ id: $route.current.params.queryId }));
}

ngModule.config($routeProvider => {
Expand Down
8 changes: 4 additions & 4 deletions client/app/pages/home/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from "react";
import axios from "@/services/axios";
import PropTypes from "prop-types";
import { includes, isEmpty } from "lodash";
import { includes, isEmpty, get } from "lodash";
import { react2angular } from "react2angular";
import Alert from "antd/lib/alert";
import Icon from "antd/lib/icon";
Expand Down Expand Up @@ -67,9 +67,9 @@ function FavoriteList({ title, resource, itemUrl, emptyState }) {

useEffect(() => {
setLoading(true);
resource
.favorites()
.$promise.then(({ results }) => setItems(results))
const request = resource.favorites();
get(request, "$promise", request)
.then(({ results }) => setItems(results))
.finally(() => setLoading(false));
}, [resource]);

Expand Down
7 changes: 4 additions & 3 deletions client/app/pages/queries/source-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { map, debounce } from "lodash";
import template from "./query.html";
import EditParameterSettingsDialog from "@/components/EditParameterSettingsDialog";
import DataSource from "@/services/data-source";
import { Query } from "@/services/query";

function QuerySourceCtrl(
Events,
Expand Down Expand Up @@ -119,7 +120,7 @@ export default function init(ngModule) {
controller: "QuerySourceCtrl",
reloadOnSearch: false,
resolve: {
query: function newQuery(Query) {
query: function newQuery() {
"ngInject";

return Query.newQuery();
Expand All @@ -137,10 +138,10 @@ export default function init(ngModule) {
controller: "QuerySourceCtrl",
reloadOnSearch: false,
resolve: {
query: (Query, $route) => {
query: $route => {
"ngInject";

return Query.get({ id: $route.current.params.queryId }).$promise;
return Query.get({ id: $route.current.params.queryId });
},
},
},
Expand Down
54 changes: 24 additions & 30 deletions client/app/pages/queries/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import AddToDashboardDialog from "@/components/queries/AddToDashboardDialog";
import PermissionsEditorDialog from "@/components/permissions-editor/PermissionsEditorDialog";
import notification from "@/services/notification";
import template from "./query.html";
import { Query } from "@/services/query";

function QueryViewCtrl(
$scope,
Expand All @@ -26,8 +27,7 @@ function QueryViewCtrl(
AlertDialog,
clientConfig,
$uibModal,
currentUser,
Query
currentUser
) {
// Should create it here since visualization registry might not be fulfilled when this file is loaded
const DEFAULT_VISUALIZATION = newVisualization("TABLE", { itemsPerPage: 50 });
Expand Down Expand Up @@ -208,7 +208,7 @@ function QueryViewCtrl(
const tabName = "duplicatedQueryTab" + Math.random().toString();

$window.open("", tabName);
Query.fork({ id: $scope.query.id }, newQuery => {
Query.fork({ id: $scope.query.id }).then(newQuery => {
const queryUrl = newQuery.getUrl(true);
$window.open(queryUrl, tabName);
});
Expand All @@ -231,7 +231,7 @@ function QueryViewCtrl(
if (request) {
// Don't save new query with partial data
if ($scope.query.isNew()) {
return $q.reject();
return Promise.reject();
}
request.id = $scope.query.id;
request.version = $scope.query.version;
Expand Down Expand Up @@ -276,13 +276,13 @@ function QueryViewCtrl(
$scope.saveQuery(options, data);
}

return Query.save(
request,
updatedQuery => {
return Query.save(request)
.then(updatedQuery => {
notification.success(options.successMessage);
$scope.query.version = updatedQuery.version;
},
error => {
return updatedQuery;
})
.catch(error => {
if (error.status === 409) {
const errorMessage = "It seems like the query has been modified by another user.";

Expand All @@ -302,8 +302,7 @@ function QueryViewCtrl(
} else {
notification.error(options.errorMessage);
}
}
).$promise;
});
};

$scope.togglePublished = () => {
Expand Down Expand Up @@ -341,16 +340,14 @@ function QueryViewCtrl(

$scope.archiveQuery = () => {
function archive() {
Query.delete(
{ id: $scope.query.id },
() => {
Query.delete({ id: $scope.query.id })
.then(() => {
$scope.query.is_archived = true;
$scope.query.schedule = null;
},
() => {
})
.catch(() => {
notification.error("Query could not be archived.");
}
);
});
}

const title = "Archive Query";
Expand All @@ -374,16 +371,13 @@ function QueryViewCtrl(
$scope.query.latest_query_data_id = null;

if ($scope.query.id) {
Query.save(
{
id: $scope.query.id,
data_source_id: $scope.query.data_source_id,
latest_query_data_id: null,
},
updatedQuery => {
$scope.query.version = updatedQuery.version;
}
);
Query.save({
id: $scope.query.id,
data_source_id: $scope.query.data_source_id,
latest_query_data_id: null,
}).then(updatedQuery => {
$scope.query.version = updatedQuery.version;
});
}

$scope.dataSource = find($scope.dataSources, ds => ds.id === $scope.query.data_source_id);
Expand Down Expand Up @@ -552,10 +546,10 @@ export default function init(ngModule) {
controller: "QueryViewCtrl",
reloadOnSearch: false,
resolve: {
query: (Query, $route) => {
query: $route => {
"ngInject";

return Query.get({ id: $route.current.params.queryId }).$promise;
return Query.get({ id: $route.current.params.queryId });
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions client/app/services/parameters/QueryBasedDropdownParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class QueryBasedDropdownParameter extends Parameter {

loadDropdownValues() {
if (this.parentQueryId) {
return Query.associatedDropdown({ queryId: this.parentQueryId, dropdownQueryId: this.queryId }).$promise;
return Query.associatedDropdown({ queryId: this.parentQueryId, dropdownQueryId: this.queryId });
}

return Query.asDropdown({ id: this.queryId }).$promise;
return Query.asDropdown({ id: this.queryId });
}
}

Expand Down
9 changes: 8 additions & 1 deletion client/app/services/query-result.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import debug from "debug";
import moment from "moment";
import { uniqBy, each, isNumber, isString, includes, extend, forOwn } from "lodash";
import { QueryResultError } from "./query";

const logger = debug("redash:services:QueryResult");
const filterTypes = ["filter", "multi-filter", "multiFilter"];

export let QueryResult = null; // eslint-disable-line import/no-mutable-exports

function getColumnNameWithoutType(column) {
let typeSplit;
if (column.indexOf("::") !== -1) {
Expand Down Expand Up @@ -35,7 +38,7 @@ function getColumnFriendlyName(column) {
return getColumnNameWithoutType(column).replace(/(?:^|\s)\S/g, a => a.toUpperCase());
}

function QueryResultService($resource, $timeout, $q, QueryResultError, Auth) {
function QueryResultService($resource, $timeout, $q, Auth) {
const QueryResultResource = $resource("api/query_results/:id", { id: "@id" }, { post: { method: "POST" } });
const QueryResultByQueryIdResource = $resource("api/queries/:queryId/results/:id.json", {
queryId: "@queryId",
Expand Down Expand Up @@ -447,6 +450,10 @@ function QueryResultService($resource, $timeout, $q, QueryResultError, Auth) {

export default function init(ngModule) {
ngModule.factory("QueryResult", QueryResultService);

ngModule.run($injector => {
QueryResult = $injector.get("QueryResult");
});
}

init.init = true;
Loading

0 comments on commit 7a9409f

Please sign in to comment.