<commit_before>'use strict';

var React = require('react');
var io = require('socket.io-client');

var Application = React.createFactory(require('./Components/Application.js'));

var prepareAPI = require('../../../../tools/prepareAPI.js');
var sendReq = require('../../../../tools/sendReq.js');

var api = prepareAPI(sendReq);

var makeMap = require('../../_common/js/makeMap.js');

var errlog = console.error.bind(console);

var PRIVATE = require('../../../../PRIVATE.json');

var BORDEAUX_COORDS = [44.84, -0.57];

// Get the day in the URL (EU notation)
var day = location.search.match(/[\? | \?.*&+]day=(\d\d)\/(\d\d)\/(\d\d\d\d)/);
day = day ? day[2] + '/' + day[1] + '/' + day[3] : undefined;
day = new Date(day).toString() !== 'Invalid Date' ? day : undefined;

var topLevelStore = {
    day: day,
    mapBoxToken: PRIVATE.mapbox_token,
    mapId: PRIVATE.map_id,
    mapCenter: BORDEAUX_COORDS,
    placeMap: undefined,
    selectedPlaceMap: new Map(),
    updatingIDs: [],
    getPlaceMeasurements: function(place){
        api.getPlaceMeasurements({id: place.id})
            .then(function(measurements){
                console.log('place measurements', place, measurements);
                
                // sort by asc time in case it's not already thus sorted
                measurements.sort(function(m1, m2){
                    return new Date(m1.date).getTime() - new Date(m2.date).getTime();
                });
            
                place.measurements = measurements;
                topLevelStore.selectedPlaceMap.set(place.id, place);
                render();
            })
            .catch(errlog);
    }
};

function render(){
    React.render(new Application(topLevelStore), document.body);
}

// Initial rendering
render();

// Render again when receiving recyclingCenters from API
api.getLiveAffluence()
    .then(function(places){
        console.log('places', places);

        topLevelStore.placeMap = makeMap(places, 'id');
        render();
    })
    .catch(errlog);

var socket = io();

socket.on('data', function (measurement) {
    // For now, this is only capable of treating measurements of type signal_strength
    // We need to implement differents measurement types push

    // GET DATA
    var id = measurement.installed_at;

    var value = measurement.value;
    var date = measurement.date;
    
    // GET PLACE
    var place = topLevelStore.placeMap.get(id);
    
    place.max = Math.max(place.max, value);
    place.latest = value;

    if (place.measurements)
    // UPDATE CURVE
        place.measurements.push({
            date: date,
            value: value
        });

    topLevelStore.updatingIDs.push(id);

    render();

    setTimeout(function(){
        topLevelStore.updatingIDs = [];
        render();
    }, 200);<commit_msg>Fixed a bug in the Dashboard frontend (socket.io)<commit_after>'use strict';

var React = require('react');
var io = require('socket.io-client');

var Application = React.createFactory(require('./Components/Application.js'));

var prepareAPI = require('../../../../tools/prepareAPI.js');
var sendReq = require('../../../../tools/sendReq.js');

var api = prepareAPI(sendReq);

var makeMap = require('../../_common/js/makeMap.js');

var errlog = console.error.bind(console);

var PRIVATE = require('../../../../PRIVATE.json');

var BORDEAUX_COORDS = [44.84, -0.57];

function safeMax(safeInt, unsafeInt) {
    return unsafeInt | 0 === unsafeInt ? Math.max(safeInt, unsafeInt) : safeInt;
}

// Get the day in the URL (EU notation)
var day = location.search.match(/[\? | \?.*&+]day=(\d\d)\/(\d\d)\/(\d\d\d\d)/);
day = day ? day[2] + '/' + day[1] + '/' + day[3] : undefined;
day = new Date(day).toString() !== 'Invalid Date' ? day : undefined;

var topLevelStore = {
    day: day,
    mapBoxToken: PRIVATE.mapbox_token,
    mapId: PRIVATE.map_id,
    mapCenter: BORDEAUX_COORDS,
    placeMap: undefined,
    selectedPlaceMap: new Map(),
    updatingIDs: [],
    getPlaceMeasurements: function(place){
        api.getPlaceMeasurements({id: place.id})
            .then(function(measurements){
                console.log('place measurements', place, measurements);
                
                // sort by asc time in case it's not already thus sorted
                measurements.sort(function(m1, m2){
                    return new Date(m1.date).getTime() - new Date(m2.date).getTime();
                });
            
                place.measurements = measurements;
                topLevelStore.selectedPlaceMap.set(place.id, place);
                render();
            })
            .catch(errlog);
    }
};

function render(){
    React.render(new Application(topLevelStore), document.body);
}

// Initial rendering
render();

// Render again when receiving recyclingCenters from API
api.getLiveAffluence()
    .then(function(places){
        console.log('places', places);

        topLevelStore.placeMap = makeMap(places, 'id');
        render();
    })
    .catch(errlog);

var socket = io();

socket.on('data', function (measurement) {
    // For now, this is only capable of treating measurements of type signal_strength
    // We need to implement differents measurement types push

    // GET DATA
    var id = measurement.installed_at;

    var value = measurement.value.length;
    var date = measurement.date;
    
    // GET PLACE
    var place = topLevelStore.placeMap.get(id);
    
    place.max = safeMax(place.max, value);
    place.latest = value;

    if (place.measurements)
    // UPDATE CURVE
        place.measurements.push({
            date: date,
            value: value
        });

    topLevelStore.updatingIDs.push(id);

    render();

    setTimeout(function(){
        topLevelStore.updatingIDs = [];
        render();
    }, 200);
{'fraction_non_alphanumeric': 0.1527417401833593, 'fraction_numerical': 0.006054315862307559, 'mean_word_length': 2.435531788472965, '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': '5144700', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1705, 'n_words': 402}