<commit_before>/*
 * Iris
 * (c) 2016+ Buenos Aires City Government
 * License: https://opensource.org/licenses/MIT
 *
 */

'use strict';

const Flow = require('./lib/Flow');
const BaseDock = require('./lib/bases/Dock');
const BaseHandler = require('./lib/bases/Handler');
const BaseHook = require('./lib/bases/Hook');
const dispatcher = require('./lib/Dispatcher');
const validator = require('propchecker');
const shortid = require('shortid');
const chalk = require('chalk');
const logger = require('winston');
const ora = require('ora');
const utils = require('./lib/utils/utils');

const consoleLog = utils.log;

class Iris {
    constructor() {
        this._name = 'main';
        this._config = {};
        this._flows = [];
        this._modules = [];
        this._logger = logger;

        this._logger.cli();
    }

    get config() {
        return this._config;
    }

    get logger() {
        return this._logger;
    }

    get modules() {
        return this._modules;
    }

    get flows() {
        return this._flows;
    }

    get Dock() {
        return BaseDock;
    }

    get Handler() {
        return BaseHandler;
    }

    get Hook() {
        return BaseHook;
    }

    set config(options) {
        const config = options;
        const spinner = ora('Checking config').start();

        this._checkConfig(config, spinner);
        this._logger.level = config.logLevel || 'info';

        if (config.events) {
            config.events.dispatcher = config.events.dispatcher && true;
            config.events.docks = config.events.docks && true;
            config.events.handlers = config.events.handlers && true;
            config.events.hooks = config.events.hooks && true;
        } else {
            process.stdout.write('\r');
            logger.verbose('No events config found, disabling all by default');
        }

        if (config.vantage) {
            config.vantage.enabled = config.vantage.enabled || false;
        } else {
            process.stdout.write('\r');
            logger.verbose('No Vantage config found, disabling by default');
        }

        spinner.succeed();
        Object.assign(this._config, options);
    }

    set(key, value) {
        this._logger.silly('Setting ' + key + '= ' + value);
        this._config[key] = value;
    }

    flow(name, options) {
        const config = options;
        const spinner = ora(`Validating '${name}'`).start();

        this._checkFlowOptions(config, spinner);
        config.inputHooks = config.inputHooks || [];
        config.outputHooks = config.outputHooks || [];

        const flow = new Flow(name, config);

        this._validateDocks(flow, spinner);
        this._validateHooks(flow, spinner);
        this._validateHandler(flow, spinner);

        spinner.succeed();
        this._flows.push(flow);
    }

    _checkConfig(config, spinner) {
        const schema = {
            threads: validator.isNumber,
            logLevel: validator.isString
        };

        if (config.events) schema.events = {
            dispatcher: validator.isBoolean,
            docks: validator.isBoolean,
            handlers: validator.isBoolean,
            hooks: validator.isBoolean
        };

        if (config.vantage) schema.vantage = {
            enabled: validator.isBoolean,
            port: validator.isNumber
        }

        validator.validate(config, schema, this._handleErrors(spinner));
    }

    _checkFlowOptions(options, spinner) {
        const flowSchema = {
            tag: [validator.isRequired, validator.isString],
            docks: [validator.isRequired, validator.isArray],
            handler: validator.isRequired,
            inputHooks: validator.isArray,
            outputHooks: validator.isArray
        };

        validator.validate(options, flowSchema, this._handleErrors(spinner));

        if (this.config.logLevel == 'silly') {
            process.stdout.write('\r');
            this._logger.silly('Checked flow options');
        }
    }

    _validateDocks(flow, spinner) {
        const dockSchema = {
            name: [validator.isRequired, validator.isString],
            protocol: [validator.isRequired, validator.isString],
            path: [validator.isRequired, validator.isString],
            validate: [validator.isRequired, validator.isFunction],
            parse: [validator.isRequired, validator.isFunction],
            process: [validator.isRequired, validator.isFunction],
            reply: [validator.isRequired, validator.isFunction],
            encode: [validator.isRequired, validator.isFunction],
            listen: [validator.isRequired, validator.isFunction],
            port: [validator.isRequired, validator.isNumber],
            send: [validator.isFunction]
        };

        flow.docks.forEach((dock) => {
            if (!dock.id) {
                validator.validate(dock, dockSchema, this._handleErrors(spinner));
                this._logger.silly('Validated dock \'' + dock.name + '\'');

                if (this.config.events && this.config.events.docks) {
                    dock.config = Object.assign(dock.config, {
                        events: this.config.events.docks
                    });
                }

                dock.id = shortid.generate();
                dock.dispatcher = dispatcher;
                this.modules.push(dock.path);
            }
        }, this);
    }

    _validateHooks(flow, spinner) {<commit_msg>Added validator rule for dock.stop()<commit_after>/*
 * Iris
 * (c) 2016+ Buenos Aires City Government
 * License: https://opensource.org/licenses/MIT
 *
 */

'use strict';

const Flow = require('./lib/Flow');
const BaseDock = require('./lib/bases/Dock');
const BaseHandler = require('./lib/bases/Handler');
const BaseHook = require('./lib/bases/Hook');
const dispatcher = require('./lib/Dispatcher');
const validator = require('propchecker');
const shortid = require('shortid');
const chalk = require('chalk');
const logger = require('winston');
const ora = require('ora');
const utils = require('./lib/utils/utils');

const consoleLog = utils.log;

class Iris {
    constructor() {
        this._name = 'main';
        this._config = {};
        this._flows = [];
        this._modules = [];
        this._logger = logger;

        this._logger.cli();
    }

    get config() {
        return this._config;
    }

    get logger() {
        return this._logger;
    }

    get modules() {
        return this._modules;
    }

    get flows() {
        return this._flows;
    }

    get Dock() {
        return BaseDock;
    }

    get Handler() {
        return BaseHandler;
    }

    get Hook() {
        return BaseHook;
    }

    set config(options) {
        const config = options;
        const spinner = ora('Checking config').start();

        this._checkConfig(config, spinner);
        this._logger.level = config.logLevel || 'info';

        if (config.events) {
            config.events.dispatcher = config.events.dispatcher && true;
            config.events.docks = config.events.docks && true;
            config.events.handlers = config.events.handlers && true;
            config.events.hooks = config.events.hooks && true;
        } else {
            process.stdout.write('\r');
            logger.verbose('No events config found, disabling all by default');
        }

        if (config.vantage) {
            config.vantage.enabled = config.vantage.enabled || false;
        } else {
            process.stdout.write('\r');
            logger.verbose('No Vantage config found, disabling by default');
        }

        spinner.succeed();
        Object.assign(this._config, options);
    }

    set(key, value) {
        this._logger.silly('Setting ' + key + '= ' + value);
        this._config[key] = value;
    }

    flow(name, options) {
        const config = options;
        const spinner = ora(`Validating '${name}'`).start();

        this._checkFlowOptions(config, spinner);
        config.inputHooks = config.inputHooks || [];
        config.outputHooks = config.outputHooks || [];

        const flow = new Flow(name, config);

        this._validateDocks(flow, spinner);
        this._validateHooks(flow, spinner);
        this._validateHandler(flow, spinner);

        spinner.succeed();
        this._flows.push(flow);
    }

    _checkConfig(config, spinner) {
        const schema = {
            threads: validator.isNumber,
            logLevel: validator.isString
        };

        if (config.events) schema.events = {
            dispatcher: validator.isBoolean,
            docks: validator.isBoolean,
            handlers: validator.isBoolean,
            hooks: validator.isBoolean
        };

        if (config.vantage) schema.vantage = {
            enabled: validator.isBoolean,
            port: validator.isNumber
        }

        validator.validate(config, schema, this._handleErrors(spinner));
    }

    _checkFlowOptions(options, spinner) {
        const flowSchema = {
            tag: [validator.isRequired, validator.isString],
            docks: [validator.isRequired, validator.isArray],
            handler: validator.isRequired,
            inputHooks: validator.isArray,
            outputHooks: validator.isArray
        };

        validator.validate(options, flowSchema, this._handleErrors(spinner));

        if (this.config.logLevel == 'silly') {
            process.stdout.write('\r');
            this._logger.silly('Checked flow options');
        }
    }

    _validateDocks(flow, spinner) {
        const dockSchema = {
            name: [validator.isRequired, validator.isString],
            protocol: [validator.isRequired, validator.isString],
            path: [validator.isRequired, validator.isString],
            validate: [validator.isRequired, validator.isFunction],
            parse: [validator.isRequired, validator.isFunction],
            process: [validator.isRequired, validator.isFunction],
            reply: [validator.isRequired, validator.isFunction],
            encode: [validator.isRequired, validator.isFunction],
            listen: [validator.isRequired, validator.isFunction],
            stop: [validator.isRequired, validator.isFunction],
            port: [validator.isRequired, validator.isNumber],
            send: [validator.isFunction]
        };

        flow.docks.forEach((dock) => {
            if (!dock.id) {
                validator.validate(dock, dockSchema, this._handleErrors(spinner));
                this._logger.silly('Validated dock \'' + dock.name + '\'');

                if (this.config.events && this.config.events.docks) {
                    dock.config = Object.assign(dock.config, {
                        events: this.config.events.docks
                    });
                }

                dock.id = shortid.generate();
                dock.dispatcher = dispatcher;
                this.modules.push(dock.path);
            }
        }, this);
    }

    _validateHooks(flow, spinner) {
{'fraction_non_alphanumeric': 0.12692062914810437, 'fraction_numerical': 0.0007273388489862715, 'mean_word_length': 2.0378348522507594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1574892', 'n_tokens_mistral': 3105, 'n_tokens_neox': 3035, 'n_words': 591}