query_troubleshooter.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import appEvents from 'app/core/app_events';
  4. import {coreModule, JsonExplorer} from 'app/core/core';
  5. const template = `
  6. <div class="query-troubleshooter" ng-if="ctrl.isOpen">
  7. <div class="query-troubleshooter__header">
  8. <a class="pointer" ng-click="ctrl.toggleExpand()" ng-hide="ctrl.allNodesExpanded">
  9. <i class="fa fa-plus-square-o"></i> Expand All
  10. </a>
  11. <a class="pointer" ng-click="ctrl.toggleExpand()" ng-show="ctrl.allNodesExpanded">
  12. <i class="fa fa-minus-square-o"></i> Collapse All
  13. </a>
  14. <a class="pointer" clipboard-button="ctrl.getClipboardText()"><i class="fa fa-clipboard"></i> Copy to Clipboard</a>
  15. </div>
  16. <div class="query-troubleshooter__body">
  17. <i class="fa fa-spinner fa-spin" ng-show="ctrl.isLoading"></i>
  18. <div class="query-troubleshooter-json"></div>
  19. </div>
  20. </div>
  21. `;
  22. export class QueryTroubleshooterCtrl {
  23. isOpen: any;
  24. isLoading: boolean;
  25. showResponse: boolean;
  26. panelCtrl: any;
  27. renderJsonExplorer: (data) => void;
  28. onRequestErrorEventListener: any;
  29. onRequestResponseEventListener: any;
  30. hasError: boolean;
  31. allNodesExpanded: boolean;
  32. jsonExplorer: JsonExplorer;
  33. /** @ngInject **/
  34. constructor($scope, private $timeout) {
  35. this.onRequestErrorEventListener = this.onRequestError.bind(this);
  36. this.onRequestResponseEventListener = this.onRequestResponse.bind(this);
  37. appEvents.on('ds-request-response', this.onRequestResponseEventListener);
  38. appEvents.on('ds-request-error', this.onRequestErrorEventListener);
  39. $scope.$on('$destroy', this.removeEventsListeners.bind(this));
  40. $scope.$watch('ctrl.isOpen', this.stateChanged.bind(this));
  41. }
  42. removeEventsListeners() {
  43. appEvents.off('ds-request-response', this.onRequestResponseEventListener);
  44. appEvents.off('ds-request-error', this.onRequestErrorEventListener);
  45. }
  46. onRequestError(err) {
  47. // ignore if closed
  48. if (!this.isOpen) {
  49. return;
  50. }
  51. this.isOpen = true;
  52. this.hasError = true;
  53. this.onRequestResponse(err);
  54. }
  55. stateChanged() {
  56. if (this.isOpen) {
  57. this.panelCtrl.refresh();
  58. this.isLoading = true;
  59. }
  60. }
  61. getClipboardText() {
  62. if (this.jsonExplorer) {
  63. return JSON.stringify(this.jsonExplorer.json, null, 2);
  64. }
  65. }
  66. onRequestResponse(data) {
  67. // ignore if closed
  68. if (!this.isOpen) {
  69. return;
  70. }
  71. this.isLoading = false;
  72. data = _.cloneDeep(data);
  73. if (data.headers) {
  74. delete data.headers;
  75. }
  76. if (data.config) {
  77. data.request = data.config;
  78. delete data.config;
  79. delete data.request.transformRequest;
  80. delete data.request.transformResponse;
  81. delete data.request.paramSerializer;
  82. delete data.request.jsonpCallbackParam;
  83. delete data.request.headers;
  84. delete data.request.requestId;
  85. delete data.request.inspect;
  86. delete data.request.retry;
  87. delete data.request.timeout;
  88. }
  89. if (data.data) {
  90. data.response = data.data;
  91. if (data.status === 200) {
  92. // if we are in error state, assume we automatically opened
  93. // and auto close it again
  94. if (this.hasError) {
  95. this.hasError = false;
  96. this.isOpen = false;
  97. }
  98. }
  99. delete data.data;
  100. delete data.status;
  101. delete data.statusText;
  102. delete data.$$config;
  103. }
  104. this.$timeout(_.partial(this.renderJsonExplorer, data));
  105. }
  106. toggleExpand(depth) {
  107. if (this.jsonExplorer) {
  108. this.allNodesExpanded = !this.allNodesExpanded;
  109. this.jsonExplorer.openAtDepth(this.allNodesExpanded ? 20 : 1);
  110. }
  111. }
  112. }
  113. export function queryTroubleshooter() {
  114. return {
  115. restrict: 'E',
  116. template: template,
  117. controller: QueryTroubleshooterCtrl,
  118. bindToController: true,
  119. controllerAs: 'ctrl',
  120. scope: {
  121. panelCtrl: "=",
  122. isOpen: "=",
  123. },
  124. link: function(scope, elem, attrs, ctrl) {
  125. ctrl.renderJsonExplorer = function(data) {
  126. var jsonElem = elem.find('.query-troubleshooter-json');
  127. ctrl.jsonExplorer = new JsonExplorer(data, 3, {
  128. animateOpen: true,
  129. });
  130. const html = ctrl.jsonExplorer.render(true);
  131. jsonElem.html(html);
  132. };
  133. }
  134. };
  135. }
  136. coreModule.directive('queryTroubleshooter', queryTroubleshooter);