filterSrv.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. 'kbn'
  6. ], function (angular, _, config, kbn) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.factory('filterSrv', function($rootScope, $timeout, $routeParams) {
  10. var result = {
  11. updateTemplateData: function(initial) {
  12. var _templateData = {};
  13. _.each(this.templateParameters, function(templateParameter) {
  14. if (initial) {
  15. var urlValue = $routeParams[ templateParameter.name ];
  16. if (urlValue) {
  17. templateParameter.current = { text: urlValue, value: urlValue };
  18. }
  19. }
  20. if (!templateParameter.current || !templateParameter.current.value) {
  21. return;
  22. }
  23. _templateData[templateParameter.name] = templateParameter.current.value;
  24. });
  25. this._templateData = _templateData;
  26. },
  27. addTemplateParameter: function(templateParameter) {
  28. this.templateParameters.push(templateParameter);
  29. this.updateTemplateData();
  30. },
  31. applyTemplateToTarget: function(target) {
  32. if (!target || target.indexOf('[[') === -1) {
  33. return target;
  34. }
  35. return _.template(target, this._templateData, this.templateSettings);
  36. },
  37. setTime: function(time) {
  38. _.extend(this.time, time);
  39. // disable refresh if we have an absolute time
  40. if (time.to !== 'now') {
  41. this.old_refresh = this.dashboard.refresh;
  42. this.dashboard.set_interval(false);
  43. }
  44. else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
  45. this.dashboard.set_interval(this.old_refresh);
  46. this.old_refresh = null;
  47. }
  48. $timeout(this.dashboard.emit_refresh, 0);
  49. },
  50. timeRange: function(parse) {
  51. var _t = this.time;
  52. if(_.isUndefined(_t) || _.isUndefined(_t.from)) {
  53. return false;
  54. }
  55. if(parse === false) {
  56. return {
  57. from: _t.from,
  58. to: _t.to
  59. };
  60. } else {
  61. var _from = _t.from;
  62. var _to = _t.to || new Date();
  63. return {
  64. from : kbn.parseDate(_from),
  65. to : kbn.parseDate(_to)
  66. };
  67. }
  68. },
  69. removeTemplateParameter: function(templateParameter) {
  70. this.templateParameters = _.without(this.templateParameters, templateParameter);
  71. this.dashboard.templating.list = this.templateParameters;
  72. },
  73. init: function(dashboard) {
  74. this.dashboard = dashboard;
  75. this.templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g };
  76. this.time = dashboard.time;
  77. this.templateParameters = dashboard.templating.list;
  78. this.updateTemplateData(true);
  79. }
  80. };
  81. return result;
  82. });
  83. });