Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Allow server URLs with custom paths or no trailing slash. #343

Merged
merged 2 commits into from
Feb 17, 2015
Merged
Show file tree
Hide file tree
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
9 changes: 4 additions & 5 deletions app/assets/javascripts/angular/controllers/pie_ctrl.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
angular.module("Prometheus.controllers").controller('PieCtrl',
["$scope", "$http",
"SharedWidgetSetup",
"URLGenerator",
function($scope,
$http,
SharedWidgetSetup) {
SharedWidgetSetup,
URLGenerator) {
SharedWidgetSetup($scope);
$scope.errorMessages = [];

Expand All @@ -15,10 +17,7 @@ angular.module("Prometheus.controllers").controller('PieCtrl',
return;
}
$scope.requestInFlight = true;
var url = document.createElement('a');
url.href = server.url;
url.pathname = 'api/query';
$http.get(url.href, {
$http.get(URLGenerator(server.url, '/api/query'), {
params: {
expr: exp.expression
}
Expand Down
9 changes: 4 additions & 5 deletions app/assets/javascripts/angular/services/graph_refresher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ angular.module("Prometheus.services").factory('GraphRefresher',
["$http",
"$q",
"VariableInterpolator",
"URLGenerator",
function($http,
$q,
VariableInterpolator) {
VariableInterpolator,
URLGenerator) {
return function($scope) {
function loadGraphData(idx, expression, server, expressionID, endTime, rangeSeconds, step) {
var deferred = $q.defer();
var url = document.createElement('a');
url.href = server.url;
url.pathname = 'api/query_range';
$http.get(url.href, {
$http.get(URLGenerator(server.url, '/api/query_range'), {
params: {
expr: expression,
range: rangeSeconds,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
angular.module("Prometheus.services").factory('MetricNamesQuerier', ["$http", function($http) {
angular.module("Prometheus.services").factory('MetricNamesQuerier', ["$http", "URLGenerator", function($http, URLGenerator) {
var metricNamesCache = {};

return function(serverID, serverURL, scope) {
if (metricNamesCache[serverID]) {
scope.metricNames = metricNamesCache[serverID];
return;
}
var url = document.createElement('a');
url.href = serverURL;
url.pathname = 'api/metrics';
$http.get(url.href).success(function(metricNames) {
$http.get(URLGenerator(serverURL, '/api/metrics')).success(function(metricNames) {
metricNamesCache[serverID] = metricNames;
scope.metricNames = metricNames;
return;
Expand Down
8 changes: 8 additions & 0 deletions app/assets/javascripts/angular/services/url_generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
angular.module("Prometheus.services").factory('URLGenerator', [function() {
return function(url, path) {
var a = document.createElement('a');
a.href = url;
a.pathname = a.pathname.replace(/\/?$/, path);
return a.href;
};
}]);
27 changes: 27 additions & 0 deletions spec/javascripts/angular/services/url_generator_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//= require spec_helper
describe('URLGenerator', function() {
var urlGenerator;
beforeEach(inject(function(_URLGenerator_) {
urlGenerator = _URLGenerator_;
}));

it('allows urls with custom paths, no trailing slash', function() {
['http://promdash.server.com/prometheus', 'http://promdash.com'].forEach(function(s) {
['/api/query_range', '/api/query', '/api/metrics', '/arbitrary/endpoint'].forEach(function(ep) {
var s = 'http://promdash.server.com/prometheus';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. First you have a s variable which can contain two different domain names, but then you override s to this server name only? Or what am I missing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good catch. Yeah, looks like this line (and the same one in the test below) should be deleted?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh this is true. when I expanded the test to have the two different server names, i forgot to delete the original s var.

var url = urlGenerator(s, ep);
expect(url).toEqual(s + ep);
});
});
});

it('allows urls with custom paths, with trailing slash', function() {
['http://promdash.server.com/prometheus/', 'http://promdash.com/'].forEach(function(s) {
['/api/query_range', '/api/query', '/api/metrics', '/arbitrary/endpoint'].forEach(function(ep) {
var s = 'http://promdash.server.com/prometheus/';
var url = urlGenerator(s, ep);
expect(url).toEqual(s.substring(0, s.length - 1) + ep);
});
});
});
});