metrics_ds_selector.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import appEvents from 'app/core/app_events';
  5. var module = angular.module('grafana.directives');
  6. var template = `
  7. <div class="gf-form-group" ng-if="ctrl.showResponse">
  8. <response-viewer response="ctrl.responseData" />
  9. </div>
  10. <div class="gf-form-group">
  11. <div class="gf-form-inline">
  12. <div class="gf-form">
  13. <label class="gf-form-label">
  14. <i class="icon-gf icon-gf-datasources"></i>
  15. </label>
  16. <label class="gf-form-label">
  17. Data Source
  18. </label>
  19. <metric-segment segment="ctrl.dsSegment"
  20. get-options="ctrl.getOptions(true)"
  21. on-change="ctrl.datasourceChanged()"></metric-segment>
  22. </div>
  23. <div class="gf-form gf-form--offset-1">
  24. <button class="btn btn-inverse gf-form-btn" ng-click="ctrl.addDataQuery()" ng-hide="ctrl.current.meta.mixed">
  25. <i class="fa fa-plus"></i>&nbsp;
  26. Add Query
  27. </button>
  28. <div class="dropdown" ng-if="ctrl.current.meta.mixed">
  29. <metric-segment segment="ctrl.mixedDsSegment"
  30. get-options="ctrl.getOptions(false)"
  31. on-change="ctrl.mixedDatasourceChanged()"></metric-segment>
  32. </div>
  33. </div>
  34. <div class="gf-form gf-form--offset-1">
  35. <button class="btn btn-inverse gf-form-btn" ng-click="ctrl.toggleShowResponse()" ng-show="ctrl.responseData">
  36. <i class="fa fa-binoculars"></i>&nbsp;
  37. Request & Response
  38. </button>
  39. </div>
  40. </div>
  41. </div>
  42. `;
  43. export class MetricsDsSelectorCtrl {
  44. dsSegment: any;
  45. mixedDsSegment: any;
  46. dsName: string;
  47. panelCtrl: any;
  48. datasources: any[];
  49. current: any;
  50. lastResponse: any;
  51. responseData: any;
  52. showResponse: boolean;
  53. /** @ngInject */
  54. constructor($scope, private uiSegmentSrv, datasourceSrv) {
  55. this.datasources = datasourceSrv.getMetricSources();
  56. var dsValue = this.panelCtrl.panel.datasource || null;
  57. for (let ds of this.datasources) {
  58. if (ds.value === dsValue) {
  59. this.current = ds;
  60. }
  61. }
  62. if (!this.current) {
  63. this.current = {name: dsValue + ' not found', value: null};
  64. }
  65. this.dsSegment = uiSegmentSrv.newSegment({value: this.current.name, selectMode: true});
  66. this.mixedDsSegment = uiSegmentSrv.newSegment({value: 'Add Query', selectMode: true});
  67. appEvents.on('ds-request-response', this.onRequestResponse.bind(this), $scope);
  68. appEvents.on('ds-request-error', this.onRequestError.bind(this), $scope);
  69. }
  70. onRequestResponse(data) {
  71. this.responseData = data;
  72. }
  73. toggleShowResponse() {
  74. this.showResponse = !this.showResponse;
  75. }
  76. onRequestError(err) {
  77. this.responseData = err;
  78. this.responseData.isError = true;
  79. this.showResponse = true;
  80. }
  81. getOptions(includeBuiltin) {
  82. return Promise.resolve(this.datasources.filter(value => {
  83. return includeBuiltin || !value.meta.builtIn;
  84. }).map(value => {
  85. return this.uiSegmentSrv.newSegment(value.name);
  86. }));
  87. }
  88. datasourceChanged() {
  89. var ds = _.find(this.datasources, {name: this.dsSegment.value});
  90. if (ds) {
  91. this.current = ds;
  92. this.panelCtrl.setDatasource(ds);
  93. this.responseData = null;
  94. }
  95. }
  96. mixedDatasourceChanged() {
  97. var target: any = {isNew: true};
  98. var ds = _.find(this.datasources, {name: this.mixedDsSegment.value});
  99. if (ds) {
  100. target.datasource = ds.name;
  101. this.panelCtrl.panel.targets.push(target);
  102. this.mixedDsSegment.value = '';
  103. }
  104. }
  105. addDataQuery() {
  106. var target: any = {isNew: true};
  107. this.panelCtrl.panel.targets.push(target);
  108. }
  109. }
  110. module.directive('metricsDsSelector', function() {
  111. return {
  112. restrict: 'E',
  113. template: template,
  114. controller: MetricsDsSelectorCtrl,
  115. bindToController: true,
  116. controllerAs: 'ctrl',
  117. transclude: true,
  118. scope: {
  119. panelCtrl: "="
  120. }
  121. };
  122. });