dashboardSrv.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'kbn',
  5. 'lodash',
  6. '../timer',
  7. ],
  8. function (angular, $, kbn, _) {
  9. 'use strict';
  10. var module = angular.module('grafana.services');
  11. module.factory('dashboardSrv', function(timer, $rootScope, $timeout, $location) {
  12. function DashboardModel (data) {
  13. if (!data) {
  14. data = {};
  15. }
  16. this.title = data.title || 'No Title';
  17. this.tags = data.tags || [];
  18. this.style = data.style || "dark";
  19. this.timezone = data.timezone || 'browser';
  20. this.editable = data.editble || true;
  21. this.rows = data.rows || [];
  22. this.pulldowns = data.pulldowns || [];
  23. this.nav = data.nav || [];
  24. this.time = data.time || { from: 'now-6h', to: 'now' };
  25. this.templating = data.templating || { list: [] };
  26. this.refresh = data.refresh;
  27. this.version = data.version || 0;
  28. this.$state = data.$state;
  29. if (this.nav.length === 0) {
  30. this.nav.push({ type: 'timepicker' });
  31. }
  32. if (!_.findWhere(this.pulldowns, {type: 'filtering'})) {
  33. this.pulldowns.push({ type: 'filtering', enable: false });
  34. }
  35. if (!_.findWhere(this.pulldowns, {type: 'annotations'})) {
  36. this.pulldowns.push({ type: 'annotations', enable: false });
  37. }
  38. this.updateSchema(data);
  39. }
  40. var p = DashboardModel.prototype;
  41. p.emit_refresh = function() {
  42. $rootScope.$broadcast('refresh');
  43. };
  44. p.start_scheduled_refresh = function (after_ms) {
  45. this.cancel_scheduled_refresh();
  46. this.refresh_timer = timer.register($timeout(function () {
  47. this.start_scheduled_refresh(after_ms);
  48. this.emit_refresh();
  49. }.bind(this), after_ms));
  50. };
  51. p.cancel_scheduled_refresh = function () {
  52. timer.cancel(this.refresh_timer);
  53. };
  54. p.set_interval = function (interval) {
  55. this.refresh = interval;
  56. if (interval) {
  57. var _i = kbn.interval_to_ms(interval);
  58. this.start_scheduled_refresh(_i);
  59. } else {
  60. this.cancel_scheduled_refresh();
  61. }
  62. };
  63. p.updateSchema = function(old) {
  64. var i, j, row, panel;
  65. var oldVersion = this.version;
  66. this.version = 3;
  67. if (oldVersion === 3) {
  68. return;
  69. }
  70. // Version 3 schema changes
  71. // ensure panel ids
  72. var panelId = 1;
  73. for (i = 0; i < this.rows.length; i++) {
  74. row = this.rows[i];
  75. for (j = 0; j < row.panels.length; j++) {
  76. panel = row.panels[j];
  77. panel.id = panelId;
  78. panelId += 1;
  79. }
  80. }
  81. if (oldVersion === 2) {
  82. return;
  83. }
  84. // Version 2 schema changes
  85. if (old.services) {
  86. if (old.services.filter) {
  87. this.time = old.services.filter.time;
  88. this.templating.list = old.services.filter.list;
  89. }
  90. delete this.services;
  91. }
  92. for (i = 0; i < this.rows.length; i++) {
  93. row = this.rows[i];
  94. for (j = 0; j < row.panels.length; j++) {
  95. panel = row.panels[j];
  96. if (panel.type === 'graphite') {
  97. panel.type = 'graph';
  98. }
  99. if (panel.type === 'graph') {
  100. if (_.isBoolean(panel.legend)) {
  101. panel.legend = { show: panel.legend };
  102. }
  103. if (panel.grid) {
  104. if (panel.grid.min) {
  105. panel.grid.leftMin = panel.grid.min;
  106. delete panel.grid.min;
  107. }
  108. if (panel.grid.max) {
  109. panel.grid.leftMax = panel.grid.max;
  110. delete panel.grid.max;
  111. }
  112. }
  113. if (panel.y_format) {
  114. panel.y_formats[0] = panel.y_format;
  115. delete panel.y_format;
  116. }
  117. if (panel.y2_format) {
  118. panel.y_formats[1] = panel.y2_format;
  119. delete panel.y2_format;
  120. }
  121. }
  122. }
  123. }
  124. this.version = 3;
  125. };
  126. // represents the transient view state
  127. // like fullscreen panel & edit
  128. function DashboardViewState() {
  129. var queryParams = $location.search();
  130. this.update({
  131. panelId: parseInt(queryParams.panelId),
  132. fullscreen: queryParams.fullscreen ? true : false,
  133. edit: queryParams.edit ? true : false
  134. });
  135. }
  136. DashboardViewState.prototype.update = function(state) {
  137. _.extend(this, state);
  138. if (!this.fullscreen) {
  139. delete this.fullscreen;
  140. delete this.panelId;
  141. delete this.edit;
  142. }
  143. if (!this.edit) { delete this.edit; }
  144. $location.search(this);
  145. };
  146. return {
  147. create: function(dashboard) {
  148. return new DashboardModel(dashboard);
  149. },
  150. createViewState: function(state) {
  151. return new DashboardViewState(state);
  152. }
  153. };
  154. });
  155. });