2014-09-21 15:02:06 +02:00
|
|
|
define([
|
|
|
|
|
'angular',
|
|
|
|
|
'lodash',
|
2015-10-30 15:58:20 +01:00
|
|
|
'../core_module',
|
2015-10-30 14:19:02 +01:00
|
|
|
'app/core/config',
|
2014-09-21 15:02:06 +02:00
|
|
|
],
|
2015-10-30 15:58:20 +01:00
|
|
|
function (angular, _, coreModule, config) {
|
2014-09-21 15:02:06 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-06-08 18:04:31 +02:00
|
|
|
coreModule.default.service('backendSrv', function($http, alertSrv, $timeout, $q) {
|
2015-01-16 15:21:07 +01:00
|
|
|
var self = this;
|
2014-09-21 15:02:06 +02:00
|
|
|
|
2015-01-06 09:10:20 +01:00
|
|
|
this.get = function(url, params) {
|
|
|
|
|
return this.request({ method: 'GET', url: url, params: params });
|
2014-09-21 15:02:06 +02:00
|
|
|
};
|
|
|
|
|
|
2014-12-17 17:31:57 +01:00
|
|
|
this.delete = function(url) {
|
|
|
|
|
return this.request({ method: 'DELETE', url: url });
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-16 16:44:45 +01:00
|
|
|
this.post = function(url, data) {
|
|
|
|
|
return this.request({ method: 'POST', url: url, data: data });
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-01 09:48:07 +02:00
|
|
|
this.patch = function(url, data) {
|
|
|
|
|
return this.request({ method: 'PATCH', url: url, data: data });
|
|
|
|
|
};
|
|
|
|
|
|
2014-12-19 12:52:00 +01:00
|
|
|
this.put = function(url, data) {
|
|
|
|
|
return this.request({ method: 'PUT', url: url, data: data });
|
|
|
|
|
};
|
|
|
|
|
|
2015-01-16 15:21:07 +01:00
|
|
|
this._handleError = function(err) {
|
2015-04-07 09:25:00 +02:00
|
|
|
return function() {
|
|
|
|
|
if (err.isHandled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-01-16 15:21:07 +01:00
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
var data = err.data || { message: 'Unexpected error' };
|
|
|
|
|
if (_.isString(data)) {
|
|
|
|
|
data = { message: data };
|
|
|
|
|
}
|
2015-01-16 15:21:07 +01:00
|
|
|
|
2015-08-31 11:35:07 +02:00
|
|
|
if (err.status === 422) {
|
|
|
|
|
alertSrv.set("Validation failed", data.message, "warning", 4000);
|
|
|
|
|
throw data;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
data.severity = 'error';
|
2015-01-16 15:21:07 +01:00
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
if (err.status < 500) {
|
|
|
|
|
data.severity = "warning";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.message) {
|
|
|
|
|
alertSrv.set("Problem!", data.message, data.severity, 10000);
|
|
|
|
|
}
|
2015-01-16 15:21:07 +01:00
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
throw data;
|
|
|
|
|
};
|
2015-01-16 15:21:07 +01:00
|
|
|
};
|
|
|
|
|
|
2014-09-21 15:02:06 +02:00
|
|
|
this.request = function(options) {
|
2015-04-07 09:25:00 +02:00
|
|
|
options.retry = options.retry || 0;
|
|
|
|
|
var requestIsLocal = options.url.indexOf('/') === 0;
|
|
|
|
|
var firstAttempt = options.retry === 0;
|
2014-09-21 15:02:06 +02:00
|
|
|
|
2015-04-23 08:24:30 +02:00
|
|
|
if (requestIsLocal && !options.hasSubUrl) {
|
2015-04-07 09:25:00 +02:00
|
|
|
options.url = config.appSubUrl + options.url;
|
2015-04-23 08:24:30 +02:00
|
|
|
options.hasSubUrl = true;
|
2015-03-27 06:47:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
return $http(options).then(function(results) {
|
2014-09-21 15:02:06 +02:00
|
|
|
if (options.method !== 'GET') {
|
2014-12-17 03:09:25 +01:00
|
|
|
if (results && results.data.message) {
|
|
|
|
|
alertSrv.set(results.data.message, '', 'success', 3000);
|
|
|
|
|
}
|
2014-09-21 15:02:06 +02:00
|
|
|
}
|
|
|
|
|
return results.data;
|
|
|
|
|
}, function(err) {
|
2015-04-07 09:25:00 +02:00
|
|
|
// handle unauthorized
|
|
|
|
|
if (err.status === 401 && firstAttempt) {
|
|
|
|
|
return self.loginPing().then(function() {
|
|
|
|
|
options.retry = 1;
|
|
|
|
|
return self.request(options);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$timeout(self._handleError(err), 50);
|
|
|
|
|
throw err;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-08 18:04:31 +02:00
|
|
|
var datasourceInFlightRequests = {};
|
|
|
|
|
var HTTP_REQUEST_ABORTED = -1;
|
2015-04-07 09:25:00 +02:00
|
|
|
this.datasourceRequest = function(options) {
|
|
|
|
|
options.retry = options.retry || 0;
|
2016-06-08 18:04:31 +02:00
|
|
|
|
2016-06-09 12:57:55 +02:00
|
|
|
// A requestID is provided by the datasource as a unique identifier for a
|
|
|
|
|
// particular query. If the requestID exists, the promise it is keyed to
|
2016-06-08 18:04:31 +02:00
|
|
|
// is canceled, canceling the previous datasource request if it is still
|
|
|
|
|
// in-flight.
|
|
|
|
|
var canceler;
|
2016-06-09 12:57:55 +02:00
|
|
|
if (options.requestID) {
|
|
|
|
|
if (canceler = datasourceInFlightRequests[options.requestID]) {
|
2016-06-08 18:04:31 +02:00
|
|
|
canceler.resolve();
|
|
|
|
|
}
|
|
|
|
|
canceler = $q.defer();
|
|
|
|
|
options.timeout = canceler.promise;
|
2016-06-09 12:57:55 +02:00
|
|
|
datasourceInFlightRequests[options.requestID] = canceler;
|
2016-06-08 18:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
var requestIsLocal = options.url.indexOf('/') === 0;
|
|
|
|
|
var firstAttempt = options.retry === 0;
|
|
|
|
|
|
2016-05-26 15:13:29 +10:00
|
|
|
if (requestIsLocal && options.headers && options.headers.Authorization) {
|
|
|
|
|
options.headers['X-DS-Authorization'] = options.headers.Authorization;
|
|
|
|
|
delete options.headers.Authorization;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
return $http(options).then(null, function(err) {
|
2016-06-08 18:04:31 +02:00
|
|
|
if (err.status === HTTP_REQUEST_ABORTED) {
|
2016-06-09 12:13:17 +02:00
|
|
|
// TODO: Hitting refresh before the original request returns cancels
|
|
|
|
|
// the "loading" animation on the panes, but it should continue to be
|
|
|
|
|
// visible.
|
|
|
|
|
err.statusText = "request aborted";
|
|
|
|
|
return err;
|
2016-06-08 18:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
// handle unauthorized for backend requests
|
2016-06-09 12:13:17 +02:00
|
|
|
if (requestIsLocal && firstAttempt && err.status === 401) {
|
2015-04-07 09:25:00 +02:00
|
|
|
return self.loginPing().then(function() {
|
|
|
|
|
options.retry = 1;
|
2016-06-09 15:16:53 +02:00
|
|
|
if (canceler) {
|
|
|
|
|
canceler.resolve();
|
|
|
|
|
}
|
2015-04-07 09:25:00 +02:00
|
|
|
return self.datasourceRequest(options);
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-12-17 03:09:25 +01:00
|
|
|
|
2016-03-21 12:30:13 +01:00
|
|
|
//populate error obj on Internal Error
|
2016-03-21 12:43:30 +01:00
|
|
|
if (_.isString(err.data) && err.status === 500) {
|
2016-03-21 12:30:13 +01:00
|
|
|
err.data = {
|
|
|
|
|
error: err.statusText
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-10 19:01:37 +09:00
|
|
|
// for Prometheus
|
|
|
|
|
if (!err.data.message && _.isString(err.data.error)) {
|
|
|
|
|
err.data.message = err.data.error;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 15:21:07 +01:00
|
|
|
throw err;
|
2014-09-21 15:02:06 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-07 09:25:00 +02:00
|
|
|
this.loginPing = function() {
|
|
|
|
|
return this.request({url: '/api/login/ping', method: 'GET', retry: 1 });
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-28 10:04:19 +01:00
|
|
|
this.search = function(query) {
|
|
|
|
|
return this.get('/api/search', query);
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-12 14:11:30 +02:00
|
|
|
this.getDashboard = function(type, slug) {
|
|
|
|
|
return this.get('/api/dashboards/' + type + '/' + slug);
|
2015-02-28 11:07:22 +01:00
|
|
|
};
|
|
|
|
|
|
2015-03-02 22:24:01 +01:00
|
|
|
this.saveDashboard = function(dash, options) {
|
|
|
|
|
options = (options || {});
|
|
|
|
|
return this.post('/api/dashboards/db/', {dashboard: dash, overwrite: options.overwrite === true});
|
2015-02-28 09:46:37 +01:00
|
|
|
};
|
|
|
|
|
|
2014-09-21 15:02:06 +02:00
|
|
|
});
|
|
|
|
|
});
|