<commit_before>'use strict';
var EventDispatcher     = require('appolo').EventDispatcher,
    usage               = require('usage');

module.exports = EventDispatcher.define({

    $config: {
        id: 'monitor',
        singleton: false,
        inject: ['env']
    },

    constructor: function(monitorMemory, monitorCpu) {

        this._pid = null;
        this._timer = null;
        this._options = { keepHistory: true };
        this._monitorMemory = monitorMemory;
        this._monitorCpu = monitorCpu;

    },

    start: function(processId) {

        if(!this._timer) {

            this._pid = processId;

            this._sendProcessUsage();

        }

    },

    stop: function() {

        if(this._timer) {
            clearTimeout(this._timer);
            usage.clearHistory && usage.clearHistory(this._pid);
            this._timer = null;
            this._pid = null;
        }
    },

    _sendProcessUsage: function() {

        usage.lookup(this._pid, this._options, function(err, result) {

            if(!err) {

                if(!this._monitorMemory) {
                    delete result['memory'];
                }

                if(!this._monitorCpu) {
                    delete result['cpu'];
                }

                this.fireEvent('data', result);
            }

            this._timer = setTimeout(this._sendProcessUsage.bind(this), this.env.serviceUsagePollInterval);

        }.bind(this));

    }
});<commit_msg>don't send monitor (cpu/memory) logs when data didn't changed<commit_after>'use strict';
var EventDispatcher     = require('appolo').EventDispatcher,
    usage               = require('usage');

module.exports = EventDispatcher.define({

    $config: {
        id: 'monitor',
        singleton: false,
        inject: ['env']
    },

    constructor: function(monitorMemory, monitorCpu) {

        this._pid = null;
        this._timer = null;
        this._options = { keepHistory: true };
        this._monitorMemory = monitorMemory;
        this._monitorCpu = monitorCpu;
        this._lastMonitorData = {};

    },

    start: function(processId) {

        if(!this._timer) {

            this._pid = processId;

            this._sendProcessUsage();

        }

    },

    stop: function() {

        if(this._timer) {
            clearTimeout(this._timer);
            usage.clearHistory && usage.clearHistory(this._pid);
            this._timer = null;
            this._pid = null;
        }
    },

    _sendProcessUsage: function() {

        usage.lookup(this._pid, this._options, function(err, result) {

            if(!err) {

                if(!this._monitorMemory) {
                    delete result['memory'];
                }

                if(!this._monitorCpu) {
                    delete result['cpu'];
                }

                if(result['cpu'] != this._lastMonitorData['cpu'] || result['memory'] != this._lastMonitorData['memory']) {

                    this.fireEvent('data', result);

                    this._lastMonitorData = result;
                }
            }

            this._timer = setTimeout(this._sendProcessUsage.bind(this), this.env.serviceUsagePollInterval);

        }.bind(this));

    }
});
{'fraction_non_alphanumeric': 0.12612333436628448, 'fraction_numerical': 0.0, 'mean_word_length': 1.678838174273859, 'pattern_counts': {'":': 0, '<': 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': '2812235', 'n_tokens_mistral': 900, 'n_tokens_neox': 830, 'n_words': 145}