/*
   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.97677838842016, "KoPercent": 0.02322161157984364};
    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.9982970818174781, 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.9996066089693155, 500, 1500, "get api/user-define/all"], "isController": false}, {"data": [1.0, 500, 1500, "delete /api/arrival-notification"], "isController": false}, {"data": [0.5625, 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.9977973568281938, 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.9988009592326139, 500, 1500, "get /apicall/area-ship"], "isController": false}, {"data": [0.9983783783783784, 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.9957466918714556, 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.125, 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", 12919, 3, 0.02322161157984364, 118.0268596640607, 13, 13327, 110.0, 214.0, 235.0, 299.7999999999993, 43.19348971567658, 1750.1912501285121, 37.85697934358868], "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", 372, 0, 0.0, 60.79569892473119, 23, 113, 75.0, 86.69999999999999, 93.0, 104.0, 1.2495297468694577, 1.2129224300666417, 1.054290723921105], "isController": false}, {"data": ["get api/user-define/all", 1271, 0, 0.0, 204.66247049567284, 94, 521, 201.0, 265.0, 295.39999999999986, 364.7999999999997, 4.273806041164385, 1293.4576633707334, 3.5350719891271822], "isController": false}, {"data": ["delete /api/arrival-notification", 525, 0, 0.0, 104.57904761904763, 31, 278, 142.0, 159.0, 167.0, 182.96000000000004, 1.771344703679336, 0.5547412729642188, 1.519274049167468], "isController": false}, {"data": ["get /api/ais/ship-track", 8, 0, 0.0, 2428.0000000000005, 177, 13327, 701.5, 13327.0, 13327.0, 13327.0, 0.026996746891999514, 17.289106237893648, 0.0238067016049566], "isController": false}, {"data": ["get /api/port", 4, 0, 0.0, 85.0, 73, 93, 87.0, 93.0, 93.0, 93.0, 0.019748600317952465, 0.021383066994658004, 0.01660502429077839], "isController": false}, {"data": ["get /api/arrival-departure-schedule/all", 637, 0, 0.0, 46.10832025117742, 20, 99, 53.0, 69.0, 76.0, 88.86000000000001, 2.143460426740426, 0.7933315446627163, 1.8662151666649842], "isController": false}, {"data": ["get /apicall/track-ship", 681, 0, 0.0, 149.50220264317153, 29, 543, 124.0, 275.80000000000007, 310.9, 412.33999999999935, 2.289483504624353, 163.8951481009642, 2.0189094349161363], "isController": false}, {"data": ["post /oauth/login", 8, 0, 0.0, 660.875, 562, 727, 681.0, 727.0, 727.0, 727.0, 10.884353741496598, 7.876275510204081, 5.7397959183673475], "isController": false}, {"data": ["get /api/port/suggestion", 4, 0, 0.0, 100.0, 41, 167, 96.0, 167.0, 167.0, 167.0, 0.019723476854499912, 0.008985353469359579, 0.01672354372941362], "isController": false}, {"data": ["get /api/ais/suggestion", 638, 0, 0.0, 118.37304075235102, 78, 178, 123.0, 144.0, 152.0, 168.83000000000004, 2.1405301670485843, 1.6242108787077638, 1.8186144973947935], "isController": false}, {"data": ["get /apicall/area-ship", 1251, 0, 0.0, 174.5219824140689, 86, 756, 199.0, 234.0, 245.0, 280.44000000000005, 4.192921303123743, 243.03202886718225, 3.680874064678576], "isController": false}, {"data": ["get /apicall/single-ship", 1850, 0, 0.0, 113.41999999999994, 54, 727, 96.0, 163.0, 175.0, 199.47000000000003, 6.212577615243315, 5.081294254121289, 5.325301241801916], "isController": false}, {"data": ["post /api/area-notification", 371, 0, 0.0, 84.38274932614557, 31, 150, 116.0, 130.0, 134.39999999999998, 145.0, 1.2507374631268438, 0.5930125368731564, 1.264772308259587], "isController": false}, {"data": ["post /api/user-define", 901, 0, 0.0, 105.75249722530525, 40, 205, 138.0, 154.0, 161.0, 177.96000000000004, 3.02375375032721, 2.3298258877032896, 3.1684450918956015], "isController": false}, {"data": ["get /api/arrival-notification", 1058, 3, 0.2835538752362949, 89.89886578449912, 24, 668, 85.0, 152.0, 161.0, 181.41000000000008, 3.5686338001565074, 8.676643446725155, 2.9766756272514776], "isController": false}, {"data": ["post /api/arrival-notification", 529, 0, 0.0, 109.33648393194707, 36, 198, 149.0, 169.0, 176.0, 191.0, 1.784479414393901, 0.5595736561588153, 1.8001369615358667], "isController": false}, {"data": ["delete /api/user-define", 525, 0, 0.0, 68.45904761904758, 20, 144, 66.0, 121.0, 125.0, 137.0, 1.7713506801986612, 0.7627186932746032, 1.5049561443094093], "isController": false}, {"data": ["get /api/user-define", 1273, 0, 0.0, 58.57816182246666, 24, 126, 51.0, 89.0, 96.0, 106.0, 4.268331528317513, 14.67878450311491, 3.576394971969166], "isController": false}, {"data": ["get /api/ais/search", 638, 0, 0.0, 187.28213166144212, 134, 307, 182.0, 232.10000000000002, 242.0, 257.22, 2.1402285817798785, 2.6502049235321152, 1.8413490044414775], "isController": false}, {"data": ["get /api/ais/around-ship-track", 4, 0, 0.0, 2461.25, 578, 3484, 2891.5, 3484.0, 3484.0, 3484.0, 0.01477481337563855, 1.0257926398805457, 0.02260950445275938], "isController": false}, {"data": ["get /api/ais/filter", 371, 0, 0.0, 20.083557951482486, 13, 123, 18.0, 26.0, 29.0, 56.47999999999956, 1.2497599854475387, 1.2631851415412134, 1.0276346755340113], "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", 3, 100.0, 0.02322161157984364], "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", 12919, 3, "404/Not Found", 3, "", "", "", "", "", "", "", ""], "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", 1058, 3, "404/Not Found", 3, "", "", "", "", "", "", "", ""], "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);

});
