/*
   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.09038819653345, "KoPercent": 0.909611803466552};
    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.9040610227761066, 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.2934312878133103, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [0.9984567901234568, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.5503816793893129, 500, 1500, "get /api/ais/ship-track"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/port"], "isController": false}, {"data": [0.9982608695652174, 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": [1.0, 500, 1500, "get /api/ais/suggestion"], "isController": false}, {"data": [0.9918065547561951, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9958656330749354, 500, 1500, "get /apicall/single-ship"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/area-notification"], "isController": false}, {"data": [0.9993351063829787, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [0.975, 500, 1500, "get /api/arrival-notification"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/arrival-notification"], "isController": false}, {"data": [0.9012345679012346, 500, 1500, "delete /api/user-define"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/user-define"], "isController": false}, {"data": [0.9977116704805492, 500, 1500, "get /api/ais/search"], "isController": false}, {"data": [0.5476190476190477, 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", 13962, 127, 0.909611803466552, 2546.960034379017, 14, 300051, 123.0, 1078.0, 1755.2499999999927, 46110.19999999953, 6.870045332851777, 2179.165959756792, 6.109179401205825], "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", 410, 0, 0.0, 57.23658536585368, 26, 187, 66.0, 87.90000000000003, 102.44999999999999, 131.77999999999997, 0.23236123217194277, 0.2255537741981554, 0.19610017269823532], "isController": false}, {"data": ["get api/user-define/all", 1157, 0, 0.0, 1593.022471910113, 982, 4299, 1341.0, 2594.800000000001, 2987.1, 3931.26, 0.6502604695026716, 2014.673763107076, 0.5379892646253809], "isController": false}, {"data": ["delete /api/arrival-notification", 324, 0, 0.0, 102.16358024691361, 34, 1063, 103.0, 159.0, 169.75, 245.25, 0.18692095608915205, 0.06373350162950074, 0.16048871392613046], "isController": false}, {"data": ["get /api/ais/ship-track", 655, 67, 10.229007633587786, 48644.25801526714, 65, 300051, 487.0, 300000.0, 300002.0, 300004.0, 0.32407966323422593, 293.60980317247396, 0.2858788309123016], "isController": false}, {"data": ["get /api/port", 35, 0, 0.0, 62.37142857142856, 27, 119, 61.0, 88.4, 114.99999999999997, 119.0, 0.022036416752713627, 0.02359507513631098, 0.01852374797737175], "isController": false}, {"data": ["get /apicall/track-ship", 1150, 0, 0.0, 144.74521739130418, 30, 1951, 114.0, 271.0, 317.45000000000005, 394.49, 0.6469779099237973, 44.21232878819631, 0.570903065128172], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 434, 0, 0.0, 47.68894009216589, 21, 118, 52.0, 69.0, 80.0, 104.29999999999995, 0.24532622452550004, 0.0907994522413716, 0.21357352039034003], "isController": false}, {"data": ["post /oauth/login", 60, 0, 0.0, 185.2666666666666, 86, 695, 145.0, 527.4, 690.9, 695.0, 3.327048907618942, 2.408428177331707, 1.7544984473771765], "isController": false}, {"data": ["get /api/port/suggestion", 35, 0, 0.0, 92.08571428571426, 28, 482, 64.0, 199.79999999999967, 413.99999999999966, 482.0, 0.022031742075025007, 0.009066578232827674, 0.018629239497311184], "isController": false}, {"data": ["get /api/ais/suggestion", 438, 0, 0.0, 115.30365296803654, 79, 247, 117.0, 144.0, 154.19999999999982, 190.22000000000003, 0.24500083624714655, 0.18590395484768835, 0.20815500735841552], "isController": false}, {"data": ["get /apicall/area-ship", 2502, 0, 0.0, 184.83812949640324, 76, 4741, 183.0, 242.0, 263.8499999999999, 578.7299999999982, 1.3989924123393143, 80.87828889200304, 1.2282153332461991], "isController": false}, {"data": ["get /apicall/single-ship", 1935, 0, 0.0, 123.43669250645996, 57, 3578, 130.0, 163.0, 187.0, 368.0399999999954, 1.0861243267151783, 0.8870936225137014, 0.9311045045954004], "isController": false}, {"data": ["post /api/area-notification", 405, 0, 0.0, 95.68148148148143, 33, 230, 110.0, 148.0, 159.0, 180.94, 0.23127236346650434, 0.10937088224553473, 0.23410861742240383], "isController": false}, {"data": ["post /api/user-define", 752, 0, 0.0, 97.30053191489367, 41, 566, 106.5, 149.70000000000005, 160.0, 186.47000000000003, 0.42176509815292684, 0.3254673400559624, 0.44202937401814824], "isController": false}, {"data": ["get /api/arrival-notification", 680, 14, 2.0588235294117645, 99.64117647058815, 23, 3244, 83.0, 143.0, 159.0, 281.0499999999944, 0.38746151737907353, 0.9230595131275381, 0.32340171767958414], "isController": false}, {"data": ["post /api/arrival-notification", 340, 0, 0.0, 103.50882352941171, 36, 298, 113.0, 159.0, 174.95, 218.12999999999982, 0.19426662065214162, 0.06530942923038421, 0.19619835043355738], "isController": false}, {"data": ["delete /api/user-define", 324, 32, 9.876543209876543, 73.33333333333337, 23, 157, 60.5, 119.0, 133.0, 150.25, 0.18693842744394587, 0.06270722254500513, 0.15904325821562654], "isController": false}, {"data": ["get /api/user-define", 1164, 0, 0.0, 67.06185567010314, 31, 204, 75.0, 97.5, 109.75, 135.0, 0.6521644126654363, 2.2443626857744117, 0.5465693854751107], "isController": false}, {"data": ["get /api/ais/search", 437, 0, 0.0, 188.31350114416497, 136, 1179, 196.0, 223.0, 238.0, 261.0, 0.24514409929177478, 0.30355734170114296, 0.21091010886333356], "isController": false}, {"data": ["get /api/ais/around-ship-track", 315, 14, 4.444444444444445, 1380.1111111111113, 15, 13672, 400.0, 3868.6000000000004, 4565.199999999999, 7586.7199999999875, 0.17721768238653143, 9.640390292057553, 0.2656875228554551], "isController": false}, {"data": ["get /api/ais/filter", 410, 0, 0.0, 23.804878048780495, 14, 113, 18.0, 42.0, 51.0, 85.11999999999989, 0.232371372527016, 0.23486754938033358, 0.19111637689673133], "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", 14, 11.023622047244094, 0.10027216731127346], "isController": false}, {"data": ["504/Gateway Time-out", 67, 52.75590551181102, 0.4798739435610944], "isController": false}, {"data": ["500/Internal Server Error", 32, 25.19685039370079, 0.22919352528291076], "isController": false}, {"data": ["404/Not Found", 14, 11.023622047244094, 0.10027216731127346], "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", 13962, 127, "504/Gateway Time-out", 67, "500/Internal Server Error", 32, "400/Bad Request", 14, "404/Not Found", 14, "", ""], "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", 655, 67, "504/Gateway Time-out", 67, "", "", "", "", "", "", "", ""], "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", 680, 14, "404/Not Found", 14, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": ["delete /api/user-define", 324, 32, "500/Internal Server Error", 32, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": ["get /api/ais/around-ship-track", 315, 14, "400/Bad Request", 14, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
