variable_srv.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import coreModule from 'app/core/core_module';
  4. import { variableTypes } from './variable';
  5. export class VariableSrv {
  6. dashboard: any;
  7. variables: any;
  8. /** @ngInject */
  9. constructor(private $rootScope, private $q, private $location, private $injector, private templateSrv) {
  10. // update time variant variables
  11. $rootScope.$on('refresh', this.onDashboardRefresh.bind(this), $rootScope);
  12. $rootScope.$on('template-variable-value-updated', this.updateUrlParamsWithCurrentVariables.bind(this), $rootScope);
  13. }
  14. init(dashboard) {
  15. this.dashboard = dashboard;
  16. // create working class models representing variables
  17. this.variables = dashboard.templating.list = dashboard.templating.list.map(this.createVariableFromModel.bind(this));
  18. this.templateSrv.init(this.variables);
  19. // init variables
  20. for (let variable of this.variables) {
  21. variable.initLock = this.$q.defer();
  22. }
  23. var queryParams = this.$location.search();
  24. return this.$q
  25. .all(
  26. this.variables.map(variable => {
  27. return this.processVariable(variable, queryParams);
  28. })
  29. )
  30. .then(() => {
  31. this.templateSrv.updateTemplateData();
  32. });
  33. }
  34. onDashboardRefresh() {
  35. var promises = this.variables.filter(variable => variable.refresh === 2).map(variable => {
  36. var previousOptions = variable.options.slice();
  37. return variable.updateOptions().then(() => {
  38. if (angular.toJson(previousOptions) !== angular.toJson(variable.options)) {
  39. this.$rootScope.$emit('template-variable-value-updated');
  40. }
  41. });
  42. });
  43. return this.$q.all(promises);
  44. }
  45. processVariable(variable, queryParams) {
  46. var dependencies = [];
  47. for (let otherVariable of this.variables) {
  48. if (variable.dependsOn(otherVariable)) {
  49. dependencies.push(otherVariable.initLock.promise);
  50. }
  51. }
  52. return this.$q
  53. .all(dependencies)
  54. .then(() => {
  55. var urlValue = queryParams['var-' + variable.name];
  56. if (urlValue !== void 0) {
  57. return variable.setValueFromUrl(urlValue).then(variable.initLock.resolve);
  58. }
  59. if (variable.refresh === 1 || variable.refresh === 2) {
  60. return variable.updateOptions().then(variable.initLock.resolve);
  61. }
  62. variable.initLock.resolve();
  63. })
  64. .finally(() => {
  65. this.templateSrv.variableInitialized(variable);
  66. delete variable.initLock;
  67. });
  68. }
  69. createVariableFromModel(model) {
  70. var ctor = variableTypes[model.type].ctor;
  71. if (!ctor) {
  72. throw {
  73. message: 'Unable to find variable constructor for ' + model.type,
  74. };
  75. }
  76. var variable = this.$injector.instantiate(ctor, { model: model });
  77. return variable;
  78. }
  79. addVariable(variable) {
  80. this.variables.push(variable);
  81. this.templateSrv.updateTemplateData();
  82. this.dashboard.updateSubmenuVisibility();
  83. }
  84. removeVariable(variable) {
  85. var index = _.indexOf(this.variables, variable);
  86. this.variables.splice(index, 1);
  87. this.templateSrv.updateTemplateData();
  88. this.dashboard.updateSubmenuVisibility();
  89. }
  90. updateOptions(variable) {
  91. return variable.updateOptions();
  92. }
  93. variableUpdated(variable, emitChangeEvents?) {
  94. // if there is a variable lock ignore cascading update because we are in a boot up scenario
  95. if (variable.initLock) {
  96. return this.$q.when();
  97. }
  98. // cascade updates to variables that use this variable
  99. var promises = _.map(this.variables, otherVariable => {
  100. if (otherVariable === variable) {
  101. return;
  102. }
  103. if (otherVariable.dependsOn(variable)) {
  104. return this.updateOptions(otherVariable);
  105. }
  106. });
  107. return this.$q.all(promises).then(() => {
  108. if (emitChangeEvents) {
  109. this.$rootScope.$emit('template-variable-value-updated');
  110. this.$rootScope.$broadcast('refresh');
  111. }
  112. });
  113. }
  114. selectOptionsForCurrentValue(variable) {
  115. var i, y, value, option;
  116. var selected: any = [];
  117. for (i = 0; i < variable.options.length; i++) {
  118. option = variable.options[i];
  119. option.selected = false;
  120. if (_.isArray(variable.current.value)) {
  121. for (y = 0; y < variable.current.value.length; y++) {
  122. value = variable.current.value[y];
  123. if (option.value === value) {
  124. option.selected = true;
  125. selected.push(option);
  126. }
  127. }
  128. } else if (option.value === variable.current.value) {
  129. option.selected = true;
  130. selected.push(option);
  131. }
  132. }
  133. return selected;
  134. }
  135. validateVariableSelectionState(variable) {
  136. if (!variable.current) {
  137. variable.current = {};
  138. }
  139. if (_.isArray(variable.current.value)) {
  140. var selected = this.selectOptionsForCurrentValue(variable);
  141. // if none pick first
  142. if (selected.length === 0) {
  143. selected = variable.options[0];
  144. } else {
  145. selected = {
  146. value: _.map(selected, function(val) {
  147. return val.value;
  148. }),
  149. text: _.map(selected, function(val) {
  150. return val.text;
  151. }).join(' + '),
  152. };
  153. }
  154. return variable.setValue(selected);
  155. } else {
  156. var currentOption = _.find(variable.options, {
  157. text: variable.current.text,
  158. });
  159. if (currentOption) {
  160. return variable.setValue(currentOption);
  161. } else {
  162. if (!variable.options.length) {
  163. return Promise.resolve();
  164. }
  165. return variable.setValue(variable.options[0]);
  166. }
  167. }
  168. }
  169. setOptionFromUrl(variable, urlValue) {
  170. var promise = this.$q.when();
  171. if (variable.refresh) {
  172. promise = variable.updateOptions();
  173. }
  174. return promise.then(() => {
  175. var option = _.find(variable.options, op => {
  176. return op.text === urlValue || op.value === urlValue;
  177. });
  178. option = option || { text: urlValue, value: urlValue };
  179. return variable.setValue(option);
  180. });
  181. }
  182. setOptionAsCurrent(variable, option) {
  183. variable.current = _.cloneDeep(option);
  184. if (_.isArray(variable.current.text)) {
  185. variable.current.text = variable.current.text.join(' + ');
  186. }
  187. this.selectOptionsForCurrentValue(variable);
  188. return this.variableUpdated(variable);
  189. }
  190. updateUrlParamsWithCurrentVariables() {
  191. // update url
  192. var params = this.$location.search();
  193. // remove variable params
  194. _.each(params, function(value, key) {
  195. if (key.indexOf('var-') === 0) {
  196. delete params[key];
  197. }
  198. });
  199. // add new values
  200. this.templateSrv.fillVariableValuesForUrl(params);
  201. // update url
  202. this.$location.search(params);
  203. }
  204. setAdhocFilter(options) {
  205. var variable = _.find(this.variables, {
  206. type: 'adhoc',
  207. datasource: options.datasource,
  208. });
  209. if (!variable) {
  210. variable = this.createVariableFromModel({
  211. name: 'Filters',
  212. type: 'adhoc',
  213. datasource: options.datasource,
  214. });
  215. this.addVariable(variable);
  216. }
  217. let filters = variable.filters;
  218. let filter = _.find(filters, { key: options.key, value: options.value });
  219. if (!filter) {
  220. filter = { key: options.key, value: options.value };
  221. filters.push(filter);
  222. }
  223. filter.operator = options.operator;
  224. this.variableUpdated(variable, true);
  225. }
  226. }
  227. coreModule.service('variableSrv', VariableSrv);