2014-01-30 14:01:22 -06:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'jquery',
|
|
|
|
'kbn',
|
|
|
|
'moment',
|
|
|
|
'underscore'
|
|
|
|
],
|
|
|
|
function (angular, $, kbn, moment, _) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('kibana.directives');
|
|
|
|
|
2014-02-05 02:42:49 -06:00
|
|
|
module.directive('grafanaGraph', function(filterSrv, $rootScope, dashboard) {
|
2014-01-30 14:01:22 -06:00
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
template: '<div> </div>',
|
|
|
|
link: function(scope, elem) {
|
|
|
|
var data, plot;
|
|
|
|
var hiddenData = {};
|
|
|
|
|
|
|
|
scope.$on('refresh',function() {
|
2014-02-05 08:26:34 -06:00
|
|
|
if (scope.otherPanelInFullscreenMode()) { return; }
|
2014-01-30 14:01:22 -06:00
|
|
|
|
|
|
|
scope.get_data();
|
|
|
|
});
|
|
|
|
|
|
|
|
scope.$on('toggleLegend', function(e, alias) {
|
|
|
|
if (hiddenData[alias]) {
|
|
|
|
data.push(hiddenData[alias]);
|
|
|
|
delete hiddenData[alias];
|
|
|
|
}
|
|
|
|
|
|
|
|
render_panel();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Receive render events
|
|
|
|
scope.$on('render',function(event, d) {
|
|
|
|
data = d || data;
|
|
|
|
render_panel();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Re-render if the window is resized
|
|
|
|
angular.element(window).bind('resize', function() {
|
|
|
|
render_panel();
|
|
|
|
});
|
|
|
|
|
2014-02-01 15:06:10 -06:00
|
|
|
function setElementHeight() {
|
|
|
|
try {
|
|
|
|
elem.css({ height: scope.height || scope.panel.height || scope.row.height });
|
|
|
|
return true;
|
|
|
|
} catch(e) { // IE throws errors sometimes
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 14:01:22 -06:00
|
|
|
// Function for rendering panel
|
|
|
|
function render_panel() {
|
2014-02-01 15:06:10 -06:00
|
|
|
if (!data) { return; }
|
2014-02-05 08:26:34 -06:00
|
|
|
if (scope.otherPanelInFullscreenMode()) { return; }
|
2014-02-01 15:06:10 -06:00
|
|
|
if (!setElementHeight()) { return; }
|
|
|
|
|
|
|
|
if (_.isString(data)) {
|
2014-02-05 01:41:46 -06:00
|
|
|
render_panel_as_graphite_png(data);
|
2014-01-30 14:01:22 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-08 07:21:41 -06:00
|
|
|
var panel = scope.panel;
|
|
|
|
|
2014-01-30 14:01:22 -06:00
|
|
|
_.each(_.keys(scope.hiddenSeries), function(seriesAlias) {
|
|
|
|
var dataSeries = _.find(data, function(series) {
|
|
|
|
return series.info.alias === seriesAlias;
|
|
|
|
});
|
|
|
|
if (dataSeries) {
|
|
|
|
hiddenData[dataSeries.info.alias] = dataSeries;
|
|
|
|
data = _.without(data, dataSeries);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set barwidth based on specified interval
|
2014-02-05 06:03:56 -06:00
|
|
|
var barwidth = kbn.interval_to_ms(scope.interval);
|
2014-01-30 14:01:22 -06:00
|
|
|
|
2014-02-08 07:21:41 -06:00
|
|
|
var stack = panel.stack ? true : null;
|
2014-01-30 14:01:22 -06:00
|
|
|
|
|
|
|
// Populate element
|
|
|
|
var options = {
|
|
|
|
legend: { show: false },
|
|
|
|
series: {
|
2014-02-08 07:21:41 -06:00
|
|
|
stackpercent: panel.stack ? panel.percentage : false,
|
|
|
|
stack: panel.percentage ? null : stack,
|
2014-01-30 14:01:22 -06:00
|
|
|
lines: {
|
2014-02-08 07:21:41 -06:00
|
|
|
show: panel.lines,
|
2014-02-07 02:28:54 -06:00
|
|
|
zero: false,
|
2014-02-11 02:15:23 -06:00
|
|
|
fill: panel.fill === 0 ? 0.001 : panel.fill/10,
|
2014-02-08 07:21:41 -06:00
|
|
|
lineWidth: panel.linewidth,
|
|
|
|
steps: panel.steppedLine
|
2014-01-30 14:01:22 -06:00
|
|
|
},
|
|
|
|
bars: {
|
2014-02-08 07:21:41 -06:00
|
|
|
show: panel.bars,
|
2014-01-30 14:01:22 -06:00
|
|
|
fill: 1,
|
|
|
|
barWidth: barwidth/1.5,
|
|
|
|
zero: false,
|
|
|
|
lineWidth: 0
|
|
|
|
},
|
|
|
|
points: {
|
2014-02-08 07:21:41 -06:00
|
|
|
show: panel.points,
|
2014-01-30 14:01:22 -06:00
|
|
|
fill: 1,
|
|
|
|
fillColor: false,
|
2014-02-08 07:21:41 -06:00
|
|
|
radius: panel.pointradius
|
2014-01-30 14:01:22 -06:00
|
|
|
},
|
|
|
|
shadowSize: 1
|
|
|
|
},
|
|
|
|
yaxes: [],
|
|
|
|
xaxis: {
|
2014-02-05 02:42:49 -06:00
|
|
|
timezone: dashboard.current.timezone,
|
2014-02-08 07:21:41 -06:00
|
|
|
show: panel['x-axis'],
|
2014-01-30 14:01:22 -06:00
|
|
|
mode: "time",
|
|
|
|
min: _.isUndefined(scope.range.from) ? null : scope.range.from.getTime(),
|
|
|
|
max: _.isUndefined(scope.range.to) ? null : scope.range.to.getTime(),
|
2014-02-05 06:03:56 -06:00
|
|
|
timeformat: time_format(scope.interval),
|
2014-01-30 14:01:22 -06:00
|
|
|
label: "Datetime",
|
|
|
|
ticks: elem.width()/100
|
|
|
|
},
|
|
|
|
grid: {
|
|
|
|
backgroundColor: null,
|
|
|
|
borderWidth: 0,
|
|
|
|
hoverable: true,
|
|
|
|
color: '#c8c8c8'
|
2014-02-05 01:56:10 -06:00
|
|
|
},
|
|
|
|
selection: {
|
|
|
|
mode: "x",
|
|
|
|
color: '#666'
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-08 07:21:41 -06:00
|
|
|
if (panel.grid.threshold1) {
|
|
|
|
var limit1 = panel.grid.thresholdLine ? panel.grid.threshold1 : (panel.grid.threshold2 || null);
|
|
|
|
options.grid.markings = [];
|
|
|
|
options.grid.markings.push({
|
|
|
|
yaxis: { from: panel.grid.threshold1, to: limit1 },
|
|
|
|
color: panel.grid.threshold1Color
|
|
|
|
});
|
|
|
|
|
|
|
|
if (panel.grid.threshold2) {
|
2014-02-09 03:13:32 -06:00
|
|
|
var limit2;
|
|
|
|
if (panel.grid.thresholdLine) {
|
|
|
|
limit2 = panel.grid.threshold2;
|
|
|
|
} else {
|
|
|
|
limit2 = panel.grid.threshold1 > panel.grid.threshold2 ? -Infinity : +Infinity;
|
|
|
|
}
|
2014-02-08 07:21:41 -06:00
|
|
|
options.grid.markings.push({
|
|
|
|
yaxis: { from: panel.grid.threshold2, to: limit2 },
|
|
|
|
color: panel.grid.threshold2Color
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 15:06:10 -06:00
|
|
|
addAnnotations(options);
|
2014-01-30 14:01:22 -06:00
|
|
|
|
|
|
|
for (var i = 0; i < data.length; i++) {
|
2014-02-08 07:21:41 -06:00
|
|
|
var _d = data[i].getFlotPairs(panel.nullPointMode);
|
2014-01-30 14:01:22 -06:00
|
|
|
data[i].data = _d;
|
|
|
|
}
|
|
|
|
|
|
|
|
configureAxisOptions(data, options);
|
|
|
|
|
|
|
|
plot = $.plot(elem, data, options);
|
|
|
|
|
2014-02-01 15:06:10 -06:00
|
|
|
addAxisLabels();
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:41:46 -06:00
|
|
|
function render_panel_as_graphite_png(url) {
|
|
|
|
url += '&width=' + elem.width();
|
|
|
|
url += '&height=' + elem.css('height').replace('px', '');
|
|
|
|
url += '&bgcolor=1f1f1f'; // @grayDarker & @kibanaPanelBackground
|
|
|
|
url += '&fgcolor=BBBFC2'; // @textColor & @grayLighter
|
|
|
|
url += scope.panel.stack ? '&areaMode=stacked' : '';
|
|
|
|
url += scope.panel.fill !== 0 ? ('&areaAlpha=' + (scope.panel.fill/10).toFixed(1)) : '';
|
|
|
|
url += scope.panel.linewidth !== 0 ? '&lineWidth=' + scope.panel.linewidth : '';
|
2014-02-05 04:14:13 -06:00
|
|
|
url += scope.panel.legend ? '' : '&hideLegend=true';
|
|
|
|
url += scope.panel.grid.min ? '&yMin=' + scope.panel.grid.min : '';
|
|
|
|
url += scope.panel.grid.max ? '&yMax=' + scope.panel.grid.max : '';
|
2014-02-05 04:21:37 -06:00
|
|
|
url += scope.panel['x-axis'] ? '' : '&hideAxes=true';
|
|
|
|
url += scope.panel['y-axis'] ? '' : '&hideYAxis=true';
|
2014-02-01 15:06:10 -06:00
|
|
|
|
2014-02-06 03:19:26 -06:00
|
|
|
switch(scope.panel.y_formats[0]) {
|
2014-02-05 04:30:54 -06:00
|
|
|
case 'bytes':
|
|
|
|
url += '&yUnitSystem=binary';
|
|
|
|
break;
|
|
|
|
case 'short':
|
|
|
|
url += '&yUnitSystem=si';
|
|
|
|
break;
|
|
|
|
case 'none':
|
|
|
|
url += '&yUnitSystem=none';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-01 15:06:10 -06:00
|
|
|
switch(scope.panel.nullPointMode) {
|
|
|
|
case 'connected':
|
2014-02-05 01:41:46 -06:00
|
|
|
url += '&lineMode=connected';
|
2014-02-01 15:06:10 -06:00
|
|
|
break;
|
|
|
|
case 'null':
|
|
|
|
break; // graphite default lineMode
|
|
|
|
case 'null as zero':
|
2014-02-05 01:41:46 -06:00
|
|
|
url += "&drawNullAsZero=true";
|
2014-02-01 15:06:10 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-05 01:41:46 -06:00
|
|
|
url += scope.panel.steppedLine ? '&lineMode=staircase' : '';
|
|
|
|
|
|
|
|
elem.html('<img src="' + url + '"></img>');
|
2014-02-01 15:06:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function addAnnotations(options) {
|
2014-02-09 12:31:06 -06:00
|
|
|
if(!data.annotations || data.annotations.length === 0) {
|
2014-02-07 02:28:54 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.events = {
|
|
|
|
levels: 1,
|
2014-02-09 12:31:06 -06:00
|
|
|
data: data.annotations,
|
2014-02-07 02:28:54 -06:00
|
|
|
types: {
|
|
|
|
'annotation': {
|
|
|
|
level: 1,
|
|
|
|
icon: {
|
|
|
|
icon: "icon-tag icon-flip-vertical",
|
|
|
|
size: 20,
|
|
|
|
color: "#222",
|
|
|
|
outline: "#bbb"
|
2014-02-01 15:06:10 -06:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 02:28:54 -06:00
|
|
|
}
|
|
|
|
};
|
2014-02-01 15:06:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function addAxisLabels() {
|
2014-01-30 14:01:22 -06:00
|
|
|
if (scope.panel.leftYAxisLabel) {
|
|
|
|
elem.css('margin-left', '10px');
|
|
|
|
var yaxisLabel = $("<div class='axisLabel yaxisLabel'></div>")
|
|
|
|
.text(scope.panel.leftYAxisLabel)
|
|
|
|
.appendTo(elem);
|
|
|
|
|
|
|
|
yaxisLabel.css("margin-top", yaxisLabel.width() / 2 - 20);
|
|
|
|
} else if (elem.css('margin-left')) {
|
|
|
|
elem.css('margin-left', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 15:06:10 -06:00
|
|
|
function configureAxisOptions(data, options) {
|
2014-01-30 14:01:22 -06:00
|
|
|
var defaults = {
|
|
|
|
position: 'left',
|
|
|
|
show: scope.panel['y-axis'],
|
2014-02-10 10:52:47 -06:00
|
|
|
min: scope.panel.grid.min,
|
|
|
|
max: scope.panel.percentage && scope.panel.stack ? 100 : scope.panel.grid.max,
|
2014-01-30 14:01:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
options.yaxes.push(defaults);
|
|
|
|
|
|
|
|
if (_.findWhere(data, {yaxis: 2})) {
|
|
|
|
var secondY = _.clone(defaults);
|
|
|
|
secondY.position = 'right';
|
|
|
|
options.yaxes.push(secondY);
|
2014-02-06 03:19:26 -06:00
|
|
|
configureAxisMode(options.yaxes[1], scope.panel.y_formats[1]);
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
|
|
|
|
2014-02-06 03:19:26 -06:00
|
|
|
configureAxisMode(options.yaxes[0], scope.panel.y_formats[0]);
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function configureAxisMode(axis, format) {
|
|
|
|
if (format === 'bytes') {
|
|
|
|
axis.mode = "byte";
|
|
|
|
}
|
|
|
|
if (format === 'short') {
|
|
|
|
axis.tickFormatter = function(val) {
|
2014-02-07 06:27:53 -06:00
|
|
|
return kbn.shortFormat(val, 1);
|
2014-01-30 14:01:22 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (format === 'ms') {
|
|
|
|
axis.tickFormatter = kbn.msFormat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function time_format(interval) {
|
|
|
|
var _int = kbn.interval_to_seconds(interval);
|
|
|
|
if(_int >= 2628000) {
|
|
|
|
return "%Y-%m";
|
|
|
|
}
|
|
|
|
if(_int >= 10000) {
|
2014-02-06 06:26:35 -06:00
|
|
|
return "%m/%d";
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
2014-02-06 03:39:24 -06:00
|
|
|
if(_int >= 3600) {
|
2014-02-06 06:26:35 -06:00
|
|
|
return "%m/%d %H:%M";
|
2014-02-06 03:39:24 -06:00
|
|
|
}
|
2014-02-05 08:39:50 -06:00
|
|
|
if(_int >= 700) {
|
2014-02-06 06:26:35 -06:00
|
|
|
return "%a %H:%M";
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
|
|
|
|
2014-02-05 08:39:50 -06:00
|
|
|
return "%H:%M";
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var $tooltip = $('<div>');
|
|
|
|
|
|
|
|
elem.bind("plothover", function (event, pos, item) {
|
|
|
|
var group, value, timestamp;
|
|
|
|
if (item) {
|
|
|
|
if (item.series.info.alias || scope.panel.tooltip.query_as_alias) {
|
|
|
|
group = '<small style="font-size:0.9em;">' +
|
|
|
|
'<i class="icon-circle" style="color:'+item.series.color+';"></i>' + ' ' +
|
|
|
|
(item.series.info.alias || item.series.info.query)+
|
|
|
|
'</small><br>';
|
|
|
|
} else {
|
|
|
|
group = kbn.query_color_dot(item.series.color, 15) + ' ';
|
|
|
|
}
|
|
|
|
value = (scope.panel.stack && scope.panel.tooltip.value_type === 'individual') ?
|
|
|
|
item.datapoint[1] - item.datapoint[2] :
|
|
|
|
item.datapoint[1];
|
|
|
|
if(item.series.info.y_format === 'bytes') {
|
2014-02-06 03:19:26 -06:00
|
|
|
value = kbn.byteFormat(value, 2);
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
|
|
|
if(item.series.info.y_format === 'short') {
|
2014-02-06 03:19:26 -06:00
|
|
|
value = kbn.shortFormat(value, 2);
|
2014-01-30 14:01:22 -06:00
|
|
|
}
|
|
|
|
if(item.series.info.y_format === 'ms') {
|
|
|
|
value = kbn.msFormat(value);
|
|
|
|
}
|
2014-02-05 02:42:49 -06:00
|
|
|
timestamp = dashboard.current.timezone === 'browser' ?
|
2014-01-30 14:01:22 -06:00
|
|
|
moment(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss') :
|
|
|
|
moment.utc(item.datapoint[0]).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
$tooltip
|
|
|
|
.html(
|
|
|
|
group + value + " @ " + timestamp
|
|
|
|
)
|
|
|
|
.place_tt(pos.pageX, pos.pageY);
|
|
|
|
} else {
|
|
|
|
$tooltip.detach();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
elem.bind("plotselected", function (event, ranges) {
|
|
|
|
filterSrv.setTime({
|
|
|
|
from : moment.utc(ranges.xaxis.from).toDate(),
|
|
|
|
to : moment.utc(ranges.xaxis.to).toDate(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2014-02-09 03:13:32 -06:00
|
|
|
});
|