/*
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
var showControllersOnly = false;
var seriesFilter = "";
var filtersOnlySampleSeries = true;

/*
 * Add header in statistics table to group metrics by category
 * format
 *
 */
function summaryTableHeader(header) {
    var newRow = header.insertRow(-1);
    newRow.className = "tablesorter-no-sort";
    var cell = document.createElement('th');
    cell.setAttribute("data-sorter", false);
    cell.colSpan = 1;
    cell.innerHTML = "Requests";
    newRow.appendChild(cell);

    cell = document.createElement('th');
    cell.setAttribute("data-sorter", false);
    cell.colSpan = 3;
    cell.innerHTML = "Executions";
    newRow.appendChild(cell);

    cell = document.createElement('th');
    cell.setAttribute("data-sorter", false);
    cell.colSpan = 7;
    cell.innerHTML = "Response Times (ms)";
    newRow.appendChild(cell);

    cell = document.createElement('th');
    cell.setAttribute("data-sorter", false);
    cell.colSpan = 1;
    cell.innerHTML = "Throughput";
    newRow.appendChild(cell);

    cell = document.createElement('th');
    cell.setAttribute("data-sorter", false);
    cell.colSpan = 2;
    cell.innerHTML = "Network (KB/sec)";
    newRow.appendChild(cell);
}

/*
 * Populates the table identified by id parameter with the specified data and
 * format
 *
 */
function createTable(table, info, formatter, defaultSorts, seriesIndex, headerCreator) {
    var tableRef = table[0];

    // Create header and populate it with data.titles array
    var header = tableRef.createTHead();

    // Call callback is available
    if(headerCreator) {
        headerCreator(header);
    }

    var newRow = header.insertRow(-1);
    for (var index = 0; index < info.titles.length; index++) {
        var cell = document.createElement('th');
        cell.innerHTML = info.titles[index];
        newRow.appendChild(cell);
    }

    var tBody;

    // Create overall body if defined
    if(info.overall){
        tBody = document.createElement('tbody');
        tBody.className = "tablesorter-no-sort";
        tableRef.appendChild(tBody);
        var newRow = tBody.insertRow(-1);
        var data = info.overall.data;
        for(var index=0;index < data.length; index++){
            var cell = newRow.insertCell(-1);
            cell.innerHTML = formatter ? formatter(index, data[index]): data[index];
        }
    }

    // Create regular body
    tBody = document.createElement('tbody');
    tableRef.appendChild(tBody);

    var regexp;
    if(seriesFilter) {
        regexp = new RegExp(seriesFilter, 'i');
    }
    // Populate body with data.items array
    for(var index=0; index < info.items.length; index++){
        var item = info.items[index];
        if((!regexp || filtersOnlySampleSeries && !info.supportsControllersDiscrimination || regexp.test(item.data[seriesIndex]))
                &&
                (!showControllersOnly || !info.supportsControllersDiscrimination || item.isController)){
            if(item.data.length > 0) {
                var newRow = tBody.insertRow(-1);
                for(var col=0; col < item.data.length; col++){
                    var cell = newRow.insertCell(-1);
                    cell.innerHTML = formatter ? formatter(col, item.data[col]) : item.data[col];
                }
            }
        }
    }

    // Add support of columns sort
    table.tablesorter({sortList : defaultSorts});
}

$(document).ready(function() {

    // Customize table sorter default options
    $.extend( $.tablesorter.defaults, {
        theme: 'blue',
        cssInfoBlock: "tablesorter-no-sort",
        widthFixed: true,
        widgets: ['zebra']
    });

    var data = {"OkPercent": 99.86838459407038, "KoPercent": 0.1316154059296204};
    var dataset = [
        {
            "label" : "FAIL",
            "data" : data.KoPercent,
            "color" : "#FF6347"
        },
        {
            "label" : "PASS",
            "data" : data.OkPercent,
            "color" : "#9ACD32"
        }];
    $.plot($("#flot-requests-summary"), dataset, {
        series : {
            pie : {
                show : true,
                radius : 1,
                label : {
                    show : true,
                    radius : 3 / 4,
                    formatter : function(label, series) {
                        return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">'
                            + label
                            + '<br/>'
                            + Math.round10(series.percent, -2)
                            + '%</div>';
                    },
                    background : {
                        opacity : 0.5,
                        color : '#000'
                    }
                }
            }
        },
        legend : {
            show : true
        }
    });

    // Creates APDEX table
    createTable($("#apdexTable"), {"supportsControllersDiscrimination": true, "overall": {"data": [0.7994319756165142, 500, 1500, "Total"], "isController": false}, "titles": ["Apdex", "T (Toleration threshold)", "F (Frustration threshold)", "Label"], "items": [{"data": [0.9714452214452215, 500, 1500, "get /api/area-notification"], "isController": false}, {"data": [0.04759599870926105, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [0.9610778443113772, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.40310077519379844, 500, 1500, "get /api/ais/ship-track"], "isController": false}, {"data": [0.9944444444444445, 500, 1500, "get /api/port"], "isController": false}, {"data": [0.6704076012117874, 500, 1500, "get /apicall/track-ship"], "isController": false}, {"data": [0.9862088376020265, 500, 1500, "get /api/arrival-departure-schedule/all"], "isController": false}, {"data": [0.755, 500, 1500, "post /oauth/login"], "isController": false}, {"data": [0.9611111111111111, 500, 1500, "get /api/port/suggestion"], "isController": false}, {"data": [0.5209851587850417, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9674753867791842, 500, 1500, "get /api/ais/suggestion"], "isController": false}, {"data": [0.8393304775503728, 500, 1500, "get /apicall/single-ship"], "isController": false}, {"data": [0.968365553602812, 500, 1500, "post /api/area-notification"], "isController": false}, {"data": [0.9603617686467173, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [0.8653253579844118, 500, 1500, "get /api/arrival-notification"], "isController": false}, {"data": [0.9411018484958318, 500, 1500, "post /api/arrival-notification"], "isController": false}, {"data": [0.9619992512167728, 500, 1500, "delete /api/user-define"], "isController": false}, {"data": [0.9782117495158167, 500, 1500, "get /api/user-define"], "isController": false}, {"data": [0.901244988394176, 500, 1500, "get /api/ais/search"], "isController": false}, {"data": [0.46540880503144655, 500, 1500, "get /api/ais/around-ship-track"], "isController": false}, {"data": [0.9865967365967366, 500, 1500, "get /api/ais/filter"], "isController": false}]}, function(index, item){
        switch(index){
            case 0:
                item = item.toFixed(3);
                break;
            case 1:
            case 2:
                item = formatDuration(item);
                break;
        }
        return item;
    }, [[0, 0]], 3);

    // Create statistics table
    createTable($("#statisticsTable"), {"supportsControllersDiscrimination": true, "overall": {"data": ["Total", 144360, 190, 0.1316154059296204, 1092.544077306745, 13, 300036, 308.0, 1539.0, 2960.850000000002, 20045.970000000165, 72.63809212800525, 7638.909370814227, 63.319157717363304], "isController": false}, "titles": ["Label", "#Samples", "FAIL", "Error %", "Average", "Min", "Max", "Median", "90th pct", "95th pct", "99th pct", "Transactions/s", "Received", "Sent"], "items": [{"data": ["get /api/area-notification", 1716, 0, 0.0, 233.46678321678309, 24, 2142, 184.5, 423.0, 506.0, 1249.3199999999997, 0.9551279125557018, 0.9271456494925466, 0.8060794208201187], "isController": false}, {"data": ["get api/user-define/all", 6198, 0, 0.0, 9644.17618586641, 697, 30595, 7405.5, 21002.0, 23759.149999999998, 28239.090000000004, 3.434280189167999, 6855.880424654003, 2.8414744015082465], "isController": false}, {"data": ["delete /api/arrival-notification", 2672, 0, 0.0, 270.4389970059882, 36, 2036, 225.0, 464.0, 549.6999999999998, 1217.5899999999997, 1.488053801609905, 0.47595915286745516, 1.2777661765287358], "isController": false}, {"data": ["get /api/ais/ship-track", 645, 57, 8.837209302325581, 46697.64341085269, 71, 300036, 1489.0, 251931.59999999995, 300002.0, 300004.54, 0.3247763574896449, 301.249685608933, 0.28644765300617026], "isController": false}, {"data": ["get /api/port", 90, 0, 0.0, 168.6888888888888, 24, 790, 136.0, 318.40000000000003, 377.45, 790.0, 0.05169401279943757, 0.05532369983096058, 0.043476594315151974], "isController": false}, {"data": ["get /apicall/track-ship", 14524, 0, 0.0, 1056.82325805563, 28, 15387, 424.0, 2861.0, 4263.0, 7658.25, 8.075331208675365, 560.227110893225, 7.122548530383128], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 14212, 0, 0.0, 188.63052350126486, 19, 2146, 146.0, 349.0, 412.0, 934.0, 7.906818379068852, 2.9264493805342724, 6.886827046623635], "isController": false}, {"data": ["post /oauth/login", 100, 0, 0.0, 541.47, 95, 1427, 482.5, 1156.9000000000005, 1345.7999999999997, 1426.4199999999996, 5.13083632632119, 3.7140440610569527, 2.7057144689584405], "isController": false}, {"data": ["get /api/port/suggestion", 90, 0, 0.0, 215.68888888888875, 33, 945, 166.0, 461.10000000000014, 606.6500000000005, 945.0, 0.05168887661118536, 0.02105581386433508, 0.04371849221967543], "isController": false}, {"data": ["get /apicall/area-ship", 18799, 0, 0.0, 1015.9056864726831, 91, 7434, 902.0, 1895.0, 2275.0, 3505.0, 10.447917141386736, 600.0150479024968, 9.173494574088565], "isController": false}, {"data": ["get /api/ais/suggestion", 14220, 0, 0.0, 282.00210970464144, 80, 2085, 242.0, 456.0, 524.0, 994.5799999999981, 7.905705283979939, 5.998762700832434, 6.7198093148194165], "isController": false}, {"data": ["get /apicall/single-ship", 31515, 0, 0.0, 410.82049817547306, 54, 9658, 296.0, 722.0, 830.0, 1879.9800000000032, 17.527516226091887, 14.297723802230776, 15.023197154489914], "isController": false}, {"data": ["post /api/area-notification", 1707, 0, 0.0, 243.34504979496216, 34, 2157, 197.0, 442.20000000000005, 526.1999999999998, 1139.3200000000033, 0.9513856957739414, 0.4500403421045298, 0.9627268780740988], "isController": false}, {"data": ["post /api/user-define", 4478, 0, 0.0, 273.62326931665996, 41, 2126, 228.0, 467.0999999999999, 547.1000000000004, 1316.63, 2.4899786422332184, 1.9208911051726831, 2.609754649804355], "isController": false}, {"data": ["get /api/arrival-notification", 5517, 85, 1.5406924052927315, 355.8272611926766, 28, 4069, 256.0, 728.0, 918.1999999999989, 1737.0199999999968, 3.0717356336366466, 7.234234379149724, 2.5641523471885295], "isController": false}, {"data": ["post /api/arrival-notification", 2759, 0, 0.0, 286.54295034432715, 35, 2019, 230.0, 516.0, 602.0, 1373.400000000001, 1.5359973989924436, 0.4927624856156822, 1.5509832380595547], "isController": false}, {"data": ["delete /api/user-define", 2671, 32, 1.1980531636091352, 228.54436540621498, 23, 2238, 173.0, 413.8000000000002, 491.0, 1300.2800000000002, 1.4879460217080795, 0.5765601515827601, 1.2656188895889458], "isController": false}, {"data": ["get /api/user-define", 6196, 0, 0.0, 217.29777275661746, 29, 2094, 166.0, 401.0, 477.0, 1124.119999999999, 3.445133359911347, 11.852739088835621, 2.887461606446336], "isController": false}, {"data": ["get /api/ais/search", 14217, 0, 0.0, 384.7285643947379, 136, 2248, 351.0, 576.0, 654.0, 1218.4599999999991, 7.905263614435321, 9.788939710062488, 6.804353279048243], "isController": false}, {"data": ["get /api/ais/around-ship-track", 318, 16, 5.031446540880503, 1373.044025157234, 22, 8447, 826.0, 3294.100000000001, 4183.350000000001, 6255.3600000000015, 0.17803197409466823, 10.383569787589101, 0.26652747654792647], "isController": false}, {"data": ["get /api/ais/filter", 1716, 0, 0.0, 157.58158508158496, 13, 1577, 113.0, 303.29999999999995, 367.0, 1058.9199999999983, 0.9551587478291846, 0.965419242190631, 0.7855844555010687], "isController": false}]}, function(index, item){
        switch(index){
            // Errors pct
            case 3:
                item = item.toFixed(2) + '%';
                break;
            // Mean
            case 4:
            // Mean
            case 7:
            // Median
            case 8:
            // Percentile 1
            case 9:
            // Percentile 2
            case 10:
            // Percentile 3
            case 11:
            // Throughput
            case 12:
            // Kbytes/s
            case 13:
            // Sent Kbytes/s
                item = item.toFixed(2);
                break;
        }
        return item;
    }, [[0, 0]], 0, summaryTableHeader);

    // Create error table
    createTable($("#errorsTable"), {"supportsControllersDiscrimination": false, "titles": ["Type of error", "Number of errors", "% in errors", "% in all samples"], "items": [{"data": ["400/Bad Request", 16, 8.421052631578947, 0.011083402604599613], "isController": false}, {"data": ["504/Gateway Time-out", 57, 30.0, 0.03948462177888612], "isController": false}, {"data": ["500/Internal Server Error", 32, 16.842105263157894, 0.022166805209199225], "isController": false}, {"data": ["404/Not Found", 85, 44.73684210526316, 0.05888057633693544], "isController": false}]}, function(index, item){
        switch(index){
            case 2:
            case 3:
                item = item.toFixed(2) + '%';
                break;
        }
        return item;
    }, [[1, 1]]);

        // Create top5 errors by sampler
    createTable($("#top5ErrorsBySamplerTable"), {"supportsControllersDiscrimination": false, "overall": {"data": ["Total", 144360, 190, "404/Not Found", 85, "504/Gateway Time-out", 57, "500/Internal Server Error", 32, "400/Bad Request", 16, "", ""], "isController": false}, "titles": ["Sample", "#Samples", "#Errors", "Error", "#Errors", "Error", "#Errors", "Error", "#Errors", "Error", "#Errors", "Error", "#Errors"], "items": [{"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": ["get /api/ais/ship-track", 645, 57, "504/Gateway Time-out", 57, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": ["get /api/arrival-notification", 5517, 85, "404/Not Found", 85, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": ["delete /api/user-define", 2671, 32, "500/Internal Server Error", 32, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": ["get /api/ais/around-ship-track", 318, 16, "400/Bad Request", 16, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
