{% extends "reports/base_report.html" %}

{% block title %}
Contribution into {{ module }} for the last {{ days }} days
{% endblock %}

{% block scripts %}
    <script type="text/javascript">

        jQuery.extend(jQuery.fn.dataTableExt.oSort, {
            "ratio-pre": function (a) {
                if (a == "&#x221E;")
                    return -1.0;
                a = a.substr(0, a.length - 1);
                return parseFloat(a);
            },

            "ratio-asc": function (a, b) {
                return ((a < b) ? -1 : ((a > b) ? 1 : 0));
            },

            "ratio-desc": function (a, b) {
                return ((a < b) ? 1 : ((a > b) ? -1 : 0));
            }
        });

        $(document).ready(function () {
            var table_column_names = ["index", "link", "metric", "-2", "-1", "1", "2", "A", "positive_ratio", "disagreements", "disagreement_ratio",
                                      "review_ratio", "commit", "email"];
            var table_id = "review_stats_table";

            $.ajax({
                url: makeURI("/api/1.0/stats/engineers_extended?project_type=all&metric=marks&module={{ module }}&release=all&start_date={{ start_date }}"),
                dataType: "json",
                success: function (data) {
                    var tableData = data["stats"];

                    var tableColumns = [];
                    var sort_by_column = 0;
                    for (var i = 0; i < table_column_names.length; i++) {
                        tableColumns.push({"mData": table_column_names[i]});
                        if (table_column_names[i] == "metric") {
                            sort_by_column = i;
                        }
                    }

                    var summary = {
                        'marks': 0,
                        'core_marks': 0,
                        'reviewers': 0,
                        'core_reviewers': 0,
                        'commits': 0,
                        'reviews': 0,
                        'patch': 0,
                        'emails': 0
                    };

                    for (i = 0; i < tableData.length; i++) {
                        if (tableData[i].id) {
                            var user_link = makeURI("/", {user_id: tableData[i].id, metric: "marks"});
                            var company_link = makeURI("/", {company: tableData[i].company, metric: "marks"});
                            tableData[i].link = "<a href=\"" + user_link + "\">" + tableData[i].name + "</a>" +
                                    " <a href=\"" + company_link + "\">" + "(" + tableData[i].company + ")</a>"
                        } else {
                            tableData[i].link = tableData[i].name
                        }

                        if (tableData[i].core == "master") {
                            tableData[i].link += "&nbsp;&#x273B;";
                            summary.core_marks += tableData[i].mark;
                            summary.core_reviewers ++;
                        } else if (tableData[i].core) {
                            tableData[i].link += "&nbsp;&#x272C; <small><i>" + tableData[i].core + "</i></small>";
                        }
                        if (tableData[i].mark > 0) {
                            summary.reviewers ++;
                        }
                        tableData[i].review_ratio = tableData[i].review + " / " + tableData[i].patch;
                        summary.marks += tableData[i].mark;
                        summary.commits += tableData[i].commit;
                        summary.reviews += tableData[i].review;
                        summary.patch += tableData[i].patch;
                        summary.emails += tableData[i].email;
                    }

                    if (table_id) {
                        $("#" + table_id).dataTable({
                            "aaSorting": [
                                [ sort_by_column, "desc" ],
                                [ 0, "asc"]
                            ],
                            "bFilter": true,
                            "bInfo": false,
                            "bAutoWidth": false,
                            "bPaginate": false,
                            "iDisplayLength": -1,
                            "aaData": tableData,
                            "aoColumns": tableColumns,
                            "aoColumnDefs": [
                                { "sClass": "center", "aTargets": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] },
                                { "sType": "ratio", "aTargets": [8, 10]}
                            ]
                        });
                    }
                    $("#review_summary_template").tmpl(summary).appendTo("#review_summary_container");
                }
            });
        });
    </script>

    <style type="text/css">
        table.dataTable tr.even {
            background-color: #EEF1F4;
        }

        table.dataTable tr.even:hover, table.dataTable tr.odd:hover {
            background-color: #F8FFEC;
        }

        table.dataTable tr.even td.sorting_1 {
            background-color: #E0E8E8;
        }
    </style>

<script id="review_summary_template" type="text/x-jquery-tmpl">
<h2>Review Summary</h2>
<div>Total reviews: <b>${marks}</b> (${(marks / {{ days }}).toFixed(1) } per day)</div>
<div>Total reviewers: <b>${reviewers}</b> (${(marks / reviewers / {{ days }}).toFixed(1) } per reviewer per day)</div>
<div>Total reviews by core team: <b>${core_marks}</b> (${(core_marks / {{ days }}).toFixed(1) } per day)</div>
<div>Core team size: <b>${core_reviewers}</b> (${(core_marks / core_reviewers / {{ days }}).toFixed(1) } per core per day)</div>
<h2>Contribution Summary</h2>
<div>On review: <b>${reviews}</b> (${(reviews / {{ days }}).toFixed(1) } per day)</div>
<div>Patch sets: <b>${patch}</b> (${(patch / {{ days }}).toFixed(1) } per day)</div>
<div>Commits: <b>${commits}</b> (${(commits / {{ days }}).toFixed(1) } per day)</div>
<div>Emails: <b>${emails}</b> (${(emails / {{ days }}).toFixed(1) } per day)</div>
</script>

{% endblock %}

{% block content %}
<h1>Contribution into {{ module }} for the last {{ days }} days</h1>

    <table id="review_stats_table">
        <thead>
            <tr>
                <th>#</th>
                <th>Engineer</th>
                <th>Reviews</th>
                <th>-2</th>
                <th>-1</th>
                <th>+1</th>
                <th>+2</th>
                <th>A</th>
                <th>+ %</th>
                <th>Disagreements</th>
                <th>Ratio</th>
                <th>On review / patch sets</th>
                <th>Commits</th>
                <th>Emails</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    
    <div id="review_summary_container"></div>

{% endblock %}
