<commit_before>"use strict";

var c3 = require('c3');

var HOUR_SECONDS = 60 * 60;
var DAY_SECONDS = HOUR_SECONDS * 24;


function fetch_pie() {
    var end = Math.floor(Date.now() / 1000);
    var start = end - DAY_SECONDS*2;
    var gran = DAY_SECONDS;
    
    fetch_aggregate([create_url(start, end, gran, "Panel1"),
             create_url(start, end, gran, "Panel2"),
             create_url(start, end, gran, "Panel3")],
             pie, true);
}

function pie(responses) {
    var data = [];
    for (var i = 0; i < responses.length; i++) {
        data[i] = ['Panel ' + (i+1), JSON.parse(responses[i]).data[0].energy];
    }
    c3.generate({
        padding: {
                top: 0,
                right: 0,
                bottom: 0,
                left: 0,
            },
        bindto: '#pie',
        data: {
            // iris data from R
            columns: data,
            type: 'pie',
        },
        pie: {
            label: {
                format: function(value, ratio, id) {
                    return Math.round(value * 100) / 100 + " kW";
                }
            }
        }
    });
}

function generate_bar_graph(data) {
    c3.generate({
        padding: {
            top: 0,
            right: 0,
            bottom: 0,
            left: 60,
        },
        bindto: '#bar',
        data: {
            x: 'x',
            xFormat: '%Y-%m-%d %H:%M:%S',
            columns: [
                ['x'].concat(data.data.map( function(x) { return x.time; })), 
                ['energy'].concat(data.data.map( function(x) { return Math.round(x.energy * 100) / 100}))
            ],
            type: 'bar',
        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    // displays day of week
                    format: '%a'
                }
            },
            y: {
                tick: {
                    format: function (d) { return d + " kW"; }
                }
            }
        },
        bar: {
            width: {
                ratio: 0.5 // this makes bar width 50% of length between ticks
            }
            // or
            //width: 100 // this makes bar width 100px
        }
    });
}

function post_data_to_server(label) {
    console.log("Sending " + JSON.stringify(label));

    var post = new XMLHttpRequest();

    // device ID is 3rd entry in url seperatered by a '/'
    var pathArray = window.location.pathname.split('/'); 
    var deviceId = pathArray[2];
    var url = "http://db.sead.systems:8080/" + deviceId + "/label";
    var params = JSON.stringify({data: label});
    post.open("POST", url, true);

    post.setRequestHeader("Content-type", "text/plain");
    post.setRequestHeader("Content-length", params.length);<commit_msg>Changed bar graph units from kW to kWh<commit_after>"use strict";

var c3 = require('c3');

var HOUR_SECONDS = 60 * 60;
var DAY_SECONDS = HOUR_SECONDS * 24;


function fetch_pie() {
    var end = Math.floor(Date.now() / 1000);
    var start = end - DAY_SECONDS*2;
    var gran = DAY_SECONDS;
    
    fetch_aggregate([create_url(start, end, gran, "Panel1"),
             create_url(start, end, gran, "Panel2"),
             create_url(start, end, gran, "Panel3")],
             pie, true);
}

function pie(responses) {
    var data = [];
    for (var i = 0; i < responses.length; i++) {
        data[i] = ['Panel ' + (i+1), JSON.parse(responses[i]).data[0].energy];
    }
    c3.generate({
        padding: {
                top: 0,
                right: 0,
                bottom: 0,
                left: 0,
            },
        bindto: '#pie',
        data: {
            // iris data from R
            columns: data,
            type: 'pie',
        },
        pie: {
            label: {
                format: function(value, ratio, id) {
                    return Math.round(value * 100) / 100 + " kW";
                }
            }
        }
    });
}

function generate_bar_graph(data) {
    c3.generate({
        padding: {
            top: 0,
            right: 0,
            bottom: 0,
            left: 60,
        },
        bindto: '#bar',
        data: {
            x: 'x',
            xFormat: '%Y-%m-%d %H:%M:%S',
            columns: [
                ['x'].concat(data.data.map( function(x) { return x.time; })), 
                ['energy'].concat(data.data.map( function(x) { return Math.round(x.energy * 100) / 100}))
            ],
            type: 'bar',
        },
        axis: {
            x: {
                type: 'timeseries',
                tick: {
                    // displays day of week
                    format: '%a'
                }
            },
            y: {
                tick: {
                    format: function (d) { return d + " kWh"; }
                }
            }
        },
        bar: {
            width: {
                ratio: 0.5 // this makes bar width 50% of length between ticks
            }
            // or
            //width: 100 // this makes bar width 100px
        }
    });
}

function post_data_to_server(label) {
    console.log("Sending " + JSON.stringify(label));

    var post = new XMLHttpRequest();

    // device ID is 3rd entry in url seperatered by a '/'
    var pathArray = window.location.pathname.split('/'); 
    var deviceId = pathArray[2];
    var url = "http://db.sead.systems:8080/" + deviceId + "/label";
    var params = JSON.stringify({data: label});
    post.open("POST", url, true);

    post.setRequestHeader("Content-type", "text/plain");
    post.setRequestHeader("Content-length", params.length);
{'fraction_non_alphanumeric': 0.14351851851851852, 'fraction_numerical': 0.020655270655270654, 'mean_word_length': 1.3443238731218699, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6317194', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1671, 'n_words': 406}