2013-01-24 04:55:32 -08:00
|
|
|
// Graph options we might want:
|
|
|
|
// Grid off/on
|
|
|
|
// Stacked off/on
|
|
|
|
// Area off/on
|
|
|
|
// Legend position
|
|
|
|
// Short link
|
|
|
|
// Graph title
|
|
|
|
// Palette
|
|
|
|
// Background
|
|
|
|
// Enable tooltips
|
|
|
|
// width/height
|
|
|
|
// Axis options
|
|
|
|
// Y-Range min/max
|
|
|
|
// (X-Range min/max)
|
|
|
|
// X-Axis format
|
|
|
|
// Y-Axis format
|
|
|
|
// Y-Axis title
|
|
|
|
// X-Axis title
|
|
|
|
// Log scale
|
|
|
|
|
|
|
|
var Prometheus = Prometheus || {};
|
|
|
|
var graphs = [];
|
2013-04-02 10:14:02 -07:00
|
|
|
var graphTemplate;
|
2013-01-24 04:55:32 -08:00
|
|
|
|
2013-04-17 07:49:21 -07:00
|
|
|
var SECOND = 1000;
|
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
Prometheus.Graph = function(element, options) {
|
|
|
|
this.el = element;
|
|
|
|
this.options = options;
|
|
|
|
this.changeHandler = null;
|
|
|
|
this.rickshawGraph = null;
|
|
|
|
this.data = [];
|
|
|
|
|
|
|
|
this.initialize();
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.timeFactors = {
|
|
|
|
"y": 60 * 60 * 24 * 365,
|
|
|
|
"w": 60 * 60 * 24 * 7,
|
|
|
|
"d": 60 * 60 * 24,
|
|
|
|
"h": 60 * 60,
|
|
|
|
"m": 60,
|
|
|
|
"s": 1
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.stepValues = [
|
|
|
|
"1s", "10s", "1m", "5m", "15m", "30m", "1h", "2h", "6h", "12h", "1d", "2d",
|
|
|
|
"1w", "2w", "4w", "8w", "1y", "2y"
|
|
|
|
];
|
|
|
|
|
|
|
|
Prometheus.Graph.numGraphs = 0;
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.initialize = function() {
|
|
|
|
var self = this;
|
|
|
|
self.id = Prometheus.Graph.numGraphs++;
|
|
|
|
|
|
|
|
// Set default options.
|
2013-07-23 17:21:31 -07:00
|
|
|
self.options["id"] = self.id;
|
|
|
|
self.options["range_input"] = self.options["range_input"] || "1h";
|
|
|
|
self.options["stacked_checked"] = self.options["stacked"] ? "checked" : "";
|
2014-12-26 08:40:06 -08:00
|
|
|
if (self.options["tab"] === undefined) {
|
|
|
|
self.options["tab"] = 1;
|
|
|
|
}
|
2013-01-24 04:55:32 -08:00
|
|
|
|
|
|
|
// Draw graph controls and container from Handlebars template.
|
2013-04-02 10:14:02 -07:00
|
|
|
|
|
|
|
var graphHtml = graphTemplate(self.options);
|
2013-01-24 04:55:32 -08:00
|
|
|
self.el.append(graphHtml);
|
|
|
|
|
|
|
|
// Get references to all the interesting elements in the graph container and
|
|
|
|
// bind event handlers.
|
|
|
|
var graphWrapper = self.el.find("#graph_wrapper" + self.id);
|
|
|
|
self.queryForm = graphWrapper.find(".query_form");
|
|
|
|
self.expr = graphWrapper.find("input[name=expr]");
|
|
|
|
self.rangeInput = self.queryForm.find("input[name=range_input]");
|
|
|
|
self.stacked = self.queryForm.find("input[name=stacked]");
|
2013-02-06 08:08:05 -08:00
|
|
|
self.insertMetric = self.queryForm.find("select[name=insert_metric]");
|
2013-04-17 07:49:21 -07:00
|
|
|
self.refreshInterval = self.queryForm.find("select[name=refresh]");
|
2013-05-21 07:54:33 -07:00
|
|
|
|
2013-04-26 07:50:59 -07:00
|
|
|
self.consoleTab = graphWrapper.find(".console");
|
|
|
|
self.graphTab = graphWrapper.find(".graph_container");
|
2013-07-23 17:21:31 -07:00
|
|
|
self.tabs = graphWrapper.find(".tabs");
|
2014-12-26 08:40:06 -08:00
|
|
|
self.tab = $(self.tabs.find("> div")[self.options["tab"]]); // active tab
|
2013-04-26 07:50:59 -07:00
|
|
|
|
|
|
|
self.tabs.tabs({
|
2013-07-23 17:21:31 -07:00
|
|
|
active: self.options["tab"],
|
2013-04-26 07:50:59 -07:00
|
|
|
activate: function(e, ui) {
|
|
|
|
storeGraphOptionsInUrl();
|
2014-12-26 08:40:06 -08:00
|
|
|
self.tab = ui.newPanel;
|
2013-07-23 17:21:31 -07:00
|
|
|
if (self.tab.hasClass("reload")) { // reload if flagged with class "reload"
|
2013-04-26 07:50:59 -07:00
|
|
|
self.submitQuery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-01-24 04:55:32 -08:00
|
|
|
|
2013-03-25 08:01:52 -07:00
|
|
|
// Return moves focus back to expr instead of submitting.
|
2013-07-23 17:21:31 -07:00
|
|
|
self.insertMetric.bind("keydown", "return", function(e) {
|
2013-03-25 08:01:52 -07:00
|
|
|
self.expr.focus();
|
|
|
|
self.expr.val(self.expr.val());
|
|
|
|
|
|
|
|
return e.preventDefault();
|
|
|
|
})
|
|
|
|
|
2014-10-30 08:18:05 -07:00
|
|
|
self.error = graphWrapper.find(".error");
|
2013-01-24 04:55:32 -08:00
|
|
|
self.graph = graphWrapper.find(".graph");
|
2013-07-01 05:47:43 -07:00
|
|
|
self.yAxis = graphWrapper.find(".y_axis");
|
2013-01-24 04:55:32 -08:00
|
|
|
self.legend = graphWrapper.find(".legend");
|
|
|
|
self.spinner = graphWrapper.find(".spinner");
|
|
|
|
self.evalStats = graphWrapper.find(".eval_stats");
|
|
|
|
|
2013-03-21 09:06:37 -07:00
|
|
|
self.endDate = graphWrapper.find("input[name=end_input]");
|
2015-01-12 05:18:28 -08:00
|
|
|
self.endDate.datetimepicker({
|
|
|
|
language: 'en',
|
|
|
|
pickSeconds: false,
|
|
|
|
});
|
|
|
|
// can we remove this conditional now?
|
2013-03-21 09:06:37 -07:00
|
|
|
if (self.options["end_input"]) {
|
2015-01-12 05:18:28 -08:00
|
|
|
self.endDate.data('datetimepicker').setValue(self.options["end_input"]);
|
2013-03-21 09:06:37 -07:00
|
|
|
}
|
2013-03-21 06:26:13 -07:00
|
|
|
self.endDate.change(function() { self.submitQuery() });
|
2013-07-23 17:21:31 -07:00
|
|
|
self.refreshInterval.change(function() { self.updateRefresh() });
|
2013-03-21 06:26:13 -07:00
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
self.stacked.change(function() { self.updateGraph(); });
|
2013-04-26 07:50:59 -07:00
|
|
|
self.queryForm.submit(function() {
|
2013-07-23 17:21:31 -07:00
|
|
|
self.consoleTab.addClass("reload");
|
|
|
|
self.graphTab.addClass("reload");
|
2013-04-26 07:50:59 -07:00
|
|
|
self.submitQuery();
|
|
|
|
return false;
|
|
|
|
});
|
2013-01-24 04:55:32 -08:00
|
|
|
self.spinner.hide();
|
|
|
|
|
2013-07-13 10:30:51 -07:00
|
|
|
self.queryForm.find("button[name=inc_range]").click(function() { self.increaseRange(); });
|
|
|
|
self.queryForm.find("button[name=dec_range]").click(function() { self.decreaseRange(); });
|
2013-03-21 06:26:13 -07:00
|
|
|
|
2013-07-13 10:30:51 -07:00
|
|
|
self.queryForm.find("button[name=inc_end]").click(function() { self.increaseEnd(); });
|
|
|
|
self.queryForm.find("button[name=dec_end]").click(function() { self.decreaseEnd(); });
|
2013-03-21 06:26:13 -07:00
|
|
|
|
2013-02-06 08:08:05 -08:00
|
|
|
self.insertMetric.change(function() {
|
2013-07-23 17:21:31 -07:00
|
|
|
self.expr.selection("replace", {text: self.insertMetric.val(), mode: "before"});
|
2014-09-16 09:20:31 -07:00
|
|
|
self.expr.focus(); // refocusing
|
2013-02-06 08:08:05 -08:00
|
|
|
});
|
2013-03-25 08:01:52 -07:00
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
self.expr.focus(); // TODO: move to external Graph method.
|
|
|
|
|
2013-02-06 08:08:05 -08:00
|
|
|
self.populateInsertableMetrics();
|
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
if (self.expr.val()) {
|
|
|
|
self.submitQuery();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-06 08:08:05 -08:00
|
|
|
Prometheus.Graph.prototype.populateInsertableMetrics = function() {
|
|
|
|
var self = this;
|
|
|
|
$.ajax({
|
|
|
|
method: "GET",
|
|
|
|
url: "/api/metrics",
|
|
|
|
dataType: "json",
|
|
|
|
success: function(json, textStatus) {
|
2013-07-23 17:21:31 -07:00
|
|
|
var availableMetrics = [];
|
2013-02-06 08:08:05 -08:00
|
|
|
for (var i = 0; i < json.length; i++) {
|
|
|
|
self.insertMetric[0].options.add(new Option(json[i], json[i]));
|
2013-07-23 17:21:31 -07:00
|
|
|
availableMetrics.push(json[i]);
|
2013-02-06 08:08:05 -08:00
|
|
|
}
|
2013-07-23 17:21:31 -07:00
|
|
|
self.expr.autocomplete({source: availableMetrics});
|
2013-02-06 08:08:05 -08:00
|
|
|
},
|
|
|
|
error: function() {
|
2014-10-30 08:18:05 -07:00
|
|
|
self.showError("Error loading available metrics!");
|
2013-02-06 08:08:05 -08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
Prometheus.Graph.prototype.onChange = function(handler) {
|
|
|
|
this.changeHandler = handler;
|
|
|
|
};
|
|
|
|
|
2013-02-06 08:08:05 -08:00
|
|
|
Prometheus.Graph.prototype.getOptions = function() {
|
2013-01-24 04:55:32 -08:00
|
|
|
var self = this;
|
|
|
|
var options = {};
|
|
|
|
|
|
|
|
var optionInputs = [
|
|
|
|
"expr",
|
|
|
|
"range_input",
|
2013-03-21 09:06:37 -07:00
|
|
|
"end_input",
|
2013-01-24 04:55:32 -08:00
|
|
|
"step_input",
|
|
|
|
"stacked"
|
|
|
|
];
|
|
|
|
|
|
|
|
self.queryForm.find("input").each(function(index, element) {
|
|
|
|
var name = element.name;
|
|
|
|
if ($.inArray(name, optionInputs) >= 0) {
|
|
|
|
if (name == "stacked") {
|
|
|
|
options[name] = element.checked;
|
|
|
|
} else {
|
|
|
|
options[name] = element.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-07-23 17:21:31 -07:00
|
|
|
options["tab"] = self.tabs.tabs("option", "active");
|
2013-01-24 04:55:32 -08:00
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
2013-04-17 07:49:21 -07:00
|
|
|
Prometheus.Graph.prototype.parseDuration = function(rangeText) {
|
2013-01-24 04:55:32 -08:00
|
|
|
var rangeRE = new RegExp("^([0-9]+)([ywdhms]+)$");
|
|
|
|
var matches = rangeText.match(rangeRE);
|
2013-04-17 07:49:21 -07:00
|
|
|
if (!matches) { return };
|
2013-01-24 04:55:32 -08:00
|
|
|
if (matches.length != 3) {
|
|
|
|
return 60;
|
|
|
|
}
|
|
|
|
var value = parseInt(matches[1]);
|
|
|
|
var unit = matches[2];
|
|
|
|
return value * Prometheus.Graph.timeFactors[unit];
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.increaseRange = function() {
|
|
|
|
var self = this;
|
2013-04-17 07:49:21 -07:00
|
|
|
var rangeSeconds = self.parseDuration(self.rangeInput.val());
|
2013-01-24 04:55:32 -08:00
|
|
|
for (var i = 0; i < Prometheus.Graph.stepValues.length; i++) {
|
2013-04-17 07:49:21 -07:00
|
|
|
if (rangeSeconds < self.parseDuration(Prometheus.Graph.stepValues[i])) {
|
2013-01-24 04:55:32 -08:00
|
|
|
self.rangeInput.val(Prometheus.Graph.stepValues[i]);
|
|
|
|
if (self.expr.val()) {
|
|
|
|
self.submitQuery();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.decreaseRange = function() {
|
|
|
|
var self = this;
|
2013-04-17 07:49:21 -07:00
|
|
|
var rangeSeconds = self.parseDuration(self.rangeInput.val());
|
2013-01-24 04:55:32 -08:00
|
|
|
for (var i = Prometheus.Graph.stepValues.length - 1; i >= 0; i--) {
|
2013-04-17 07:49:21 -07:00
|
|
|
if (rangeSeconds > self.parseDuration(Prometheus.Graph.stepValues[i])) {
|
2013-01-24 04:55:32 -08:00
|
|
|
self.rangeInput.val(Prometheus.Graph.stepValues[i]);
|
|
|
|
if (self.expr.val()) {
|
|
|
|
self.submitQuery();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-21 06:26:13 -07:00
|
|
|
Prometheus.Graph.prototype.getEndDate = function() {
|
2013-03-21 09:54:03 -07:00
|
|
|
var self = this;
|
2013-03-21 06:26:13 -07:00
|
|
|
if (!self.endDate || !self.endDate.val()) {
|
2013-07-13 10:30:51 -07:00
|
|
|
return null;
|
2013-03-21 06:26:13 -07:00
|
|
|
}
|
2015-01-12 05:18:28 -08:00
|
|
|
//
|
|
|
|
// This is returning the time set according to the system clock,
|
|
|
|
// but the datetimepicker is converting this to UTC, which is
|
|
|
|
// causing the time to decrease instead of increase. It should
|
|
|
|
// be increasing by 30min, but UTC is one hour behind berlin time,
|
|
|
|
// so incrementing UTC by 30min e.g. decrements the time the user sees by 30min.
|
|
|
|
//
|
2013-03-21 09:54:03 -07:00
|
|
|
return new Date(self.endDate.val()).getTime();
|
2013-03-21 06:26:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.getOrSetEndDate = function() {
|
2013-03-21 09:54:03 -07:00
|
|
|
var self = this;
|
2013-03-21 06:26:13 -07:00
|
|
|
var date = self.getEndDate();
|
|
|
|
if (date) {
|
2013-03-21 09:54:03 -07:00
|
|
|
return date;
|
2013-03-21 06:26:13 -07:00
|
|
|
}
|
2013-03-21 09:54:03 -07:00
|
|
|
date = new Date();
|
|
|
|
self.setEndDate(date);
|
|
|
|
return date;
|
2013-03-21 06:26:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.setEndDate = function(date) {
|
|
|
|
var self = this;
|
2015-01-12 05:18:28 -08:00
|
|
|
self.endDate.data('datetimepicker').setValue(date);
|
2013-03-21 06:26:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.increaseEnd = function() {
|
|
|
|
var self = this;
|
2013-04-17 07:49:21 -07:00
|
|
|
self.setEndDate(new Date(self.getOrSetEndDate() + self.parseDuration(self.rangeInput.val()) * 1000/2 )) // increase by 1/2 range & convert ms in s
|
2013-03-21 09:44:21 -07:00
|
|
|
self.submitQuery();
|
2013-03-21 06:26:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.decreaseEnd = function() {
|
|
|
|
var self = this;
|
2013-04-17 07:49:21 -07:00
|
|
|
self.setEndDate(new Date(self.getOrSetEndDate() - self.parseDuration(self.rangeInput.val()) * 1000/2 ))
|
2013-03-21 09:44:21 -07:00
|
|
|
self.submitQuery();
|
2013-03-21 06:26:13 -07:00
|
|
|
};
|
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
Prometheus.Graph.prototype.submitQuery = function() {
|
|
|
|
var self = this;
|
2014-10-30 08:18:05 -07:00
|
|
|
self.clearError();
|
2013-05-02 07:55:47 -07:00
|
|
|
if (!self.expr.val()) {
|
2013-07-23 17:21:31 -07:00
|
|
|
return;
|
2013-05-02 07:55:47 -07:00
|
|
|
}
|
2013-01-24 04:55:32 -08:00
|
|
|
|
|
|
|
self.spinner.show();
|
|
|
|
self.evalStats.empty();
|
|
|
|
|
|
|
|
var startTime = new Date().getTime();
|
|
|
|
|
2013-04-17 07:49:21 -07:00
|
|
|
var rangeSeconds = self.parseDuration(self.rangeInput.val());
|
2013-01-24 04:55:32 -08:00
|
|
|
self.queryForm.find("input[name=range]").val(rangeSeconds);
|
|
|
|
var resolution = self.queryForm.find("input[name=step_input]").val() || Math.max(Math.floor(rangeSeconds / 250), 1);
|
|
|
|
self.queryForm.find("input[name=step]").val(resolution);
|
2013-03-21 09:36:04 -07:00
|
|
|
var endDate = self.getEndDate() / 1000;
|
|
|
|
self.queryForm.find("input[name=end]").val(endDate);
|
2013-01-24 04:55:32 -08:00
|
|
|
|
2013-04-17 07:49:21 -07:00
|
|
|
if (self.queryXhr) {
|
2013-07-23 17:18:49 -07:00
|
|
|
self.queryXhr.abort();
|
2013-04-17 07:49:21 -07:00
|
|
|
}
|
2013-07-23 17:18:49 -07:00
|
|
|
var url;
|
|
|
|
var success;
|
2013-04-26 07:50:59 -07:00
|
|
|
if (self.tab[0] == self.graphTab[0]) {
|
2013-07-23 17:18:49 -07:00
|
|
|
url = self.queryForm.attr("action");
|
|
|
|
success = function(json, textStatus) { self.handleGraphResponse(json, textStatus); };
|
2013-04-26 07:50:59 -07:00
|
|
|
} else {
|
2013-07-23 17:18:49 -07:00
|
|
|
url = "/api/query";
|
|
|
|
success = function(text, textStatus) { self.handleConsoleResponse(text, textStatus); };
|
2013-04-26 07:50:59 -07:00
|
|
|
}
|
|
|
|
|
2013-04-17 07:49:21 -07:00
|
|
|
self.queryXhr = $.ajax({
|
2013-01-24 04:55:32 -08:00
|
|
|
method: self.queryForm.attr("method"),
|
2013-04-26 07:50:59 -07:00
|
|
|
url: url,
|
2013-07-23 17:18:49 -07:00
|
|
|
dataType: "json",
|
2013-03-21 09:36:04 -07:00
|
|
|
data: self.queryForm.serialize(),
|
2013-04-26 07:50:59 -07:00
|
|
|
success: success,
|
|
|
|
error: function(xhr, resp) {
|
2014-10-15 07:38:09 -07:00
|
|
|
if (resp != "abort") {
|
2014-10-30 08:18:05 -07:00
|
|
|
self.showError("Error executing query: " + resp);
|
2014-10-15 07:38:09 -07:00
|
|
|
}
|
2013-01-24 04:55:32 -08:00
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
var duration = new Date().getTime() - startTime;
|
2015-01-09 05:23:09 -08:00
|
|
|
self.evalStats.html("Load time: " + duration + "ms <br /> Resolution: " + resolution + "s");
|
2013-01-24 04:55:32 -08:00
|
|
|
self.spinner.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-10-30 08:18:05 -07:00
|
|
|
Prometheus.Graph.prototype.showError = function(msg) {
|
|
|
|
var self = this;
|
|
|
|
self.error.text(msg);
|
|
|
|
self.error.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.clearError = function(msg) {
|
|
|
|
var self = this;
|
|
|
|
self.error.text('');
|
|
|
|
self.error.hide();
|
|
|
|
}
|
|
|
|
|
2013-04-17 07:49:21 -07:00
|
|
|
Prometheus.Graph.prototype.updateRefresh = function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (self.timeoutID) {
|
2013-07-23 17:21:31 -07:00
|
|
|
window.clearTimeout(self.timeoutID);
|
2013-04-17 07:49:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
interval = self.parseDuration(self.refreshInterval.val());
|
|
|
|
if (!interval) { return };
|
|
|
|
|
|
|
|
self.timeoutID = window.setTimeout(function() {
|
2013-07-23 17:21:31 -07:00
|
|
|
self.submitQuery();
|
|
|
|
self.updateRefresh();
|
|
|
|
}, interval * SECOND);
|
2013-04-17 07:49:21 -07:00
|
|
|
}
|
|
|
|
|
2013-04-12 01:39:15 -07:00
|
|
|
Prometheus.Graph.prototype.renderLabels = function(labels) {
|
2013-01-24 04:55:32 -08:00
|
|
|
var labelStrings = [];
|
|
|
|
for (label in labels) {
|
2014-03-14 04:51:33 -07:00
|
|
|
if (label != "__name__") {
|
2013-04-12 04:41:53 -07:00
|
|
|
labelStrings.push("<strong>" + label + "</strong>: " + labels[label]);
|
2013-01-24 04:55:32 -08:00
|
|
|
}
|
|
|
|
}
|
2013-07-23 17:21:31 -07:00
|
|
|
return labels = "<div class=\"labels\">" + labelStrings.join("<br>") + "</div>";
|
2013-04-12 01:39:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.metricToTsName = function(labels) {
|
2014-10-15 08:03:08 -07:00
|
|
|
var tsName = (labels["__name__"] || '') + "{";
|
2013-04-15 01:04:09 -07:00
|
|
|
var labelStrings = [];
|
|
|
|
for (label in labels) {
|
2014-03-14 04:51:33 -07:00
|
|
|
if (label != "__name__") {
|
2013-07-23 17:21:31 -07:00
|
|
|
labelStrings.push(label + "=\"" + labels[label] + "\"");
|
2013-04-15 01:04:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tsName += labelStrings.join(",") + "}";
|
|
|
|
return tsName;
|
2013-01-24 04:55:32 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.parseValue = function(value) {
|
|
|
|
if (value == "NaN" || value == "Inf" || value == "-Inf") {
|
|
|
|
return 0; // TODO: what should we really do here?
|
|
|
|
} else {
|
2013-03-21 09:54:03 -07:00
|
|
|
return parseFloat(value);
|
2013-01-24 04:55:32 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.transformData = function(json) {
|
|
|
|
self = this;
|
|
|
|
var palette = new Rickshaw.Color.Palette();
|
|
|
|
if (json.Type != "matrix") {
|
2014-10-30 08:18:05 -07:00
|
|
|
self.showError("Result is not of matrix type! Please enter a correct expression.");
|
2013-01-24 04:55:32 -08:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
var data = json.Value.map(function(ts) {
|
|
|
|
return {
|
2014-10-15 08:03:08 -07:00
|
|
|
name: escapeHTML(self.metricToTsName(ts.Metric)),
|
2013-04-11 09:17:33 -07:00
|
|
|
labels: ts.Metric,
|
2013-01-24 04:55:32 -08:00
|
|
|
data: ts.Values.map(function(value) {
|
|
|
|
return {
|
|
|
|
x: value.Timestamp,
|
|
|
|
y: self.parseValue(value.Value)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
color: palette.color()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
Rickshaw.Series.zeroFill(data);
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.showGraph = function() {
|
|
|
|
var self = this;
|
|
|
|
self.rickshawGraph = new Rickshaw.Graph({
|
|
|
|
element: self.graph[0],
|
2013-03-21 11:12:04 -07:00
|
|
|
height: Math.max(self.graph.innerHeight(), 100),
|
2013-07-01 05:47:43 -07:00
|
|
|
width: Math.max(self.graph.innerWidth() - 80, 200),
|
2013-01-24 04:55:32 -08:00
|
|
|
renderer: (self.stacked.is(":checked") ? "stack" : "line"),
|
|
|
|
interpolation: "linear",
|
2013-05-21 07:54:33 -07:00
|
|
|
series: self.data,
|
|
|
|
min: "auto",
|
2013-01-24 04:55:32 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
var xAxis = new Rickshaw.Graph.Axis.Time({ graph: self.rickshawGraph });
|
|
|
|
|
|
|
|
var yAxis = new Rickshaw.Graph.Axis.Y({
|
|
|
|
graph: self.rickshawGraph,
|
2013-07-01 05:47:43 -07:00
|
|
|
orientation: "left",
|
2013-01-24 04:55:32 -08:00
|
|
|
tickFormat: Rickshaw.Fixtures.Number.formatKMBT,
|
2013-07-01 05:47:43 -07:00
|
|
|
element: self.yAxis[0],
|
2013-01-24 04:55:32 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
self.rickshawGraph.render();
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.updateGraph = function(reloadGraph) {
|
|
|
|
var self = this;
|
|
|
|
if (self.data.length == 0) { return; }
|
2013-02-04 05:06:58 -08:00
|
|
|
self.legend.empty();
|
2013-01-24 04:55:32 -08:00
|
|
|
if (self.rickshawGraph == null || reloadGraph) {
|
2013-07-01 05:47:43 -07:00
|
|
|
self.yAxis.empty();
|
2013-01-24 04:55:32 -08:00
|
|
|
self.graph.empty();
|
|
|
|
self.showGraph();
|
|
|
|
} else {
|
|
|
|
self.rickshawGraph.configure({
|
|
|
|
renderer: (self.stacked.is(":checked") ? "stack" : "line"),
|
|
|
|
interpolation: "linear",
|
|
|
|
series: self.data
|
|
|
|
});
|
|
|
|
self.rickshawGraph.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
var hoverDetail = new Rickshaw.Graph.HoverDetail({
|
2013-04-11 09:17:33 -07:00
|
|
|
graph: self.rickshawGraph,
|
|
|
|
formatter: function(series, x, y) {
|
|
|
|
var swatch = '<span class="detail_swatch" style="background-color: ' + series.color + '"></span>';
|
2014-10-15 08:03:08 -07:00
|
|
|
var content = swatch + (series.labels["__name__"] || 'value') + ": <strong>" + y + '</strong><br>';
|
2013-04-12 01:39:15 -07:00
|
|
|
return content + self.renderLabels(series.labels);
|
2013-04-12 01:39:37 -07:00
|
|
|
},
|
|
|
|
onRender: function() {
|
|
|
|
var width = this.graph.width;
|
2013-04-12 04:43:53 -07:00
|
|
|
var element = $(this.element);
|
2013-04-12 01:39:37 -07:00
|
|
|
|
2013-04-12 04:43:53 -07:00
|
|
|
$(".x_label", element).each(function() {
|
2013-04-12 02:59:49 -07:00
|
|
|
if ($(this).outerWidth() + element.offset().left > width) {
|
2013-04-12 04:41:53 -07:00
|
|
|
$(this).addClass("flipped");
|
2013-04-12 02:59:49 -07:00
|
|
|
} else {
|
2013-04-12 04:41:53 -07:00
|
|
|
$(this).removeClass("flipped");
|
2013-04-12 02:59:49 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2013-04-12 04:43:53 -07:00
|
|
|
$(".item", element).each(function() {
|
2013-04-12 01:39:37 -07:00
|
|
|
if ($(this).outerWidth() + element.offset().left > width) {
|
2013-04-12 04:41:53 -07:00
|
|
|
$(this).addClass("flipped");
|
2013-04-12 01:39:37 -07:00
|
|
|
} else {
|
2013-04-12 04:41:53 -07:00
|
|
|
$(this).removeClass("flipped");
|
2013-04-12 01:39:37 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2013-01-24 04:55:32 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
var legend = new Rickshaw.Graph.Legend({
|
|
|
|
element: self.legend[0],
|
2013-04-12 01:39:15 -07:00
|
|
|
graph: self.rickshawGraph,
|
2013-01-24 04:55:32 -08:00
|
|
|
});
|
|
|
|
|
2013-04-11 09:21:00 -07:00
|
|
|
var highlighter = new Rickshaw.Graph.Behavior.Series.Highlight( {
|
|
|
|
graph: self.rickshawGraph,
|
|
|
|
legend: legend
|
|
|
|
});
|
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({
|
|
|
|
graph: self.rickshawGraph,
|
|
|
|
legend: legend
|
|
|
|
});
|
|
|
|
|
|
|
|
self.changeHandler();
|
|
|
|
};
|
2013-03-26 06:07:56 -07:00
|
|
|
|
2013-03-21 11:12:04 -07:00
|
|
|
Prometheus.Graph.prototype.resizeGraph = function() {
|
|
|
|
var self = this;
|
2013-07-23 17:20:42 -07:00
|
|
|
if (self.rickshawGraph != null) {
|
|
|
|
self.rickshawGraph.configure({
|
|
|
|
width: Math.max(self.graph.innerWidth() - 80, 200),
|
|
|
|
});
|
|
|
|
self.rickshawGraph.render();
|
|
|
|
}
|
2013-04-26 07:50:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.handleGraphResponse = function(json, textStatus) {
|
|
|
|
var self = this
|
|
|
|
if (json.Type == "error") {
|
2014-10-30 08:18:05 -07:00
|
|
|
self.showError(json.Value);
|
2013-04-26 07:50:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.data = self.transformData(json);
|
|
|
|
if (self.data.length == 0) {
|
2014-10-30 08:18:05 -07:00
|
|
|
self.showError("No datapoints found.");
|
2013-04-26 07:50:59 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-07-23 17:21:31 -07:00
|
|
|
self.graphTab.removeClass("reload");
|
2013-04-26 07:50:59 -07:00
|
|
|
self.updateGraph(true);
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:18:49 -07:00
|
|
|
Prometheus.Graph.prototype.handleConsoleResponse = function(data, textStatus) {
|
2013-04-26 07:50:59 -07:00
|
|
|
var self = this;
|
2013-07-23 17:18:49 -07:00
|
|
|
self.consoleTab.removeClass("reload");
|
|
|
|
|
|
|
|
var tBody = self.consoleTab.find(".console_table tbody");
|
|
|
|
tBody.empty();
|
|
|
|
|
|
|
|
switch(data.Type) {
|
|
|
|
case "vector":
|
|
|
|
for (var i = 0; i < data.Value.length; i++) {
|
|
|
|
var v = data.Value[i];
|
|
|
|
var tsName = self.metricToTsName(v.Metric);
|
2014-10-15 08:03:08 -07:00
|
|
|
tBody.append("<tr><td>" + escapeHTML(tsName) + "</td><td>" + v.Value + "</td></tr>")
|
2013-07-23 17:18:49 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "matrix":
|
|
|
|
for (var i = 0; i < data.Value.length; i++) {
|
|
|
|
var v = data.Value[i];
|
|
|
|
var tsName = self.metricToTsName(v.Metric);
|
|
|
|
var valueText = "";
|
|
|
|
for (var j = 0; j < v.Values.length; j++) {
|
|
|
|
valueText += v.Values[j].Value + " @" + v.Values[j].Timestamp + "<br/>";
|
|
|
|
}
|
2014-10-15 08:03:08 -07:00
|
|
|
tBody.append("<tr><td>" + escapeHTML(tsName) + "</td><td>" + valueText + "</td></tr>")
|
2013-07-23 17:18:49 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "scalar":
|
|
|
|
tBody.append("<tr><td>scalar</td><td>" + data.Value + "</td></tr>");
|
|
|
|
break;
|
2013-08-02 00:04:13 -07:00
|
|
|
case "error":
|
2014-10-30 08:18:05 -07:00
|
|
|
self.showError(data.Value);
|
2013-08-02 00:04:13 -07:00
|
|
|
break;
|
2013-07-23 17:18:49 -07:00
|
|
|
default:
|
2014-10-30 08:18:05 -07:00
|
|
|
self.showError("Unsupported value type!");
|
2013-07-23 17:18:49 -07:00
|
|
|
break;
|
2013-07-13 10:30:51 -07:00
|
|
|
}
|
2013-04-26 07:50:59 -07:00
|
|
|
}
|
2013-01-24 04:55:32 -08:00
|
|
|
|
|
|
|
function parseGraphOptionsFromUrl() {
|
|
|
|
var hashOptions = window.location.hash.slice(1);
|
|
|
|
if (!hashOptions) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
var optionsJSON = decodeURIComponent(window.location.hash.slice(1));
|
2013-04-26 07:50:59 -07:00
|
|
|
options = JSON.parse(optionsJSON);
|
|
|
|
return options;
|
2013-01-24 04:55:32 -08:00
|
|
|
}
|
|
|
|
|
2014-07-25 05:23:47 -07:00
|
|
|
// NOTE: This needs to be kept in sync with rules/helpers.go:GraphLinkForExpression!
|
2013-04-26 07:50:59 -07:00
|
|
|
function storeGraphOptionsInUrl() {
|
2013-01-24 04:55:32 -08:00
|
|
|
var allGraphsOptions = [];
|
|
|
|
for (var i = 0; i < graphs.length; i++) {
|
|
|
|
allGraphsOptions.push(graphs[i].getOptions());
|
|
|
|
}
|
|
|
|
var optionsJSON = JSON.stringify(allGraphsOptions);
|
|
|
|
window.location.hash = encodeURIComponent(optionsJSON);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addGraph(options) {
|
|
|
|
var graph = new Prometheus.Graph($("#graph_container"), options);
|
|
|
|
graphs.push(graph);
|
|
|
|
graph.onChange(function() {
|
|
|
|
storeGraphOptionsInUrl();
|
|
|
|
});
|
2013-03-21 11:12:04 -07:00
|
|
|
$(window).resize(function() {
|
|
|
|
graph.resizeGraph();
|
|
|
|
});
|
2013-01-24 04:55:32 -08:00
|
|
|
}
|
|
|
|
|
2014-10-15 08:03:08 -07:00
|
|
|
function escapeHTML(string) {
|
|
|
|
var entityMap = {
|
|
|
|
"&": "&",
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
'"': '"',
|
|
|
|
"'": ''',
|
|
|
|
"/": '/'
|
|
|
|
};
|
|
|
|
|
|
|
|
return String(string).replace(/[&<>"'\/]/g, function (s) {
|
|
|
|
return entityMap[s];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
function init() {
|
2013-04-12 04:43:53 -07:00
|
|
|
$.ajaxSetup({
|
2013-01-24 04:55:32 -08:00
|
|
|
cache: false
|
|
|
|
});
|
|
|
|
|
2013-05-21 07:54:33 -07:00
|
|
|
$.ajax({
|
|
|
|
url: "/static/js/graph_template.handlebar",
|
2013-04-02 10:14:02 -07:00
|
|
|
success: function(data) {
|
|
|
|
graphTemplate = Handlebars.compile(data);
|
|
|
|
var options = parseGraphOptionsFromUrl();
|
|
|
|
if (options.length == 0) {
|
|
|
|
options.push({});
|
|
|
|
}
|
|
|
|
for (var i = 0; i < options.length; i++) {
|
|
|
|
addGraph(options[i]);
|
|
|
|
}
|
|
|
|
$("#add_graph").click(function() { addGraph({}); });
|
2013-05-21 07:54:33 -07:00
|
|
|
}
|
2013-04-02 10:14:02 -07:00
|
|
|
})
|
2013-01-24 04:55:32 -08:00
|
|
|
}
|
2013-06-13 07:10:05 -07:00
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
$(init);
|