/*
   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": 100.0, "KoPercent": 0.0};
    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.05779220779220779, 500, 1500, "Total"], "isController": false}, "titles": ["Apdex", "T (Toleration threshold)", "F (Frustration threshold)", "Label"], "items": [{"data": [0.0, 500, 1500, "[TPS - Provinsi] GET One Data By ID Provinsi"], "isController": false}, {"data": [0.0, 500, 1500, "[TPS] Add Data Dinamix Driven Value"], "isController": false}, {"data": [0.05714285714285714, 500, 1500, "[TPS - Nasional] GET Data National"], "isController": false}, {"data": [0.0, 500, 1500, "[TPS - Provinsi] GET One Data By ID"], "isController": false}, {"data": [0.0, 500, 1500, "[TPS - Provinsi] GET One Data By ID Kelurahan"], "isController": false}, {"data": [0.0, 500, 1500, "[TPS - Provinsi] GET One Data By ID Kecamatan"], "isController": false}, {"data": [0.06428571428571428, 500, 1500, "[TPS - Nasional] Fetch Data By ID"], "isController": false}, {"data": [0.34285714285714286, 500, 1500, "Login"], "isController": false}, {"data": [0.07857142857142857, 500, 1500, "[TPS - Provinsi] Fetch Data By ID"], "isController": false}, {"data": [0.09285714285714286, 500, 1500, "[TPS - Nasional] GET One Data By ID"], "isController": false}, {"data": [0.0, 500, 1500, "[TPS - Provinsi] GET One Data By ID Kabupaten"], "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", 770, 0, 0.0, 8173.938961038946, 128, 15347, 9477.5, 11258.0, 12079.45, 14234.029999999999, 7.92474579062204, 4.157194923016755, 3.94725670001235], "isController": false}, "titles": ["Label", "#Samples", "FAIL", "Error %", "Average", "Min", "Max", "Median", "90th pct", "95th pct", "99th pct", "Transactions/s", "Received", "Sent"], "items": [{"data": ["[TPS - Provinsi] GET One Data By ID Provinsi", 70, 0, 0.0, 10636.485714285714, 8804, 12477, 10684.0, 11349.9, 11355.0, 12477.0, 3.401195277197415, 1.78363463267091, 1.577702887371848], "isController": false}, {"data": ["[TPS] Add Data Dinamix Driven Value", 70, 0, 0.0, 11807.171428571428, 9210, 15347, 11405.5, 14238.3, 14526.250000000002, 15347.0, 3.8305789646492285, 1.4286648722228301, 3.7165380116559046], "isController": false}, {"data": ["[TPS - Nasional] GET Data National", 70, 0, 0.0, 5732.9571428571435, 582, 9010, 6445.5, 7824.2, 8149.650000000001, 9010.0, 1.9747235387045812, 1.0452149980252765, 0.919866335900474], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID", 70, 0, 0.0, 10647.171428571432, 8599, 13206, 10084.5, 12078.6, 12187.1, 13206.0, 3.840877914951989, 2.7081189986282577, 1.8754286694101507], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID Kelurahan", 70, 0, 0.0, 9374.028571428567, 7410, 10445, 9418.5, 9710.0, 9726.0, 10445.0, 3.617197188921042, 1.854520043147995, 1.7662095649028524], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID Kecamatan", 70, 0, 0.0, 9805.957142857147, 8620, 10550, 9829.0, 10091.6, 10227.800000000001, 10550.0, 3.6572622779519333, 1.8893474072622778, 1.7464855995297806], "isController": false}, {"data": ["[TPS - Nasional] Fetch Data By ID", 70, 0, 0.0, 6863.0, 541, 8882, 7719.0, 8107.9, 8341.45, 8882.0, 2.1900322247598782, 1.2318931264274318, 1.020161495322717], "isController": false}, {"data": ["Login", 70, 0, 0.0, 1495.6857142857145, 128, 2623, 1923.5, 2449.1, 2552.05, 2623.0, 21.691973969631235, 9.55378931670282, 5.634829175704989], "isController": false}, {"data": ["[TPS - Provinsi] Fetch Data By ID", 70, 0, 0.0, 6345.0714285714275, 407, 10213, 7924.0, 9760.8, 9909.8, 10213.0, 3.511412089290193, 1.9511655066466016, 1.6253997366440933], "isController": false}, {"data": ["[TPS - Nasional] GET One Data By ID", 70, 0, 0.0, 7239.628571428571, 488, 9811, 8107.0, 9656.5, 9810.45, 9811.0, 2.8280542986425337, 1.49688030260181, 1.3201269089366516], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID Kabupaten", 70, 0, 0.0, 9966.171428571426, 9246, 10740, 9989.5, 10214.9, 10229.5, 10740.0, 3.523785552479235, 1.8375991064686636, 1.6552156745532345], "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": []}, 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", 770, 0, "", "", "", "", "", "", "", "", "", ""], "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}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
