/*
   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.16478373143964, "KoPercent": 0.8352162685603616};
    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.8969294706262104, 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.05832147937411095, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [1.0, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.5619307832422586, 500, 1500, "get /api/ais/ship-track"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/port"], "isController": false}, {"data": [0.980625275209159, 500, 1500, "get /apicall/track-ship"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/arrival-departure-schedule/all"], "isController": false}, {"data": [0.97, 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.9895858271486713, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9957939011566772, 500, 1500, "get /apicall/single-ship"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/area-notification"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [0.9526194144838213, 500, 1500, "get /api/arrival-notification"], "isController": false}, {"data": [0.9976993865030674, 500, 1500, "post /api/arrival-notification"], "isController": false}, {"data": [0.9185059422750425, 500, 1500, "delete /api/user-define"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/user-define"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/ais/search"], "isController": false}, {"data": [0.47877358490566035, 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", 24784, 207, 0.8352162685603616, 1768.609627178824, 14, 300016, 146.0, 967.1000000000131, 3192.0, 10259.710000000046, 11.837801485647908, 4359.539652936261, 10.417039977506791], "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", 730, 0, 0.0, 79.5356164383561, 25, 323, 76.5, 121.0, 139.44999999999993, 171.75999999999976, 0.41370625835559127, 0.40158595781782985, 0.34914545749111525], "isController": false}, {"data": ["get api/user-define/all", 2109, 0, 0.0, 4056.706970128014, 1292, 15424, 3022.0, 8218.0, 10010.5, 12714.100000000006, 1.185833423296988, 4691.439610789819, 0.9811622652300033], "isController": false}, {"data": ["delete /api/arrival-notification", 590, 0, 0.0, 130.65423728813582, 37, 317, 140.0, 200.0, 215.0, 260.4400000000005, 0.3389637705180803, 0.11439690625181331, 0.2914293980118339], "isController": false}, {"data": ["get /api/ais/ship-track", 549, 81, 14.754098360655737, 57264.30418943536, 67, 300016, 409.0, 300001.0, 300002.0, 300003.5, 0.2636324128608438, 172.3364744289465, 0.23256119534369454], "isController": false}, {"data": ["get /api/port", 70, 0, 0.0, 76.04285714285717, 26, 178, 69.5, 124.99999999999999, 153.35000000000002, 178.0, 0.04373767144386176, 0.04694416507440715, 0.03677308185317763], "isController": false}, {"data": ["get /apicall/track-ship", 2271, 0, 0.0, 209.10039630118868, 31, 4715, 159.0, 360.0, 437.0, 828.6800000000012, 1.2777046999953865, 90.3325735386991, 1.1270830523175346], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 861, 0, 0.0, 72.3019744483159, 22, 214, 65.0, 115.80000000000007, 133.0, 180.03999999999996, 0.4856521106406885, 0.17974819329377045, 0.4229113601347304], "isController": false}, {"data": ["post /oauth/login", 100, 0, 0.0, 163.66000000000008, 86, 633, 145.5, 196.60000000000002, 579.9999999999995, 632.93, 3.601656762110571, 2.607156323158653, 1.8993111831442464], "isController": false}, {"data": ["get /api/port/suggestion", 70, 0, 0.0, 80.61428571428574, 30, 167, 74.0, 127.5, 148.0, 167.0, 0.0437309223851095, 0.018337947336099624, 0.03700473070966541], "isController": false}, {"data": ["get /api/ais/suggestion", 866, 0, 0.0, 137.6939953810621, 78, 319, 134.0, 181.0, 195.0, 230.66000000000008, 0.48578054162847545, 0.3686049617630131, 0.4128190196124077], "isController": false}, {"data": ["get /apicall/area-ship", 4177, 0, 0.0, 229.2497007421596, 85, 5081, 208.0, 309.0, 354.0999999999999, 641.9800000000023, 2.336591060763115, 131.23938909377102, 2.050989001708671], "isController": false}, {"data": ["get /apicall/single-ship", 3804, 0, 0.0, 162.51104100946384, 58, 4969, 147.0, 230.5, 264.0, 397.89999999999964, 2.1336133280161267, 1.7435592932349762, 1.8288787179081614], "isController": false}, {"data": ["post /api/area-notification", 726, 0, 0.0, 120.03030303030293, 34, 367, 117.0, 184.0, 206.0, 259.6500000000001, 0.4140238902049704, 0.1957689474383555, 0.41915363319507826], "isController": false}, {"data": ["post /api/user-define", 1387, 0, 0.0, 122.57606344628694, 42, 353, 129.0, 183.0, 201.0, 242.1199999999999, 0.7787274347303452, 0.6009975567918001, 0.8162122677573155], "isController": false}, {"data": ["get /api/arrival-notification", 1298, 55, 4.237288135593221, 134.50000000000014, 24, 3992, 120.0, 210.0, 237.04999999999995, 428.36999999999966, 0.7400434789797434, 1.7315883361336777, 0.6181868998905898], "isController": false}, {"data": ["post /api/arrival-notification", 652, 0, 0.0, 129.5322085889569, 36, 880, 138.0, 194.0, 210.0, 263.23000000000025, 0.3705454975312122, 0.12531707001377612, 0.3743006362215044], "isController": false}, {"data": ["delete /api/user-define", 589, 48, 8.149405772495756, 100.15110356536509, 22, 255, 104.0, 159.0, 175.0, 203.30000000000007, 0.3395435013449151, 0.11261745308361994, 0.28894378677907534], "isController": false}, {"data": ["get /api/user-define", 2127, 0, 0.0, 90.43441466854732, 31, 282, 87.0, 133.20000000000005, 148.0, 188.4399999999996, 1.1894310079686845, 4.093315304767231, 0.9969156298056759], "isController": false}, {"data": ["get /api/ais/search", 866, 0, 0.0, 211.57505773672045, 135, 414, 209.5, 266.30000000000007, 285.0, 323.32000000000016, 0.48576065013397685, 0.6015083050487136, 0.4180202476930576], "isController": false}, {"data": ["get /api/ais/around-ship-track", 212, 23, 10.849056603773585, 2185.4905660377353, 15, 15938, 389.5, 6257.6, 7749.4, 12852.730000000009, 0.11943103505582556, 5.831172643582018, 0.17387831301409568], "isController": false}, {"data": ["get /api/ais/filter", 730, 0, 0.0, 42.23561643835618, 14, 156, 34.0, 79.0, 92.0, 123.06999999999982, 0.4137261880742576, 0.4181705123602116, 0.3402736285118552], "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", 23, 11.11111111111111, 0.09280180761781795], "isController": false}, {"data": ["504/Gateway Time-out", 81, 39.130434782608695, 0.32682375726275015], "isController": false}, {"data": ["500/Internal Server Error", 48, 23.18840579710145, 0.19367333763718528], "isController": false}, {"data": ["404/Not Found", 55, 26.570048309178745, 0.22191736604260814], "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", 24784, 207, "504/Gateway Time-out", 81, "404/Not Found", 55, "500/Internal Server Error", 48, "400/Bad Request", 23, "", ""], "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", 549, 81, "504/Gateway Time-out", 81, "", "", "", "", "", "", "", ""], "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", 1298, 55, "404/Not Found", 55, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": ["delete /api/user-define", 589, 48, "500/Internal Server Error", 48, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": ["get /api/ais/around-ship-track", 212, 23, "400/Bad Request", 23, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
