Skip to content
Snippets Groups Projects
helper-functions.blade.php 5.37 KiB
Newer Older
//method to parse the data used for the timeline and package it to be displayed
function create_timeline_evidences(data, unique) {
    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);
            if (unique == true) {
                filtered_ev = filter_unique_ev(filtered_ev);
            }
            //console.log(filtered_ev);
            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 filtering unique evidences from an array of create_donut_evidences
function filter_unique_ev(ev_array) {
    //console.log(ev_array);
    var unique_evs = [];
    //check the format of the filtered_ev array and leave only last unique ones
    for (var i = 0; i < ev_array.length; i++) {
        if (unique_evs.length == 0) {
            unique_evs.push(ev_array[i]);
        } else {
            const index = unique_evs.findIndex(e => _.isEqual(e.subject, ev_array[i].subject));
            if (index > -1) {
                if (ev_array[i].score > unique_evs[index]) {
                    unique_evs[index] = ev_array[i];
                }
            } else {
                unique_evs.push(ev_array[i]);
            }
        }
    }
    //console.log(unique_evs);
    return unique_evs;
}

//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);
    }
}

//function for the use of a Filter bar in the ranking blade
function filterFunction() {
    // Declare and assign variables
    var input, select, selectValue, filter, table, tr, td, i, txtValue;
    input = document.getElementById("filterInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("evTable");
    tr = table.getElementsByTagName("tr");
    select = document.getElementById("selectFilter");
    selectValue = select.value;
    console.log(select.options[select.selectedIndex].text);

    //Loop over the table rows (elements) and hide those who don't match the search query
    for (i = 0; i < tr.length; i++) {
        //select the column of the row, depending on the selected value in the select field

        if (selectValue == "subject") {
            td = tr[i].getElementsByTagName("td")[0];
        } else {
            td = tr[i].getElementsByTagName("td")[1];
        }
        if (td) {
            txtValue = td.textContent || td.innerText;
            if (txtValue.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }
    }
}