<commit_before>/**
 * Copyright 2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

module.exports = function(RED) {

    var statistics = require('simple-statistics');
    var util = require('util');


    function StatisticsNode(config) {
        RED.nodes.createNode(this,config);

        var node = this;

        node.dataSetSize = ((config.dataSetSize != undefined) ? config.dataSetSize : 0) * 1;
        node.stripFunction = (config.stripFunction != undefined) ? config.stripFunction : true;
        node.inputField = config.inputField || "payload";
        node.inputFieldType = config.inputFieldType || "msg";
        node.resultField = config.resultField || "payload";
        node.resultFieldType = config.resultFieldType || "msg";
        node.parameterField = config.parameterField || "";
        node.parameterFieldType = config.parameterFieldType || "msg";
        node.resultOnly = (config.resultOnly != undefined) ? config.resultOnly : true;

        node.data= [];

        if (node.dataSetSize < 0) node.dataSetSize = 0;

        var setNodeProperty = function(field, type, node, msg, value) {
            if (type === 'msg') {
                RED.util.setMessageProperty(msg,field,value);
            } else if (type === 'flow') {
                node.context().flow.set(field,value);
            } else if (type === 'global') {
                node.context().global.set(field,value);
            }
        };


        var saveData = function(value) {
            if (value != undefined) {
                if (Array.isArray(value)) {
                    value.forEach(function (element) {
                        saveData(element);
                    });
                } else {
                    value = parseFloat(value);
                    if (!isNaN(value)) {
                        node.data.push(value);

                        if (node.dataSetSize > 0) {
                            while (node.data.length > node.dataSetSize) {
                                node.data.shift();
                            }
                        }
                    }
                }
            }
        };


        this.on('input', function(msg) {

            var funcIndex = msg.topic.lastIndexOf('/');

            var func = msg.topic.slice(funcIndex+1);

            if (node.stripFunction && (funcIndex != -1)) {
                msg.topic = msg.topic.substring(0,funcIndex);
            }

            var value = RED.util.evaluateNodeProperty(node.inputField, node.inputFieldType, node, msg);

            var result;
            var parameter;

            switch (func) {
                case 'clear':
                    node.data =[];
                    saveData(value);
                    break;

                case 'dump':
                    saveData(value);
                    result = Array.from(node.data);
                    break;

                case 'size':
                case 'count':
                    saveData(value);
                    result = node.data.length;
                    break;

                case 'bernoulliDistribution':
                case 'cumulativeStdNormalProbability':
                case 'errorFunction':
                case 'factorial':
                case 'inverseErrorFunction':
                case 'poissonDistribution':
                case 'probit':<commit_msg>Fixed behaviour of clear function to respect "Only output result messages" option<commit_after>/**
 * Copyright 2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

module.exports = function(RED) {

    var statistics = require('simple-statistics');
    var util = require('util');


    function StatisticsNode(config) {
        RED.nodes.createNode(this,config);

        var node = this;

        node.dataSetSize = ((config.dataSetSize != undefined) ? config.dataSetSize : 0) * 1;
        node.stripFunction = (config.stripFunction != undefined) ? config.stripFunction : true;
        node.inputField = config.inputField || "payload";
        node.inputFieldType = config.inputFieldType || "msg";
        node.resultField = config.resultField || "payload";
        node.resultFieldType = config.resultFieldType || "msg";
        node.parameterField = config.parameterField || "";
        node.parameterFieldType = config.parameterFieldType || "msg";
        node.resultOnly = (config.resultOnly != undefined) ? config.resultOnly : true;

        node.data= [];

        if (node.dataSetSize < 0) node.dataSetSize = 0;

        var setNodeProperty = function(field, type, node, msg, value) {
            if (type === 'msg') {
                RED.util.setMessageProperty(msg,field,value);
            } else if (type === 'flow') {
                node.context().flow.set(field,value);
            } else if (type === 'global') {
                node.context().global.set(field,value);
            }
        };


        var saveData = function(value) {
            if (value != undefined) {
                if (Array.isArray(value)) {
                    value.forEach(function (element) {
                        saveData(element);
                    });
                } else {
                    value = parseFloat(value);
                    if (!isNaN(value)) {
                        node.data.push(value);

                        if (node.dataSetSize > 0) {
                            while (node.data.length > node.dataSetSize) {
                                node.data.shift();
                            }
                        }
                    }
                }
            }
        };


        this.on('input', function(msg) {

            var funcIndex = msg.topic.lastIndexOf('/');

            var func = msg.topic.slice(funcIndex+1);

            if (node.stripFunction && (funcIndex != -1)) {
                msg.topic = msg.topic.substring(0,funcIndex);
            }

            var value = RED.util.evaluateNodeProperty(node.inputField, node.inputFieldType, node, msg);

            var result;
            var parameter;

            switch (func) {
                case 'clear':
                    node.data =[];
                    saveData(value);
                    if (node.resultsOnly) {
                        return null;
                    }
                    break;

                case 'dump':
                    saveData(value);
                    result = Array.from(node.data);
                    break;

                case 'size':
                case 'count':
                    saveData(value);
                    result = node.data.length;
                    break;

                case 'bernoulliDistribution':
                case 'cumulativeStdNormalProbability':
                case 'errorFunction':
                case 'factorial':
                case 'inverseErrorFunction':
                case 'poissonDistribution':
                case 'probit':
{'fraction_non_alphanumeric': 0.1073183020531553, 'fraction_numerical': 0.004030734349414284, 'mean_word_length': 1.532695374800638, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6907975', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1885, 'n_words': 492}