Skip to content

Commit

Permalink
DataSource service
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldutra committed Dec 27, 2019
1 parent 2d8d163 commit e26df5b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 87 deletions.
10 changes: 5 additions & 5 deletions client/app/pages/data-sources/DataSourcesList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import Button from "antd/lib/button";
import { react2angular } from "react2angular";
import { isEmpty, get } from "lodash";
import { DataSource, IMG_ROOT } from "@/services/data-source";
import DataSource, { IMG_ROOT } from "@/services/data-source";
import { policy } from "@/services/policy";
import navigateTo from "@/services/navigateTo";
import { $route } from "@/services/ng";
Expand All @@ -23,7 +23,7 @@ class DataSourcesList extends React.Component {
};

componentDidMount() {
Promise.all([DataSource.query().$promise, DataSource.types().$promise]).then(values =>
Promise.all([DataSource.query(), DataSource.types()]).then(values =>
this.setState(
{
dataSources: values[0],
Expand All @@ -48,10 +48,10 @@ class DataSourcesList extends React.Component {
const target = { options: {}, type: selectedType.type };
helper.updateTargetWithValues(target, values);

return DataSource.save(target)
.$promise.then(dataSource => {
return DataSource.create(target)
.then(dataSource => {
this.setState({ loading: true });
DataSource.query(dataSources => this.setState({ dataSources, loading: false }));
DataSource.query().then(dataSources => this.setState({ dataSources, loading: false }));
return dataSource;
})
.catch(error => {
Expand Down
38 changes: 17 additions & 21 deletions client/app/pages/data-sources/EditDataSource.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from "prop-types";
import { get, find, toUpper } from "lodash";
import { react2angular } from "react2angular";
import Modal from "antd/lib/modal";
import { DataSource, IMG_ROOT } from "@/services/data-source";
import DataSource, { IMG_ROOT } from "@/services/data-source";
import navigateTo from "@/services/navigateTo";
import { $route } from "@/services/ng";
import notification from "@/services/notification";
Expand Down Expand Up @@ -31,10 +31,10 @@ class EditDataSource extends React.Component {

componentDidMount() {
DataSource.get({ id: $route.current.params.dataSourceId })
.$promise.then(dataSource => {
.then(dataSource => {
const { type } = dataSource;
this.setState({ dataSource });
DataSource.types(types => this.setState({ type: find(types, { type }), loading: false }));
DataSource.types().then(types => this.setState({ type: find(types, { type }), loading: false }));
})
.catch(error => {
// ANGULAR_REMOVE_ME This code is related to Angular's HTTP services
Expand All @@ -48,28 +48,26 @@ class EditDataSource extends React.Component {
saveDataSource = (values, successCallback, errorCallback) => {
const { dataSource } = this.state;
helper.updateTargetWithValues(dataSource, values);
dataSource.$save(
() => successCallback("Saved."),
error => {
DataSource.save(dataSource)
.then(() => successCallback("Saved."))
.catch(error => {
const message = get(error, "data.message", "Failed saving.");
errorCallback(message);
}
);
});
};

deleteDataSource = callback => {
const { dataSource } = this.state;

const doDelete = () => {
dataSource.$delete(
() => {
DataSource.delete(dataSource)
.then(() => {
notification.success("Data source deleted successfully.");
navigateTo("/data_sources", true);
},
() => {
})
.catch(() => {
callback();
}
);
});
};

Modal.confirm({
Expand All @@ -86,25 +84,23 @@ class EditDataSource extends React.Component {

testConnection = callback => {
const { dataSource } = this.state;
DataSource.test(
{ id: dataSource.id },
httpResponse => {
DataSource.test({ id: dataSource.id })
.then(httpResponse => {
if (httpResponse.ok) {
notification.success("Success");
} else {
notification.error("Connection Test Failed:", httpResponse.message, { duration: 10 });
}
callback();
},
() => {
})
.catch(() => {
notification.error(
"Connection Test Failed:",
"Unknown error occurred while performing connection test. Please try again later.",
{ duration: 10 }
);
callback();
}
);
});
};

renderForm() {
Expand Down
4 changes: 2 additions & 2 deletions client/app/pages/groups/GroupDataSources.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import wrapSettingsTab from "@/components/SettingsWrapper";
import notification from "@/services/notification";
import { currentUser } from "@/services/auth";
import { Group } from "@/services/group";
import { DataSource } from "@/services/data-source";
import DataSource from "@/services/data-source";
import navigateTo from "@/services/navigateTo";
import { routesToAngularRoutes } from "@/lib/utils";

Expand Down Expand Up @@ -134,7 +134,7 @@ class GroupDataSources extends React.Component {
};

addDataSources = () => {
const allDataSources = DataSource.query().$promise;
const allDataSources = DataSource.query();
const alreadyAddedDataSources = map(this.props.controller.allItems, ds => ds.id);
SelectItemsDialog.showModal({
dialogTitle: "Add Data Sources",
Expand Down
5 changes: 3 additions & 2 deletions client/app/pages/queries/source-view.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { map, debounce } from "lodash";
import template from "./query.html";
import EditParameterSettingsDialog from "@/components/EditParameterSettingsDialog";
import DataSource from "@/services/data-source";

function QuerySourceCtrl(
Events,
Expand Down Expand Up @@ -123,10 +124,10 @@ export default function init(ngModule) {

return Query.newQuery();
},
dataSources(DataSource) {
dataSources() {
"ngInject";

return DataSource.query().$promise;
return DataSource.query();
},
},
},
Expand Down
12 changes: 7 additions & 5 deletions client/app/pages/queries/view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pick, some, find, minBy, map, intersection, isEmpty, isArray } from "lodash";
import { SCHEMA_NOT_SUPPORTED, SCHEMA_LOAD_ERROR } from "@/services/data-source";
import DataSource, { SCHEMA_NOT_SUPPORTED, SCHEMA_LOAD_ERROR } from "@/services/data-source";
import getTags from "@/services/getTags";
import { policy } from "@/services/policy";
import { Visualization } from "@/services/visualization";
Expand Down Expand Up @@ -27,8 +27,7 @@ function QueryViewCtrl(
clientConfig,
$uibModal,
currentUser,
Query,
DataSource
Query
) {
// 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 @@ -76,7 +75,7 @@ function QueryViewCtrl(
function getSchema(refresh = undefined) {
// TODO: is it possible this will be called before dataSource is set?
$scope.schema = [];
$scope.dataSource.getSchema(refresh).then(data => {
DataSource.fetchSchema($scope.dataSource, refresh).then(data => {
if (data.schema) {
$scope.schema = data.schema;
$scope.schema.forEach(table => {
Expand All @@ -90,6 +89,7 @@ function QueryViewCtrl(
notification.error("Schema refresh failed.", "Please try again later.");
}
});
$scope.$applyAsync();
}

$scope.refreshSchema = () => getSchema(true);
Expand All @@ -115,6 +115,7 @@ function QueryViewCtrl(
$scope.canCreateQuery = some(dataSources, ds => !ds.view_only);

getSchema();
$scope.$applyAsync();
}

$scope.updateSelectedQuery = selectedQueryText => {
Expand Down Expand Up @@ -180,7 +181,8 @@ function QueryViewCtrl(
$scope.dataSources = $route.current.locals.dataSources;
updateDataSources($route.current.locals.dataSources);
} else {
$scope.dataSources = DataSource.query(updateDataSources);
$scope.dataSources = [];
DataSource.query().then(updateDataSources);
}

// in view mode, latest dataset is always visible
Expand Down
67 changes: 15 additions & 52 deletions client/app/services/data-source.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,26 @@
import axios from "@/services/axios";

export const SCHEMA_NOT_SUPPORTED = 1;
export const SCHEMA_LOAD_ERROR = 2;
export const IMG_ROOT = "/static/images/db-logos";

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

function DataSourceService($q, $resource, $http) {
function fetchSchema(dataSourceId, refresh = false) {
const DataSource = {
query: () => axios.get("api/data_sources"),
get: ({ id }) => axios.get(`api/data_sources/${id}`),
types: () => axios.get("api/data_sources/types"),
create: data => axios.post(`api/data_sources`, data),
save: data => axios.post(`api/data_sources/${data.id}`, data),
test: data => axios.post(`api/data_sources/${data.id}/test`),
delete: ({ id }) => axios.delete(`api/data_sources/${id}`),
fetchSchema: (data, refresh = false) => {
const params = {};

if (refresh) {
params.refresh = true;
}

return $http.get(`api/data_sources/${dataSourceId}/schema`, { params });
}

const actions = {
get: { method: "GET", cache: false, isArray: false },
query: { method: "GET", cache: false, isArray: true },
save: { method: "POST" },
types: {
method: "GET",
cache: false,
isArray: true,
url: "api/data_sources/types",
},
test: {
method: "POST",
cache: false,
isArray: false,
url: "api/data_sources/:id/test",
},
};

const DataSourceResource = $resource("api/data_sources/:id", { id: "@id" }, actions);

DataSourceResource.prototype.getSchema = function getSchema(refresh = false) {
if (this._schema === undefined || refresh) {
return fetchSchema(this.id, refresh).then(response => {
const data = response.data;

this._schema = data;

return data;
});
}

return $q.resolve(this._schema);
};

return DataSourceResource;
}

export default function init(ngModule) {
ngModule.factory("DataSource", DataSourceService);

ngModule.run($injector => {
DataSource = $injector.get("DataSource");
});
}
return axios.get(`api/data_sources/${data.id}/schema`, { params });
},
};

init.init = true;
export default DataSource;

0 comments on commit e26df5b

Please sign in to comment.