<commit_before>import React, { PureComponent } from 'react';
import { Button, getTheme, JSONFormatter, LoadingPlaceholder } from 'src/packages/datav-core/src';
import { AppEvents, PanelEvents, DataFrame } from 'src/packages/datav-core/src';


import { CopyToClipboard } from 'src/views/components/CopyToClipboard/CopyToClipboard';
import { PanelModel } from 'src/views/dashboard/model';
import { getPanelInspectorStyles } from './styles';


import { css } from 'emotion';
import { Unsubscribable } from 'rxjs';

import appEvents from 'src/core/library/utils/app_events';
import { backendSrv } from 'src/core/services/backend';
import { supportsDataQuery } from 'src/views/dashboard/components/PanelEditor/utils';

interface DsQuery {
  isLoading: boolean;
  response: {};
}

interface ExecutedQueryInfo {
  refId: string;
  query: string;
  frames: number;
  rows: number;
}

interface Props {
  panel: PanelModel;
  data: DataFrame[];
}

interface State {
  allNodesExpanded: boolean | null;
  isMocking: boolean;
  mockedResponse: string;
  dsQuery: DsQuery;
  executedQueries: ExecutedQueryInfo[];
}

export class QueryInspector extends PureComponent<Props, State> {
  formattedJson: any;
  clipboard: any;
  subscription?: Unsubscribable;

  constructor(props: Props) {
    super(props);
    this.state = {
      executedQueries: [],
      allNodesExpanded: null,
      isMocking: false,
      mockedResponse: '',
      dsQuery: {
        isLoading: false,
        response: {},
      },
    };
  }

  componentDidMount() {
    this.subscription = backendSrv.getInspectorStream().subscribe({
      next: response => this.onDataSourceResponse(response),
    });

    this.props.panel.events.on(PanelEvents.refresh, this.onPanelRefresh);
    this.updateQueryList();
  }

  componentDidUpdate(oldProps: Props) {
    if (this.props.data !== oldProps.data) {
      this.updateQueryList();
    }
  }

  /**
   * Find the list of executed queries
   */
  updateQueryList() {
    const { data } = this.props;
    const executedQueries: ExecutedQueryInfo[] = [];

    if (data?.length) {
      let last: ExecutedQueryInfo | undefined = undefined;

      data.forEach((frame, idx) => {
        const query = frame.meta?.executedQueryString;

        if (query) {
          const refId = frame.refId || '?';

          if (last?.refId === refId) {
            last.frames++;
            last.rows += frame.length;
          } else {
            last = {
              refId,
              frames: 0,
              rows: frame.length,
              query,
            };
            executedQueries.push(last);
          }
        }
      });
    }

    this.setState({ executedQueries });
  }

  onIssueNewQuery = () => {
    this.props.panel.refresh();
  };

  componentWillUnmount() {
    const { panel } = this.props;

    if (this.subscription) {
      this.subscription.unsubscribe();
    }

    panel.events.off(PanelEvents.refresh, this.onPanelRefresh);
  }

  onPanelRefresh = () => {
    this.setState(prevState => ({
      ...prevState,
      dsQuery: {
        isLoading: true,
        response: {},
      },
    }));
  };

  onDataSourceResponse(response: any) {
    // ignore silent requests
    if (response.config?.hideFromInspector) {
      return;
    }

    response = { ...response }; // clone - dont modify the response

    if (response.headers) {
      delete response.headers;
    }

    if (response.config) {
      response.request = response.config;

      delete response.config;
      delete response.request.transformRequest;
      delete response.request.transformResponse;
      delete response.request.paramSerializer;
      delete response.request.jsonpCallbackParam;
      delete response.request.headers;
      delete response.request.requestId;
      delete response.request.inspect;
      delete response.request.retry;
      delete response.request.timeout;<commit_msg>fix a bug about inspector<commit_after>import React, { PureComponent } from 'react';
import { Button, getTheme, JSONFormatter, LoadingPlaceholder } from 'src/packages/datav-core/src';
import { AppEvents, PanelEvents, DataFrame } from 'src/packages/datav-core/src';


import { CopyToClipboard } from 'src/views/components/CopyToClipboard/CopyToClipboard';
import { PanelModel } from 'src/views/dashboard/model';
import { getPanelInspectorStyles } from './styles';


import { css } from 'emotion';
import { Unsubscribable } from 'rxjs';

import appEvents from 'src/core/library/utils/app_events';
import { backendSrv } from 'src/core/services/backend';
import { supportsDataQuery } from 'src/views/dashboard/components/PanelEditor/utils';
import { CoreEvents } from 'src/types';

interface DsQuery {
  isLoading: boolean;
  response: {};
}

interface ExecutedQueryInfo {
  refId: string;
  query: string;
  frames: number;
  rows: number;
}

interface Props {
  panel: PanelModel;
  data: DataFrame[];
}

interface State {
  allNodesExpanded: boolean | null;
  isMocking: boolean;
  mockedResponse: string;
  dsQuery: DsQuery;
  executedQueries: ExecutedQueryInfo[];
}

export class QueryInspector extends PureComponent<Props, State> {
  formattedJson: any;
  clipboard: any;
  subscription?: Unsubscribable;

  constructor(props: Props) {
    super(props);
    this.state = {
      executedQueries: [],
      allNodesExpanded: null,
      isMocking: false,
      mockedResponse: '',
      dsQuery: {
        isLoading: false,
        response: {},
      },
    };
  }

  componentDidMount() {
    appEvents.on(CoreEvents.dsRequestResponse, (res) => this.onDataSourceResponse(res.data));

    this.props.panel.events.on(PanelEvents.refresh, this.onPanelRefresh);
    this.updateQueryList();
  }

  componentDidUpdate(oldProps: Props) {
    if (this.props.data !== oldProps.data) {
      this.updateQueryList();
    }
  }

  /**
   * Find the list of executed queries
   */
  updateQueryList() {
    const { data } = this.props;
    const executedQueries: ExecutedQueryInfo[] = [];

    if (data?.length) {
      let last: ExecutedQueryInfo | undefined = undefined;

      data.forEach((frame, idx) => {
        const query = frame.meta?.executedQueryString;

        if (query) {
          const refId = frame.refId || '?';

          if (last?.refId === refId) {
            last.frames++;
            last.rows += frame.length;
          } else {
            last = {
              refId,
              frames: 0,
              rows: frame.length,
              query,
            };
            executedQueries.push(last);
          }
        }
      });
    }

    this.setState({ executedQueries });
  }

  onIssueNewQuery = () => {
    this.props.panel.refresh();
  };

  componentWillUnmount() {
    const { panel } = this.props;

    if (this.subscription) {
      this.subscription.unsubscribe();
    }

    panel.events.off(PanelEvents.refresh, this.onPanelRefresh);
  }

  onPanelRefresh = () => {
    this.setState(prevState => ({
      ...prevState,
      dsQuery: {
        isLoading: true,
        response: {},
      },
    }));
  };

  onDataSourceResponse(response: any) {
    console.log(response)
    // ignore silent requests
    if (response.config?.hideFromInspector) {
      return;
    }

    response = { ...response }; // clone - dont modify the response

    if (response.headers) {
      delete response.headers;
    }

    if (response.config) {
      response.request = response.config;

      delete response.config;
      delete response.request.transformRequest;
      delete response.request.transformResponse;
      delete response.request.paramSerializer;
      delete response.request.jsonpCallbackParam;
      delete response.request.headers;
      delete response.request.requestId;
      delete response.request.inspect;
      delete response.request.retry;
      delete response.request.timeout;
{'fraction_non_alphanumeric': 0.12188895979578813, 'fraction_numerical': 0.00025526483726866624, 'mean_word_length': 2.713744075829384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3516847', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2292, 'n_words': 472}