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 = [];
|
|
|
|
|
|
|
|
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.
|
|
|
|
self.options['id'] = self.id;
|
|
|
|
self.options['range_input'] = self.options['range_input'] || "1h";
|
|
|
|
self.options['stacked_checked'] = self.options['stacked'] ? "checked" : "";
|
|
|
|
|
|
|
|
// Draw graph controls and container from Handlebars template.
|
|
|
|
var source = $("#graph_template").html();
|
|
|
|
var template = Handlebars.compile(source);
|
|
|
|
var graphHtml = template(self.options);
|
|
|
|
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-01-24 04:55:32 -08:00
|
|
|
|
|
|
|
self.graph = graphWrapper.find(".graph");
|
|
|
|
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]");
|
|
|
|
if (self.options["end_input"]) {
|
2013-03-21 09:54:03 -07:00
|
|
|
self.endDate.appendDtpicker({"current": self.options["end_input"]});
|
2013-03-21 09:06:37 -07:00
|
|
|
} else {
|
|
|
|
self.endDate.appendDtpicker();
|
2013-03-21 09:54:03 -07:00
|
|
|
self.endDate.val("");
|
2013-03-21 09:06:37 -07:00
|
|
|
}
|
2013-03-21 06:26:13 -07:00
|
|
|
self.endDate.change(function() { self.submitQuery() });
|
|
|
|
|
2013-01-24 04:55:32 -08:00
|
|
|
self.stacked.change(function() { self.updateGraph(); });
|
|
|
|
self.queryForm.submit(function() { self.submitQuery(); return false; });
|
|
|
|
self.spinner.hide();
|
|
|
|
|
|
|
|
self.queryForm.find("input[name=inc_range]").click(function() { self.increaseRange(); });
|
|
|
|
self.queryForm.find("input[name=dec_range]").click(function() { self.decreaseRange(); });
|
2013-03-21 06:26:13 -07:00
|
|
|
|
|
|
|
self.queryForm.find("input[name=inc_end]").click(function() { self.increaseEnd(); });
|
|
|
|
self.queryForm.find("input[name=dec_end]").click(function() { self.decreaseEnd(); });
|
|
|
|
|
2013-02-06 08:08:05 -08:00
|
|
|
self.insertMetric.change(function() {
|
|
|
|
self.expr.val(self.expr.val() + self.insertMetric.val());
|
|
|
|
});
|
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) {
|
|
|
|
for (var i = 0; i < json.length; i++) {
|
|
|
|
self.insertMetric[0].options.add(new Option(json[i], json[i]));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
alert("Error loading available metrics!");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.parseRange = function(rangeText) {
|
|
|
|
var rangeRE = new RegExp("^([0-9]+)([ywdhms]+)$");
|
|
|
|
var matches = rangeText.match(rangeRE);
|
|
|
|
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;
|
|
|
|
var rangeSeconds = self.parseRange(self.rangeInput.val());
|
|
|
|
for (var i = 0; i < Prometheus.Graph.stepValues.length; i++) {
|
|
|
|
if (rangeSeconds < self.parseRange(Prometheus.Graph.stepValues[i])) {
|
|
|
|
self.rangeInput.val(Prometheus.Graph.stepValues[i]);
|
|
|
|
if (self.expr.val()) {
|
|
|
|
self.submitQuery();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.decreaseRange = function() {
|
|
|
|
var self = this;
|
|
|
|
var rangeSeconds = self.parseRange(self.rangeInput.val());
|
|
|
|
for (var i = Prometheus.Graph.stepValues.length - 1; i >= 0; i--) {
|
|
|
|
if (rangeSeconds > self.parseRange(Prometheus.Graph.stepValues[i])) {
|
|
|
|
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()) {
|
|
|
|
return null;
|
|
|
|
}
|
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;
|
2013-03-21 07:56:41 -07:00
|
|
|
dateString = date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate() + ' ' +
|
2013-03-21 09:54:03 -07:00
|
|
|
date.getHours() + ':' + date.getMinutes();
|
|
|
|
self.endDate.val("");
|
|
|
|
self.endDate.appendDtpicker({"current": dateString});
|
2013-03-21 06:26:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.increaseEnd = function() {
|
|
|
|
var self = this;
|
2013-03-21 09:47:40 -07:00
|
|
|
self.setEndDate(new Date(self.getOrSetEndDate() + self.parseRange(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-03-21 09:47:40 -07:00
|
|
|
self.setEndDate(new Date(self.getOrSetEndDate() - self.parseRange(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;
|
|
|
|
|
|
|
|
self.spinner.show();
|
|
|
|
self.evalStats.empty();
|
|
|
|
|
|
|
|
var startTime = new Date().getTime();
|
|
|
|
|
|
|
|
var rangeSeconds = self.parseRange(self.rangeInput.val());
|
|
|
|
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
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
method: self.queryForm.attr("method"),
|
|
|
|
url: self.queryForm.attr("action"),
|
|
|
|
dataType: "json",
|
2013-03-21 09:36:04 -07:00
|
|
|
data: self.queryForm.serialize(),
|
2013-01-24 04:55:32 -08:00
|
|
|
success: function(json, textStatus) {
|
|
|
|
if (json.Type == "error") {
|
|
|
|
alert(json.Value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.data = self.transformData(json);
|
|
|
|
if (self.data.length == 0) {
|
|
|
|
alert("No datapoints found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.updateGraph(true);
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
alert("Error executing query!");
|
|
|
|
},
|
|
|
|
complete: function() {
|
|
|
|
var duration = new Date().getTime() - startTime;
|
|
|
|
self.evalStats.html("Load time: " + duration + "ms, resolution: " + resolution + "s");
|
|
|
|
self.spinner.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Prometheus.Graph.prototype.metricToTsName = function(labels) {
|
|
|
|
var tsName = labels["name"] + "{";
|
|
|
|
var labelStrings = [];
|
|
|
|
for (label in labels) {
|
|
|
|
if (label != "name") {
|
|
|
|
labelStrings.push(label + "='" + labels[label] + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tsName += labelStrings.join(",") + "}";
|
|
|
|
return tsName;
|
|
|
|
};
|
|
|
|
|
|
|
|
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") {
|
|
|
|
alert("Result is not of matrix type! Please enter a correct expression.");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
var data = json.Value.map(function(ts) {
|
|
|
|
return {
|
|
|
|
name: self.metricToTsName(ts.Metric),
|
|
|
|
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-02-05 15:46:10 -08:00
|
|
|
height: Math.max($(window).height() - 200, 100),
|
|
|
|
width: Math.max($(window).width() - 200, 200),
|
2013-01-24 04:55:32 -08:00
|
|
|
renderer: (self.stacked.is(":checked") ? "stack" : "line"),
|
|
|
|
interpolation: "linear",
|
|
|
|
series: self.data
|
|
|
|
});
|
|
|
|
|
|
|
|
var xAxis = new Rickshaw.Graph.Axis.Time({ graph: self.rickshawGraph });
|
|
|
|
|
|
|
|
var yAxis = new Rickshaw.Graph.Axis.Y({
|
|
|
|
graph: self.rickshawGraph,
|
|
|
|
tickFormat: Rickshaw.Fixtures.Number.formatKMBT,
|
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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({
|
|
|
|
graph: self.rickshawGraph
|
|
|
|
});
|
|
|
|
|
|
|
|
var legend = new Rickshaw.Graph.Legend({
|
|
|
|
element: self.legend[0],
|
|
|
|
graph: self.rickshawGraph
|
|
|
|
});
|
|
|
|
|
|
|
|
var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({
|
|
|
|
graph: self.rickshawGraph,
|
|
|
|
legend: legend
|
|
|
|
});
|
|
|
|
|
|
|
|
self.changeHandler();
|
|
|
|
};
|
|
|
|
|
|
|
|
function parseGraphOptionsFromUrl() {
|
|
|
|
var hashOptions = window.location.hash.slice(1);
|
|
|
|
if (!hashOptions) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
var optionsJSON = decodeURIComponent(window.location.hash.slice(1));
|
|
|
|
return JSON.parse(optionsJSON);
|
|
|
|
}
|
|
|
|
|
|
|
|
function storeGraphOptionsInUrl(options) {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
jQuery.ajaxSetup({
|
|
|
|
cache: false
|
|
|
|
});
|
|
|
|
|
|
|
|
var options = parseGraphOptionsFromUrl();
|
2013-01-30 08:57:21 -08:00
|
|
|
if (options.length == 0) {
|
|
|
|
options.push({});
|
|
|
|
}
|
2013-01-24 04:55:32 -08:00
|
|
|
for (var i = 0; i < options.length; i++) {
|
|
|
|
addGraph(options[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#add_graph").click(function() { addGraph({}); });
|
|
|
|
}
|
|
|
|
|
|
|
|
$(init);
|