/*
   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.93849523970006, "KoPercent": 0.06150476029994102};
    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.9035554806639144, 500, 1500, "Total"], "isController": false}, "titles": ["Apdex", "T (Toleration threshold)", "F (Frustration threshold)", "Label"], "items": [{"data": [0.9995894909688013, 500, 1500, "get /api/area-notification"], "isController": false}, {"data": [0.0396485694976346, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [1.0, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.5, 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.8386434390938597, 500, 1500, "get /apicall/track-ship"], "isController": false}, {"data": [0.83, 500, 1500, "post /oauth/login"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/port/suggestion"], "isController": false}, {"data": [0.9997626507167948, 500, 1500, "get /api/ais/suggestion"], "isController": false}, {"data": [0.8137604456824512, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.967687074829932, 500, 1500, "get /apicall/single-ship"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/area-notification"], "isController": false}, {"data": [0.9996891513832763, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [0.9677177177177178, 500, 1500, "get /api/arrival-notification"], "isController": false}, {"data": [0.9974974974974975, 500, 1500, "post /api/arrival-notification"], "isController": false}, {"data": [0.9933909506863243, 500, 1500, "delete /api/user-define"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/user-define"], "isController": false}, {"data": [0.9944919278252612, 500, 1500, "get /api/ais/search"], "isController": false}, {"data": [0.5436893203883495, 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", 118690, 73, 0.06150476029994102, 664.0794422445026, 12, 300004, 204.0, 621.0, 1370.0, 10648.980000000163, 58.10446957458266, 7076.425926106532, 50.63607220223234], "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", 1218, 0, 0.0, 136.54351395730706, 23, 514, 123.5, 233.0, 278.0, 368.0, 0.6779672338101926, 0.6581049125071595, 0.5721674864336975], "isController": false}, {"data": ["get api/user-define/all", 4439, 0, 0.0, 6937.557107456626, 975, 26542, 5473.0, 14897.0, 17345.0, 22475.000000000015, 2.4647869727762917, 6645.065780546644, 2.0392283744302366], "isController": false}, {"data": ["delete /api/arrival-notification", 1967, 0, 0.0, 181.23284189120497, 38, 478, 178.0, 294.0, 326.0, 406.6399999999999, 1.0962761166745436, 0.3482820882652754, 0.9412572371570723], "isController": false}, {"data": ["get /api/ais/ship-track", 401, 24, 5.985037406483791, 38709.62842892768, 66, 300004, 722.0, 171523.00000000012, 299996.0, 300003.0, 0.19643305013010628, 157.34595438004678, 0.17327912814954188], "isController": false}, {"data": ["get /api/port", 45, 0, 0.0, 122.17777777777776, 28, 342, 110.0, 201.8, 263.3999999999999, 342.0, 0.026937913496972176, 0.02885244619451089, 0.022644098943255583], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 10528, 0, 0.0, 115.6329787234044, 20, 446, 102.0, 208.0, 244.54999999999927, 323.7099999999991, 5.85630036234746, 2.1675174192672726, 5.098525492435705], "isController": false}, {"data": ["get /apicall/track-ship", 15097, 0, 0.0, 474.70451082996703, 28, 7305, 248.0, 1136.0, 1700.199999999997, 3159.180000000004, 8.397696015307982, 585.8597831696685, 7.406900268494594], "isController": false}, {"data": ["post /oauth/login", 50, 0, 0.0, 485.9200000000001, 103, 1330, 367.0, 1126.6, 1273.8999999999996, 1330.0, 2.7445383686463938, 1.986574061367878, 1.4473151553408716], "isController": false}, {"data": ["get /api/port/suggestion", 45, 0, 0.0, 131.51111111111118, 46, 332, 114.0, 233.79999999999998, 291.89999999999975, 332.0, 0.026933092810839315, 0.011126850864582205, 0.022774478328436498], "isController": false}, {"data": ["get /api/ais/suggestion", 10533, 0, 0.0, 199.47147061615854, 78, 622, 187.0, 300.0, 341.0, 418.0, 5.856166154328576, 4.44359482608721, 4.975453666275255], "isController": false}, {"data": ["get /apicall/area-ship", 17950, 0, 0.0, 467.0354874651816, 87, 5801, 356.0, 870.8999999999996, 1028.0, 1526.4700000000048, 9.979668163524229, 567.5581858610341, 8.762703314889944], "isController": false}, {"data": ["get /apicall/single-ship", 27636, 0, 0.0, 249.746128238531, 54, 5731, 195.0, 377.0, 452.0, 629.9900000000016, 15.370753520656745, 12.543471576814834, 13.17095844474432], "isController": false}, {"data": ["post /api/area-notification", 1214, 0, 0.0, 152.53706754530498, 31, 415, 147.0, 256.5, 288.0, 367.8499999999999, 0.6766789888386984, 0.31993181145822647, 0.6849092921291197], "isController": false}, {"data": ["post /api/user-define", 3217, 0, 0.0, 177.1367733913582, 39, 527, 174.0, 276.0, 308.0999999999999, 394.6400000000003, 1.789152518995494, 1.3806540688620723, 1.8751171185453974], "isController": false}, {"data": ["get /api/arrival-notification", 3996, 30, 0.7507507507507507, 208.8946446446447, 27, 5434, 166.0, 375.0, 487.0, 705.239999999998, 2.226871134600874, 5.257340522854965, 1.858700618812157], "isController": false}, {"data": ["post /api/arrival-notification", 1998, 0, 0.0, 183.54154154154165, 36, 573, 180.0, 296.0, 341.0, 461.01, 1.1133685584273307, 0.35315671139597915, 1.1244404685648912], "isController": false}, {"data": ["delete /api/user-define", 1967, 13, 0.6609049313675648, 139.94102694458564, 22, 457, 127.0, 237.0, 277.0, 360.31999999999994, 1.0964783923519656, 0.45719298291455884, 0.9328646726103377], "isController": false}, {"data": ["get /api/user-define", 4435, 0, 0.0, 135.36392333709216, 28, 491, 124.0, 225.4000000000001, 263.0, 349.0, 2.466167353504933, 8.484850235071347, 2.066861807603358], "isController": false}, {"data": ["get /api/ais/search", 10530, 0, 0.0, 285.2603988603981, 134, 877, 275.0, 396.0, 433.0, 506.0, 5.854859533412733, 7.249962781608735, 5.037237547789665], "isController": false}, {"data": ["get /api/ais/around-ship-track", 206, 6, 2.912621359223301, 1136.2864077669894, 29, 6008, 611.5, 2570.4000000000005, 3648.399999999998, 5626.430000000002, 0.11610506945168536, 6.9685046945760325, 0.17547748033681743], "isController": false}, {"data": ["get /api/ais/filter", 1218, 0, 0.0, 84.10919540229885, 12, 360, 71.5, 159.0, 189.04999999999995, 270.0499999999997, 0.6779536486764041, 0.685236353886795, 0.557590610898578], "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", 6, 8.219178082191782, 0.005055185778077344], "isController": false}, {"data": ["504/Gateway Time-out", 24, 32.87671232876713, 0.020220743112309376], "isController": false}, {"data": ["500/Internal Server Error", 13, 17.80821917808219, 0.01095290251916758], "isController": false}, {"data": ["404/Not Found", 30, 41.0958904109589, 0.025275928890386722], "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", 118690, 73, "404/Not Found", 30, "504/Gateway Time-out", 24, "500/Internal Server Error", 13, "400/Bad Request", 6, "", ""], "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", 401, 24, "504/Gateway Time-out", 24, "", "", "", "", "", "", "", ""], "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", 3996, 30, "404/Not Found", 30, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": ["delete /api/user-define", 1967, 13, "500/Internal Server Error", 13, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": ["get /api/ais/around-ship-track", 206, 6, "400/Bad Request", 6, "", "", "", "", "", "", "", ""], "isController": false}, {"data": [], "isController": false}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
