/*
   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": 98.85321100917432, "KoPercent": 1.146788990825688};
    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.8979357798165137, 500, 1500, "Total"], "isController": false}, "titles": ["Apdex", "T (Toleration threshold)", "F (Frustration threshold)", "Label"], "items": [{"data": [1.0, 500, 1500, "get /api/area-notification"], "isController": false}, {"data": [0.4111111111111111, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [1.0, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.5049019607843137, 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 /apicall/track-ship"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/arrival-departure-schedule/all"], "isController": false}, {"data": [0.9416666666666667, 500, 1500, "post /oauth/login"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/port/suggestion"], "isController": false}, {"data": [0.9889705882352942, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9857142857142858, 500, 1500, "get /api/ais/suggestion"], "isController": false}, {"data": [1.0, 500, 1500, "get /apicall/single-ship"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/area-notification"], "isController": false}, {"data": [0.9928571428571429, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [0.9509803921568627, 500, 1500, "get /api/arrival-notification"], "isController": false}, {"data": [0.9666666666666667, 500, 1500, "post /api/arrival-notification"], "isController": false}, {"data": [0.8888888888888888, 500, 1500, "delete /api/user-define"], "isController": false}, {"data": [0.9905660377358491, 500, 1500, "get /api/user-define"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/ais/search"], "isController": false}, {"data": [0.6444444444444445, 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", 1308, 15, 1.146788990825688, 4751.07262996942, 15, 300005, 127.5, 1048.3000000000004, 1841.1499999999976, 282435.9900000013, 2.273276316041023, 653.0739319359694, 2.011605272202245], "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", 30, 0, 0.0, 55.83333333333333, 26, 127, 62.0, 78.80000000000001, 114.34999999999998, 127.0, 0.1659613310098747, 0.1610991826404448, 0.14006228736204465], "isController": false}, {"data": ["get api/user-define/all", 90, 0, 0.0, 1257.1777777777784, 974, 2176, 1099.5, 1968.5000000000007, 2130.05, 2176.0, 0.3856950009642375, 1164.5555375691038, 0.31910234845400587], "isController": false}, {"data": ["delete /api/arrival-notification", 18, 0, 0.0, 101.99999999999999, 45, 167, 108.5, 159.8, 167.0, 167.0, 0.1599061883694899, 0.054950401320114424, 0.13729792410674627], "isController": false}, {"data": ["get /api/ais/ship-track", 102, 11, 10.784313725490197, 57911.009803921566, 68, 300005, 706.0, 300001.0, 300004.0, 300005.0, 0.18422060236524804, 137.52257393770634, 0.16249115018674912], "isController": false}, {"data": ["get /api/port", 5, 0, 0.0, 49.0, 27, 74, 51.0, 74.0, 74.0, 74.0, 0.36456434560699963, 0.3887736966824645, 0.30653310699963543], "isController": false}, {"data": ["get /apicall/track-ship", 89, 0, 0.0, 147.67415730337078, 31, 450, 125.0, 263.0, 288.5, 450.0, 0.3469501522292522, 30.160508149186615, 0.30615868803138924], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 32, 0, 0.0, 47.59375, 23, 111, 52.5, 69.89999999999999, 89.54999999999993, 111.0, 0.1313806878600139, 0.04862625068256373, 0.11439277360028247], "isController": false}, {"data": ["post /oauth/login", 60, 0, 0.0, 223.23333333333338, 93, 909, 152.0, 846.0999999999999, 872.95, 909.0, 3.3454139949818793, 2.421722539726791, 1.7641831614162253], "isController": false}, {"data": ["get /api/port/suggestion", 5, 0, 0.0, 109.4, 29, 369, 55.0, 369.0, 369.0, 369.0, 0.3578329635726043, 0.1432030746797395, 0.302201120017176], "isController": false}, {"data": ["get /apicall/area-ship", 272, 0, 0.0, 170.06985294117646, 83, 638, 176.5, 232.10000000000005, 254.0999999999998, 583.039999999999, 0.9784665414805062, 59.035104707386346, 0.8589018702830359], "isController": false}, {"data": ["get /api/ais/suggestion", 35, 0, 0.0, 131.9142857142857, 82, 1090, 94.0, 130.0, 355.5999999999961, 1090.0, 0.13615498327238776, 0.1033129121119583, 0.11567855024118882], "isController": false}, {"data": ["get /apicall/single-ship", 155, 0, 0.0, 104.47096774193547, 60, 196, 107.0, 142.4, 152.2, 183.67999999999995, 0.5810989142822867, 0.47508277730602544, 0.49819200328227164], "isController": false}, {"data": ["post /api/area-notification", 30, 0, 0.0, 89.63333333333334, 38, 150, 85.0, 141.8, 147.8, 150.0, 0.16673058005568803, 0.07880625072944629, 0.1687767203122308], "isController": false}, {"data": ["post /api/user-define", 70, 0, 0.0, 102.77142857142857, 45, 507, 127.5, 154.39999999999998, 168.25, 507.0, 0.26572725754285803, 0.20505632706090468, 0.27849462187011254], "isController": false}, {"data": ["get /api/arrival-notification", 51, 2, 3.9215686274509802, 117.31372549019605, 32, 769, 84.0, 187.6000000000001, 341.1999999999998, 769.0, 0.2408420973096521, 0.658464987284954, 0.20093233340574337], "isController": false}, {"data": ["post /api/arrival-notification", 30, 0, 0.0, 145.86666666666667, 37, 724, 132.5, 176.90000000000003, 692.65, 724.0, 0.1393605210225346, 0.05134836384941631, 0.1407486824624075], "isController": false}, {"data": ["delete /api/user-define", 18, 2, 11.11111111111111, 61.44444444444446, 28, 108, 40.0, 107.1, 108.0, 108.0, 0.16000711142717455, 0.05415171229832437, 0.1361345226454509], "isController": false}, {"data": ["get /api/user-define", 106, 0, 0.0, 79.1603773584906, 31, 768, 77.0, 95.89999999999999, 110.64999999999999, 760.3699999999992, 0.38371040723981903, 1.3205033936651585, 0.32157805429864256], "isController": false}, {"data": ["get /api/ais/search", 35, 0, 0.0, 185.05714285714282, 137, 237, 198.0, 225.0, 231.39999999999998, 237.0, 0.13663175400038258, 0.16918853913328621, 0.11755134304134476], "isController": false}, {"data": ["get /api/ais/around-ship-track", 45, 0, 0.0, 1289.511111111111, 87, 5838, 307.0, 3939.7999999999997, 5505.399999999998, 5838.0, 0.17638896510634294, 11.04159705018854, 0.2699539710545708], "isController": false}, {"data": ["get /api/ais/filter", 30, 0, 0.0, 21.933333333333334, 15, 58, 18.0, 41.60000000000003, 50.29999999999999, 58.0, 0.1660651418196311, 0.1678490447102717, 0.13658209222704426], "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": ["504/Gateway Time-out", 11, 73.33333333333333, 0.8409785932721713], "isController": false}, {"data": ["500/Internal Server Error", 2, 13.333333333333334, 0.1529051987767584], "isController": false}, {"data": ["404/Not Found", 2, 13.333333333333334, 0.1529051987767584], "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", 1308, 15, "504/Gateway Time-out", 11, "500/Internal Server Error", 2, "404/Not Found", 2, "", "", "", ""], "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", 102, 11, "504/Gateway Time-out", 11, "", "", "", "", "", "", "", ""], "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", 51, 2, "404/Not Found", 2, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": ["delete /api/user-define", 18, 2, "500/Internal Server Error", 2, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
