dynamicDashboardSrv.js 4.7 KB

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