<commit_before>'use strict'

const _ = require('lodash'),
      request = require('request'),
      urljoin = require('url-join');

class Client {
  constructor(options) {
    const _defaultOptions = {
      endpoint: null,
      token: null,
      intervalInSeconds: 5 * 60,
      requestTimeoutInSeconds: 1 * 60,
      production: process.env.NODE_ENV === 'production',
      debug: false
    };

    const mergedOptions = _.extend(
      _.clone(_defaultOptions),
      _.clone(options)
    );

    this.endpoint = mergedOptions.endpoint;
    this.intervalInSeconds = mergedOptions.intervalInSeconds;
    this.requestTimeoutInSeconds = mergedOptions.requestTimeoutInSeconds;
    this.token = mergedOptions.token;
    this.production = mergedOptions.production;
    this.debug = mergedOptions.debug;

    if(!this.endpoint) {
      throw new Error('An endpoint must be provided!');
    }

    if(!this.token) {
      throw new Error('A token must be provided!');
    }

    if(this.intervalInSeconds <= this.requestTimeoutInSeconds) {
      throw new Error(`The interval (${this.intervalInSeconds}s) must be greater than the request timeout (${this.requestTimeoutInSeconds}s)`);
    }

    if(!this.production) {
      console.log('meerkat-client:', 'The production option is set to false. No API calls will be made');
    }

    if(this.debug) {
      console.log('meerkat-client:', 'Debug mode is active');
    }

    this._sendPing();

    setInterval(() => {
        this._sendPing();
      }.bind(this),
      this.intervalInSeconds * 1000
    );
  }

  _sendPing() {
    const options = {
      url: urljoin(this.endpoint, '/api/v1/heartbeat'),
      method: 'POST',
      json: true,
      timeout: this.requestTimeoutInSeconds * 1000,
      headers: {
        'X-MEERKAT-TOKEN': this.token
      }
    };

    if(!this.production) {
      return;
    }

    request(options, (error, response, body) => {
      if(this.debug) {
        if(error) {
          return console.error('meerkat-client:', 'Failed to send meerkat heartbeat:', error);
        }

        console.log('meerkat-client:', 'heartbeat sent!');
      }
    });
  }

}

module.exports = Client<commit_msg>renamed ping to heatbeat<commit_after>'use strict'

const _ = require('lodash'),
      request = require('request'),
      urljoin = require('url-join');

class Client {
  constructor(options) {
    const _defaultOptions = {
      endpoint: null,
      token: null,
      intervalInSeconds: 5 * 60,
      requestTimeoutInSeconds: 1 * 60,
      production: process.env.NODE_ENV === 'production',
      debug: false
    };

    const mergedOptions = _.extend(
      _.clone(_defaultOptions),
      _.clone(options)
    );

    this.endpoint = mergedOptions.endpoint;
    this.intervalInSeconds = mergedOptions.intervalInSeconds;
    this.requestTimeoutInSeconds = mergedOptions.requestTimeoutInSeconds;
    this.token = mergedOptions.token;
    this.production = mergedOptions.production;
    this.debug = mergedOptions.debug;

    if(!this.endpoint) {
      throw new Error('An endpoint must be provided!');
    }

    if(!this.token) {
      throw new Error('A token must be provided!');
    }

    if(this.intervalInSeconds <= this.requestTimeoutInSeconds) {
      throw new Error(`The interval (${this.intervalInSeconds}s) must be greater than the request timeout (${this.requestTimeoutInSeconds}s)`);
    }

    if(!this.production) {
      console.log('meerkat-client:', 'The production option is set to false. No API calls will be made');
    }

    if(this.debug) {
      console.log('meerkat-client:', 'Debug mode is active');
    }

    this._sendHeartbeat();

    setInterval(() => {
        this._sendHeartbeat();
      }.bind(this),
      this.intervalInSeconds * 1000
    );
  }

  _sendHeartbeat() {
    const options = {
      url: urljoin(this.endpoint, '/api/v1/heartbeat'),
      method: 'POST',
      json: true,
      timeout: this.requestTimeoutInSeconds * 1000,
      headers: {
        'X-MEERKAT-TOKEN': this.token
      }
    };

    if(!this.production) {
      return;
    }

    request(options, (error, response, body) => {
      if(this.debug) {
        if(error || response.statusCode !== 200) {
          return console.error('meerkat-client:', 'Failed to send meerkat heartbeat:', error);
        }

        console.log('meerkat-client:', 'heartbeat sent!');
      }
    });
  }

  _sendReport(type, payload, cb) {
    const options = {
      url: urljoin(this.endpoint, '/api/v1/report'),
      method: 'POST',
      json: true,
      body: { type: type, payload: JSON.stringify(payload) },
      timeout: this.requestTimeoutInSeconds * 1000,
      headers: {
        'X-MEERKAT-TOKEN': this.token
      }
    };

    request(options, (error, response, body) => {
      if(error || response.statusCode !== 201) {
        return cb(error || new Error(`Failed to send heartbeat. Server sent a HTTP ${response.statusCode}`));
      }

      return cb(null);
    });
  }

  reportError(error, cb) {
    return this._sendReport(
      'error',
      {
        message: error.message,
        stack: error.stack ? error.stack : null
      }, cb
    );
  }

}

module.exports = Client
{'fraction_non_alphanumeric': 0.1340782122905028, 'fraction_numerical': 0.007898285494124446, 'mean_word_length': 2.620641562064156, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '6897112', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1539, 'n_words': 340}