variable_srv.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import $ from 'jquery';
  5. import coreModule from 'app/core/core_module';
  6. import appEvents from 'app/core/app_events';
  7. interface Variable {
  8. }
  9. class ConstantVariable implements Variable {
  10. constructor(private model) {
  11. }
  12. }
  13. class CustomVariable implements Variable {
  14. constructor(private model) {
  15. }
  16. }
  17. class IntervalVariable implements Variable {
  18. constructor(private model) {
  19. }
  20. }
  21. class QueryVariable implements Variable {
  22. constructor(private model,
  23. private variableSrv: VariableSrv,
  24. private datasourceSrv) {
  25. _.extend(this, model);
  26. }
  27. updateOptions() {
  28. return this.datasourceSrv.get(this.datasource)
  29. .then(_.partial(this.updateOptionsFromMetricFindQuery, variable))
  30. .then(_.partial(this.updateTags, variable))
  31. .then(_.partial(this.validateVariableSelectionState, variable));
  32. }
  33. }
  34. class DatasourceVariable implements Variable {
  35. constructor(private model) {
  36. }
  37. }
  38. export class VariableSrv {
  39. dashboard: any;
  40. variables: any;
  41. variableLock: any;
  42. /** @ngInject */
  43. constructor(
  44. private $q,
  45. private $rootScope,
  46. private datasourceSrv,
  47. private $location,
  48. private templateSrv,
  49. private timeSrv) {
  50. }
  51. init(dashboard) {
  52. this.variableLock = {};
  53. this.dashboard = dashboard;
  54. this.variables = dashboard.templating.list.map(item => {
  55. return new QueryVariable(item, this);
  56. });
  57. this.templateSrv.init(this.variables);
  58. return this.$q.when();
  59. }
  60. updateOptions(variable) {
  61. return variable.updateOptions();
  62. }
  63. variableUpdated(variable) {
  64. // if there is a variable lock ignore cascading update because we are in a boot up scenario
  65. if (this.variableLock[variable.name]) {
  66. return this.$q.when();
  67. }
  68. var promises = _.map(this.variables, otherVariable => {
  69. if (otherVariable === variable) {
  70. return;
  71. }
  72. if (this.templateSrv.containsVariable(otherVariable.regex, variable.name) ||
  73. this.templateSrv.containsVariable(otherVariable.query, variable.name) ||
  74. this.templateSrv.containsVariable(otherVariable.datasource, variable.name)) {
  75. return this.updateOptions(otherVariable);
  76. }
  77. });
  78. return this.$q.all(promises);
  79. }
  80. }
  81. coreModule.service('variableSrv', VariableSrv);