Add backward compatibility to old query format

This commit is contained in:
Bora Tunca 2016-09-16 17:01:21 -04:00
parent 5ac89fbc1f
commit 44377dc458

View file

@ -777,10 +777,47 @@ function init() {
url: PATH_PREFIX + "/static/js/graph_template.handlebar",
success: function(data) {
graphTemplate = Handlebars.compile(data);
var Page = new Prometheus.Page();
Page.init();
if (isDeprecatedGraphURL()) {
redirectToMigratedURL();
} else {
var Page = new Prometheus.Page();
Page.init();
}
}
});
}
// These two methods (isDeprecatedGraphURL and redirectToMigratedURL)
// are added only for backward compatibility to old query format.
function isDeprecatedGraphURL() {
if (window.location.hash.length == 0) {
return false;
}
var decodedFragment = decodeURIComponent(window.location.hash);
try {
JSON.parse(decodedFragment.substr(1)); // drop the hash #
} catch (e) {
return false;
}
return true;
}
function redirectToMigratedURL() {
var decodedFragment = decodeURIComponent(window.location.hash);
var graphOptions = JSON.parse(decodedFragment.substr(1)); // drop the hash #
var queryObject = {};
graphOptions.map(function(options, index){
var prefix = "g" + index + ".";
Object.keys(options).forEach(function(key) {
queryObject[prefix + key] = options[key];
});
});
var query = $.param(queryObject);
window.location = "/graph?" + query;
}
$(init);