dynamicDashboardSrv.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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);
  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. }
  35. // repeat panels
  36. for (j = 0; j < row.panels.length; j++) {
  37. panel = row.panels[j];
  38. if (panel.repeat) {
  39. this.repeatPanel(panel, row);
  40. }
  41. // clean up old left overs
  42. else if (panel.repeatPanelId && panel.repeatIteration !== this.iteration) {
  43. row.panels = _.without(row.panels, panel);
  44. j = j - 1;
  45. }
  46. // clean up left over scoped vars
  47. else if (panel.scopedVars && panel.repeatIteration !== this.iteration) {
  48. delete panel.scopedVars;
  49. }
  50. }
  51. }
  52. };
  53. // returns a new row clone or reuses a clone from previous iteration
  54. this.getRowClone = function(sourceRow, index) {
  55. if (index === 0) {
  56. return sourceRow;
  57. }
  58. var i, panel, row, copy;
  59. var sourceRowId = _.indexOf(this.dashboard.rows, sourceRow) + 1;
  60. // look for row to reuse
  61. for (i = 0; i < this.dashboard.rows.length; i++) {
  62. row = this.dashboard.rows[i];
  63. if (row.repeatRowId === sourceRowId && row.repeatIteration !== this.iteration) {
  64. copy = row;
  65. break;
  66. }
  67. }
  68. if (!copy) {
  69. copy = angular.copy(sourceRow);
  70. this.dashboard.rows.push(copy);
  71. // set new panel ids
  72. for (i = 0; i < copy.panels.length; i++) {
  73. panel = copy.panels[i];
  74. panel.id = this.dashboard.getNextPanelId();
  75. }
  76. }
  77. copy.repeat = null;
  78. copy.repeatRowId = sourceRowId;
  79. copy.repeatIteration = this.iteration;
  80. return copy;
  81. };
  82. // returns a new panel clone or reuses a clone from previous iteration
  83. this.repeatRow = function(row) {
  84. var variables = this.dashboard.templating.list;
  85. var variable = _.findWhere(variables, {name: row.repeat});
  86. if (!variable) {
  87. return;
  88. }
  89. var selected, copy, i, panel;
  90. if (variable.current.text === 'All') {
  91. selected = variable.options.slice(1, variable.options.length);
  92. } else {
  93. selected = _.filter(variable.options, {selected: true});
  94. }
  95. _.each(selected, function(option, index) {
  96. copy = self.getRowClone(row, index);
  97. copy.scopedVars = {};
  98. copy.scopedVars[variable.name] = option;
  99. for (i = 0; i < copy.panels.length; i++) {
  100. panel = copy.panels[i];
  101. panel.scopedVars = {};
  102. panel.scopedVars[variable.name] = option;
  103. panel.repeatIteration = self.iteration;
  104. }
  105. });
  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. });