<commit_before>'use strict';

const Command = require('cmnd').Command;
const APIResource = require('api-res');
const Credentials = require('../credentials.js');

const chalk = require('chalk');

class LogsCommand extends Command {

  constructor() {

    super('logs');

  }

  help() {

    return {
      description: 'Retrieves logs for a given service',
      args: ['service'],
      flags: {
        s: '<stream> The log stream you want to retrieve. Allowed values are "stdout" and "stderr"',
        n: '<number of lines> The number of log lines you want to retrieve',
        u: '<function> The specific function within the service that you want to retrieve logs from'
      },
      vflags: {
        stream: '<stream> The log stream you want to retrieve. Allowed values are "stdout" and "stderr"',
        num: '<number of lines> The number of log lines you want to retrieve',
        function: '<function> The specific function within the service that you want to retrieve logs from'
      }
    };

  }

  run(params, callback) {

    let host = 'api.polybit.com';
    let port = 443;

    let hostname = (params.flags.h && params.flags.h[0]) || '';
    let matches = hostname.match(/^(https?:\/\/)?(.*?)(:\d+)?$/);

    if (hostname && matches) {
      host = matches[2];
      port = parseInt((matches[3] || '').substr(1) || (hostname.indexOf('https') === 0 ? 443 : 80));
    }

    let serviceName = params.args[0];

    if (!serviceName) {
      return callback(new Error('Please specify a service'));
    }

    // Allow dot syntax to be in line with other parts of CLI
    serviceName = serviceName.replace(/\./, '/');

    if (serviceName.split('/').length !== 2) {
      return callback(new Error('Please specify services as <username>/<service name>'));
    }

    let stream = params.flags.s || params.vflags.stream || 'stdout';
    let requestedLineCount = params.flags.n || params.vflags.num || 100;
    let functionName = params.flags.u || params.vflags.function;

    let queryParams = {
      service_name: serviceName,
      stream: stream,
      count: requestedLineCount
    };

    if (functionName) {
      queryParams.function_name = functionName;
    }

    let resource = new APIResource(host, port);
    resource.authorize(Credentials.read('ACCESS_TOKEN'));

    resource.request('v1/logs/read').index(queryParams, (err, results) => {

      if (err) {
        return callback(err);
      }

      let logs = results.data[0].logs.split('\n');
      logs.map((log) => {

        if (!log) {
          return;
        }

        let logComponents = log.split('\t');

        if (stream === 'stderr') {
          console.log(chalk.red(logComponents[0]) + '\t' + logComponents[1]);
        } else {
          console.log(chalk.green(logComponents[0]) + '\t' + logComponents[1]);
        }

      });

      return callback(null);

    });
  }

}

module.exports = LogsCommand;<commit_msg>Update log code to use parameters properly<commit_after>'use strict';

const Command = require('cmnd').Command;
const APIResource = require('api-res');
const Credentials = require('../credentials.js');

const chalk = require('chalk');

const VALID_STREAMS = ['stdout', 'stderr'];
const STREAM_COLORS = {
  'stdout': 'green',
  'stderr': 'red'
};

class LogsCommand extends Command {

  constructor() {

    super('logs');

  }

  help() {

    return {
      description: 'Retrieves logs for a given service',
      args: ['service'],
      flags: {
        s: 'The log stream you want to retrieve. Allowed values are "stdout" and "stderr"',
        n: 'The number of log lines you want to retrieve',
        u: 'The specific function within the service that you want to retrieve logs from'
      },
      vflags: {
        stream: 'The log stream you want to retrieve. Allowed values are "stdout" and "stderr"',
        num: 'The number of log lines you want to retrieve',
        function: 'The specific function within the service that you want to retrieve logs from'
      }
    };

  }

  run(params, callback) {

    let host = 'api.polybit.com';
    let port = 443;

    let hostname = (params.flags.h && params.flags.h[0]) || '';
    let matches = hostname.match(/^(https?:\/\/)?(.*?)(:\d+)?$/);

    if (hostname && matches) {
      host = matches[2];
      port = parseInt((matches[3] || '').substr(1) || (hostname.indexOf('https') === 0 ? 443 : 80));
    }

    let serviceName = params.args[0] || '';

    // Allow dot syntax to be in line with other parts of CLI
    serviceName = serviceName.replace(/\./, '/');

    if (serviceName.split('/').length !== 2) {
      return callback(new Error('Please specify a service as <username>.<service>'));
    }

    let stream = (params.flags.s || params.vflags.stream || [])[0] || 'stdout';
    let requestedLineCount = Math.max(parseInt((params.flags.n || params.vflags.num || [])[0]) || 100, 1);
    let functionName = (params.flags.u || params.vflags.function || [])[0];

    if (VALID_STREAMS.indexOf(stream) === -1) {
      return callback(new Error(`Stream must be one of: ${VALID_STREAMS.join(', ')}`));
    }

    let queryParams = {
      service_name: serviceName,
      stream: stream,
      count: requestedLineCount
    };

    if (functionName) {
      queryParams.function_name = functionName;
    }

    let resource = new APIResource(host, port);
    resource.authorize(Credentials.read('ACCESS_TOKEN'));

    resource.request('v1/logs/read').index(queryParams, (err, results) => {

      if (err) {
        return callback(err);
      }

      console.log(
        results.data[0].logs.trim()
          .split('\n')
          .map(line => {
            let sline = line.split('\t');
            return chalk[STREAM_COLORS[stream]](sline[0]) + ' ' + sline.slice(1).join('\t');
          })
          .join('\n')
      );

      return callback(null);

    });
  }

}

module.exports = LogsCommand;
{'fraction_non_alphanumeric': 0.1570458404074703, 'fraction_numerical': 0.00865874363327674, 'mean_word_length': 2.6658369632856256, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '3907267', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1753, 'n_words': 493}