query_troubleshooter.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(): string {
  62. if (this.jsonExplorer) {
  63. return JSON.stringify(this.jsonExplorer.json, null, 2);
  64. }
  65. return '';
  66. }
  67. onRequestResponse(data) {
  68. // ignore if closed
  69. if (!this.isOpen) {
  70. return;
  71. }
  72. this.isLoading = false;
  73. data = _.cloneDeep(data);
  74. if (data.headers) {
  75. delete data.headers;
  76. }
  77. if (data.config) {
  78. data.request = data.config;
  79. delete data.config;
  80. delete data.request.transformRequest;
  81. delete data.request.transformResponse;
  82. delete data.request.paramSerializer;
  83. delete data.request.jsonpCallbackParam;
  84. delete data.request.headers;
  85. delete data.request.requestId;
  86. delete data.request.inspect;
  87. delete data.request.retry;
  88. delete data.request.timeout;
  89. }
  90. if (data.data) {
  91. data.response = data.data;
  92. if (data.status === 200) {
  93. // if we are in error state, assume we automatically opened
  94. // and auto close it again
  95. if (this.hasError) {
  96. this.hasError = false;
  97. this.isOpen = false;
  98. }
  99. }
  100. delete data.data;
  101. delete data.status;
  102. delete data.statusText;
  103. delete data.$$config;
  104. }
  105. this.$timeout(_.partial(this.renderJsonExplorer, data));
  106. }
  107. toggleExpand(depth) {
  108. if (this.jsonExplorer) {
  109. this.allNodesExpanded = !this.allNodesExpanded;
  110. this.jsonExplorer.openAtDepth(this.allNodesExpanded ? 20 : 1);
  111. }
  112. }
  113. }
  114. export function queryTroubleshooter() {
  115. return {
  116. restrict: 'E',
  117. template: template,
  118. controller: QueryTroubleshooterCtrl,
  119. bindToController: true,
  120. controllerAs: 'ctrl',
  121. scope: {
  122. panelCtrl: "=",
  123. isOpen: "=",
  124. },
  125. link: function(scope, elem, attrs, ctrl) {
  126. ctrl.renderJsonExplorer = function(data) {
  127. var jsonElem = elem.find('.query-troubleshooter-json');
  128. ctrl.jsonExplorer = new JsonExplorer(data, 3, {
  129. animateOpen: true,
  130. });
  131. const html = ctrl.jsonExplorer.render(true);
  132. jsonElem.html(html);
  133. };
  134. }
  135. };
  136. }
  137. coreModule.directive('queryTroubleshooter', queryTroubleshooter);