module.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. define([
  2. 'angular',
  3. 'app',
  4. 'jquery',
  5. 'lodash',
  6. 'kbn',
  7. 'moment',
  8. 'components/timeSeries',
  9. 'components/panelmeta',
  10. './seriesOverridesCtrl',
  11. './graph',
  12. './legend',
  13. ],
  14. function (angular, app, $, _, kbn, moment, TimeSeries, PanelMeta) {
  15. 'use strict';
  16. var module = angular.module('grafana.panels.graph');
  17. module.controller('GraphCtrl', function($scope, $rootScope, panelSrv, annotationsSrv, timeSrv) {
  18. $scope.panelMeta = new PanelMeta({
  19. description: 'Graph panel',
  20. fullscreen: true,
  21. metricsEditor: true
  22. });
  23. $scope.panelMeta.addEditorTab('Axes & Grid', 'app/panels/graph/axisEditor.html');
  24. $scope.panelMeta.addEditorTab('Display Styles', 'app/panels/graph/styleEditor.html');
  25. $scope.panelMeta.addEditorTab('Time range', 'app/features/dashboard/partials/panelTime.html');
  26. $scope.panelMeta.addExtendedMenuItem('Export CSV', '', 'exportCsv()');
  27. $scope.panelMeta.addExtendedMenuItem('Toggle legend', '', 'toggleLegend()');
  28. // Set and populate defaults
  29. var _d = {
  30. // datasource name, null = default datasource
  31. datasource: null,
  32. // sets client side (flot) or native graphite png renderer (png)
  33. renderer: 'flot',
  34. // Show/hide the x-axis
  35. 'x-axis' : true,
  36. // Show/hide y-axis
  37. 'y-axis' : true,
  38. // y axis formats, [left axis,right axis]
  39. y_formats : ['short', 'short'],
  40. // grid options
  41. grid : {
  42. leftMax: null,
  43. rightMax: null,
  44. leftMin: null,
  45. rightMin: null,
  46. threshold1: null,
  47. threshold2: null,
  48. threshold1Color: 'rgba(216, 200, 27, 0.27)',
  49. threshold2Color: 'rgba(234, 112, 112, 0.22)'
  50. },
  51. // show/hide lines
  52. lines : true,
  53. // fill factor
  54. fill : 0,
  55. // line width in pixels
  56. linewidth : 1,
  57. // show hide points
  58. points : false,
  59. // point radius in pixels
  60. pointradius : 5,
  61. // show hide bars
  62. bars : false,
  63. // enable/disable stacking
  64. stack : false,
  65. // stack percentage mode
  66. percentage : false,
  67. // legend options
  68. legend: {
  69. show: true, // disable/enable legend
  70. values: false, // disable/enable legend values
  71. min: false,
  72. max: false,
  73. current: false,
  74. total: false,
  75. avg: false
  76. },
  77. // how null points should be handled
  78. nullPointMode : 'connected',
  79. // staircase line mode
  80. steppedLine: false,
  81. // tooltip options
  82. tooltip : {
  83. value_type: 'cumulative',
  84. shared: false,
  85. },
  86. // time overrides
  87. timeFrom: null,
  88. timeShift: null,
  89. // metric queries
  90. targets: [{}],
  91. // series color overrides
  92. aliasColors: {},
  93. // other style overrides
  94. seriesOverrides: [],
  95. };
  96. _.defaults($scope.panel,_d);
  97. _.defaults($scope.panel.tooltip, _d.tooltip);
  98. _.defaults($scope.panel.annotate, _d.annotate);
  99. _.defaults($scope.panel.grid, _d.grid);
  100. _.defaults($scope.panel.legend, _d.legend);
  101. $scope.hiddenSeries = {};
  102. $scope.seriesList = [];
  103. $scope.unitFormats = kbn.getUnitFormats();
  104. $scope.setUnitFormat = function(axis, subItem) {
  105. $scope.panel.y_formats[axis] = subItem.value;
  106. $scope.render();
  107. };
  108. $scope.updateTimeRange = function () {
  109. $scope.range = timeSrv.timeRange();
  110. $scope.rangeUnparsed = timeSrv.timeRange(false);
  111. $scope.panelMeta.timeInfo = "";
  112. // check panel time overrrides
  113. if ($scope.panel.timeFrom) {
  114. if (_.isString($scope.rangeUnparsed.from)) {
  115. $scope.panelMeta.timeInfo = "last " + $scope.panel.timeFrom;
  116. $scope.rangeUnparsed.from = 'now-' + $scope.panel.timeFrom;
  117. $scope.range.from = kbn.parseDate($scope.rangeUnparsed.from);
  118. }
  119. }
  120. if ($scope.panel.timeShift) {
  121. var timeShift = '-' + $scope.panel.timeShift;
  122. $scope.panelMeta.timeInfo += ' timeshift ' + timeShift;
  123. $scope.range.from = kbn.parseDateMath(timeShift, $scope.range.from);
  124. $scope.range.to = kbn.parseDateMath(timeShift, $scope.range.to);
  125. $scope.rangeUnparsed = $scope.range;
  126. }
  127. if ($scope.panel.maxDataPoints) {
  128. $scope.resolution = $scope.panel.maxDataPoints;
  129. }
  130. else {
  131. $scope.resolution = Math.ceil($(window).width() * ($scope.panel.span / 12));
  132. }
  133. $scope.interval = kbn.calculateInterval($scope.range, $scope.resolution, $scope.panel.interval);
  134. };
  135. $scope.get_data = function() {
  136. $scope.updateTimeRange();
  137. var metricsQuery = {
  138. range: $scope.rangeUnparsed,
  139. interval: $scope.interval,
  140. targets: $scope.panel.targets,
  141. format: $scope.panel.renderer === 'png' ? 'png' : 'json',
  142. maxDataPoints: $scope.resolution,
  143. cacheTimeout: $scope.panel.cacheTimeout
  144. };
  145. $scope.annotationsPromise = annotationsSrv.getAnnotations($scope.rangeUnparsed, $scope.dashboard);
  146. return $scope.datasource.query(metricsQuery)
  147. .then($scope.dataHandler)
  148. .then(null, function(err) {
  149. $scope.panelMeta.loading = false;
  150. $scope.panelMeta.error = err.message || "Timeseries data request error";
  151. $scope.inspector.error = err;
  152. $scope.seriesList = [];
  153. $scope.render([]);
  154. });
  155. };
  156. $scope.dataHandler = function(results) {
  157. // png renderer returns just a url
  158. if (_.isString(results)) {
  159. $scope.panelMeta.loading = false;
  160. $scope.render(results);
  161. return;
  162. }
  163. $scope.datapointsWarning = false;
  164. $scope.datapointsCount = 0;
  165. $scope.datapointsOutside = false;
  166. $scope.seriesList = _.map(results.data, $scope.seriesHandler);
  167. $scope.datapointsWarning = $scope.datapointsCount === 0 || $scope.datapointsOutside;
  168. $scope.annotationsPromise
  169. .then(function(annotations) {
  170. $scope.panelMeta.loading = false;
  171. $scope.seriesList.annotations = annotations;
  172. $scope.render($scope.seriesList);
  173. }, function() {
  174. $scope.panelMeta.loading = false;
  175. $scope.render($scope.seriesList);
  176. });
  177. };
  178. $scope.seriesHandler = function(seriesData, index) {
  179. var datapoints = seriesData.datapoints;
  180. var alias = seriesData.target;
  181. var color = $scope.panel.aliasColors[alias] || $rootScope.colors[index];
  182. var series = new TimeSeries({
  183. datapoints: datapoints,
  184. alias: alias,
  185. color: color,
  186. });
  187. if (datapoints && datapoints.length > 0) {
  188. var last = moment.utc(datapoints[datapoints.length - 1][1]);
  189. var from = moment.utc($scope.range.from);
  190. if (last - from < -10000) {
  191. $scope.datapointsOutside = true;
  192. }
  193. $scope.datapointsCount += datapoints.length;
  194. }
  195. return series;
  196. };
  197. $scope.render = function(data) {
  198. $scope.$broadcast('render', data);
  199. };
  200. $scope.changeSeriesColor = function(series, color) {
  201. series.color = color;
  202. $scope.panel.aliasColors[series.alias] = series.color;
  203. $scope.render();
  204. };
  205. $scope.toggleSeries = function(serie, event) {
  206. if (event.ctrlKey || event.metaKey || event.shiftKey) {
  207. if ($scope.hiddenSeries[serie.alias]) {
  208. delete $scope.hiddenSeries[serie.alias];
  209. }
  210. else {
  211. $scope.hiddenSeries[serie.alias] = true;
  212. }
  213. } else {
  214. $scope.toggleSeriesExclusiveMode(serie);
  215. }
  216. $scope.render();
  217. };
  218. $scope.toggleSeriesExclusiveMode = function(serie) {
  219. var hidden = $scope.hiddenSeries;
  220. if (hidden[serie.alias]) {
  221. delete hidden[serie.alias];
  222. }
  223. // check if every other series is hidden
  224. var alreadyExclusive = _.every($scope.seriesList, function(value) {
  225. if (value.alias === serie.alias) {
  226. return true;
  227. }
  228. return hidden[value.alias];
  229. });
  230. if (alreadyExclusive) {
  231. // remove all hidden series
  232. _.each($scope.seriesList, function(value) {
  233. delete $scope.hiddenSeries[value.alias];
  234. });
  235. }
  236. else {
  237. // hide all but this serie
  238. _.each($scope.seriesList, function(value) {
  239. if (value.alias === serie.alias) {
  240. return;
  241. }
  242. $scope.hiddenSeries[value.alias] = true;
  243. });
  244. }
  245. };
  246. $scope.toggleYAxis = function(info) {
  247. var override = _.findWhere($scope.panel.seriesOverrides, { alias: info.alias });
  248. if (!override) {
  249. override = { alias: info.alias };
  250. $scope.panel.seriesOverrides.push(override);
  251. }
  252. override.yaxis = info.yaxis === 2 ? 1 : 2;
  253. $scope.render();
  254. };
  255. $scope.addSeriesOverride = function(override) {
  256. $scope.panel.seriesOverrides.push(override || {});
  257. };
  258. $scope.removeSeriesOverride = function(override) {
  259. $scope.panel.seriesOverrides = _.without($scope.panel.seriesOverrides, override);
  260. $scope.render();
  261. };
  262. // Called from panel menu
  263. $scope.toggleLegend = function() {
  264. $scope.panel.legend.show = !$scope.panel.legend.show;
  265. $scope.get_data();
  266. };
  267. $scope.legendValuesOptionChanged = function() {
  268. var legend = $scope.panel.legend;
  269. legend.values = legend.min || legend.max || legend.avg || legend.current || legend.total;
  270. $scope.render();
  271. };
  272. $scope.exportCsv = function() {
  273. kbn.exportSeriesListToCsv($scope.seriesList);
  274. };
  275. panelSrv.init($scope);
  276. });
  277. });