2013-09-13 15:52:13 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
2013-09-16 12:48:08 -05:00
|
|
|
'jquery',
|
|
|
|
'kbn',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2014-08-18 06:17:18 -05:00
|
|
|
'moment',
|
2014-08-05 03:15:27 -05:00
|
|
|
'../timer',
|
2013-09-13 15:52:13 -05:00
|
|
|
],
|
2014-08-18 06:17:18 -05:00
|
|
|
function (angular, $, kbn, _, moment) {
|
2013-09-13 15:52:13 -05:00
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.services');
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2014-08-13 08:02:57 -05:00
|
|
|
module.factory('dashboardSrv', function(timer, $rootScope, $timeout) {
|
2014-06-07 12:43:15 -05:00
|
|
|
|
|
|
|
function DashboardModel (data) {
|
2014-06-08 13:46:30 -05:00
|
|
|
|
2014-06-08 09:08:12 -05:00
|
|
|
if (!data) {
|
|
|
|
data = {};
|
2014-06-08 09:09:36 -05:00
|
|
|
}
|
2014-06-08 09:08:12 -05:00
|
|
|
|
2014-08-21 14:59:23 -05:00
|
|
|
this.id = data.id || null;
|
2014-08-05 03:15:27 -05:00
|
|
|
this.title = data.title || 'No Title';
|
2014-08-21 14:59:23 -05:00
|
|
|
this.originalTitle = this.title;
|
2014-06-07 12:43:15 -05:00
|
|
|
this.tags = data.tags || [];
|
|
|
|
this.style = data.style || "dark";
|
2014-07-21 11:49:30 -05:00
|
|
|
this.timezone = data.timezone || 'browser';
|
2014-06-07 12:43:15 -05:00
|
|
|
this.editable = data.editble || true;
|
|
|
|
this.rows = data.rows || [];
|
|
|
|
this.pulldowns = data.pulldowns || [];
|
|
|
|
this.nav = data.nav || [];
|
2014-08-05 03:15:27 -05:00
|
|
|
this.time = data.time || { from: 'now-6h', to: 'now' };
|
|
|
|
this.templating = data.templating || { list: [] };
|
2014-08-07 00:20:56 -05:00
|
|
|
this.refresh = data.refresh;
|
2014-08-12 11:21:48 -05:00
|
|
|
this.version = data.version || 0;
|
2014-06-07 12:43:15 -05:00
|
|
|
|
|
|
|
if (this.nav.length === 0) {
|
|
|
|
this.nav.push({ type: 'timepicker' });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.findWhere(this.pulldowns, {type: 'filtering'})) {
|
|
|
|
this.pulldowns.push({ type: 'filtering', enable: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.findWhere(this.pulldowns, {type: 'annotations'})) {
|
|
|
|
this.pulldowns.push({ type: 'annotations', enable: false });
|
|
|
|
}
|
|
|
|
|
2014-08-05 03:15:27 -05:00
|
|
|
this.updateSchema(data);
|
2014-06-07 12:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var p = DashboardModel.prototype;
|
|
|
|
|
2014-08-13 09:35:34 -05:00
|
|
|
p.getNextPanelId = function() {
|
|
|
|
var i, j, row, panel, max = 0;
|
|
|
|
for (i = 0; i < this.rows.length; i++) {
|
2014-08-13 10:20:54 -05:00
|
|
|
row = this.rows[i];
|
|
|
|
for (j = 0; j < row.panels.length; j++) {
|
|
|
|
panel = row.panels[j];
|
|
|
|
if (panel.id > max) { max = panel.id; }
|
|
|
|
}
|
2014-08-13 09:35:34 -05:00
|
|
|
}
|
|
|
|
return max + 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
p.rowSpan = function(row) {
|
|
|
|
return _.reduce(row.panels, function(p,v) {
|
|
|
|
return p + v.span;
|
|
|
|
},0);
|
|
|
|
};
|
|
|
|
|
|
|
|
p.add_panel = function(panel, row) {
|
|
|
|
var rowSpan = this.rowSpan(row);
|
|
|
|
var panelCount = row.panels.length;
|
|
|
|
var space = (12 - rowSpan) - panel.span;
|
|
|
|
panel.id = this.getNextPanelId();
|
|
|
|
|
|
|
|
// try to make room of there is no space left
|
|
|
|
if (space <= 0) {
|
|
|
|
if (panelCount === 1) {
|
|
|
|
row.panels[0].span = 6;
|
|
|
|
panel.span = 6;
|
|
|
|
}
|
|
|
|
else if (panelCount === 2) {
|
|
|
|
row.panels[0].span = 4;
|
|
|
|
row.panels[1].span = 4;
|
|
|
|
panel.span = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
row.panels.push(panel);
|
|
|
|
};
|
|
|
|
|
|
|
|
p.duplicatePanel = function(panel, row) {
|
|
|
|
var rowIndex = _.indexOf(this.rows, row);
|
|
|
|
var newPanel = angular.copy(panel);
|
|
|
|
newPanel.id = this.getNextPanelId();
|
|
|
|
|
|
|
|
while(rowIndex < this.rows.length) {
|
|
|
|
var currentRow = this.rows[rowIndex];
|
|
|
|
if (this.rowSpan(currentRow) <= 9) {
|
|
|
|
currentRow.panels.push(newPanel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rowIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newRow = angular.copy(row);
|
|
|
|
newRow.panels = [newPanel];
|
|
|
|
this.rows.push(newRow);
|
|
|
|
};
|
|
|
|
|
2014-08-18 06:17:18 -05:00
|
|
|
p.formatDate = function(date, format) {
|
|
|
|
format = format || 'YYYY-MM-DD HH:mm:ss';
|
|
|
|
|
|
|
|
return this.timezone === 'browser' ?
|
|
|
|
moment(date).format(format) :
|
|
|
|
moment.utc(date).format(format);
|
|
|
|
};
|
|
|
|
|
2014-06-07 14:00:05 -05:00
|
|
|
p.emit_refresh = function() {
|
2014-06-07 12:43:15 -05:00
|
|
|
$rootScope.$broadcast('refresh');
|
|
|
|
};
|
|
|
|
|
2014-06-22 12:01:04 -05:00
|
|
|
p.start_scheduled_refresh = function (after_ms) {
|
|
|
|
this.cancel_scheduled_refresh();
|
|
|
|
this.refresh_timer = timer.register($timeout(function () {
|
|
|
|
this.start_scheduled_refresh(after_ms);
|
|
|
|
this.emit_refresh();
|
|
|
|
}.bind(this), after_ms));
|
|
|
|
};
|
|
|
|
|
|
|
|
p.cancel_scheduled_refresh = function () {
|
|
|
|
timer.cancel(this.refresh_timer);
|
|
|
|
};
|
2014-06-07 12:43:15 -05:00
|
|
|
|
2014-06-22 12:01:04 -05:00
|
|
|
p.set_interval = function (interval) {
|
|
|
|
this.refresh = interval;
|
2014-06-07 12:43:15 -05:00
|
|
|
if (interval) {
|
|
|
|
var _i = kbn.interval_to_ms(interval);
|
2014-06-22 12:01:04 -05:00
|
|
|
this.start_scheduled_refresh(_i);
|
2014-06-07 12:43:15 -05:00
|
|
|
} else {
|
2014-06-22 12:01:04 -05:00
|
|
|
this.cancel_scheduled_refresh();
|
2014-06-07 12:43:15 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-05 03:15:27 -05:00
|
|
|
p.updateSchema = function(old) {
|
2014-08-12 11:21:48 -05:00
|
|
|
var oldVersion = this.version;
|
2014-08-20 02:31:22 -05:00
|
|
|
var panelUpgrades = [];
|
|
|
|
this.version = 4;
|
2014-08-05 03:15:27 -05:00
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
if (oldVersion === 4) {
|
2014-08-05 03:15:27 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
// version 2 schema changes
|
|
|
|
if (oldVersion < 2) {
|
2014-08-12 11:21:48 -05:00
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
if (old.services) {
|
|
|
|
if (old.services.filter) {
|
|
|
|
this.time = old.services.filter.time;
|
|
|
|
this.templating.list = old.services.filter.list;
|
|
|
|
}
|
|
|
|
delete this.services;
|
2014-08-05 03:15:27 -05:00
|
|
|
}
|
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
panelUpgrades.push(function(panel) {
|
|
|
|
// rename panel type
|
2014-08-05 03:15:27 -05:00
|
|
|
if (panel.type === 'graphite') {
|
|
|
|
panel.type = 'graph';
|
|
|
|
}
|
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
if (panel.type !== 'graph') {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-05 03:15:27 -05:00
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
if (_.isBoolean(panel.legend)) { panel.legend = { show: panel.legend }; }
|
2014-08-05 03:15:27 -05:00
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
if (panel.grid) {
|
|
|
|
if (panel.grid.min) {
|
|
|
|
panel.grid.leftMin = panel.grid.min;
|
|
|
|
delete panel.grid.min;
|
2014-08-05 03:15:27 -05:00
|
|
|
}
|
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
if (panel.grid.max) {
|
|
|
|
panel.grid.leftMax = panel.grid.max;
|
|
|
|
delete panel.grid.max;
|
2014-08-05 03:15:27 -05:00
|
|
|
}
|
2014-08-20 02:31:22 -05:00
|
|
|
}
|
2014-08-05 03:15:27 -05:00
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
if (panel.y_format) {
|
|
|
|
panel.y_formats[0] = panel.y_format;
|
|
|
|
delete panel.y_format;
|
2014-08-05 03:15:27 -05:00
|
|
|
}
|
2014-08-20 02:31:22 -05:00
|
|
|
|
|
|
|
if (panel.y2_format) {
|
|
|
|
panel.y_formats[1] = panel.y2_format;
|
|
|
|
delete panel.y2_format;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// schema version 3 changes
|
|
|
|
if (oldVersion < 3) {
|
|
|
|
// ensure panel ids
|
|
|
|
var maxId = this.getNextPanelId();
|
|
|
|
panelUpgrades.push(function(panel) {
|
|
|
|
if (!panel.id) {
|
|
|
|
panel.id = maxId;
|
|
|
|
maxId += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// schema version 4 changes
|
|
|
|
if (oldVersion < 4) {
|
|
|
|
// move aliasYAxis changes
|
|
|
|
panelUpgrades.push(function(panel) {
|
|
|
|
if (panel.type !== 'graph') { return; }
|
|
|
|
_.each(panel.aliasYAxis, function(value, key) {
|
|
|
|
panel.seriesOverrides = [{ alias: key, yaxis: value }];
|
|
|
|
});
|
|
|
|
delete panel.aliasYAxis;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (panelUpgrades.length === 0) {
|
|
|
|
return;
|
2014-08-05 03:15:27 -05:00
|
|
|
}
|
|
|
|
|
2014-08-20 02:31:22 -05:00
|
|
|
for (var i = 0; i < this.rows.length; i++) {
|
|
|
|
var row = this.rows[i];
|
|
|
|
for (var j = 0; j < row.panels.length; j++) {
|
|
|
|
for (var k = 0; k < panelUpgrades.length; k++) {
|
|
|
|
panelUpgrades[k](row.panels[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 03:15:27 -05:00
|
|
|
};
|
|
|
|
|
2014-06-07 12:43:15 -05:00
|
|
|
return {
|
|
|
|
create: function(dashboard) {
|
|
|
|
return new DashboardModel(dashboard);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
2013-10-09 03:36:41 -05:00
|
|
|
});
|