<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-06-20T15:09:51+10:00
 * @Email:  <EMAIL>
 * @Last modified by:   chris
 * @Last modified time: 2017-06-27T13:52:32+10:00
 */
import React from 'react';
import { reaction } from 'mobx';
import { inject, observer } from 'mobx-react';
import ReactGA from 'react-ga';
import { analytics, protocol } from '../../env';
import { AnalyticsEvents } from './Events';
import { Broker, EventType } from '../../helpers/broker';

@inject(allStores => ({
  store: allStores.store,
  config: allStores.config,
}))
@observer
export default class Analytics extends React.Component {
  constructor(props) {
    super(props);
    let siteUrl = '';
    const gaCode = analytics;
    if (process.env.NODE_ENV === 'development') {
      siteUrl = protocol + 'dev.dbkoda.com';
      ReactGA.initialize(gaCode.development, {
        debug: true,
        titleCase: false,
      });
    } else if (process.env.NODE_ENV === 'production') {
      siteUrl = protocol + 'electron.dbkoda.com';
      ReactGA.initialize(gaCode.prod, {
        titleCase: false,
      });
    }
    ReactGA.set({ page: siteUrl });

    if (this.props.config.settings.telemetryEnabled) {
      // TODO Get App Version
      const appVersion = this.props.store.version;
      this._sendEvent(AnalyticsEvents.APP_OPEN, 'App', appVersion);
    }

    /**
     * Reaction function for when a change occurs on the telemetryEnabled state
     * @param {function()} - The state that will trigger the reaction.
     * @param {function()} - The reaction to any change on the state.
    //  */
    reaction(
      () => this.props.config.settings.telemetryEnabled,
      (telemetryEnabled) => {
        if (!this.props.store.layout.optInVisible) {
          if (telemetryEnabled) {
            this._sendEvent(AnalyticsEvents.OPT_IN, 'App');
          } else {
            this._sendEvent(AnalyticsEvents.OPT_OUT, 'App');
          }
          this.props.config.save();
        }
      },
      { name: 'analyticsReactionToTelemetryChange' },
    );

    /**
     * send opt events when clicking ok button.
     */
    reaction(
      () => this.props.store.layout.optInVisible,
      (_) => {
        if (this.props.config.settings.telemetryEnabled) {
          this._sendEvent(AnalyticsEvents.OPT_IN, 'App');
        } else {
          this._sendEvent(AnalyticsEvents.OPT_OUT, 'App');
        }
        this.props.config.save();
      },
      { name: 'analyticsReactionToTelemetryChange' },
    );

    this._sendEvent = this._sendEvent.bind(this);
    this.newProfileCreated = this.newProfileCreated.bind(this);
    this.feedbackEvent = this.feedbackEvent.bind(this);
    this.keyFeatureEvent = this.keyFeatureEvent.bind(this);
    this.controllerActivity = this.controllerActivity.bind(this);
  }

  componentDidMount() {
    Broker.on(EventType.NEW_PROFILE_CREATED, this.newProfileCreated);
    Broker.on(EventType.FEEDBACK, this.feedbackEvent);
    Broker.on(EventType.FEATURE_USE, this.keyFeatureEvent);
    Broker.on(EventType.CONTROLLER_ACTIVITY, this.controllerActivity);
  }

  componentWillUnmount() {
    Broker.off(EventType.NEW_PROFILE_CREATED, this.newProfileCreated);
    Broker.off(EventType.FEEDBACK, this.feedbackEvent);
    Broker.off(EventType.FEATURE_USE, this.keyFeatureEvent);
    Broker.off(EventType.CONTROLLER_ACTIVITY, this.controllerActivity);
  }

  /**
   *  Function to be called after a new profile event has been received
   *  @param {Object} profile - An object that represents the newly created profile
   */
  newProfileCreated(profile) {
    if (this.props.config.settings.telemetryEnabled) {
      console.log(profile);
      let mongoInfo =
        '{ dbVersion: ' +
        profile.dbVersion +
        ', shellVersion: ' +
        profile.shellVersion +
        ', authorization: ' +
        profile.authorization;
      if (profile.hostRadio) {
        mongoInfo += ', connectionType: hostname';
      } else {
        mongoInfo += ', connectionType: url';<commit_msg>Added Replica Set to Telemetry.<commit_after>/*
 * 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-06-20T15:09:51+10:00
 * @Email:  <EMAIL>
 * @Last modified by:   chris
 * @Last modified time: 2017-06-27T13:52:32+10:00
 */
import React from 'react';
import { reaction } from 'mobx';
import { inject, observer } from 'mobx-react';
import ReactGA from 'react-ga';
import { analytics, protocol } from '../../env';
import { AnalyticsEvents } from './Events';
import { Broker, EventType } from '../../helpers/broker';

@inject(allStores => ({
  store: allStores.store,
  config: allStores.config,
}))
@observer
export default class Analytics extends React.Component {
  constructor(props) {
    super(props);
    let siteUrl = '';
    const gaCode = analytics;
    if (process.env.NODE_ENV === 'development') {
      siteUrl = protocol + 'dev.dbkoda.com';
      ReactGA.initialize(gaCode.development, {
        debug: true,
        titleCase: false,
      });
    } else if (process.env.NODE_ENV === 'production') {
      siteUrl = protocol + 'electron.dbkoda.com';
      ReactGA.initialize(gaCode.prod, {
        titleCase: false,
      });
    }
    ReactGA.set({ page: siteUrl });

    if (this.props.config.settings.telemetryEnabled) {
      // TODO Get App Version
      const appVersion = this.props.store.version;
      this._sendEvent(AnalyticsEvents.APP_OPEN, 'App', appVersion);
    }

    /**
     * Reaction function for when a change occurs on the telemetryEnabled state
     * @param {function()} - The state that will trigger the reaction.
     * @param {function()} - The reaction to any change on the state.
     //  */
    reaction(
      () => this.props.config.settings.telemetryEnabled,
      (telemetryEnabled) => {
        if (!this.props.store.layout.optInVisible) {
          if (telemetryEnabled) {
            this._sendEvent(AnalyticsEvents.OPT_IN, 'App');
          } else {
            this._sendEvent(AnalyticsEvents.OPT_OUT, 'App');
          }
          this.props.config.save();
        }
      },
      { name: 'analyticsReactionToTelemetryChange' },
    );

    /**
     * send opt events when clicking ok button.
     */
    reaction(
      () => this.props.store.layout.optInVisible,
      (_) => {
        if (this.props.config.settings.telemetryEnabled) {
          this._sendEvent(AnalyticsEvents.OPT_IN, 'App');
        } else {
          this._sendEvent(AnalyticsEvents.OPT_OUT, 'App');
        }
        this.props.config.save();
      },
      { name: 'analyticsReactionToTelemetryChange' },
    );

    this._sendEvent = this._sendEvent.bind(this);
    this.newProfileCreated = this.newProfileCreated.bind(this);
    this.feedbackEvent = this.feedbackEvent.bind(this);
    this.keyFeatureEvent = this.keyFeatureEvent.bind(this);
    this.controllerActivity = this.controllerActivity.bind(this);
  }

  componentDidMount() {
    Broker.on(EventType.NEW_PROFILE_CREATED, this.newProfileCreated);
    Broker.on(EventType.FEEDBACK, this.feedbackEvent);
    Broker.on(EventType.FEATURE_USE, this.keyFeatureEvent);
    Broker.on(EventType.CONTROLLER_ACTIVITY, this.controllerActivity);
  }

  componentWillUnmount() {
    Broker.off(EventType.NEW_PROFILE_CREATED, this.newProfileCreated);
    Broker.off(EventType.FEEDBACK, this.feedbackEvent);
    Broker.off(EventType.FEATURE_USE, this.keyFeatureEvent);
    Broker.off(EventType.CONTROLLER_ACTIVITY, this.controllerActivity);
  }

  /**
   *  Function to be called after a new profile event has been received
   *  @param {Object} profile - An object that represents the newly created profile
   */
  newProfileCreated(profile) {
    if (this.props.config.settings.telemetryEnabled) {
      console.log(profile);
      let mongoInfo =
        '{ dbVersion: ' +
        profile.dbVersion +
        ', shellVersion: ' +
        profile.shellVersion +
        ', instanceType: ' +
        profile.mongoType +
        ', authorization: ' +
        profile.authorization;
      if (profile.hostRadio) {
        mongoInfo += ', connectionType: hostname';
      } else {
        mongoInfo += ', connectionType: url';
{'fraction_non_alphanumeric': 0.1197110423116615, 'fraction_numerical': 0.009287925696594427, 'mean_word_length': 2.939430894308943, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '657550', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2869, 'n_words': 717}