2014-11-20 10:41:25 -08:00
|
|
|
/*
|
|
|
|
* Functions to make it easier to write prometheus consoles, such
|
|
|
|
* as graphs.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
PromConsole = {};
|
|
|
|
|
|
|
|
PromConsole.NumberFormatter = {};
|
|
|
|
PromConsole.NumberFormatter.prefixesBig = ["k", "M", "G", "T", "P", "E", "Z", "Y"];
|
|
|
|
PromConsole.NumberFormatter.prefixesBig1024 = ["ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"];
|
|
|
|
PromConsole.NumberFormatter.prefixesSmall = ["m", "u", "n", "p", "f", "a", "z", "y"];
|
|
|
|
|
|
|
|
PromConsole._stripTrailingZero = function(x) {
|
|
|
|
if (x.indexOf("e") == -1) {
|
|
|
|
// It's not safe to strip if it's scientific notation.
|
|
|
|
return x.replace(/\.?0*$/, '');
|
|
|
|
}
|
|
|
|
return x;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
// Humanize a number.
|
|
|
|
PromConsole.NumberFormatter.humanize = function(x) {
|
2018-11-24 22:16:48 -08:00
|
|
|
if (x === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
2014-11-20 10:41:25 -08:00
|
|
|
var ret = PromConsole.NumberFormatter._humanize(
|
|
|
|
x, PromConsole.NumberFormatter.prefixesBig,
|
|
|
|
PromConsole.NumberFormatter.prefixesSmall, 1000);
|
|
|
|
x = ret[0];
|
|
|
|
var prefix = ret[1];
|
|
|
|
if (Math.abs(x) < 1) {
|
|
|
|
return x.toExponential(3) + prefix;
|
|
|
|
}
|
|
|
|
return PromConsole._stripTrailingZero(x.toFixed(3)) + prefix;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
// Humanize a number, don't use milli/micro/etc. prefixes.
|
|
|
|
PromConsole.NumberFormatter.humanizeNoSmallPrefix = function(x) {
|
2018-11-24 22:16:48 -08:00
|
|
|
if (x === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
2014-11-20 10:41:25 -08:00
|
|
|
if (Math.abs(x) < 1) {
|
|
|
|
return PromConsole._stripTrailingZero(x.toPrecision(3));
|
|
|
|
}
|
|
|
|
var ret = PromConsole.NumberFormatter._humanize(
|
|
|
|
x, PromConsole.NumberFormatter.prefixesBig,
|
|
|
|
[], 1000);
|
|
|
|
x = ret[0];
|
|
|
|
var prefix = ret[1];
|
|
|
|
return PromConsole._stripTrailingZero(x.toFixed(3)) + prefix;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
// Humanize a number with 1024 as the base, rather than 1000.
|
|
|
|
PromConsole.NumberFormatter.humanize1024 = function(x) {
|
2018-11-24 22:16:48 -08:00
|
|
|
if (x === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
2014-11-20 10:41:25 -08:00
|
|
|
var ret = PromConsole.NumberFormatter._humanize(
|
|
|
|
x, PromConsole.NumberFormatter.prefixesBig1024,
|
|
|
|
[], 1024);
|
|
|
|
x = ret[0];
|
|
|
|
var prefix = ret[1];
|
|
|
|
if (Math.abs(x) < 1) {
|
|
|
|
return x.toExponential(3) + prefix;
|
|
|
|
}
|
|
|
|
return PromConsole._stripTrailingZero(x.toFixed(3)) + prefix;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
// Humanize a number, returning an exact representation.
|
|
|
|
PromConsole.NumberFormatter.humanizeExact = function(x) {
|
2018-11-24 22:16:48 -08:00
|
|
|
if (x === null) {
|
|
|
|
return "null";
|
|
|
|
}
|
2014-11-20 10:41:25 -08:00
|
|
|
var ret = PromConsole.NumberFormatter._humanize(
|
|
|
|
x, PromConsole.NumberFormatter.prefixesBig,
|
|
|
|
PromConsole.NumberFormatter.prefixesSmall, 1000);
|
|
|
|
return ret[0] + ret[1];
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
PromConsole.NumberFormatter._humanize = function(x, prefixesBig, prefixesSmall, factor) {
|
2015-11-11 11:04:37 -08:00
|
|
|
var prefix = "";
|
|
|
|
if (x === 0) {
|
2014-11-20 10:41:25 -08:00
|
|
|
/* Do nothing. */
|
|
|
|
} else if (Math.abs(x) >= 1) {
|
|
|
|
for (var i=0; i < prefixesBig.length && Math.abs(x) >= factor; ++i) {
|
|
|
|
x /= factor;
|
|
|
|
prefix = prefixesBig[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (var i=0; i < prefixesSmall.length && Math.abs(x) < 1; ++i) {
|
|
|
|
x *= factor;
|
|
|
|
prefix = prefixesSmall[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [x, prefix];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PromConsole.TimeControl = function() {
|
|
|
|
document.getElementById("prom_graph_duration_shrink").onclick = this.decreaseDuration.bind(this);
|
|
|
|
document.getElementById("prom_graph_duration_grow").onclick = this.increaseDuration.bind(this);
|
|
|
|
document.getElementById("prom_graph_time_back").onclick = this.decreaseEnd.bind(this);
|
|
|
|
document.getElementById("prom_graph_time_forward").onclick = this.increaseEnd.bind(this);
|
|
|
|
document.getElementById("prom_graph_refresh_button").onclick = this.refresh.bind(this);
|
|
|
|
this.durationElement = document.getElementById("prom_graph_duration");
|
|
|
|
this.endElement = document.getElementById("prom_graph_time_end");
|
|
|
|
this.durationElement.oninput = this.dispatch.bind(this);
|
|
|
|
this.endElement.oninput = this.dispatch.bind(this);
|
|
|
|
this.endElement.oninput = this.dispatch.bind(this);
|
|
|
|
this.refreshValueElement = document.getElementById("prom_graph_refresh_button_value");
|
|
|
|
|
|
|
|
var refreshList = document.getElementById("prom_graph_refresh_intervals");
|
|
|
|
var refreshIntervals = ["Off", "1m", "5m", "15m", "1h"];
|
|
|
|
for (var i=0; i < refreshIntervals.length; ++i) {
|
|
|
|
var li = document.createElement("li");
|
|
|
|
li.onclick = this.setRefresh.bind(this, refreshIntervals[i]);
|
|
|
|
li.textContent = refreshIntervals[i];
|
|
|
|
refreshList.appendChild(li);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.durationElement.value = PromConsole.TimeControl.prototype.getHumanDuration(
|
|
|
|
PromConsole.TimeControl._initialValues.duration);
|
2016-12-18 11:30:52 -08:00
|
|
|
if (!PromConsole.TimeControl._initialValues.endTimeNow) {
|
2014-11-20 10:41:25 -08:00
|
|
|
this.endElement.value = PromConsole.TimeControl.prototype.getHumanDate(
|
|
|
|
new Date(PromConsole.TimeControl._initialValues.endTime * 1000));
|
|
|
|
}
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
PromConsole.TimeControl.timeFactors = {
|
|
|
|
"y": 60 * 60 * 24 * 365,
|
|
|
|
"w": 60 * 60 * 24 * 7,
|
|
|
|
"d": 60 * 60 * 24,
|
|
|
|
"h": 60 * 60,
|
|
|
|
"m": 60,
|
|
|
|
"s": 1
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.stepValues = [
|
|
|
|
"10s", "1m", "5m", "15m", "30m", "1h", "2h", "6h", "12h", "1d", "2d",
|
|
|
|
"1w", "2w", "4w", "8w", "1y", "2y"
|
|
|
|
];
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype._setHash = function() {
|
|
|
|
var duration = this.parseDuration(this.durationElement.value);
|
|
|
|
var endTime = this.getEndDate() / 1000;
|
|
|
|
window.location.hash = "#pctc" + encodeURIComponent(JSON.stringify(
|
|
|
|
{duration: duration, endTime: endTime}));
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
PromConsole.TimeControl._initialValues = function() {
|
|
|
|
var hash = window.location.hash;
|
2016-12-18 11:30:52 -08:00
|
|
|
var values;
|
2015-11-11 11:04:37 -08:00
|
|
|
if (hash.indexOf('#pctc') === 0) {
|
2016-12-18 11:30:52 -08:00
|
|
|
values = JSON.parse(decodeURIComponent(hash.substring(5)));
|
|
|
|
} else {
|
|
|
|
values = {duration: 3600, endTime: 0};
|
|
|
|
}
|
|
|
|
if (values.endTime == 0) {
|
|
|
|
values.endTime = new Date().getTime() / 1000;
|
|
|
|
values.endTimeNow = true;
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
2016-12-18 11:30:52 -08:00
|
|
|
return values;
|
2014-11-20 10:41:25 -08:00
|
|
|
}();
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.parseDuration = function(durationText) {
|
|
|
|
var durationRE = new RegExp("^([0-9]+)([ywdhms]?)$");
|
|
|
|
var matches = durationText.match(durationRE);
|
|
|
|
if (!matches) { return 3600; }
|
|
|
|
var value = parseInt(matches[1]);
|
|
|
|
var unit = matches[2] || 's';
|
|
|
|
return value * PromConsole.TimeControl.timeFactors[unit];
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.getHumanDuration = function(duration) {
|
|
|
|
var units = [];
|
|
|
|
for (var key in PromConsole.TimeControl.timeFactors) {
|
|
|
|
units.push([PromConsole.TimeControl.timeFactors[key], key]);
|
|
|
|
}
|
2015-11-11 11:04:37 -08:00
|
|
|
units.sort(function(a, b) { return b[0] - a[0]; });
|
2014-11-20 10:41:25 -08:00
|
|
|
for (var i = 0; i < units.length; ++i) {
|
2015-11-11 11:04:37 -08:00
|
|
|
if (duration % units[i][0] === 0) {
|
2014-11-20 10:41:25 -08:00
|
|
|
return (duration / units[i][0]) + units[i][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return duration;
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.increaseDuration = function() {
|
|
|
|
var durationSeconds = this.parseDuration(this.durationElement.value);
|
|
|
|
for (var i = 0; i < PromConsole.TimeControl.stepValues.length; i++) {
|
|
|
|
if (durationSeconds < this.parseDuration(PromConsole.TimeControl.stepValues[i])) {
|
|
|
|
this.setDuration(PromConsole.TimeControl.stepValues[i]);
|
|
|
|
this.dispatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.decreaseDuration = function() {
|
|
|
|
var durationSeconds = this.parseDuration(this.durationElement.value);
|
|
|
|
for (var i = PromConsole.TimeControl.stepValues.length - 1; i >= 0; i--) {
|
|
|
|
if (durationSeconds > this.parseDuration(PromConsole.TimeControl.stepValues[i])) {
|
|
|
|
this.setDuration(PromConsole.TimeControl.stepValues[i]);
|
|
|
|
this.dispatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.setDuration = function(duration) {
|
|
|
|
this.durationElement.value = duration;
|
|
|
|
this._setHash();
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.getEndDate = function() {
|
2015-11-11 11:04:37 -08:00
|
|
|
if (this.endElement.value === '') {
|
2014-11-20 10:41:25 -08:00
|
|
|
return null;
|
|
|
|
}
|
2016-06-09 09:52:03 -07:00
|
|
|
var dateParts = this.endElement.value.split(/[- :]/)
|
|
|
|
return Date.UTC(dateParts[0], dateParts[1] - 1, dateParts[2], dateParts[3], dateParts[4]);
|
2014-11-20 10:41:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.getOrSetEndDate = function() {
|
|
|
|
var date = this.getEndDate();
|
|
|
|
if (date) {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
date = new Date();
|
|
|
|
this.setEndDate(date);
|
2016-06-09 09:52:03 -07:00
|
|
|
return date.getTime();
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.getHumanDate = function(date) {
|
2016-06-09 09:52:03 -07:00
|
|
|
var hours = date.getUTCHours() < 10 ? '0' + date.getUTCHours() : date.getUTCHours();
|
|
|
|
var minutes = date.getUTCMinutes() < 10 ? '0' + date.getUTCMinutes() : date.getUTCMinutes();
|
|
|
|
return date.getUTCFullYear() + "-" + (date.getUTCMonth()+1) + "-" + date.getUTCDate() + " " +
|
2014-11-20 10:41:25 -08:00
|
|
|
hours + ":" + minutes;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.setEndDate = function(date) {
|
|
|
|
this.setRefresh("Off");
|
|
|
|
this.endElement.value = this.getHumanDate(date);
|
|
|
|
this._setHash();
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.increaseEnd = function() {
|
|
|
|
// Increase duration 25% range & convert ms to s.
|
|
|
|
this.setEndDate(new Date(this.getOrSetEndDate() + this.parseDuration(this.durationElement.value) * 1000/4 ));
|
|
|
|
this.dispatch();
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.decreaseEnd = function() {
|
|
|
|
this.setEndDate(new Date(this.getOrSetEndDate() - this.parseDuration(this.durationElement.value) * 1000/4 ));
|
|
|
|
this.dispatch();
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.refresh = function() {
|
|
|
|
this.endElement.value = '';
|
|
|
|
this._setHash();
|
|
|
|
this.dispatch();
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.dispatch = function() {
|
|
|
|
var durationSeconds = this.parseDuration(this.durationElement.value);
|
|
|
|
var end = this.getEndDate();
|
|
|
|
if (end === null) {
|
|
|
|
end = new Date().getTime();
|
|
|
|
}
|
|
|
|
for (var i = 0; i< PromConsole._graph_registry.length; i++) {
|
|
|
|
var graph = PromConsole._graph_registry[i];
|
|
|
|
graph.params.duration = durationSeconds;
|
|
|
|
graph.params.endTime = end / 1000;
|
|
|
|
graph.dispatch();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype._refreshInterval = null;
|
|
|
|
|
|
|
|
PromConsole.TimeControl.prototype.setRefresh = function(duration) {
|
|
|
|
if (this._refreshInterval !== null) {
|
|
|
|
window.clearInterval(this._refreshInterval);
|
|
|
|
this._refreshInterval = null;
|
|
|
|
}
|
|
|
|
if (duration != "Off") {
|
2015-11-11 11:04:37 -08:00
|
|
|
if (this.endElement.value !== '') {
|
2014-11-20 10:41:25 -08:00
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
var durationSeconds = this.parseDuration(duration);
|
|
|
|
this._refreshInterval = window.setInterval(this.dispatch.bind(this), durationSeconds * 1000);
|
|
|
|
}
|
|
|
|
this.refreshValueElement.textContent = duration;
|
|
|
|
};
|
|
|
|
|
|
|
|
// List of all graphs, used by time controls.
|
|
|
|
PromConsole._graph_registry = [];
|
|
|
|
|
|
|
|
PromConsole.graphDefaults = {
|
|
|
|
expr: null, // Expression to graph. Can be a list of strings.
|
|
|
|
node: null, // DOM node to place graph under.
|
|
|
|
// How long the graph is over, in seconds.
|
|
|
|
duration: PromConsole.TimeControl._initialValues.duration,
|
|
|
|
// The unixtime the graph ends at.
|
|
|
|
endTime: PromConsole.TimeControl._initialValues.endTime,
|
|
|
|
width: null, // Height of the graph div, excluding titles and legends.
|
|
|
|
// Defaults to auto-detection.
|
|
|
|
height: 200, // Height of the graph div, excluding titles and legends.
|
|
|
|
min: "auto", // Minimum Y-axis value, defaults to lowest data value.
|
|
|
|
max: undefined, // Maximum Y-axis value, defaults to highest data value.
|
|
|
|
renderer: 'line', // Type of graphs, options are 'line' and 'area'.
|
|
|
|
name: null, // What to call plots, defaults to trying to do
|
|
|
|
// something reasonable.
|
|
|
|
// If a string, it'll use that. [[ label ]] will be substituted.
|
|
|
|
// If a function it'll be called with a map of keys to values,
|
|
|
|
// and should return the name to use.
|
2015-08-11 12:03:20 -07:00
|
|
|
// Can be a list of strings/functions, each element
|
|
|
|
// will be applied to the plots from the corresponding
|
|
|
|
// element of the expr list.
|
2014-11-20 10:41:25 -08:00
|
|
|
xTitle: "Time", // The title of the x axis.
|
|
|
|
yUnits: "", // The units of the y axis.
|
|
|
|
yTitle: "", // The title of the y axis.
|
|
|
|
// Number formatter for y axis.
|
|
|
|
yAxisFormatter: PromConsole.NumberFormatter.humanize,
|
2021-08-19 23:24:31 -07:00
|
|
|
// Number of ticks (horizontal lines) for the y axis
|
|
|
|
yAxisTicks: null,
|
2014-11-20 10:41:25 -08:00
|
|
|
// Number formatter for y values hover detail.
|
|
|
|
yHoverFormatter: PromConsole.NumberFormatter.humanizeExact,
|
2017-07-04 07:30:12 -07:00
|
|
|
// Color scheme to be used by the plots. Can be either a list of hex color
|
|
|
|
// codes or one of the color scheme names supported by Rickshaw.
|
|
|
|
colorScheme: null,
|
2014-11-20 10:41:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.Graph = function(params) {
|
|
|
|
for (var k in PromConsole.graphDefaults) {
|
|
|
|
if (!(k in params)) {
|
|
|
|
params[k] = PromConsole.graphDefaults[k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof params.expr == "string") {
|
2015-08-11 12:03:20 -07:00
|
|
|
params.expr = [params.expr];
|
|
|
|
}
|
|
|
|
if (typeof params.name == "string" || typeof params.name == "function") {
|
|
|
|
var name = [];
|
|
|
|
for (var i = 0; i < params.expr.length; i++) {
|
2015-11-11 11:04:37 -08:00
|
|
|
name.push(params.name);
|
2015-08-11 12:03:20 -07:00
|
|
|
}
|
|
|
|
params.name = name;
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.params = params;
|
|
|
|
this.rendered_data = null;
|
2017-02-18 02:03:32 -08:00
|
|
|
// Keep a reference so that further updates (e.g. annotations) can be made
|
|
|
|
// by the user in their templates.
|
|
|
|
this.rickshawGraph = null;
|
2014-11-20 10:41:25 -08:00
|
|
|
PromConsole._graph_registry.push(this);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Table layout:
|
|
|
|
* | yTitle | Graph |
|
|
|
|
* | | xTitle |
|
|
|
|
* | /graph | Legend |
|
|
|
|
*/
|
|
|
|
var table = document.createElement("table");
|
|
|
|
table.className = "prom_graph_table";
|
|
|
|
params.node.appendChild(table);
|
|
|
|
var tr = document.createElement("tr");
|
|
|
|
table.appendChild(tr);
|
|
|
|
var yTitleTd = document.createElement("td");
|
|
|
|
tr.appendChild(yTitleTd);
|
|
|
|
var yTitleDiv = document.createElement("td");
|
|
|
|
yTitleTd.appendChild(yTitleDiv);
|
|
|
|
yTitleDiv.className = "prom_graph_ytitle";
|
|
|
|
yTitleDiv.textContent = params.yTitle + (params.yUnits ? " (" + params.yUnits.trim() + ")" : "");
|
|
|
|
|
|
|
|
this.graphTd = document.createElement("td");
|
|
|
|
tr.appendChild(this.graphTd);
|
|
|
|
this.graphTd.className = "rickshaw_graph";
|
|
|
|
this.graphTd.width = params.width;
|
|
|
|
this.graphTd.height = params.height;
|
|
|
|
|
|
|
|
tr = document.createElement("tr");
|
|
|
|
table.appendChild(tr);
|
|
|
|
tr.appendChild(document.createElement("td"));
|
|
|
|
var xTitleTd = document.createElement("td");
|
|
|
|
tr.appendChild(xTitleTd);
|
|
|
|
xTitleTd.className = "prom_graph_xtitle";
|
|
|
|
xTitleTd.textContent = params.xTitle;
|
|
|
|
|
|
|
|
tr = document.createElement("tr");
|
|
|
|
table.appendChild(tr);
|
|
|
|
var graphLinkTd = document.createElement("td");
|
|
|
|
tr.appendChild(graphLinkTd);
|
|
|
|
var graphLinkA = document.createElement("a");
|
|
|
|
graphLinkTd.appendChild(graphLinkA);
|
|
|
|
graphLinkA.className = "prom_graph_link";
|
|
|
|
graphLinkA.textContent = "+";
|
|
|
|
graphLinkA.href = PromConsole._graphsToSlashGraphURL(params.expr);
|
|
|
|
var legendTd = document.createElement("td");
|
|
|
|
tr.appendChild(legendTd);
|
|
|
|
this.legendDiv = document.createElement("div");
|
|
|
|
legendTd.width = params.width;
|
|
|
|
legendTd.appendChild(this.legendDiv);
|
|
|
|
|
|
|
|
window.addEventListener('resize', function() {
|
|
|
|
if(this.rendered_data !== null) {
|
|
|
|
this._render(this.rendered_data);
|
|
|
|
}
|
2015-11-11 11:04:37 -08:00
|
|
|
}.bind(this));
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
this.dispatch();
|
|
|
|
};
|
|
|
|
|
2015-03-13 13:08:22 -07:00
|
|
|
PromConsole.Graph.prototype._parseValue = function(value) {
|
2015-03-16 14:50:35 -07:00
|
|
|
var val = parseFloat(value);
|
|
|
|
if (isNaN(val)) {
|
|
|
|
// "+Inf", "-Inf", "+Inf" will be parsed into NaN by parseFloat(). The
|
|
|
|
// can't be graphed, so show them as gaps (null).
|
2015-11-11 11:04:37 -08:00
|
|
|
return null;
|
2015-03-13 13:08:22 -07:00
|
|
|
}
|
2015-03-16 14:50:35 -07:00
|
|
|
return val;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2015-03-13 13:08:22 -07:00
|
|
|
|
2015-03-22 15:47:00 -07:00
|
|
|
PromConsole.Graph.prototype._escapeHTML = function(string) {
|
|
|
|
var entityMap = {
|
|
|
|
"&": "&",
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
'"': '"',
|
|
|
|
"'": ''',
|
|
|
|
"/": '/'
|
|
|
|
};
|
|
|
|
|
|
|
|
return string.replace(/[&<>"'\/]/g, function (s) {
|
|
|
|
return entityMap[s];
|
|
|
|
});
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2015-03-22 15:47:00 -07:00
|
|
|
|
2014-11-20 10:41:25 -08:00
|
|
|
PromConsole.Graph.prototype._render = function(data) {
|
2015-03-16 14:50:35 -07:00
|
|
|
var self = this;
|
2017-07-04 07:30:12 -07:00
|
|
|
var palette = new Rickshaw.Color.Palette({scheme: this.params.colorScheme});
|
2014-11-20 10:41:25 -08:00
|
|
|
var series = [];
|
|
|
|
|
|
|
|
// This will be used on resize.
|
|
|
|
this.rendered_data = data;
|
|
|
|
|
2015-08-11 12:03:20 -07:00
|
|
|
|
|
|
|
var nameFuncs = [];
|
2014-11-20 10:41:25 -08:00
|
|
|
if (this.params.name === null) {
|
2015-08-11 12:03:20 -07:00
|
|
|
var chooser = PromConsole._chooseNameFunction(data);
|
|
|
|
for (var i = 0; i < this.params.expr.length; i++) {
|
|
|
|
nameFuncs.push(chooser);
|
|
|
|
}
|
2014-11-20 10:41:25 -08:00
|
|
|
} else {
|
2015-08-11 12:03:20 -07:00
|
|
|
for (var i = 0; i < this.params.name.length; i++) {
|
|
|
|
if (typeof this.params.name[i] == "string") {
|
|
|
|
nameFuncs.push(function(i, metric) {
|
|
|
|
return PromConsole._interpolateName(this.params.name[i], metric);
|
|
|
|
}.bind(this, i));
|
|
|
|
} else {
|
|
|
|
nameFuncs.push(this.params.name[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the data into the right format.
|
2015-01-26 02:51:38 -08:00
|
|
|
var seriesLen = 0;
|
2017-05-26 08:17:48 -07:00
|
|
|
|
2014-11-20 10:41:25 -08:00
|
|
|
for (var e = 0; e < data.length; e++) {
|
2016-07-18 12:28:21 -07:00
|
|
|
for (var i = 0; i < data[e].data.result.length; i++) {
|
2017-05-26 08:17:48 -07:00
|
|
|
series[seriesLen] = {
|
2016-07-18 12:28:21 -07:00
|
|
|
data: data[e].data.result[i].values.map(function(s) { return {x: s[0], y: self._parseValue(s[1])}; }),
|
2014-11-20 10:41:25 -08:00
|
|
|
color: palette.color(),
|
2016-07-18 12:28:21 -07:00
|
|
|
name: self._escapeHTML(nameFuncs[e](data[e].data.result[i].metric)),
|
2014-11-20 10:41:25 -08:00
|
|
|
};
|
2017-05-26 08:17:48 -07:00
|
|
|
// Insert nulls for all missing steps.
|
|
|
|
var newSeries = [];
|
|
|
|
var pos = 0;
|
|
|
|
var start = self.params.endTime - self.params.duration;
|
2020-08-11 04:33:40 -07:00
|
|
|
var step = Math.floor(self.params.duration / this.graphTd.offsetWidth * 1000) / 1000;
|
2017-05-26 08:17:48 -07:00
|
|
|
for (var t = start; t <= self.params.endTime; t += step) {
|
|
|
|
// Allow for floating point inaccuracy.
|
|
|
|
if (series[seriesLen].data.length > pos && series[seriesLen].data[pos].x < t + step / 100) {
|
|
|
|
newSeries.push(series[seriesLen].data[pos]);
|
|
|
|
pos++;
|
|
|
|
} else {
|
|
|
|
newSeries.push({x: t, y: null});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
series[seriesLen].data = newSeries;
|
|
|
|
seriesLen++;
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this._clearGraph();
|
|
|
|
if (!series.length) {
|
|
|
|
var errorText = document.createElement("div");
|
|
|
|
errorText.className = 'prom_graph_error';
|
|
|
|
errorText.textContent = 'No timeseries returned';
|
|
|
|
this.graphTd.appendChild(errorText);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Render.
|
|
|
|
var graph = new Rickshaw.Graph({
|
|
|
|
interpolation: "linear",
|
|
|
|
width: this.graphTd.offsetWidth,
|
|
|
|
height: this.params.height,
|
|
|
|
element: this.graphTd,
|
|
|
|
renderer: this.params.renderer,
|
|
|
|
max: this.params.max,
|
|
|
|
min: this.params.min,
|
|
|
|
series: series
|
|
|
|
});
|
|
|
|
var hoverDetail = new Rickshaw.Graph.HoverDetail({
|
|
|
|
graph: graph,
|
|
|
|
onRender: function() {
|
|
|
|
var xLabel = this.element.getElementsByClassName("x_label")[0];
|
|
|
|
var item = this.element.getElementsByClassName("item")[0];
|
2015-11-11 11:04:37 -08:00
|
|
|
if (xLabel.offsetWidth + xLabel.offsetLeft + this.element.offsetLeft > graph.element.offsetWidth ||
|
|
|
|
item.offsetWidth + item.offsetLeft + this.element.offsetLeft > graph.element.offsetWidth) {
|
2014-11-20 10:41:25 -08:00
|
|
|
xLabel.classList.add("prom_graph_hover_flipped");
|
|
|
|
item.classList.add("prom_graph_hover_flipped");
|
|
|
|
} else {
|
|
|
|
xLabel.classList.remove("prom_graph_hover_flipped");
|
|
|
|
item.classList.remove("prom_graph_hover_flipped");
|
|
|
|
}
|
|
|
|
},
|
2015-11-11 11:04:37 -08:00
|
|
|
yFormatter: function(y) {
|
2017-05-26 08:17:48 -07:00
|
|
|
if (y === null) {
|
|
|
|
return "";
|
|
|
|
}
|
2015-11-11 11:04:37 -08:00
|
|
|
return this.params.yHoverFormatter(y) + this.params.yUnits;
|
|
|
|
}.bind(this)
|
2014-11-20 10:41:25 -08:00
|
|
|
});
|
|
|
|
var yAxis = new Rickshaw.Graph.Axis.Y({
|
|
|
|
graph: graph,
|
2021-08-19 23:24:31 -07:00
|
|
|
tickFormat: this.params.yAxisFormatter,
|
|
|
|
ticks: this.params.yAxisTicks
|
2014-11-20 10:41:25 -08:00
|
|
|
});
|
|
|
|
var xAxis = new Rickshaw.Graph.Axis.Time({
|
|
|
|
graph: graph,
|
|
|
|
});
|
|
|
|
var legend = new Rickshaw.Graph.Legend({
|
|
|
|
graph: graph,
|
|
|
|
element: this.legendDiv
|
|
|
|
});
|
|
|
|
xAxis.render();
|
|
|
|
yAxis.render();
|
|
|
|
graph.render();
|
2017-02-18 02:03:32 -08:00
|
|
|
|
|
|
|
this.rickshawGraph = graph;
|
2014-11-20 10:41:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
PromConsole.Graph.prototype._clearGraph = function() {
|
|
|
|
while (this.graphTd.lastChild) {
|
|
|
|
this.graphTd.removeChild(this.graphTd.lastChild);
|
|
|
|
}
|
|
|
|
while (this.legendDiv.lastChild) {
|
|
|
|
this.legendDiv.removeChild(this.legendDiv.lastChild);
|
|
|
|
}
|
2017-02-18 02:03:32 -08:00
|
|
|
this.rickshawGraph = null;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
2015-11-11 11:04:37 -08:00
|
|
|
PromConsole.Graph.prototype._xhrs = [];
|
2014-11-20 10:41:25 -08:00
|
|
|
|
2017-03-01 14:37:50 -08:00
|
|
|
PromConsole.Graph.prototype.buildQueryUrl = function(expr) {
|
|
|
|
var p = this.params;
|
|
|
|
return PATH_PREFIX + "/api/v1/query_range?query=" +
|
|
|
|
encodeURIComponent(expr) +
|
2020-08-11 04:33:40 -07:00
|
|
|
"&step=" + Math.floor(p.duration / this.graphTd.offsetWidth * 1000) / 1000 +
|
2017-03-01 14:37:50 -08:00
|
|
|
"&start=" + (p.endTime - p.duration) + "&end=" + p.endTime;
|
|
|
|
};
|
|
|
|
|
2014-11-20 10:41:25 -08:00
|
|
|
PromConsole.Graph.prototype.dispatch = function() {
|
|
|
|
for (var j = 0; j < this._xhrs.length; j++) {
|
|
|
|
this._xhrs[j].abort();
|
|
|
|
}
|
|
|
|
var all_data = new Array(this.params.expr.length);
|
|
|
|
this._xhrs = new Array(this.params.expr.length);
|
|
|
|
var pending_requests = this.params.expr.length;
|
|
|
|
for (var i = 0; i < this.params.expr.length; ++i) {
|
|
|
|
var endTime = this.params.endTime;
|
2017-03-01 14:37:50 -08:00
|
|
|
var url = this.buildQueryUrl(this.params.expr[i]);
|
2014-11-20 10:41:25 -08:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('get', url, true);
|
|
|
|
xhr.responseType = 'json';
|
|
|
|
xhr.onerror = function(xhr, i) {
|
|
|
|
this._clearGraph();
|
|
|
|
var errorText = document.createElement("div");
|
|
|
|
errorText.className = 'prom_graph_error';
|
|
|
|
errorText.textContent = 'Error loading data';
|
|
|
|
this.graphTd.appendChild(errorText);
|
|
|
|
console.log('Error loading data for ' + this.params.expr[i]);
|
|
|
|
pending_requests = 0;
|
|
|
|
// onabort gets any aborts.
|
|
|
|
for (var j = 0; j < pending_requests; j++) {
|
|
|
|
this._xhrs[j].abort();
|
|
|
|
}
|
2015-11-11 11:04:37 -08:00
|
|
|
}.bind(this, xhr, i);
|
2014-11-20 10:41:25 -08:00
|
|
|
xhr.onload = function(xhr, i) {
|
2015-11-11 11:04:37 -08:00
|
|
|
if (pending_requests === 0) {
|
2014-11-20 10:41:25 -08:00
|
|
|
// Got an error before this success.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var data = xhr.response;
|
2016-06-01 07:50:37 -07:00
|
|
|
if (typeof data !== "object") {
|
|
|
|
data = JSON.parse(xhr.responseText);
|
|
|
|
}
|
2014-11-20 10:41:25 -08:00
|
|
|
pending_requests -= 1;
|
|
|
|
all_data[i] = data;
|
2015-11-11 11:04:37 -08:00
|
|
|
if (pending_requests === 0) {
|
2014-11-20 10:41:25 -08:00
|
|
|
this._xhrs = [];
|
|
|
|
this._render(all_data);
|
|
|
|
}
|
2015-11-11 11:04:37 -08:00
|
|
|
}.bind(this, xhr, i);
|
2014-11-20 10:41:25 -08:00
|
|
|
xhr.send();
|
|
|
|
this._xhrs[i] = xhr;
|
|
|
|
}
|
|
|
|
|
|
|
|
var loadingImg = document.createElement("img");
|
2020-11-03 05:51:48 -08:00
|
|
|
loadingImg.src = PATH_PREFIX + '/classic/static/img/ajax-loader.gif';
|
2014-11-20 10:41:25 -08:00
|
|
|
loadingImg.alt = 'Loading...';
|
|
|
|
loadingImg.className = 'prom_graph_loading';
|
|
|
|
this.graphTd.appendChild(loadingImg);
|
|
|
|
};
|
|
|
|
|
2015-03-22 15:47:00 -07:00
|
|
|
// Substitute the value of 'label' for [[ label ]].
|
2014-11-20 10:41:25 -08:00
|
|
|
PromConsole._interpolateName = function(name, metric) {
|
|
|
|
var re = /(.*?)\[\[\s*(\w+)+\s*\]\](.*?)/g;
|
|
|
|
var result = '';
|
|
|
|
while (match = re.exec(name)) {
|
2015-11-11 11:04:37 -08:00
|
|
|
result = result + match[1] + metric[match[2]] + match[3];
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
|
|
|
if (!result) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
return result;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
// Given the data returned by the API, return an appropriate function
|
|
|
|
// to return plot names.
|
|
|
|
PromConsole._chooseNameFunction = function(data) {
|
|
|
|
// By default, use the full metric name.
|
|
|
|
var nameFunc = function (metric) {
|
2019-08-31 07:33:31 -07:00
|
|
|
var name = metric.__name__ + "{";
|
2014-11-20 10:41:25 -08:00
|
|
|
for (var label in metric) {
|
|
|
|
if (label.substring(0,2) == "__") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
name += label + "='" + metric[label] + "',";
|
|
|
|
}
|
|
|
|
return name + "}";
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2020-11-03 05:51:48 -08:00
|
|
|
|
2014-11-20 10:41:25 -08:00
|
|
|
// If only one label varies, use that value.
|
|
|
|
var labelValues = {};
|
|
|
|
for (var e = 0; e < data.length; e++) {
|
2016-07-18 12:28:21 -07:00
|
|
|
for (var i = 0; i < data[e].data.result.length; i++) {
|
|
|
|
for (var label in data[e].data.result[i].metric) {
|
2014-11-20 10:41:25 -08:00
|
|
|
if (!(label in labelValues)) {
|
|
|
|
labelValues[label] = {};
|
|
|
|
}
|
2016-07-18 12:28:21 -07:00
|
|
|
labelValues[label][data[e].data.result[i].metric[label]] = 1;
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-03 05:51:48 -08:00
|
|
|
|
2014-11-20 10:41:25 -08:00
|
|
|
var multiValueLabels = [];
|
|
|
|
for (var label in labelValues) {
|
|
|
|
if (Object.keys(labelValues[label]).length > 1) {
|
|
|
|
multiValueLabels.push(label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (multiValueLabels.length == 1) {
|
|
|
|
nameFunc = function(metric) {
|
|
|
|
return metric[multiValueLabels[0]];
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
|
|
|
return nameFunc;
|
2015-11-11 11:04:37 -08:00
|
|
|
};
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
// Given a list of expressions, produce the /graph url for them.
|
|
|
|
PromConsole._graphsToSlashGraphURL = function(exprs) {
|
|
|
|
var data = [];
|
|
|
|
for (var i = 0; i < exprs.length; ++i) {
|
2014-12-26 08:40:06 -08:00
|
|
|
data.push({'expr': exprs[i], 'tab': 0});
|
2014-11-20 10:41:25 -08:00
|
|
|
}
|
2015-06-01 03:23:13 -07:00
|
|
|
return PATH_PREFIX + '/graph#' + encodeURIComponent(JSON.stringify(data));
|
2014-11-20 10:41:25 -08:00
|
|
|
|
|
|
|
};
|