<commit_before>var anomaly_chart;
var dimension_chart;
var source_data;

var x_axis
var y_axis
var score_tolerance = 0.5

$(document).ready(function() {

    $('#scoreTolerance').bootstrapSlider({
        formatter: function(value) {

            if (value == 2){
                value = "2+";
            }

            return 'Current value: ' + value;
        },
    });


    $("#scoreTolerance").on("slideStop", function(slideEvt) {
        updateChartTolerance(slideEvt.value)
        updateTableTolerance(slideEvt.value)
    });

});

function updateChartTolerance(tolerance = 0.5){

    // updating anomaly chart
    var column_a = []

    // Adjust anomalies table
    for(i=0; i < source_data.result.records.length; i++)
    {

        if ( json["result"][i][0] > 1 + tolerance ){
            column_a[i] = json["result"][i][2];
        } else {
            column_a[i] = null;
        }
    }

    column_a.unshift("Anomaly")

    dimension_chart.load({
        columns: [
            column_a
        ],
    });

    return false
}

function updateChartType(type = 'scatter' ){
    // TODO enum values
    dimension_chart.transform(type, y_axis);
}

function updateTableTolerance(tolerance = 0.5){

    // updating anomaly table
    $('.table-anomalies tr.anomaly').hide();

    $(".table-anomalies tr.anomaly").each(function () {

        if ( $(this).attr("anomaly-score") >= tolerance + 1 ){
            $(this).show();
        }

    });

    return false
}

function detectAnomaly(){

    var api_url = $( "#api-link" ).val();
    var field_name = $( "#field-name" ).val();

    $(".anomalyDetectionResults").fadeIn();
    $("#detectAnomaly").fadeOut();

    var response = $.getJSON(api_url, function(data) {
        data["x"] = "_id";
        data["y"] = field_name;
        data["analysisFeatures"] = [];

        json = JSON.stringify(data);

        // TODO dynamic links
        var url = "http://vmrtpa05.deri.ie:8003/detectAnomalies/lof";
        var dataType ="aplication/json";

        x_axis = data["x"]
        y_axis = data["y"]

        var data_source_dimensions = []

        source_data = data;

        $.ajax({
            type: "POST",
            url: url,
            data: json,
            success: function(data){

                json = JSON.parse(data);

                if (json["result"].length == 0) {

                   $(".anomalyDetectionResults").hide();
                   $(".noAnomalyDetected").fadeIn();

                   return false;

                }

                var table = "<tr><thead>"

                data_source_dimensions = []
                for (field in source_data.result.fields){
                    data_source_dimensions.push(source_data.result.fields[field].id)
                    table += "<th>" + source_data.result.fields[field].id + "</th>"
                }

                table += "<th> Anomaly Score </th> </thead> </tr>";

                for (index in json["result"]){
                    if (json["result"][index][0] > 1.0){

                        table += "<tr class='anomaly' anomaly-score='" + json["result"][index][0] + "'>";

                        for (dimension in data_source_dimensions) {
                            table += "<td>" + source_data.result.records[index][data_source_dimensions[dimension]] + "</td>"
                        }

                        table += "<td>" + json["result"][index][0] + "</td>"
                        table += "</tr>"

                    }
                }

                 table = "<table class='span12 table table-hover -table-striped table-anomalies'>" + table + "</table>";
                 table = "<div class='alert alert-error top20'>The following values are detected as potential anomalies</div>" + table;

                 $("#anomaly-output").html(table);

                // Pares the data for the chart
                var column_x    = []
                var column_y    = []
                var column_a    = []
                var column_as   = []

                //json["result"][i][j] :: j
                // 0 - score
                // 1 - x
                // 2 - y

                // Parse X + Y + make Anomalies null
                for(i=0; i < source_data.result.records.length; i++)
                {
                    column_x[i] = source_data.result.records[i][x_axis];
                    column_y[i] = source_data.result.records[i][y_axis];
                    column_as[i] = json["result"][i][0];

                    if ( json["result"][i][0] > 1 + score_tolerance ){
                        column_a[i] = json["result"][i][2];
                    } else {
                        column_a[i] = null;
                    }
                }

                column_x.unshift(x_axis)
                column_y.unshift(y_axis)
                column_a.unshift("Anomaly")
                column_as.unshift("Anomaly Score")

                dimension_chart = c3.generate({
                    bindto: '#dimension-chart',
                    point: {
                        r: function(d) {<commit_msg>anomaly detection - dynamic dimension chart - work in progress<commit_after>var anomaly_chart;
var dimension_chart;
var source_data;
var data_source_dimensions = []

var x_axis
var y_axis
var score_tolerance = 0.5

var column_as = []

$(document).ready(function() {

    $('#scoreTolerance').bootstrapSlider({
        formatter: function(value) {

            if (value == 2){
                value = "2+";
            }

            return 'Current value: ' + value;
        },
    });


    $("#scoreTolerance").on("slideStop", function(slideEvt) {
        updateChartTolerance(slideEvt.value)
        updateTableTolerance(slideEvt.value)
    });

    $( "#field-name" ).change(function() {
        updateChartDimension( $(this).val() )
        return false
    });

});

function updateChartTolerance(tolerance = 0.5){

    // updating anomaly chart
    var column_a = []

    // Adjust anomalies table
    for(i=0; i < source_data.result.records.length; i++)
    {

        if ( json["result"][i][0] > 1 + tolerance ){
            column_a[i] = json["result"][i][2];
        } else {
            column_a[i] = null;
        }
    }

    column_a.unshift("Anomaly")

    dimension_chart.load({
        columns: [
            column_a
        ],
    });

    return false
}

function updateChartType(type = 'scatter' ){
    // TODO enum values
    dimension_chart.transform(type, y_axis);
}

function updateTableTolerance(tolerance = 0.5){

    // updating anomaly table
    $('.table-anomalies tr.anomaly').hide();

    $(".table-anomalies tr.anomaly").each(function () {

        if ( $(this).attr("anomaly-score") >= tolerance + 1 ){
            $(this).show();
        }

    });

    return false
}

function updateChartDimension(field_name){
    if ( data_source_dimensions.indexOf(field_name) >= 0 ){

        var column_x  = []
        var column_y  = []
        var column_a  = []

        var current_y = y_axis;

        y_axis = field_name;

        for (i=0; i < source_data.result.records.length; i++){

            column_y[i] = source_data.result.records[i][y_axis];
            column_x[i] = source_data.result.records[i][x_axis];

            if ( json["result"][i][0] > 1 + score_tolerance ){
                column_a[i] = column_y[i];
            } else {
                column_a[i] = null;
            }

        }

        column_x.unshift(x_axis)
        column_y.unshift(y_axis)
        column_a.unshift("Anomaly")

        // TODO replace with load()
        dimension_chart = dimension_chart.destroy();

        dimension_chart = c3.generate({
            bindto: '#dimension-chart',
            point: {
                r: function(d) {

                    if (d.id == "Anomaly"){
                        return 4 * column_as[d.index + 1];
                    }
                    return 3;

                }
            },
            data: {
                x: x_axis,
                columns: [
                    column_x,
                    column_y,
                    column_a,
                ],
                type: 'scatter'
            },
            grid: {
                x: {
                    show: false,
                },
                y: {
                    show: true,
                }
            },
            axis: {
                x: {
                    type: 'category',
                    show: true,
                    tick: {
                        rotate: 75,
                        multiline: false,
                    },
                },
                y: {
                    inner: true
                }
            },
            color: {
                pattern: [ '#66A4E0', "#FF360C" ]
            }
        });
    }
}


function detectAnomaly(){

    var api_url = $( "#api-link" ).val();
    var field_name = $( "#field-name" ).val();

    $(".anomalyDetectionResults").fadeIn();
    $("#detectAnomaly").fadeOut();

    var response = $.getJSON(api_url, function(data) {
        data["x"] = "_id";
        data["y"] = field_name;
        data["analysisFeatures"] = [];

        json = JSON.stringify(data);

        // TODO dynamic links
        var url = "http://vmrtpa05.deri.ie:8003/detectAnomalies/lof";
        var dataType ="aplication/json";

        x_axis = data["x"]
        y_axis = data["y"]

        source_data = data;

        $.ajax({
            type: "POST",
            url: url,
            data: json,
            success: function(data){

                json = JSON.parse(data);

                if (json["result"].length == 0) {

                   $(".anomalyDetectionResults").hide();
                   $(".noAnomalyDetected").fadeIn();

                   return false;

                }

                var table = "<tr><thead>"

                data_source_dimensions = []
                for (field in source_data.result.fields){
                    data_source_dimensions.push(source_data.result.fields[field].id)
                    table += "<th>" + source_data.result.fields[field].id + "</th>"
                }

                table += "<th> Anomaly Score </th> </thead> </tr>";

                for (index in json["result"]){
                    if (json["result"][index][0] > 1.0){

                        table += "<tr class='anomaly' anomaly-score='" + json["result"][index][0] + "'>";

                        for (dimension in data_source_dimensions) {
                            table += "<td>" + source_data.result.records[index][data_source_dimensions[dimension]] + "</td>"
                        }

                        table += "<td>" + json["result"][index][0] + "</td>"
                        table += "</tr>"

                    }
                }

                table = "<table class='span12 table table-hover -table-striped table-anomalies'>" + table + "</table>";
                table = "<div class='alert alert-error top20'>The following values are detected as potential anomalies</div>" + table;

                $("#anomaly-output").html(table);

                updateTableTolerance(score_tolerance)

                // Pares the data for the chart
                var column_x    = []
                var column_y    = []
                var column_a    = []
                column_as       = []

                //json["result"][i][j] :: j
                // 0 - score
                // 1 - x
                // 2 - y
                // 3 - type (?) // TODO check updated API

                // Parse X + Y + make Anomalies null
                for(i=0; i < source_data.result.records.length; i++)
                {
                    column_x[i] = source_data.result.records[i][x_axis];
                    column_y[i] = source_data.result.records[i][y_axis];
                    column_as[i] = json["result"][i][0];

                    if ( json["result"][i][0] > 1 + score_tolerance ){
                        column_a[i] = json["result"][i][2];
                    } else {
                        column_a[i] = null;
                    }
                }

                column_x.unshift(x_axis)
                column_y.unshift(y_axis)
                column_a.unshift("Anomaly")
                column_as.unshift("Anomaly Score")

                dimension_chart = c3.generate({
                    bindto: '#dimension-chart',
                    point: {
                        r: function(d) {
{'fraction_non_alphanumeric': 0.13931665062560153, 'fraction_numerical': 0.007539300609560475, 'mean_word_length': 1.3637914691943127, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 49, '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': '2000618', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3388, 'n_words': 656}