Skip to content
Snippets Groups Projects
helper-functions.blade.php 3.26 KiB
Newer Older
//method to parse the data used for the timeline and package it to be displayed
function create_timeline_evidences(data) {
    var period = data.period;
    var interval = data.interval;
    var evidences = data.evidences;
    var agents = data.agents;
    var current_time = Date.now();
    //go through the array of Evidence arrays and create a new array with the data to be displayed
    const timeline_evidences = [];
    for (var i = 0; i < evidences.length; i++) {
        var bin = evidences[i];
        //get the time for this specific bin
        var bin_timestamp = current_time - ((period * 1000) - (interval * i * 1000));
        //loop over the bin and extract all evidences for the given agent and create a new evidence for the timeline
        for (var a = 0; a < agents.length; a++) {
            var filtered_ev = [];
            (bin === null) ? filtered_ev = [] : filtered_ev = bin.filter(ev => ev.label === agents[a].label);
            const timeline_evidence = {"label" : agents[a].label,
                "amount" : filtered_ev.length,
                "evidences" : filtered_ev,
                "time" : bin_timestamp};
            timeline_evidences.push(timeline_evidence);
        }
    }
    return timeline_evidences;
}

//method for counting number of evidences per agent in the database to be used by the donut chart
function create_donut_evidences(data) {
    const donut_evidences = [];
    //loop over the items in the data recieved
    data.forEach(function(item) {
        var temp = {value: item.amount, label: item.label};
        //check if an evidence with the same label as temp already exists
        const duplicate = donut_evidences.some(evidence => evidence.label == temp.label);
        //if no donut evidences have been created, make a new one
        if (!duplicate) {
            donut_evidences.push(temp);
        //else loop over the evidences created to see if one exists already and update its value
        } else {
            donut_evidences.forEach(function(evidence) {
                if (evidence.label == temp.label) {
                    evidence.value = evidence.value + temp.value;
                }
            })
        }
    });
    return donut_evidences;
}


async function load_ranking_html(container, url) {
    //d3.select(container).select("svg").remove();
    clearContent(container.id);
    var url_as_html = await fetchHtmlAsText(url);
    const document = new DOMParser().parseFromString(url_as_html, 'text/html');
    //remove the redundant bubble and radar buttons from the loaded document
    var fs_bubble_button = document.getElementById("fullscreen_bubble_button");
    fs_bubble_button.parentNode.removeChild(fs_bubble_button);
    var fs_radar_button = document.getElementById("fullscreen_radar_button");
    fs_radar_button.parentNode.removeChild(fs_radar_button);
    //get the main part of the HTML and set it to the innerHTML of the container
    const main = document.getElementsByTagName("main")[0];
    //console.log(main);
    container.innerHTML = main.innerHTML;
}

async function fetchHtmlAsText(url) {
    const response = await fetch(url);
    return await response.text();
}

function clearContent(elementID) {
    var div = document.getElementById(elementID);

    while(div.firstChild) {
        div.removeChild(div.lastChild);
    }
}