<commit_before>/*
 * dbKoda - a modern, open source code editor, for MongoDB.
 * Copyright (C) 2017-2018 Southbank Software
 *
 * This file is part of dbKoda.
 *
 * dbKoda is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * dbKoda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with dbKoda.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @Author: chris
 * @Date:   2017-04-20T17:58:30+10:00
 * @Email:  <EMAIL>
 * @Last modified by:   wahaj
 * @Last modified time: 2017-07-06T10:14:08+10:00
 */

/**
 * explain component is used to handle explain output
 */
import React from 'react';
import { inject, observer } from 'mobx-react';
import { action, observable } from 'mobx';
import _ from 'lodash';
import Panel from './Panel';
import { Broker, EventType } from '../../helpers/broker/index';

export const parseOutput = (output) => {
  return output
    .replace(/NumberLong\((\d*)\)/g, '$1')
    .replace(/\n/g, '')
    .replace(/\s/g, '')
    .replace(/\r/g, '')
    .replace(/:(\/[^\/]*\/)/g, ':"$1"');
};

@inject(allStores => ({
  explainPanel: allStores.store.explainPanel,
  editors: allStores.store.editors,
  outputPanel: allStores.store.outputPanel,
}))
@observer
export default class Explain extends React.Component {
  constructor(props) {
    super(props);

    this.state = { viewType: 0 };
  }

  componentDidMount() {
    const { editor } = this.props;
    if (editor) {
      Broker.on(
        EventType.EXPLAIN_OUTPUT_AVAILABLE,
        this.explainOutputAvailable,
      );
    }
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.editor.explains) {
      this.setState({ viewType: nextProps.editor.explains.viewType });
    }
  }

  componentWillUnmount() {
    const { editor } = this.props;
    if (editor) {
      Broker.removeListener(
        EventType.EXPLAIN_OUTPUT_AVAILABLE,
        this.explainOutputAvailable,
      );
    }
  }

  @action.bound
  explainOutputAvailable({ id, shell, command, type, output }) {
    this.explainCommand = command;
    this.explainOutput = '';
    this.explainType = type;
    let currentEditorId = false;
    let currentEditor = null;
    this.props.editors.forEach((value, key) => {
      if (value.currentProfile === id && value.shellId === shell) {
        currentEditorId = key;
        currentEditor = value;
      }
    });

    if (!currentEditor) {
      console.log('can"t find editor by id:', id, shell);
      return;
    }
    // const currentEditor = editor;
    let explainOutputJson;
    try {
      explainOutputJson = {
        output: JSON.parse(output),
        type: this.explainType,
        command: this.explainCommand,
        viewType: 0,
      };
      if (explainOutputJson.output.stages && explainOutputJson.output.stages.length > 0) {
        // this is aggregate framework explain output, convert stages to regular stage
        const aggStages = explainOutputJson.output.stages;
        const converted = {queryPlanner: {winningPlan: {}}};<commit_msg>udpate profile id for explain output<commit_after>/*
 * dbKoda - a modern, open source code editor, for MongoDB.
 * Copyright (C) 2017-2018 <NAME>
 *
 * This file is part of dbKoda.
 *
 * dbKoda is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * dbKoda is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with dbKoda.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @Author: chris
 * @Date:   2017-04-20T17:58:30+10:00
 * @Email:  <EMAIL>
 * @Last modified by:   wahaj
 * @Last modified time: 2017-07-06T10:14:08+10:00
 */

/**
 * explain component is used to handle explain output
 */
import React from 'react';
import { inject, observer } from 'mobx-react';
import { action, observable } from 'mobx';
import _ from 'lodash';
import Panel from './Panel';
import { Broker, EventType } from '../../helpers/broker/index';

export const parseOutput = (output) => {
  return output
    .replace(/NumberLong\((\d*)\)/g, '$1')
    .replace(/\n/g, '')
    .replace(/\s/g, '')
    .replace(/\r/g, '')
    .replace(/:(\/[^\/]*\/)/g, ':"$1"');
};

@inject(allStores => ({
  explainPanel: allStores.store.explainPanel,
  editors: allStores.store.editors,
  outputPanel: allStores.store.outputPanel,
}))
@observer
export default class Explain extends React.Component {
  constructor(props) {
    super(props);

    this.state = { viewType: 0 };
  }

  componentDidMount() {
    const { editor } = this.props;
    if (editor) {
      Broker.on(
        EventType.EXPLAIN_OUTPUT_AVAILABLE,
        this.explainOutputAvailable,
      );
    }
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.editor.explains) {
      this.setState({ viewType: nextProps.editor.explains.viewType });
    }
  }

  componentWillUnmount() {
    const { editor } = this.props;
    if (editor) {
      Broker.removeListener(
        EventType.EXPLAIN_OUTPUT_AVAILABLE,
        this.explainOutputAvailable,
      );
    }
  }

  @action.bound
  explainOutputAvailable({ id, shell, command, type, output }) {
    this.explainCommand = command;
    this.explainOutput = '';
    this.explainType = type;
    let currentEditorId = false;
    let currentEditor = null;
    this.props.editors.forEach((value, key) => {
      if (value.profileId === id && value.shellId === shell) {
        currentEditorId = key;
        currentEditor = value;
      }
    });

    if (!currentEditor) {
      console.log('can"t find editor by id:', id, shell);
      return;
    }
    // const currentEditor = editor;
    let explainOutputJson;
    try {
      explainOutputJson = {
        output: JSON.parse(output),
        type: this.explainType,
        command: this.explainCommand,
        viewType: 0,
      };
      if (explainOutputJson.output.stages && explainOutputJson.output.stages.length > 0) {
        // this is aggregate framework explain output, convert stages to regular stage
        const aggStages = explainOutputJson.output.stages;
        const converted = {queryPlanner: {winningPlan: {}}};
{'fraction_non_alphanumeric': 0.12619669277632725, 'fraction_numerical': 0.01450536698578474, 'mean_word_length': 2.951289398280802, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1200106', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2105, 'n_words': 604}