<commit_before>/**
 * Created by ralemy on 2/21/16.
 * Node-Red node to run an Itemsense Job
 */
module.exports = function (RED) {
    "use strict";

    var lib = require("./lib/itemsense");
    lib.hookIntoApp(RED);

    function triageError(err) {
        if (err.response)
            if (err.response.body)
                if (err.response.body.message.indexOf("JobId=") !== -1)
                    return {jobId: err.response.body.message.match(/JobId=([^,]+)/)[1]};
                else
                    return {
                        jobId: null,
                        body: {message: err.response.body, statusCode: err.response.statusCode},
                        message: "Job Start Failed:" + err.response.statusCode + " " + JSON.stringify(err.response.body, null, " ")
                    };
            else
                return {
                    jobId: null,
                    body: {statusCode: err.response.statusCode},
                    message: "Job Start Failed: " + err.response.statusCode
                };
        return {jobId: null, body: {statusCode: 500, message: err.message}, message: err.message};
    }
    function getReaderGroups(g){
        if(!g) return null;
        return g.split(",");
    }
    function RunJobNode(config) {
        RED.nodes.createNode(this, config);
        var node = this,
            LocalItemsense = RED.nodes.getNode(config.itemsense);
        this.on("input", function (msg) {
            var itemsense = lib.registerItemsense(node, msg, LocalItemsense),
                jobObject = {
                    "readerGroups":getReaderGroups(config.readerGroups),
                    "recipeName": config.recipe,
                    "durationSeconds": config.runLength,
//                    "presenceLoggingEnabled": true,
                    "startDelay": config.startDelay,
                    "reportToDatabaseEnabled": true,
                    "reportToHistoryEnabled": true,
                    "reportToMessageQueueEnabled": true,
                    "reportToFileEnabled": false,
                    "facility": config.facility
                };
            console.log(jobObject);
            Object.keys(jobObject).forEach(function (key) {
                if (msg.payload && msg.payload[key] !== undefined){
                    jobObject[key] = msg.payload[key];
                  }
            });
            lib.status("enter", "Running Job", node);
            if (itemsense)
                itemsense.jobs.start(jobObject).then(function (job) {
                    msg.topic = "Job";
                    msg.payload = job;
                    node.send([msg, null, {topic: "success", payload: "Job Starting: " + job.id}]);
                    lib.status("exit", "", node);
                }).catch(function (err) {
                    var triage = triageError(err);
                    console.log("error", triage);
                    lib.status("error", "Failed Running Job", node);<commit_msg>removed presenceLoggingEnabled from job object for 2017r1<commit_after>/**
 * Created by ralemy on 2/21/16.
 * Node-Red node to run an Itemsense Job
 */
module.exports = function (RED) {
    "use strict";

    var lib = require("./lib/itemsense");
    lib.hookIntoApp(RED);

    function triageError(err) {
        if (err.response)
            if (err.response.body)
                if (err.response.body.message.indexOf("JobId=") !== -1)
                    return {jobId: err.response.body.message.match(/JobId=([^,]+)/)[1]};
                else
                    return {
                        jobId: null,
                        body: {message: err.response.body, statusCode: err.response.statusCode},
                        message: "Job Start Failed:" + err.response.statusCode + " " + JSON.stringify(err.response.body, null, " ")
                    };
            else
                return {
                    jobId: null,
                    body: {statusCode: err.response.statusCode},
                    message: "Job Start Failed: " + err.response.statusCode
                };
        return {jobId: null, body: {statusCode: 500, message: err.message}, message: err.message};
    }
    function getReaderGroups(g){
        if(!g) return null;
        return g.split(",");
    }
    function RunJobNode(config) {
        RED.nodes.createNode(this, config);
        var node = this,
            LocalItemsense = RED.nodes.getNode(config.itemsense);
        this.on("input", function (msg) {
            var itemsense = lib.registerItemsense(node, msg, LocalItemsense),
                jobObject = {
                    "readerGroups":getReaderGroups(config.readerGroups),
                    "recipeName": config.recipe,
                    "durationSeconds": config.runLength,
                    "startDelay": config.startDelay,
                    "reportToDatabaseEnabled": true,
                    "reportToHistoryEnabled": true,
                    "reportToMessageQueueEnabled": true,
                    "reportToFileEnabled": false,
                    "facility": config.facility
                };
            console.log(jobObject);
            Object.keys(jobObject).forEach(function (key) {
                if (msg.payload && msg.payload[key] !== undefined){
                    jobObject[key] = msg.payload[key];
                  }
            });
            lib.status("enter", "Running Job", node);
            if (itemsense)
                itemsense.jobs.start(jobObject).then(function (job) {
                    msg.topic = "Job";
                    msg.payload = job;
                    node.send([msg, null, {topic: "success", payload: "Job Starting: " + job.id}]);
                    lib.status("exit", "", node);
                }).catch(function (err) {
                    var triage = triageError(err);
                    console.log("error", triage);
                    lib.status("error", "Failed Running Job", node);
{'fraction_non_alphanumeric': 0.12466487935656836, 'fraction_numerical': 0.0041890080428954425, 'mean_word_length': 1.523890063424947, 'pattern_counts': {'":': 19, '<': 3, '<?xml version=': 0, '>': 3, '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': '2054124', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1496, 'n_words': 321}