2014-08-06 08:57:40 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2015-10-30 08:19:02 -05:00
|
|
|
'app/core/config',
|
2014-08-06 08:57:40 -05:00
|
|
|
],
|
2015-02-04 04:35:19 -06:00
|
|
|
function (angular, _, config) {
|
2014-08-06 08:57:40 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
2015-03-01 04:03:41 -06:00
|
|
|
|
2015-03-01 04:48:09 -06:00
|
|
|
module.service('panelSrv', function($rootScope, $timeout, datasourceSrv, $q) {
|
2014-08-06 08:57:40 -05:00
|
|
|
|
|
|
|
this.init = function($scope) {
|
2015-03-14 15:13:25 -05:00
|
|
|
|
2014-09-05 10:44:38 -05:00
|
|
|
if (!$scope.panel.span) { $scope.panel.span = 12; }
|
2014-08-06 08:57:40 -05:00
|
|
|
|
|
|
|
$scope.inspector = {};
|
|
|
|
|
2014-09-23 15:30:01 -05:00
|
|
|
$scope.editPanel = function() {
|
2015-03-30 05:25:17 -05:00
|
|
|
$scope.toggleFullscreen(true);
|
2014-09-23 15:30:01 -05:00
|
|
|
};
|
|
|
|
|
2014-09-24 09:20:55 -05:00
|
|
|
$scope.sharePanel = function() {
|
2014-09-24 09:26:39 -05:00
|
|
|
$scope.appEvent('show-modal', {
|
2015-03-29 07:30:03 -05:00
|
|
|
src: './app/features/dashboard/partials/shareModal.html',
|
2014-09-24 09:20:55 -05:00
|
|
|
scope: $scope.$new()
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-03 04:15:01 -05:00
|
|
|
$scope.editPanelJson = function() {
|
2014-09-24 09:26:39 -05:00
|
|
|
$scope.appEvent('show-json-editor', { object: $scope.panel, updateHandler: $scope.replacePanel });
|
2014-09-03 04:15:01 -05:00
|
|
|
};
|
|
|
|
|
2014-11-09 02:44:16 -06:00
|
|
|
$scope.duplicatePanel = function() {
|
|
|
|
$scope.dashboard.duplicatePanel($scope.panel, $scope.row);
|
|
|
|
};
|
|
|
|
|
2014-08-06 08:57:40 -05:00
|
|
|
$scope.updateColumnSpan = function(span) {
|
2015-03-24 15:10:44 -05:00
|
|
|
$scope.updatePanelSpan($scope.panel, span);
|
2014-08-06 08:57:40 -05:00
|
|
|
|
|
|
|
$timeout(function() {
|
2015-02-08 02:02:03 -06:00
|
|
|
$scope.$broadcast('render');
|
2014-08-06 08:57:40 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-15 16:11:37 -05:00
|
|
|
$scope.addDataQuery = function(datasource) {
|
2015-08-30 03:09:45 -05:00
|
|
|
$scope.dashboard.addDataQueryTo($scope.panel, datasource);
|
2014-08-06 08:57:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
$scope.removeDataQuery = function (query) {
|
2015-08-30 03:09:45 -05:00
|
|
|
$scope.dashboard.removeDataQuery($scope.panel, query);
|
2014-08-06 08:57:40 -05:00
|
|
|
$scope.get_data();
|
|
|
|
};
|
|
|
|
|
2015-08-30 03:09:45 -05:00
|
|
|
$scope.duplicateDataQuery = function(query) {
|
|
|
|
$scope.dashboard.duplicateDataQuery($scope.panel, query);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.moveDataQuery = function(fromIndex, toIndex) {
|
|
|
|
$scope.dashboard.moveDataQuery($scope.panel, fromIndex, toIndex);
|
|
|
|
};
|
|
|
|
|
2014-08-06 08:57:40 -05:00
|
|
|
$scope.setDatasource = function(datasource) {
|
2015-08-17 10:07:33 -05:00
|
|
|
// switching to mixed
|
|
|
|
if (datasource.meta.mixed) {
|
|
|
|
_.each($scope.panel.targets, function(target) {
|
|
|
|
target.datasource = $scope.panel.datasource;
|
|
|
|
if (target.datasource === null) {
|
|
|
|
target.datasource = config.defaultDatasource;
|
|
|
|
}
|
|
|
|
});
|
2015-08-15 18:34:09 -05:00
|
|
|
}
|
2015-08-17 10:07:33 -05:00
|
|
|
// switching from mixed
|
|
|
|
else if ($scope.datasource && $scope.datasource.meta.mixed) {
|
|
|
|
_.each($scope.panel.targets, function(target) {
|
|
|
|
delete target.datasource;
|
|
|
|
});
|
2015-08-15 18:34:09 -05:00
|
|
|
}
|
|
|
|
|
2015-08-17 10:07:33 -05:00
|
|
|
$scope.panel.datasource = datasource.value;
|
|
|
|
$scope.datasource = null;
|
2014-08-06 08:57:40 -05:00
|
|
|
$scope.get_data();
|
|
|
|
};
|
|
|
|
|
2014-11-06 02:56:50 -06:00
|
|
|
$scope.toggleEditorHelp = function(index) {
|
|
|
|
if ($scope.editorHelpIndex === index) {
|
|
|
|
$scope.editorHelpIndex = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$scope.editorHelpIndex = index;
|
|
|
|
};
|
|
|
|
|
2015-02-04 04:35:19 -06:00
|
|
|
$scope.isNewPanel = function() {
|
|
|
|
return $scope.panel.title === config.new_panel_title;
|
|
|
|
};
|
|
|
|
|
2014-08-13 07:22:21 -05:00
|
|
|
$scope.toggleFullscreen = function(edit) {
|
|
|
|
$scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
|
2014-08-06 08:57:40 -05:00
|
|
|
};
|
|
|
|
|
2014-08-12 11:21:48 -05:00
|
|
|
$scope.otherPanelInFullscreenMode = function() {
|
2014-08-13 03:07:32 -05:00
|
|
|
return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
|
2014-08-12 11:21:48 -05:00
|
|
|
};
|
|
|
|
|
2015-03-01 04:48:09 -06:00
|
|
|
$scope.getCurrentDatasource = function() {
|
2015-03-01 05:06:05 -06:00
|
|
|
if ($scope.datasource) {
|
2015-03-01 04:48:09 -06:00
|
|
|
return $q.when($scope.datasource);
|
|
|
|
}
|
|
|
|
|
|
|
|
return datasourceSrv.get($scope.panel.datasource);
|
|
|
|
};
|
|
|
|
|
2015-06-19 13:13:25 -05:00
|
|
|
$scope.panelRenderingComplete = function() {
|
|
|
|
$rootScope.performance.panelsRendered++;
|
|
|
|
};
|
|
|
|
|
2015-02-27 15:29:00 -06:00
|
|
|
$scope.get_data = function() {
|
|
|
|
if ($scope.otherPanelInFullscreenMode()) { return; }
|
|
|
|
|
2015-03-23 14:32:38 -05:00
|
|
|
if ($scope.panel.snapshotData) {
|
|
|
|
if ($scope.loadSnapshot) {
|
|
|
|
$scope.loadSnapshot($scope.panel.snapshotData);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-27 15:29:00 -06:00
|
|
|
delete $scope.panelMeta.error;
|
|
|
|
$scope.panelMeta.loading = true;
|
|
|
|
|
2015-03-01 04:48:09 -06:00
|
|
|
$scope.getCurrentDatasource().then(function(datasource) {
|
2015-02-27 15:29:00 -06:00
|
|
|
$scope.datasource = datasource;
|
2015-03-05 05:25:48 -06:00
|
|
|
return $scope.refreshData($scope.datasource) || $q.when({});
|
|
|
|
}).then(function() {
|
|
|
|
$scope.panelMeta.loading = false;
|
2015-02-28 02:46:37 -06:00
|
|
|
}, function(err) {
|
2015-03-01 04:03:41 -06:00
|
|
|
console.log('Panel data error:', err);
|
2015-02-28 02:46:37 -06:00
|
|
|
$scope.panelMeta.loading = false;
|
2015-03-01 04:03:41 -06:00
|
|
|
$scope.panelMeta.error = err.message || "Timeseries data request error";
|
|
|
|
$scope.inspector.error = err;
|
2015-02-27 15:29:00 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($scope.refreshData) {
|
|
|
|
$scope.$on("refresh", $scope.get_data);
|
|
|
|
}
|
|
|
|
|
2014-08-06 08:57:40 -05:00
|
|
|
// Post init phase
|
|
|
|
$scope.fullscreen = false;
|
2014-08-21 02:29:56 -05:00
|
|
|
$scope.editor = { index: 1 };
|
2014-08-06 08:57:40 -05:00
|
|
|
|
2014-08-13 05:16:37 -05:00
|
|
|
$scope.dashboardViewState.registerPanel($scope);
|
2015-02-27 15:29:00 -06:00
|
|
|
$scope.datasources = datasourceSrv.getMetricSources();
|
2014-08-12 11:21:48 -05:00
|
|
|
|
2015-02-27 15:29:00 -06:00
|
|
|
if (!$scope.skipDataOnInit) {
|
|
|
|
$timeout(function() {
|
2014-08-12 11:21:48 -05:00
|
|
|
$scope.get_data();
|
2015-02-27 15:29:00 -06:00
|
|
|
}, 30);
|
2014-08-12 11:21:48 -05:00
|
|
|
}
|
2014-08-06 08:57:40 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|