dynamicDashboardSrv.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 (!_.isEmpty(panel.scopedVars) && panel.repeatIteration !== this.iteration) {
  47. panel.scopedVars = {};
  48. }
  49. }
  50. }
  51. };
  52. // returns a new row clone or reuses a clone from previous iteration
  53. this.getRowClone = function(sourceRow, repeatIndex, sourceRowIndex) {
  54. if (repeatIndex === 0) {
  55. return sourceRow;
  56. }
  57. var i, panel, row, copy;
  58. var sourceRowId = sourceRowIndex + 1;
  59. // look for row to reuse
  60. for (i = 0; i < this.dashboard.rows.length; i++) {
  61. row = this.dashboard.rows[i];
  62. if (row.repeatRowId === sourceRowId && row.repeatIteration !== this.iteration) {
  63. copy = row;
  64. break;
  65. }
  66. }
  67. if (!copy) {
  68. copy = angular.copy(sourceRow);
  69. this.dashboard.rows.splice(sourceRowIndex + repeatIndex, 0, copy);
  70. // set new panel ids
  71. for (i = 0; i < copy.panels.length; i++) {
  72. panel = copy.panels[i];
  73. panel.id = this.dashboard.getNextPanelId();
  74. }
  75. }
  76. copy.repeat = null;
  77. copy.repeatRowId = sourceRowId;
  78. copy.repeatIteration = this.iteration;
  79. return copy;
  80. };
  81. // returns a new row clone or reuses a clone from previous iteration
  82. this.repeatRow = function(row, rowIndex) {
  83. var variables = this.dashboard.templating.list;
  84. var variable = _.findWhere(variables, {name: row.repeat});
  85. if (!variable) {
  86. return;
  87. }
  88. var selected, copy, i, panel;
  89. if (variable.current.text === 'All') {
  90. selected = variable.options.slice(1, variable.options.length);
  91. } else {
  92. selected = _.filter(variable.options, {selected: true});
  93. }
  94. _.each(selected, function(option, index) {
  95. copy = self.getRowClone(row, index, rowIndex);
  96. copy.scopedVars = {};
  97. copy.scopedVars[variable.name] = option;
  98. for (i = 0; i < copy.panels.length; i++) {
  99. panel = copy.panels[i];
  100. panel.scopedVars = {};
  101. panel.scopedVars[variable.name] = option;
  102. panel.repeatIteration = this.iteration;
  103. }
  104. }, this);
  105. };
  106. this.getPanelClone = function(sourcePanel, row, index) {
  107. // if first clone return source
  108. if (index === 0) {
  109. return sourcePanel;
  110. }
  111. var i, tmpId, panel, clone;
  112. // first try finding an existing clone to use
  113. for (i = 0; i < row.panels.length; i++) {
  114. panel = row.panels[i];
  115. if (panel.repeatIteration !== this.iteration && panel.repeatPanelId === sourcePanel.id) {
  116. clone = panel;
  117. break;
  118. }
  119. }
  120. if (!clone) {
  121. clone = { id: this.dashboard.getNextPanelId() };
  122. row.panels.push(clone);
  123. }
  124. // save id
  125. tmpId = clone.id;
  126. // copy properties from source
  127. angular.copy(sourcePanel, clone);
  128. // restore id
  129. clone.id = tmpId;
  130. clone.repeatIteration = this.iteration;
  131. clone.repeatPanelId = sourcePanel.id;
  132. clone.repeat = null;
  133. return clone;
  134. };
  135. this.repeatPanel = function(panel, row) {
  136. var variables = this.dashboard.templating.list;
  137. var variable = _.findWhere(variables, {name: panel.repeat});
  138. if (!variable) { return; }
  139. var selected;
  140. if (variable.current.text === 'All') {
  141. selected = variable.options.slice(1, variable.options.length);
  142. } else {
  143. selected = _.filter(variable.options, {selected: true});
  144. }
  145. _.each(selected, function(option, index) {
  146. var copy = self.getPanelClone(panel, row, index);
  147. copy.span = Math.max(12 / selected.length, panel.minSpan);
  148. copy.scopedVars = copy.scopedVars || {};
  149. copy.scopedVars[variable.name] = option;
  150. });
  151. };
  152. });
  153. });