/*
   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.96256083863722, "KoPercent": 0.037439161362785474};
    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.9968176712841632, 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": [1.0, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [1.0, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [1.0, 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.9963768115942029, 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": [0.9959677419354839, 500, 1500, "get /api/ais/suggestion"], "isController": false}, {"data": [0.9980392156862745, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9986486486486487, 500, 1500, "get /apicall/single-ship"], "isController": false}, {"data": [1.0, 500, 1500, "post /api/area-notification"], "isController": false}, {"data": [0.9973821989528796, 500, 1500, "post /api/user-define"], "isController": false}, {"data": [0.9953703703703703, 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": [0.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", 2671, 1, 0.037439161362785474, 112.99550730063646, 14, 5727, 103.0, 189.60000000000036, 220.4000000000001, 274.0, 44.492937100213226, 1311.4125266003966, 38.969515737440034], "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", 83, 0, 0.0, 61.951807228915676, 24, 109, 77.0, 85.60000000000001, 91.6, 109.0, 1.4576235467668857, 1.4149197319201996, 1.2298698675845596], "isController": false}, {"data": ["get api/user-define/all", 273, 0, 0.0, 146.13553113553115, 79, 274, 151.0, 186.2, 199.3, 266.5999999999999, 4.766643968362056, 916.0976915114887, 3.9427221105494734], "isController": false}, {"data": ["delete /api/arrival-notification", 107, 0, 0.0, 111.55140186915891, 33, 180, 144.0, 162.2, 170.6, 179.84, 1.8976341645089205, 0.601410506375696, 1.627073043397297], "isController": false}, {"data": ["get /api/ais/ship-track", 1, 0, 0.0, 353.0, 353, 353, 353.0, 353.0, 353.0, 353.0, 2.8328611898017, 93.91875442634561, 2.4981188031161476], "isController": false}, {"data": ["get /api/port", 2, 0, 0.0, 82.0, 77, 87, 82.0, 87.0, 87.0, 87.0, 22.47191011235955, 23.689870084269664, 18.894838483146067], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 123, 0, 0.0, 42.56910569105691, 22, 111, 33.0, 66.0, 75.0, 106.2000000000001, 2.153172866520788, 0.7969262855579868, 1.8744871444201312], "isController": false}, {"data": ["get /apicall/track-ship", 138, 0, 0.0, 139.14492753623182, 29, 893, 120.0, 240.0, 298.04999999999995, 724.9099999999936, 2.4174053182917006, 152.1816111327646, 2.1317548851732475], "isController": false}, {"data": ["post /oauth/login", 8, 0, 0.0, 804.2499999999999, 713, 899, 802.5, 899.0, 899.0, 899.0, 8.898776418242491, 6.43944660734149, 4.692714126807564], "isController": false}, {"data": ["get /api/port/suggestion", 2, 0, 0.0, 168.0, 32, 304, 168.0, 304.0, 304.0, 304.0, 6.578947368421052, 2.3900082236842106, 5.541349712171053], "isController": false}, {"data": ["get /api/ais/suggestion", 124, 0, 0.0, 123.08870967741936, 80, 810, 124.0, 142.0, 146.5, 652.5, 2.1434004010233005, 1.6263887808545945, 1.8210530750881562], "isController": false}, {"data": ["get /apicall/area-ship", 255, 0, 0.0, 172.6941176470587, 96, 532, 162.0, 232.4, 240.0, 352.3599999999995, 4.394884698907311, 257.4761849492434, 3.8581809486057015], "isController": false}, {"data": ["get /apicall/single-ship", 370, 0, 0.0, 113.5189189189189, 57, 1306, 85.0, 168.0, 176.0, 200.4500000000001, 6.310009038661596, 5.145968255964664, 5.408757615711923], "isController": false}, {"data": ["post /api/area-notification", 82, 0, 0.0, 75.07317073170732, 33, 151, 43.5, 130.7, 138.94999999999996, 151.0, 1.4610505309671442, 0.6948376864175754, 1.4770238111146745], "isController": false}, {"data": ["post /api/user-define", 191, 0, 0.0, 108.35602094240836, 40, 538, 139.0, 154.0, 160.39999999999998, 197.5999999999941, 3.3240515140967632, 2.5612076607640097, 3.483112572876784], "isController": false}, {"data": ["get /api/arrival-notification", 216, 1, 0.46296296296296297, 89.91203703703701, 25, 174, 86.0, 154.0, 160.5999999999999, 172.48999999999995, 3.8182107440207878, 9.29123151106574, 3.184328100970462], "isController": false}, {"data": ["post /api/arrival-notification", 108, 0, 0.0, 105.98148148148148, 32, 258, 102.5, 170.20000000000002, 181.49999999999997, 255.5699999999999, 1.9082620679906708, 0.6054425224397484, 1.9250339025726202], "isController": false}, {"data": ["delete /api/user-define", 106, 0, 0.0, 74.00000000000003, 23, 146, 67.5, 123.0, 127.64999999999999, 145.64999999999998, 1.8972614999105066, 0.7716364439770897, 1.6118961987649902], "isController": false}, {"data": ["get /api/user-define", 274, 0, 0.0, 57.95985401459857, 23, 115, 45.0, 90.5, 95.25, 110.25, 4.745903626978903, 16.3186783892507, 3.97654815619912], "isController": false}, {"data": ["get /api/ais/search", 124, 0, 0.0, 185.82258064516125, 137, 260, 186.0, 231.0, 240.5, 259.75, 2.141660477728458, 2.6519780134371924, 1.8425809383581755], "isController": false}, {"data": ["get /api/ais/around-ship-track", 1, 0, 0.0, 5727.0, 5727, 5727, 5727.0, 5727.0, 5727.0, 5727.0, 0.17461148943600488, 11.520777403090623, 0.2672033241662301], "isController": false}, {"data": ["get /api/ais/filter", 83, 0, 0.0, 19.253012048192776, 14, 51, 17.0, 24.0, 26.599999999999994, 51.0, 1.4591610702858548, 1.4748356520955663, 1.1998179894342675], "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": ["404/Not Found", 1, 100.0, 0.037439161362785474], "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", 2671, 1, "404/Not Found", 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": [], "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", 216, 1, "404/Not Found", 1, "", "", "", "", "", "", "", ""], "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);

});
