dynamicDashboardSrv.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.services');
  8. module.service('dynamicDashboardSrv', function() {
  9. var self = this;
  10. this.init = function(dashboard) {
  11. if (dashboard.snapshot) { return; }
  12. this.iteration = new Date().getTime();
  13. this.process(dashboard);
  14. };
  15. this.update = function(dashboard) {
  16. if (dashboard.snapshot) { return; }
  17. this.iteration = this.iteration + 1;
  18. this.process(dashboard);
  19. };
  20. this.process = function(dashboard) {
  21. if (dashboard.templating.list.length === 0) { return; }
  22. this.dashboard = dashboard;
  23. var i, j, row, panel;
  24. for (i = 0; i < this.dashboard.rows.length; i++) {
  25. row = this.dashboard.rows[i];
  26. // handle row repeats
  27. if (row.repeat) {
  28. this.repeatRow(row, i);
  29. }
  30. // clean up old left overs
  31. else if (row.repeatRowId && row.repeatIteration !== this.iteration) {
  32. this.dashboard.rows.splice(i, 1);
  33. i = i - 1;
  34. continue;
  35. }
  36. // repeat panels
  37. for (j = 0; j < row.panels.length; j++) {
  38. panel = row.panels[j];
  39. if (panel.repeat) {
  40. this.repeatPanel(panel, row);
  41. }
  42. // clean up old left overs
  43. else if (panel.repeatPanelId && panel.repeatIteration !== this.iteration) {
  44. row.panels = _.without(row.panels, panel);
  45. j = j - 1;
  46. } else if (row.repeat || row.repeatRowId) {
  47. continue;
  48. } else if (!_.isEmpty(panel.scopedVars) && panel.repeatIteration !== this.iteration) {
  49. panel.scopedVars = {};
  50. }
  51. }
  52. }
  53. };
  54. // returns a new row clone or reuses a clone from previous iteration
  55. this.getRowClone = function(sourceRow, repeatIndex, sourceRowIndex) {
  56. if (repeatIndex === 0) {
  57. return sourceRow;
  58. }
  59. var i, panel, row, copy;
  60. var sourceRowId = sourceRowIndex + 1;
  61. // look for row to reuse
  62. for (i = 0; i < this.dashboard.rows.length; i++) {
  63. row = this.dashboard.rows[i];
  64. if (row.repeatRowId === sourceRowId && row.repeatIteration !== this.iteration) {
  65. copy = row;
  66. break;
  67. }
  68. }
  69. if (!copy) {
  70. copy = angular.copy(sourceRow);
  71. this.dashboard.rows.splice(sourceRowIndex + repeatIndex, 0, copy);
  72. // set new panel ids
  73. for (i = 0; i < copy.panels.length; i++) {
  74. panel = copy.panels[i];
  75. panel.id = this.dashboard.getNextPanelId();
  76. }
  77. }
  78. copy.repeat = null;
  79. copy.repeatRowId = sourceRowId;
  80. copy.repeatIteration = this.iteration;
  81. return copy;
  82. };
  83. // returns a new row clone or reuses a clone from previous iteration
  84. this.repeatRow = function(row, rowIndex) {
  85. var variables = this.dashboard.templating.list;
  86. var variable = _.findWhere(variables, {name: row.repeat});
  87. if (!variable) {
  88. return;
  89. }
  90. var selected, copy, i, panel;
  91. if (variable.current.text === 'All') {
  92. selected = variable.options.slice(1, variable.options.length);
  93. } else {
  94. selected = _.filter(variable.options, {selected: true});
  95. }
  96. _.each(selected, function(option, index) {
  97. copy = self.getRowClone(row, index, rowIndex);
  98. copy.scopedVars = {};
  99. copy.scopedVars[variable.name] = option;
  100. for (i = 0; i < copy.panels.length; i++) {
  101. panel = copy.panels[i];
  102. panel.scopedVars = {};
  103. panel.scopedVars[variable.name] = option;
  104. }
  105. }, this);
  106. };
  107. this.getPanelClone = function(sourcePanel, row, index) {
  108. // if first clone return source
  109. if (index === 0) {
  110. return sourcePanel;
  111. }
  112. var i, tmpId, panel, clone;
  113. // first try finding an existing clone to use
  114. for (i = 0; i < row.panels.length; i++) {
  115. panel = row.panels[i];
  116. if (panel.repeatIteration !== this.iteration && panel.repeatPanelId === sourcePanel.id) {
  117. clone = panel;
  118. break;
  119. }
  120. }
  121. if (!clone) {
  122. clone = { id: this.dashboard.getNextPanelId() };
  123. row.panels.push(clone);
  124. }
  125. // save id
  126. tmpId = clone.id;
  127. // copy properties from source
  128. angular.copy(sourcePanel, clone);
  129. // restore id
  130. clone.id = tmpId;
  131. clone.repeatIteration = this.iteration;
  132. clone.repeatPanelId = sourcePanel.id;
  133. clone.repeat = null;
  134. return clone;
  135. };
  136. this.repeatPanel = function(panel, row) {
  137. var variables = this.dashboard.templating.list;
  138. var variable = _.findWhere(variables, {name: panel.repeat});
  139. if (!variable) { return; }
  140. var selected;
  141. if (variable.current.text === 'All') {
  142. selected = variable.options.slice(1, variable.options.length);
  143. } else {
  144. selected = _.filter(variable.options, {selected: true});
  145. }
  146. _.each(selected, function(option, index) {
  147. var copy = self.getPanelClone(panel, row, index);
  148. copy.span = Math.max(12 / selected.length, panel.minSpan);
  149. copy.scopedVars = copy.scopedVars || {};
  150. copy.scopedVars[variable.name] = option;
  151. });
  152. };
  153. });
  154. });