/*
   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.92277992277992, "KoPercent": 0.07722007722007722};
    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.9548262548262548, 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.13333333333333333, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [1.0, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.25, 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.9956896551724138, 500, 1500, "get /apicall/track-ship"], "isController": false}, {"data": [0.5, 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.9968944099378882, 500, 1500, "get /apicall/area-ship"], "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": [1.0, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/arrival-notification"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/arrival-notification"], "isController": false}, {"data": [1.0, 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": [1.0, 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", 1295, 1, 0.07722007722007722, 461.51351351351354, 15, 300003, 129.0, 257.4000000000001, 747.8000000000031, 2850.0, 4.1851280907736506, 733.797525038054, 3.6408575604177376], "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", 12, 0, 0.0, 57.58333333333333, 28, 89, 58.5, 88.4, 89.0, 89.0, 0.21328783192918843, 0.20703916497813799, 0.17996160819025275], "isController": false}, {"data": ["get api/user-define/all", 60, 0, 0.0, 2196.5000000000005, 1225, 4527, 2189.5, 3174.8, 4185.549999999996, 4527.0, 1.0354646647683148, 3604.500428747088, 0.8564829795495729], "isController": false}, {"data": ["delete /api/arrival-notification", 36, 0, 0.0, 108.16666666666666, 43, 167, 125.0, 165.0, 167.0, 167.0, 0.6816116328385338, 0.2123379989018479, 0.585759996970615], "isController": false}, {"data": ["get /api/ais/ship-track", 2, 1, 50.0, 150289.5, 576, 300003, 150289.5, 300003.0, 300003.0, 300003.0, 0.006491230347800123, 2.659696057267257, 0.005724200199280771], "isController": false}, {"data": ["get /api/port", 2, 0, 0.0, 130.5, 129, 132, 130.5, 132.0, 132.0, 132.0, 15.037593984962406, 15.904017857142856, 12.64391447368421], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 116, 0, 0.0, 54.55172413793104, 23, 112, 54.0, 85.3, 92.44999999999997, 110.29999999999998, 2.0186551580119727, 0.7471389696157594, 1.7573672484076988], "isController": false}, {"data": ["get /apicall/track-ship", 116, 0, 0.0, 165.84482758620692, 34, 640, 135.5, 314.3, 346.19999999999993, 612.4599999999997, 2.0101895816726163, 125.41970898281807, 1.7725897220393025], "isController": false}, {"data": ["post /oauth/login", 8, 0, 0.0, 699.0, 525, 806, 730.0, 806.0, 806.0, 806.0, 9.864364981504316, 7.138178175092478, 5.201911220715166], "isController": false}, {"data": ["get /api/port/suggestion", 2, 0, 0.0, 393.5, 392, 395, 393.5, 395.0, 395.0, 395.0, 5.063291139240507, 1.8443433544303796, 4.2672072784810124], "isController": false}, {"data": ["get /api/ais/suggestion", 117, 0, 0.0, 123.99145299145303, 88, 229, 123.0, 153.0, 169.0, 218.91999999999962, 2.006998764923837, 1.522888711275216, 1.705164966292713], "isController": false}, {"data": ["get /apicall/area-ship", 161, 0, 0.0, 200.40372670807463, 93, 711, 197.0, 274.8, 298.9000000000001, 520.0399999999986, 2.7668459674508927, 156.2437400915551, 2.428861259215659], "isController": false}, {"data": ["get /apicall/single-ship", 269, 0, 0.0, 126.82527881040885, 61, 424, 128.0, 185.0, 200.0, 226.80000000000018, 4.657766696102367, 3.7821019579069484, 3.9911684782608696], "isController": false}, {"data": ["post /api/area-notification", 11, 0, 0.0, 106.72727272727272, 51, 189, 97.0, 187.20000000000002, 189.0, 189.0, 0.2266125543355102, 0.10710984013514349, 0.22930840681073733], "isController": false}, {"data": ["post /api/user-define", 49, 0, 0.0, 110.20408163265304, 42, 213, 119.0, 160.0, 168.5, 213.0, 0.8442453480358373, 0.6513220946760855, 0.8846438070727085], "isController": false}, {"data": ["get /api/arrival-notification", 72, 0, 0.0, 102.58333333333333, 31, 187, 97.5, 164.4, 176.04999999999998, 187.0, 1.3600559134097736, 2.889731430278245, 1.1355935605130434], "isController": false}, {"data": ["post /api/arrival-notification", 37, 0, 0.0, 102.40540540540539, 41, 177, 125.0, 165.00000000000003, 175.2, 177.0, 0.6965754843082252, 0.21699958934992564, 0.7033412090291244], "isController": false}, {"data": ["delete /api/user-define", 35, 0, 0.0, 74.88571428571431, 27, 146, 69.0, 115.19999999999997, 139.59999999999997, 146.0, 0.6904306314481289, 0.29275876970193127, 0.5872705859290237], "isController": false}, {"data": ["get /api/user-define", 61, 0, 0.0, 72.90163934426232, 35, 118, 75.0, 108.60000000000001, 115.9, 118.0, 1.051434087148373, 3.6184118389754554, 0.8809867644270546], "isController": false}, {"data": ["get /api/ais/search", 116, 0, 0.0, 190.9051724137931, 136, 265, 192.5, 233.3, 240.29999999999998, 264.49, 2.0065038400332114, 2.4846160831661246, 1.7262987139348234], "isController": false}, {"data": ["get /api/ais/around-ship-track", 1, 0, 0.0, 278.0, 278, 278, 278.0, 278.0, 278.0, 278.0, 3.5971223021582737, 277.5791085881295, 5.504580710431654], "isController": false}, {"data": ["get /api/ais/filter", 12, 0, 0.0, 31.0, 15, 52, 28.5, 52.0, 52.0, 52.0, 0.21372088053002777, 0.21601671030134642, 0.17573533340457362], "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", 1, 100.0, 0.07722007722007722], "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", 1295, 1, "504/Gateway Time-out", 1, "", "", "", "", "", "", "", ""], "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", 2, 1, "504/Gateway Time-out", 1, "", "", "", "", "", "", "", ""], "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": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}, {"data": [], "isController": false}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
