/*
   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.029, 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 - 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.0, 500, 1500, "[TPS - Nasional] Fetch Data By ID"], "isController": false}, {"data": [0.29, 500, 1500, "Login"], "isController": false}, {"data": [0.0, 500, 1500, "[TPS - Provinsi] Fetch Data By ID"], "isController": false}, {"data": [0.0, 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", 1000, 0, 0.0, 16843.015000000003, 388, 47107, 18833.0, 22014.6, 23344.199999999997, 28815.860000000004, 5.168706582864704, 2.6868289565156713, 2.3304607708092124], "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", 100, 0, 0.0, 19558.400000000005, 16344, 28106, 19213.0, 22612.4, 23100.5, 28072.99999999998, 2.0673544065659177, 1.0581664698372992, 0.9589778741394637], "isController": false}, {"data": ["[TPS - Nasional] GET Data National", 100, 0, 0.0, 18429.169999999995, 4925, 47107, 18427.5, 22729.0, 29296.249999999956, 46947.73999999992, 1.808678037222594, 0.905045533469587, 0.8425189685109153], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID", 100, 0, 0.0, 12455.85, 5690, 15686, 14000.0, 14487.0, 14666.95, 15676.669999999995, 5.030434126465114, 3.514622843201368, 2.456266663313044], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID Kelurahan", 100, 0, 0.0, 15029.949999999999, 12788, 17531, 14883.0, 16450.2, 16728.0, 17526.64, 3.210891343436938, 1.6421012574653224, 1.5678180387875675], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID Kecamatan", 100, 0, 0.0, 18443.5, 14692, 20560, 19224.5, 20328.8, 20355.2, 20559.37, 2.702629658657874, 1.3605523330450529, 1.2906112334801763], "isController": false}, {"data": ["[TPS - Nasional] Fetch Data By ID", 100, 0, 0.0, 20563.359999999997, 16588, 30390, 20132.5, 24464.4, 27138.89999999997, 30384.299999999996, 1.8654280224597535, 0.9962551532449121, 0.8689542643684594], "isController": false}, {"data": ["Login", 100, 0, 0.0, 2020.6800000000005, 388, 5553, 1415.0, 4508.0, 5173.95, 5552.82, 14.019346698443853, 6.174536485349782, 3.641744357212954], "isController": false}, {"data": ["[TPS - Provinsi] Fetch Data By ID", 100, 0, 0.0, 20865.940000000002, 17137, 39094, 20534.0, 22764.500000000004, 23748.949999999993, 38961.05999999993, 2.038154247513452, 0.9862357711356595, 0.9434424934779064], "isController": false}, {"data": ["[TPS - Nasional] GET One Data By ID", 100, 0, 0.0, 21622.26000000001, 17511, 28819, 21344.5, 24212.700000000004, 24766.749999999996, 28806.539999999994, 1.9270421829533848, 0.9809849112597074, 0.8995372689958183], "isController": false}, {"data": ["[TPS - Provinsi] GET One Data By ID Kabupaten", 100, 0, 0.0, 19441.03999999999, 17475, 21793, 19529.0, 20475.0, 20475.0, 21792.77, 2.490349894160129, 1.2578212551363466, 1.1697834952060764], "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", 1000, 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}]}, function(index, item){
        return item;
    }, [[0, 0]], 0);

});
