query_ctrl.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import moment from 'moment';
  5. import * as dateMath from 'app/core/utils/datemath';
  6. import {QueryCtrl} from 'app/plugins/sdk';
  7. class PrometheusQueryCtrl extends QueryCtrl {
  8. static templateUrl = 'partials/query.editor.html';
  9. metric: any;
  10. resolutions: any;
  11. formats: any;
  12. oldTarget: any;
  13. suggestMetrics: any;
  14. linkToPrometheus: any;
  15. /** @ngInject */
  16. constructor($scope, $injector, private templateSrv) {
  17. super($scope, $injector);
  18. var target = this.target;
  19. target.expr = target.expr || '';
  20. target.intervalFactor = target.intervalFactor || 2;
  21. target.format = target.format || this.getDefaultFormat();
  22. this.metric = '';
  23. this.resolutions = _.map([1,2,3,4,5,10], function(f) {
  24. return {factor: f, label: '1/' + f};
  25. });
  26. this.formats = [
  27. {text: 'Time series', value: 'time_series'},
  28. {text: 'Table', value: 'table'},
  29. ];
  30. $scope.$on('typeahead-updated', () => {
  31. this.$scope.$apply(() => {
  32. this.target.expr += this.target.metric;
  33. this.metric = '';
  34. this.refreshMetricData();
  35. });
  36. });
  37. // called from typeahead so need this
  38. // here in order to ensure this ref
  39. this.suggestMetrics = (query, callback) => {
  40. console.log(this);
  41. this.datasource.performSuggestQuery(query).then(callback);
  42. };
  43. this.updateLink();
  44. }
  45. getDefaultFormat() {
  46. if (this.panelCtrl.panel.type === 'table') {
  47. return 'table';
  48. }
  49. return 'time_series';
  50. }
  51. refreshMetricData() {
  52. if (!_.isEqual(this.oldTarget, this.target)) {
  53. this.oldTarget = angular.copy(this.target);
  54. this.panelCtrl.refresh();
  55. this.updateLink();
  56. }
  57. }
  58. updateLink() {
  59. var range = this.panelCtrl.range;
  60. if (!range) {
  61. return;
  62. }
  63. var rangeDiff = Math.ceil((range.to.valueOf() - range.from.valueOf()) / 1000);
  64. var endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
  65. var expr = {
  66. expr: this.templateSrv.replace(this.target.expr, this.panelCtrl.panel.scopedVars, this.datasource.interpolateQueryExpr),
  67. range_input: rangeDiff + 's',
  68. end_input: endTime,
  69. step_input: this.target.step,
  70. stacked: this.panelCtrl.panel.stack,
  71. tab: 0
  72. };
  73. var hash = encodeURIComponent(JSON.stringify([expr]));
  74. this.linkToPrometheus = this.datasource.directUrl + '/graph#' + hash;
  75. }
  76. getCollapsedText() {
  77. return this.target.expr;
  78. }
  79. }
  80. export {PrometheusQueryCtrl};