/*
   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.85548390883015, "KoPercent": 0.14451609116984676};
    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.885417812892009, 500, 1500, "Total"], "isController": false}, "titles": ["Apdex", "T (Toleration threshold)", "F (Frustration threshold)", "Label"], "items": [{"data": [0.9997957516339869, 500, 1500, "get /api/area-notification"], "isController": false}, {"data": [0.14339668615984405, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [0.9979763387297634, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.481651376146789, 500, 1500, "get /api/ais/ship-track"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/port"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/arrival-departure-schedule/all"], "isController": false}, {"data": [0.832810539523212, 500, 1500, "get /apicall/track-ship"], "isController": false}, {"data": [0.84, 500, 1500, "post /oauth/login"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/port/suggestion"], "isController": false}, {"data": [0.9994764397905759, 500, 1500, "get /api/ais/suggestion"], "isController": false}, {"data": [0.8016217935128259, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9518446387888587, 500, 1500, "get /apicall/single-ship"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/area-notification"], "isController": false}, {"data": [0.9993918331885318, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [0.9507862110674328, 500, 1500, "get /api/arrival-notification"], "isController": false}, {"data": [0.9954641669186574, 500, 1500, "post /api/arrival-notification"], "isController": false}, {"data": [0.9886363636363636, 500, 1500, "delete /api/user-define"], "isController": false}, {"data": [0.9999390466902353, 500, 1500, "get /api/user-define"], "isController": false}, {"data": [0.9865968586387435, 500, 1500, "get /api/ais/search"], "isController": false}, {"data": [0.5682281059063137, 500, 1500, "get /api/ais/around-ship-track"], "isController": false}, {"data": [1.0, 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", 136317, 197, 0.14451609116984676, 541.3126389225138, 13, 289920, 205.0, 728.8000000000029, 1665.9000000000015, 7176.700000000048, 72.11753639550399, 6186.3783619599535, 63.08823625325097], "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", 2448, 0, 0.0, 155.7410130718956, 25, 521, 139.0, 270.0, 315.0, 393.50999999999976, 1.3616475935882417, 1.3217555742448364, 1.1491508892426503], "isController": false}, {"data": ["get api/user-define/all", 8208, 0, 0.0, 3193.430799220272, 313, 11609, 2782.5, 6202.1, 7164.549999999999, 9178.109999999997, 4.554029161765685, 4828.231179688541, 3.7673771731923704], "isController": false}, {"data": ["delete /api/arrival-notification", 3212, 0, 0.0, 203.78673723536755, 34, 625, 196.5, 320.0, 367.0, 452.0, 1.7887603256078417, 0.5706019962331337, 1.5354690685637624], "isController": false}, {"data": ["get /api/ais/ship-track", 763, 0, 0.0, 13281.237221494113, 68, 289920, 771.0, 38037.40000000002, 68086.79999999994, 157932.6400000001, 0.4039390142088063, 457.0488704249804, 0.35619759926946865], "isController": false}, {"data": ["get /api/port", 45, 0, 0.0, 147.77777777777777, 31, 349, 134.0, 256.0, 294.9999999999999, 349.0, 0.0269687647766357, 0.0288749554640815, 0.022667691592098034], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 9545, 0, 0.0, 131.54541644840302, 19, 482, 116.0, 238.0, 277.0, 339.0, 5.310599110133869, 1.9655440065827505, 4.6234401592985], "isController": false}, {"data": ["get /apicall/track-ship", 14346, 0, 0.0, 485.9348250383379, 27, 6334, 288.0, 1065.300000000001, 1569.2999999999993, 3181.5300000000007, 7.978665717487605, 517.6227997152811, 7.037349289436582], "isController": false}, {"data": ["post /oauth/login", 50, 0, 0.0, 447.82, 117, 1483, 309.5, 919.3, 1143.4499999999996, 1483.0, 2.7566435108611755, 1.9950130595986326, 1.4536987264306978], "isController": false}, {"data": ["get /api/port/suggestion", 45, 0, 0.0, 147.73333333333332, 36, 317, 145.0, 260.59999999999997, 285.69999999999993, 317.0, 0.026965500338866456, 0.011036075757176269, 0.02279837082686411], "isController": false}, {"data": ["get /api/ais/suggestion", 9550, 0, 0.0, 216.86544502617753, 82, 594, 204.0, 326.0, 363.0, 428.0, 5.310721707616687, 4.029717545720865, 4.512038950807146], "isController": false}, {"data": ["get /apicall/area-ship", 18868, 0, 0.0, 485.6993322026688, 93, 5787, 403.0, 848.0, 1004.0, 1472.1700000000092, 10.490918027609553, 594.1036329661864, 9.211197342116353], "isController": false}, {"data": ["get /apicall/single-ship", 27214, 0, 0.0, 284.82053354890866, 55, 5447, 217.0, 398.0, 464.0, 594.9900000000016, 15.132642779770947, 12.357434334240732, 12.967931156804184], "isController": false}, {"data": ["post /api/area-notification", 2444, 0, 0.0, 180.99713584288043, 33, 489, 169.0, 292.0, 341.0, 421.5500000000002, 1.3601315610069646, 0.6435969617212565, 1.376117341607838], "isController": false}, {"data": ["post /api/user-define", 5755, 0, 0.0, 199.08427454387453, 41, 587, 190.0, 314.0, 354.0, 433.4399999999996, 3.200762621321695, 2.4664733107217485, 3.354184818978937], "isController": false}, {"data": ["get /api/arrival-notification", 6614, 94, 1.4212276988206833, 237.0205624433017, 27, 4096, 192.0, 430.0, 542.0, 762.4000000000015, 3.6827141837392636, 8.639017992938866, 3.0731242871144535], "isController": false}, {"data": ["post /api/arrival-notification", 3307, 0, 0.0, 211.93226489265243, 33, 665, 203.0, 345.0, 392.5999999999999, 489.84000000000015, 1.8415631959112508, 0.5890811951101959, 1.8577248820065855], "isController": false}, {"data": ["delete /api/user-define", 3212, 36, 1.1207970112079702, 162.72851805728536, 21, 501, 148.0, 282.0, 319.0, 399.0, 1.7886457746306323, 0.693472422654435, 1.5196502186803225], "isController": false}, {"data": ["get /api/user-define", 8203, 0, 0.0, 155.37242472266277, 27, 501, 139.0, 269.0, 308.7999999999993, 387.0, 4.561520187910387, 15.689639355157693, 3.8225763257352483], "isController": false}, {"data": ["get /api/ais/search", 9550, 0, 0.0, 307.0887958115174, 136, 792, 296.0, 425.0, 468.0, 547.4899999999998, 5.310069114302713, 6.575359020445156, 4.56852625947333], "isController": false}, {"data": ["get /api/ais/around-ship-track", 491, 67, 13.64562118126273, 681.9246435845209, 20, 6031, 513.0, 1282.4, 1787.3999999999996, 3499.5999999999913, 0.27501641136629945, 14.765205045108853, 0.39540118281561915], "isController": false}, {"data": ["get /api/ais/filter", 2447, 0, 0.0, 97.60890886800163, 13, 396, 87.0, 185.0, 223.0, 292.0, 1.3626489405362723, 1.3772867709521892, 1.1207204132690933], "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", 67, 34.01015228426396, 0.049150142682130624], "isController": false}, {"data": ["500/Internal Server Error", 36, 18.274111675126903, 0.026409031888906007], "isController": false}, {"data": ["404/Not Found", 94, 47.71573604060914, 0.06895691659881012], "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", 136317, 197, "404/Not Found", 94, "400/Bad Request", 67, "500/Internal Server Error", 36, "", "", "", ""], "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": [], "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", 6614, 94, "404/Not Found", 94, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": ["delete /api/user-define", 3212, 36, "500/Internal Server Error", 36, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": ["get /api/ais/around-ship-track", 491, 67, "400/Bad Request", 67, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
