<commit_before>/**
 * Wake Up Platform
 * (c) Telefonica Digital, 2014 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <frsela at tid dot es>
 * <NAME> <gll at tid dot es>
 */

'use strict';

var request = require('request'),
    net = require('net'),
    config = process.configuration,
    log = require('../shared_libs/logger.js'),
    helpers = require('../shared_libs/helpers.js');

var mn = function mobileNetwork(networksPath) {
    var networks = require(networksPath);

    var checkWakeup = function(URL, callback) {
        log.debug('Checking ' + URL);
        var options = {
            url: URL,
            // We need to fake this for the local wakeup to
            // accept our query
            headers: {
                'x-client-cert-verified': 'SUCCESS',
                'x-client-cert-dn': 'Wakeup_checker'
            }
        };
        // Callback with true/false (online/offline) and the trackingID
        // if it is set to trace between wakeups
        request(options, function(error, response/*, body*/) {
            if (!error && response.statusCode === 200) {
                log.debug(URL + ' success');
                callback(true, response.headers['x-tracking-id']);
            } else {
                log.debug(URL + ' failed');
                callback(false, (response && response.headers &&
                   response.headers['x-tracking-id'])
                );
            }
        });
    };

    var checkNetworks = function() {
        log.debug('checkNetworks() + ' + JSON.stringify(networks));
        var keys = Object.keys(networks);
        keys.forEach(function(id) {
            if (networks[id].host) {
                var toCheck = networks[id].host + '/status';
                checkWakeup(toCheck, function(online, trackingID) {
                    var on = online ? enableNetwork(id) : disableNetwork(id);
                    log.info(Date.now() + ' -- ' + trackingID +
                        ' -- wakeup_check -- ' + id + ' -- ' + networks[id].host +
                        ' -- ' + (on ? 'OK' : 'KO'));
                });
            }
        });
    };

    checkNetworks();
    setInterval(checkNetworks, config.checkInterval);

    function getNetwork(netid) {
        var res = networks[netid];
        if (!res) {
            return null;
        }
        return res;
    }

    function getAllNetworks() {
        return networks;
    }

    function enableNetwork(networkName) {
        log.debug('Enabling network ' + networkName);
        var net = getNetwork(networkName);
        if (net) {
            networks[networkName].offline = false;
        }
        return !networks[networkName].offline;
    }

    function disableNetwork(networkName) {
        log.debug('Disabling network ' + networkName);
        var net = getNetwork(networkName);
        if (net) {
            networks[networkName].offline = true;
        }
        return !networks[networkName].offline;
    }

    return {
        getNetworkIDForMCCMNC: function(mcc, mnc) {
            return mcc + '-' + mnc;<commit_msg>Changed default netid for backward compatibility. For queries without netid, (mcc-mnc.default) will be used<commit_after>/**
 * Wake Up Platform
 * (c) Telefonica Digital, 2014 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <frsela at tid dot es>
 * <NAME> <gll at tid dot es>
 */

'use strict';

var request = require('request'),
    net = require('net'),
    config = process.configuration,
    log = require('../shared_libs/logger.js'),
    helpers = require('../shared_libs/helpers.js');

var mn = function mobileNetwork(networksPath) {
    var networks = require(networksPath);

    var checkWakeup = function(URL, callback) {
        log.debug('Checking ' + URL);
        var options = {
            url: URL,
            // We need to fake this for the local wakeup to
            // accept our query
            headers: {
                'x-client-cert-verified': 'SUCCESS',
                'x-client-cert-dn': 'Wakeup_checker'
            }
        };
        // Callback with true/false (online/offline) and the trackingID
        // if it is set to trace between wakeups
        request(options, function(error, response/*, body*/) {
            if (!error && response.statusCode === 200) {
                log.debug(URL + ' success');
                callback(true, response.headers['x-tracking-id']);
            } else {
                log.debug(URL + ' failed');
                callback(false, (response && response.headers &&
                   response.headers['x-tracking-id'])
                );
            }
        });
    };

    var checkNetworks = function() {
        log.debug('checkNetworks() + ' + JSON.stringify(networks));
        var keys = Object.keys(networks);
        keys.forEach(function(id) {
            if (networks[id].host) {
                var toCheck = networks[id].host + '/status';
                checkWakeup(toCheck, function(online, trackingID) {
                    var on = online ? enableNetwork(id) : disableNetwork(id);
                    log.info(Date.now() + ' -- ' + trackingID +
                        ' -- wakeup_check -- ' + id + ' -- ' + networks[id].host +
                        ' -- ' + (on ? 'OK' : 'KO'));
                });
            }
        });
    };

    checkNetworks();
    setInterval(checkNetworks, config.checkInterval);

    function getNetwork(netid) {
        var res = networks[netid];
        if (!res) {
            return null;
        }
        return res;
    }

    function getAllNetworks() {
        return networks;
    }

    function enableNetwork(networkName) {
        log.debug('Enabling network ' + networkName);
        var net = getNetwork(networkName);
        if (net) {
            networks[networkName].offline = false;
        }
        return !networks[networkName].offline;
    }

    function disableNetwork(networkName) {
        log.debug('Disabling network ' + networkName);
        var net = getNetwork(networkName);
        if (net) {
            networks[networkName].offline = true;
        }
        return !networks[networkName].offline;
    }

    return {
        getNetworkIDForMCCMNC: function(mcc, mnc) {
            return mcc + '-' + mnc + '.default';
{'fraction_non_alphanumeric': 0.1297263091283025, 'fraction_numerical': 0.0025312450561619996, 'mean_word_length': 1.780123131046614, 'pattern_counts': {'":': 0, '<': 11, '<?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': '7220099', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1739, 'n_words': 405}