/*
   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.99158674070335, "KoPercent": 0.008413259296651522};
    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.997055359246172, 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.9902957486136784, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [1.0, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.5217391304347826, 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 /apicall/track-ship"], "isController": false}, {"data": [1.0, 500, 1500, "get /api/arrival-departure-schedule/all"], "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.9966969446738233, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9977703455964325, 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.9989561586638831, 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.9642857142857143, 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", 11886, 1, 0.008413259296651522, 134.03054013124776, 13, 14666, 111.0, 242.0, 294.0, 412.0, 39.72633415442, 2232.4054834690855, 34.820158337357995], "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", 301, 0, 0.0, 62.60797342192691, 23, 125, 74.0, 88.80000000000001, 98.0, 120.84000000000015, 1.0113295792062575, 0.9817007829404492, 0.8533093324552797], "isController": false}, {"data": ["get api/user-define/all", 1082, 0, 0.0, 299.3548983364137, 155, 673, 289.0, 391.0, 434.8499999999999, 556.3400000000001, 3.6305435413569915, 1721.487217260307, 3.002998417509152], "isController": false}, {"data": ["delete /api/arrival-notification", 478, 0, 0.0, 105.4142259414226, 31, 212, 142.0, 166.0, 174.04999999999995, 187.20999999999998, 1.6139542893029946, 0.5057126237055446, 1.3854158401341137], "isController": false}, {"data": ["get /api/ais/ship-track", 23, 0, 0.0, 4008.9999999999995, 71, 14666, 751.0, 14439.2, 14625.599999999999, 14666.0, 0.08096139901297494, 105.44206422636982, 0.07138779608111628], "isController": false}, {"data": ["get /api/port", 4, 0, 0.0, 60.25, 41, 69, 65.5, 69.0, 69.0, 69.0, 0.019796883970463048, 0.021483679015797914, 0.01662628927206858], "isController": false}, {"data": ["get /apicall/track-ship", 676, 0, 0.0, 150.39940828402368, 28, 477, 127.5, 281.0, 312.29999999999995, 401.69000000000005, 2.2712458926063555, 149.5978951392482, 2.002813753637017], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 637, 0, 0.0, 48.10832025117737, 19, 111, 53.0, 78.0, 87.0, 101.0, 2.1402484300924978, 0.7921427294971256, 1.8632840980297618], "isController": false}, {"data": ["post /oauth/login", 8, 0, 0.0, 600.75, 513, 678, 613.5, 678.0, 678.0, 678.0, 11.7820324005891, 8.525865243004418, 6.213181148748158], "isController": false}, {"data": ["get /api/port/suggestion", 4, 0, 0.0, 46.0, 39, 58, 43.5, 58.0, 58.0, 58.0, 0.019783469921706917, 0.009032004090232406, 0.01678407177195594], "isController": false}, {"data": ["get /api/ais/suggestion", 637, 0, 0.0, 115.46310832025121, 78, 189, 119.0, 145.20000000000005, 154.0, 175.62, 2.1387106630003054, 1.6228302589367551, 1.8170686296975251], "isController": false}, {"data": ["get /apicall/area-ship", 1211, 0, 0.0, 171.25928984310494, 85, 794, 155.0, 238.0, 249.0, 281.8799999999999, 4.062681369704005, 230.0917853886889, 3.566440623752428], "isController": false}, {"data": ["get /apicall/single-ship", 1794, 0, 0.0, 116.8768115942028, 54, 673, 107.0, 174.0, 188.0, 208.04999999999995, 6.023651393766831, 4.9224585254897155, 5.163033270684896], "isController": false}, {"data": ["post /api/area-notification", 300, 0, 0.0, 78.6066666666666, 31, 159, 52.0, 132.0, 140.95, 148.98000000000002, 1.0119647971179244, 0.47923384988514206, 1.0234910498038476], "isController": false}, {"data": ["post /api/user-define", 781, 0, 0.0, 107.91165172855315, 38, 204, 138.0, 161.0, 168.0, 182.17999999999995, 2.619891044736065, 2.0186465178679254, 2.7452569248064433], "isController": false}, {"data": ["get /api/arrival-notification", 958, 1, 0.10438413361169102, 93.33298538622131, 24, 368, 87.0, 157.0, 168.0, 200.0, 3.2336570365794794, 7.871764417916418, 2.6983983767159816], "isController": false}, {"data": ["post /api/arrival-notification", 479, 0, 0.0, 111.23799582463464, 33, 210, 150.0, 174.0, 184.0, 200.39999999999992, 1.6179808679673566, 0.5061369112863454, 1.6321882082888586], "isController": false}, {"data": ["delete /api/user-define", 478, 0, 0.0, 70.9665271966527, 20, 164, 67.0, 121.0, 133.0, 151.20999999999998, 1.6139270423942844, 0.6863984177690665, 1.3712075457842063], "isController": false}, {"data": ["get /api/user-define", 1083, 0, 0.0, 60.38781163434898, 23, 141, 72.0, 91.0, 98.0, 114.32000000000016, 3.6298674746445547, 12.48475902900207, 3.0414319269970975], "isController": false}, {"data": ["get /api/ais/search", 637, 0, 0.0, 186.91208791208797, 132, 310, 183.0, 235.0, 248.0, 263.24, 2.1385527000486797, 2.648129710607154, 1.8399071569754755], "isController": false}, {"data": ["get /api/ais/around-ship-track", 14, 0, 0.0, 315.57142857142856, 185, 637, 325.0, 525.0, 637.0, 637.0, 0.049074937429454776, 3.2520190557543867, 0.07509122680332868], "isController": false}, {"data": ["get /api/ais/filter", 301, 0, 0.0, 20.6544850498339, 13, 64, 18.0, 28.0, 36.69999999999993, 52.98000000000002, 1.0114655160826378, 1.0223308683061816, 0.8316933247476376], "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.008413259296651522], "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", 11886, 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", 958, 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);

});
