<commit_before>'use strict';

const _ = require('lodash');
const Api = require('kubernetes-client');
const BbPromise = require('bluebird');
const helpers = require('../lib/helpers');
const moment = require('moment');

class KubelessLogs {
  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options || {};
    this.provider = this.serverless.getProvider('google');
    this.commands = {
      logs: {
        usage: 'Output the logs of a deployed function',
        lifecycleEvents: [
          'logs',
        ],
        options: {
          count: {
            usage: 'Number of lines to print',
            shortcut: 'n',
          },
        },
      },
    };
    this.hooks = {
      'logs:logs': () => BbPromise.bind(this)
        .then(this.validate)
        .then(this.logsFunction)
        .then(() => {
          if (this.options.tail) {
            let m = moment.utc().valueOf();
            setInterval(() => {
              this.printLogs({
                startTime: m,
                count: null,
              });
              m = moment.utc().valueOf();
            }, this.options.interval || 1000);
          }
        }),
    };
  }

  validate() {
    helpers.validateEnv();
    const unsupportedOptions = ['stage', 'region'];
    helpers.warnUnsupportedOptions(
      unsupportedOptions,
      this.options,
      this.serverless.cli.log.bind(this.serverless.cli)
    );
    return BbPromise.resolve();
  }

  filterLogs(logs, options) {
    const opts = _.defaults({}, options, {
      startTime: null,
      count: null,
      filter: null,
    });
    let logEntries = _.compact(logs.split('\n'));
    if (opts.count) {
      logEntries = logEntries.slice(logEntries.length - opts.count);
    }
    if (opts.filter) {
      logEntries = _.filter(logEntries, entry => !!entry.match(opts.filter));
    }
    if (opts.startTime) {
      const since = !!opts.startTime.toString().match(/(?:m|h|d)/);
      let startMoment = null;
      if (since) {
        startMoment = moment.utc().subtract(
          opts.startTime.replace(/\D/g, ''),
          opts.startTime.replace(/\d/g, '')
        ).valueOf();
      } else {
        startMoment = moment.utc(opts.startTime).valueOf();
      }
      const logIndex = _.findIndex(logEntries, entry => {
        const entryDate = entry.match(/(\d{2}\/[a-zA-Z]{3}\/\d{4}:\d{2}:\d{2}:\d{2})/);
        if (entryDate) {
          const entryMoment = moment.utc(entryDate[1], 'DD/MMM/YYYY:hh:mm:ss').valueOf();
          return entryMoment >= startMoment;
        }
        return false;
      });
      if (logIndex > -1) {
        logEntries = logEntries.slice(logIndex);
      } else {
        // There is no entry after the given startTime
        logEntries = [];
      }
    }
    return logEntries.join('\n');
  }

  printLogs(options) {
    const opts = _.defaults({}, options, {
      startTime: this.options.startTime,
      count: this.options.count,
      filter: this.options.filter,
      silent: false,
    });
    const core = new Api.Core(
      Object.assign(helpers.getMinikubeCredentials(), {
        url: process.env.KUBE_API_URL,
        group: 'k8s.io',
      })
    );
    return new BbPromise((resolve, reject) => {
      core.ns.pods.get((err, podsInfo) => {
        if (err) reject(err);
        const functionPod = _.find(
          podsInfo.items,
          (podInfo) => podInfo.metadata.labels.function === this.options.function
        );
        if (!functionPod) {
          reject(
            `Unable to find the pod for the function ${this.options.function}. ` +
            'Please ensure that there is a function deployed with that ID'
          );
        }
        core.ns.pods(functionPod.metadata.name).log.get((errLog, logs) => {
          if (errLog) reject(errLog);
          const filteredLogs = this.filterLogs(logs, opts);
          if (!_.isEmpty(filteredLogs)) {
            if (!opts.silent) {
              console.log(filteredLogs);
            }
          }
          return BbPromise.resolve(filteredLogs);
        });
      });
    });
  }

  logsFunction() {
    this.serverless.cli.log(`Retrieving logs of ${this.options.function}...`);
    this.printLogs();
  }
}

module.exports = KubelessLogs;<commit_msg>Throw error instead of rejecting the promise<commit_after>'use strict';

const _ = require('lodash');
const Api = require('kubernetes-client');
const BbPromise = require('bluebird');
const helpers = require('../lib/helpers');
const moment = require('moment');

class KubelessLogs {
  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options || {};
    this.provider = this.serverless.getProvider('google');
    this.commands = {
      logs: {
        usage: 'Output the logs of a deployed function',
        lifecycleEvents: [
          'logs',
        ],
        options: {
          count: {
            usage: 'Number of lines to print',
            shortcut: 'n',
          },
        },
      },
    };
    this.hooks = {
      'logs:logs': () => BbPromise.bind(this)
        .then(this.validate)
        .then(this.logsFunction)
        .then(() => {
          if (this.options.tail) {
            let m = moment.utc().valueOf();
            setInterval(() => {
              this.printLogs({
                startTime: m,
                count: null,
              });
              m = moment.utc().valueOf();
            }, this.options.interval || 1000);
          }
        }),
    };
  }

  validate() {
    helpers.validateEnv();
    const unsupportedOptions = ['stage', 'region'];
    helpers.warnUnsupportedOptions(
      unsupportedOptions,
      this.options,
      this.serverless.cli.log.bind(this.serverless.cli)
    );
    return BbPromise.resolve();
  }

  filterLogs(logs, options) {
    const opts = _.defaults({}, options, {
      startTime: null,
      count: null,
      filter: null,
    });
    let logEntries = _.compact(logs.split('\n'));
    if (opts.count) {
      logEntries = logEntries.slice(logEntries.length - opts.count);
    }
    if (opts.filter) {
      logEntries = _.filter(logEntries, entry => !!entry.match(opts.filter));
    }
    if (opts.startTime) {
      const since = !!opts.startTime.toString().match(/(?:m|h|d)/);
      let startMoment = null;
      if (since) {
        startMoment = moment.utc().subtract(
          opts.startTime.replace(/\D/g, ''),
          opts.startTime.replace(/\d/g, '')
        ).valueOf();
      } else {
        startMoment = moment.utc(opts.startTime).valueOf();
      }
      const logIndex = _.findIndex(logEntries, entry => {
        const entryDate = entry.match(/(\d{2}\/[a-zA-Z]{3}\/\d{4}:\d{2}:\d{2}:\d{2})/);
        if (entryDate) {
          const entryMoment = moment.utc(entryDate[1], 'DD/MMM/YYYY:hh:mm:ss').valueOf();
          return entryMoment >= startMoment;
        }
        return false;
      });
      if (logIndex > -1) {
        logEntries = logEntries.slice(logIndex);
      } else {
        // There is no entry after the given startTime
        logEntries = [];
      }
    }
    return logEntries.join('\n');
  }

  printLogs(options) {
    const opts = _.defaults({}, options, {
      startTime: this.options.startTime,
      count: this.options.count,
      filter: this.options.filter,
      silent: false,
    });
    const core = new Api.Core(
      Object.assign(helpers.getMinikubeCredentials(), {
        url: process.env.KUBE_API_URL,
        group: 'k8s.io',
      })
    );
    return new BbPromise((resolve) => {
      core.ns.pods.get((err, podsInfo) => {
        if (err) throw new this.serverless.classes.Error(err);
        const functionPod = _.find(
          podsInfo.items,
          (podInfo) => podInfo.metadata.labels.function === this.options.function
        );
        if (!functionPod) {
          throw new this.serverless.classes.Error(
            `Unable to find the pod for the function ${this.options.function}. ` +
            'Please ensure that there is a function deployed with that ID'
          );
        }
        core.ns.pods(functionPod.metadata.name).log.get((errLog, logs) => {
          if (errLog) throw new this.serverless.classes.Error(errLog);
          const filteredLogs = this.filterLogs(logs, opts);
          if (!_.isEmpty(filteredLogs)) {
            if (!opts.silent) {
              console.log(filteredLogs);
            }
          }
          return resolve(filteredLogs);
        });
      });
    });
  }

  logsFunction() {
    this.serverless.cli.log(`Retrieving logs of ${this.options.function}...`);
    this.printLogs();
  }
}

module.exports = KubelessLogs;
{'fraction_non_alphanumeric': 0.15163081193615544, 'fraction_numerical': 0.003007170946102244, 'mean_word_length': 1.9920415224913495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '2317141', 'n_tokens_mistral': 2649, 'n_tokens_neox': 2626, 'n_words': 500}