metrics_ds_selector.ts 4.1 KB

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