<commit_before>/**
 * Copyright DataStax, Inc.
 *
 * Please see the license for details:
 * http://www.datastax.com/terms/datastax-dse-driver-license-terms
 */

'use strict';

const os = require('os');
const path = require('path');
const fs = require('fs');
const utils = require('./utils');
const types = require('./types');
const requests = require('./requests');
const ExecutionOptions = require('./execution-options').ExecutionOptions;
const packageInfo = require('../package.json');
const VersionNumber = require('./types/version-number');

let kerberosModule;

try {
  // eslint-disable-next-line
  kerberosModule = require('kerberos');
}
catch (err) {
  // Kerberos is an optional dependency
}

const minDse6Version = new VersionNumber(6, 0, 5);
const minDse51Version = new VersionNumber(5, 1, 13);
const dse600Version = new VersionNumber(6, 0, 0);
const rpc = "CALL InsightsRpc.reportInsight(?)";
const maxStatusErrorLogs = 5;

/**
 * Contains methods and functionality to send events to DSE Insights.
 */
class InsightsClient {

  /**
   * Creates a new instance of the {@link InsightsClient} using the driver {@link Client}.
   * @param {Client} client
   * @param {Object} [options]
   * @param {Number} [options.statusEventDelay]
   * @param {Function} [options.errorCallback]
   */
  constructor(client, options) {
    this._client = client;
    this._sessionId = types.Uuid.random().toString();
    this._enabled = false;
    this._closed = false;
    this._firstTimeout = null;
    this._recurrentTimeout = null;
    this._statusErrorLogs = 0;

    options = options || {};

    this._statusEventDelay = options.statusEventDelay || 300000;
    this._errorCallback = options.errorCallback || utils.noop;
  }

  /**
   * Initializes the insights client in the background by sending the startup event and scheduling status events at
   * regular intervals.
   */
  init() {
    this._enabled = this._client.options.monitorReporting.enabled && this._dseSupportsInsights();
    if (!this._enabled) {
      return;
    }

    this._sendStartupEvent()
      .then(() => {

        if (this._closed) {
          // The client was shutdown
          return;
        }

        // Send the status event the first time with a delay containing some random portion
        // Initial delay should be statusEventDelay - (0 to 10%)
        const firstDelay = Math.floor(this._statusEventDelay - 0.1 * this._statusEventDelay * Math.random());

        // Schedule the first timer
        this._firstTimeout = setTimeout(() => {

          // Send the first status event, the promise will never be rejected
          this._sendStatusEvent();

          // The following status events are sent at regular intervals
          this._recurrentTimeout = setInterval(() => this._sendStatusEvent(), this._statusEventDelay);
        }, firstDelay);
      })
      .catch(err => {

        if (this._closed) {
          // Sending failed because the Client was shutdown
          return;
        }

        // We shouldn't try to recover
        this._client.log('error', `Insights startup message could not be sent (${err})`, err);
        this._errorCallback(err);
      });
  }

  /**<commit_msg>Lower log severity for Insights errors<commit_after>/**
 * Copyright DataStax, Inc.
 *
 * Please see the license for details:
 * http://www.datastax.com/terms/datastax-dse-driver-license-terms
 */

'use strict';

const os = require('os');
const path = require('path');
const fs = require('fs');
const utils = require('./utils');
const types = require('./types');
const requests = require('./requests');
const ExecutionOptions = require('./execution-options').ExecutionOptions;
const packageInfo = require('../package.json');
const VersionNumber = require('./types/version-number');

let kerberosModule;

try {
  // eslint-disable-next-line
  kerberosModule = require('kerberos');
}
catch (err) {
  // Kerberos is an optional dependency
}

const minDse6Version = new VersionNumber(6, 0, 5);
const minDse51Version = new VersionNumber(5, 1, 13);
const dse600Version = new VersionNumber(6, 0, 0);
const rpc = "CALL InsightsRpc.reportInsight(?)";
const maxStatusErrorLogs = 5;

/**
 * Contains methods and functionality to send events to DSE Insights.
 */
class InsightsClient {

  /**
   * Creates a new instance of the {@link InsightsClient} using the driver {@link Client}.
   * @param {Client} client
   * @param {Object} [options]
   * @param {Number} [options.statusEventDelay]
   * @param {Function} [options.errorCallback]
   */
  constructor(client, options) {
    this._client = client;
    this._sessionId = types.Uuid.random().toString();
    this._enabled = false;
    this._closed = false;
    this._firstTimeout = null;
    this._recurrentTimeout = null;
    this._statusErrorLogs = 0;

    options = options || {};

    this._statusEventDelay = options.statusEventDelay || 300000;
    this._errorCallback = options.errorCallback || utils.noop;
  }

  /**
   * Initializes the insights client in the background by sending the startup event and scheduling status events at
   * regular intervals.
   */
  init() {
    this._enabled = this._client.options.monitorReporting.enabled && this._dseSupportsInsights();
    if (!this._enabled) {
      return;
    }

    this._sendStartupEvent()
      .then(() => {

        if (this._closed) {
          // The client was shutdown
          return;
        }

        // Send the status event the first time with a delay containing some random portion
        // Initial delay should be statusEventDelay - (0 to 10%)
        const firstDelay = Math.floor(this._statusEventDelay - 0.1 * this._statusEventDelay * Math.random());

        // Schedule the first timer
        this._firstTimeout = setTimeout(() => {

          // Send the first status event, the promise will never be rejected
          this._sendStatusEvent();

          // The following status events are sent at regular intervals
          this._recurrentTimeout = setInterval(() => this._sendStatusEvent(), this._statusEventDelay);
        }, firstDelay);
      })
      .catch(err => {

        if (this._closed) {
          // Sending failed because the Client was shutdown
          return;
        }

        // We shouldn't try to recover
        this._client.log('verbose', `Insights startup message could not be sent (${err})`, err);
        this._errorCallback(err);
      });
  }

  /**
{'fraction_non_alphanumeric': 0.12431672653443698, 'fraction_numerical': 0.009058253943464002, 'mean_word_length': 2.962871287128713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '5072985', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1758, 'n_words': 527}