<commit_before>'use_strict';

var m2xReporter = require('./m2xReporter');
var config = require('config');

var m2xMap = config.get('m2x.endpoints');

var winston = require('winston');

// Helper functions
var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)(),
        new (winston.transports.File)({ filename: './logs/coopReporter.log' })
    ]
});

var logMessage = function(streamName, dataValue, logLevel) {
    var logLine = streamName + ' = ' + dataValue;
    logLevel = logLevel || 'info';
    logger.log(logLevel, logLine);
};

function report (streamName, dataValue, callback) {
    var url = m2xMap[streamName];
    if (!url) {
        throw new Error('No endpoint defined for streamName = ' + streamName);
    }
    return m2xReporter(url, dataValue, callback);
}

module.exports = function (coop) {
    coop.on('change', function ( updatedFields ) {
        console.log('got a change');
        updatedFields.forEach( function(field) {
            report( field, coop[field], function(err, data){ 
                if(err) logMessage(err.message + ' ' + field, coop[field], 'error');
                else logMessage(field, coop[field]);
            });
        });
    });
};<commit_msg>Added heartbeat log to coopReporter.<commit_after>'use_strict';

var m2xReporter = require('./m2xReporter');
var config = require('config');

var m2xMap = config.get('m2x.endpoints');

var winston = require('winston');

// Helper functions
var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)(),
        new (winston.transports.File)({ filename: './logs/coopReporter.log' })
    ]
});

var logMessage = function(streamName, dataValue, logLevel) {
    var logLine = streamName + ' = ' + dataValue;
    logLevel = logLevel || 'info';
    logger.log(logLevel, logLine);
};

function report (streamName, dataValue, callback) {
    var url = m2xMap[streamName];
    if (!url) {
        throw new Error('No endpoint defined for streamName = ' + streamName);
    }
    return m2xReporter(url, dataValue, callback);
}

module.exports = function (coop) {
    coop.on('change', function ( updatedFields ) {
        updatedFields.forEach( function(field) {
            report( field, coop[field], function(err, data){ 
                if(err) logMessage(err.message + ' ' + field, coop[field], 'error');
                else logMessage(field, coop[field]);
            });
        });
    });

    // for a heartbeat, just update the temperature
    coop.on('heartbeat', function ( ) {
        console.log('heartbeat');
        coop.checkLegality( function (result) {
            console.log('sending heartbeat; valid: "' + result + '"');
            report('legalState', result, function() { return; });
        }
    });
};
{'fraction_non_alphanumeric': 0.15200286123032905, 'fraction_numerical': 0.004291845493562232, 'mean_word_length': 2.9673758865248225, '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': '2534386', 'n_tokens_mistral': 869, 'n_tokens_neox': 850, 'n_words': 194}