<commit_before>/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
var cp         = require('child_process');
var utils      = require(__dirname + '/lib/utils'); // Get common adapter utils
var path       = require('path');
var dataDir    = path.normalize(utils.controllerDir + '/' + require(utils.controllerDir + '/lib/tools').getDefaultDataDir());
var fs         = require('fs');
var GetHistory = require(__dirname + '/lib/getHistory.js');
var Aggregate  = require(__dirname + '/lib/aggregate.js');

var history    = {};
var aliasMap   = {};
var subscribeAll = false;
var bufferChecker = null;
var tasksStart = [];
var finished   = false;

var adapter = new utils.Adapter({

    name: 'history',

    objectChange: function (id, obj) {
        var formerAliasId = aliasMap[id] ? aliasMap[id] : id;
        if (obj && obj.common &&
            (
                // todo remove history sometime (2016.08) - Do not forget object selector in io-package.json
                (obj.common.history && obj.common.history[adapter.namespace] && obj.common.history[adapter.namespace].enabled) ||
                (obj.common.custom  && obj.common.custom[adapter.namespace]  && obj.common.custom[adapter.namespace].enabled)
            )
        ) {
            var realId = id;
            var checkForRemove = true;
            if (obj.common.custom && obj.common.custom[adapter.namespace] && obj.common.custom[adapter.namespace].aliasId) {
                if (obj.common.custom[adapter.namespace].aliasId !== id) {
                    aliasMap[id] = obj.common.custom[adapter.namespace].aliasId;
                    adapter.log.debug('Registered Alias: ' + id + ' --> ' + aliasMap[id]);
                    id = aliasMap[id];
                    checkForRemove = false;
                }
                else {
                    adapter.log.warn('Ignoring Alias-ID because identical to ID for ' + id);
                    obj.common.custom[adapter.namespace].aliasId = '';
                }
            }
            if (checkForRemove && aliasMap[id]) {
                adapter.log.debug('Removed Alias: ' + id + ' !-> ' + aliasMap[id]);
                delete aliasMap[id];
            }

            var writeNull = !history[id];
            var state     = history[id] ? history[id].state   : null;
            var list      = history[id] ? history[id].list    : null;
            var timeout   = history[id] ? history[id].timeout : null;

            if (!history[formerAliasId] && !subscribeAll) {
                // unsubscribe
                for (var _id in history) {
                    if (history.hasOwnProperty(history[_id].realId)) {
                        adapter.unsubscribeForeignStates(history[_id].realId);
                    }
                }
                subscribeAll = true;
                adapter.subscribeForeignStates('*');
            }

            if (history[formerAliasId] && history[formerAliasId].relogTimeout) clearTimeout(history[formerAliasId].relogTimeout);

            // todo remove history somewhen (2016.08)
            history[id] = obj.common.custom || obj.common.history;
            history[id].state   = state;
            history[id].list    = list;
            history[id].timeout = timeout;
            history[id].realId  = realId;

            if (!history[id][adapter.namespace].maxLength && history[id][adapter.namespace].maxLength !== '0' && history[id][adapter.namespace].maxLength !== 0) {
                history[id][adapter.namespace].maxLength = parseInt(adapter.config.maxLength, 10) || 960;
            } else {
                history[id][adapter.namespace].maxLength = parseInt(history[id][adapter.namespace].maxLength, 10);
            }
            if (!history[id][adapter.namespace].retention && history[id][adapter.namespace].retention !== '0' && history[id][adapter.namespace].retention !== 0) {
                history[id][adapter.namespace].retention = parseInt(adapter.config.retention, 10) || 0;
            } else {
                history[id][adapter.namespace].retention = parseInt(history[id][adapter.namespace].retention, 10) || 0;
            }
            if (!history[id][adapter.namespace].debounce && history[id][adapter.namespace].debounce !== '0' && history[id][adapter.namespace].debounce !== 0) {
                history[id][adapter.namespace].debounce = parseInt(adapter.config.debounce, 10) || 1000;
            } else {
                history[id][adapter.namespace].debounce = parseInt(history[id][adapter.namespace].debounce, 10);
            }
            history[id][adapter.namespace].changesOnly = history[id][adapter.namespace].changesOnly === 'true' || history[id][adapter.namespace].changesOnly === true;
            if (history[id][adapter.namespace].changesRelogInterval !== undefined && history[id][adapter.namespace].changesRelogInterval !== null && history[id][adapter.namespace].changesRelogInterval !== '') {
                history[id][adapter.namespace].changesRelogInterval = parseInt(history[id][adapter.namespace].changesRelogInterval, 10) || 0;
            } else {
                history[id][adapter.namespace].changesRelogInterval = adapter.config.changesRelogInterval;
            }
            if (history[id][adapter.namespace].changesRelogInterval > 0) {
                history[id].relogTimeout = setTimeout(reLogHelper, (history[id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + history[id][adapter.namespace].changesRelogInterval * 500, id);
            }
            if (history[id][adapter.namespace].changesMinDelta !== undefined && history[id][adapter.namespace].changesMinDelta !== null && history[id][adapter.namespace].changesMinDelta !== '') {
                history[id][adapter.namespace].changesMinDelta = parseFloat(history[id][adapter.namespace].changesMinDelta.toString().replace(/,/g, '.')) || 0;
            } else {
                history[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
            }


            // add one day if retention is too small
            if (history[id][adapter.namespace].retention && history[id][adapter.namespace].retention <= 604800) {
                history[id][adapter.namespace].retention += 86400;
            }
            if (writeNull) {
                writeNulls(id);
            }

            adapter.log.info('enabled logging of ' + id + ', Alias=' + (id !== realId) + ', ' + Object.keys(history).length + ' points now activated');
        } else {
            if (aliasMap[id]) {
                adapter.log.debug('Removed Alias: ' + id + ' !-> ' + aliasMap[id]);
                delete aliasMap[id];
            }
            id = formerAliasId;
            if (history[id]) {
                adapter.log.info('disabled logging of ' + id + ', ' + Object.keys(history).length + ' points now activated');
                if (history[id].relogTimeout) clearTimeout(history[id].relogTimeout);
                if (history[id].timeout) clearTimeout(history[id].timeout);
                storeCached(true, id);
                delete history[id];
            }
        }
    },
<commit_msg>remove number activated in onChange<commit_after>/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
var cp         = require('child_process');
var utils      = require(__dirname + '/lib/utils'); // Get common adapter utils
var path       = require('path');
var dataDir    = path.normalize(utils.controllerDir + '/' + require(utils.controllerDir + '/lib/tools').getDefaultDataDir());
var fs         = require('fs');
var GetHistory = require(__dirname + '/lib/getHistory.js');
var Aggregate  = require(__dirname + '/lib/aggregate.js');

var history    = {};
var aliasMap   = {};
var subscribeAll = false;
var bufferChecker = null;
var tasksStart = [];
var finished   = false;

var adapter = new utils.Adapter({

    name: 'history',

    objectChange: function (id, obj) {
        var formerAliasId = aliasMap[id] ? aliasMap[id] : id;
        if (obj && obj.common &&
            (
                // todo remove history sometime (2016.08) - Do not forget object selector in io-package.json
                (obj.common.history && obj.common.history[adapter.namespace] && obj.common.history[adapter.namespace].enabled) ||
                (obj.common.custom  && obj.common.custom[adapter.namespace]  && obj.common.custom[adapter.namespace].enabled)
            )
        ) {
            var realId = id;
            var checkForRemove = true;
            if (obj.common.custom && obj.common.custom[adapter.namespace] && obj.common.custom[adapter.namespace].aliasId) {
                if (obj.common.custom[adapter.namespace].aliasId !== id) {
                    aliasMap[id] = obj.common.custom[adapter.namespace].aliasId;
                    adapter.log.debug('Registered Alias: ' + id + ' --> ' + aliasMap[id]);
                    id = aliasMap[id];
                    checkForRemove = false;
                }
                else {
                    adapter.log.warn('Ignoring Alias-ID because identical to ID for ' + id);
                    obj.common.custom[adapter.namespace].aliasId = '';
                }
            }
            if (checkForRemove && aliasMap[id]) {
                adapter.log.debug('Removed Alias: ' + id + ' !-> ' + aliasMap[id]);
                delete aliasMap[id];
            }

            var writeNull = !history[id];
            var state     = history[id] ? history[id].state   : null;
            var list      = history[id] ? history[id].list    : null;
            var timeout   = history[id] ? history[id].timeout : null;

            if (!history[formerAliasId] && !subscribeAll) {
                // unsubscribe
                for (var _id in history) {
                    if (history.hasOwnProperty(history[_id].realId)) {
                        adapter.unsubscribeForeignStates(history[_id].realId);
                    }
                }
                subscribeAll = true;
                adapter.subscribeForeignStates('*');
            }

            if (history[formerAliasId] && history[formerAliasId].relogTimeout) clearTimeout(history[formerAliasId].relogTimeout);

            // todo remove history somewhen (2016.08)
            history[id] = obj.common.custom || obj.common.history;
            history[id].state   = state;
            history[id].list    = list;
            history[id].timeout = timeout;
            history[id].realId  = realId;

            if (!history[id][adapter.namespace].maxLength && history[id][adapter.namespace].maxLength !== '0' && history[id][adapter.namespace].maxLength !== 0) {
                history[id][adapter.namespace].maxLength = parseInt(adapter.config.maxLength, 10) || 960;
            } else {
                history[id][adapter.namespace].maxLength = parseInt(history[id][adapter.namespace].maxLength, 10);
            }
            if (!history[id][adapter.namespace].retention && history[id][adapter.namespace].retention !== '0' && history[id][adapter.namespace].retention !== 0) {
                history[id][adapter.namespace].retention = parseInt(adapter.config.retention, 10) || 0;
            } else {
                history[id][adapter.namespace].retention = parseInt(history[id][adapter.namespace].retention, 10) || 0;
            }
            if (!history[id][adapter.namespace].debounce && history[id][adapter.namespace].debounce !== '0' && history[id][adapter.namespace].debounce !== 0) {
                history[id][adapter.namespace].debounce = parseInt(adapter.config.debounce, 10) || 1000;
            } else {
                history[id][adapter.namespace].debounce = parseInt(history[id][adapter.namespace].debounce, 10);
            }
            history[id][adapter.namespace].changesOnly = history[id][adapter.namespace].changesOnly === 'true' || history[id][adapter.namespace].changesOnly === true;
            if (history[id][adapter.namespace].changesRelogInterval !== undefined && history[id][adapter.namespace].changesRelogInterval !== null && history[id][adapter.namespace].changesRelogInterval !== '') {
                history[id][adapter.namespace].changesRelogInterval = parseInt(history[id][adapter.namespace].changesRelogInterval, 10) || 0;
            } else {
                history[id][adapter.namespace].changesRelogInterval = adapter.config.changesRelogInterval;
            }
            if (history[id][adapter.namespace].changesRelogInterval > 0) {
                history[id].relogTimeout = setTimeout(reLogHelper, (history[id][adapter.namespace].changesRelogInterval * 500 * Math.random()) + history[id][adapter.namespace].changesRelogInterval * 500, id);
            }
            if (history[id][adapter.namespace].changesMinDelta !== undefined && history[id][adapter.namespace].changesMinDelta !== null && history[id][adapter.namespace].changesMinDelta !== '') {
                history[id][adapter.namespace].changesMinDelta = parseFloat(history[id][adapter.namespace].changesMinDelta.toString().replace(/,/g, '.')) || 0;
            } else {
                history[id][adapter.namespace].changesMinDelta = adapter.config.changesMinDelta;
            }


            // add one day if retention is too small
            if (history[id][adapter.namespace].retention && history[id][adapter.namespace].retention <= 604800) {
                history[id][adapter.namespace].retention += 86400;
            }
            if (writeNull) {
                writeNulls(id);
            }

            adapter.log.info('enabled logging of ' + id + ', Alias=' + (id !== realId));
        } else {
            if (aliasMap[id]) {
                adapter.log.debug('Removed Alias: ' + id + ' !-> ' + aliasMap[id]);
                delete aliasMap[id];
            }
            id = formerAliasId;
            if (history[id]) {
                adapter.log.info('disabled logging of ' + id);
                if (history[id].relogTimeout) clearTimeout(history[id].relogTimeout);
                if (history[id].timeout) clearTimeout(history[id].timeout);
                storeCached(true, id);
                delete history[id];
            }
        }
    },

{'fraction_non_alphanumeric': 0.14199097574732092, 'fraction_numerical': 0.009024252679075015, 'mean_word_length': 2.2056497175141243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '281687', 'n_tokens_mistral': 3886, 'n_tokens_neox': 3682, 'n_words': 654}