xref: /web-bugs/www/stats.php (revision 4efb5c34)
1<?php
2
3use App\Repository\BugRepository;
4
5session_start();
6
7// Obtain common includes
8require_once '../include/prepend.php';
9
10// Authenticate
11bugs_authenticate($user, $pw, $logged_in, $user_flags);
12
13response_header('Bugs Stats');
14
15$titles = [
16    'Closed'    => 'Closed',
17    'Open'        => 'Open',
18    'Critical'    => 'Crit',
19    'Verified'    => 'Verified',
20    'Analyzed'    => 'Analyzed',
21    'Assigned'    => 'Assigned',
22    'Feedback'    => 'Fdbk',
23    'No Feedback'    => 'No&nbsp;Fdbk',
24    'Suspended'    => 'Susp',
25    'Not a bug'    => 'Not&nbsp;a&nbsp;bug',
26    'Duplicate'    => 'Dupe',
27    'Wont fix'    => 'Wont&nbsp;Fix',
28];
29
30$rev = isset($_GET['rev']) ? $_GET['rev'] : 1;
31$sort_by = isset($_GET['sort_by']) ? $_GET['sort_by'] : 'Open';
32$total = 0;
33$row = [];
34$pkg = [];
35$pkg_tmp = [];
36$pkg_total = [];
37$pkg_names = [];
38$all = [];
39$pseudo = true;
40
41if (!array_key_exists($sort_by, $titles)) {
42    $sort_by = 'Open';
43}
44
45$bug_type = $_GET['bug_type'] ?? 'All';
46$bugRepository = $container->get(BugRepository::class);
47
48foreach ($bugRepository->findAllByBugType($bug_type) as $row) {
49    $pkg_tmp[$row['status']][$row['package_name']] = $row['quant'];
50    @$pkg_total[$row['package_name']] += $row['quant'];
51    @$all[$row['status']] += $row['quant'];
52    @$total += $row['quant'];
53    $pkg_names[$row['package_name']] = 0;
54}
55
56if (count($pkg_tmp)) {
57    foreach ($titles as $key => $val) {
58        if (isset($pkg_tmp[$key]) && is_array($pkg_tmp[$key])) {
59            $pkg[$key] = array_merge($pkg_names, $pkg_tmp[$key]);
60        } else {
61            $pkg[$key] = $pkg_names;
62        }
63    }
64}
65
66if ($total > 0) {
67    if ($rev == 1) {
68        arsort($pkg[$sort_by]);
69    } else {
70        asort($pkg[$sort_by]);
71    }
72}
73?>
74
75<form method="get" action="stats.php">
76    <table>
77        <tr>
78            <td style="white-space: nowrap">
79                <strong>Bug Type:</strong>
80                <select class="small" id="bug_type" name="bug_type" onchange="this.form.submit(); return false;">
81                    <?php show_type_options($bug_type, /* deprecated */ true, /* all */ true) ?>
82                </select>
83                <input class="small" type="submit" name="submitStats" value="Search">
84            </td>
85        </tr>
86    </table>
87</form>
88
89<table style="width: 100%; margin-top: 1em;" class="stats-table">
90
91<?php // Exit if there are no bugs for this version
92
93if ($total == 0) {
94    echo '<tr><td>No bugs found</td></tr></table>' . "\n";
95    response_footer();
96    exit;
97}
98
99echo display_stat_header($total, true);
100
101echo <<< OUTPUT
102    <tr>
103        <td class="bug_head">All</td>
104        <td class="bug_bg0">{$total}</td>
105OUTPUT;
106
107$i = 1;
108foreach ($titles as $key => $val) {
109    echo '        <td class="bug_bg' , $i++ % 2 , '">';
110    echo bugstats($key, 'all') , "</td>\n";
111}
112echo "    </tr>\n";
113
114$stat_row = 1;
115foreach ($pkg[$sort_by] as $name => $value) {
116    if ($name != 'all') {
117        // Output a new header row every 40 lines
118        if (($stat_row++ % 40) == 0) {
119            echo display_stat_header($total, false);
120        }
121        echo <<< OUTPUT
122    <tr>
123        <td class="bug_head">{$name}</td>
124        <td class="bug_bg0">{$pkg_total[$name]}</td>
125OUTPUT;
126
127        $i = 1;
128        foreach ($titles as $key => $val) {
129            echo '        <td class="bug_bg', $i++ % 2, '">';
130            echo bugstats($key, $name), "</td>\n";
131        }
132        echo "    </tr>\n";
133    }
134}
135
136echo "</table>\n<hr>\n<p><b>PHP Versions for recent bug reports:</b></p>";
137
138echo '<div class="bugstatrecent">';
139
140$last_date = null;
141foreach ($bugRepository->findPhpVersions($bug_type) as $row) {
142    if ($row['d'] != $last_date) {
143        if ($last_date !== null) {
144            echo "</table>\n\n";
145        }
146        echo "<table>\n".
147             "<tr class='bug_header'><th colspan='2'>{$row["d"]}</th></tr>\n";
148        $last_date = $row['d'];
149    }
150    $version = htmlentities($row["formatted_version"], ENT_QUOTES, 'UTF-8');
151    echo "<tr><td class='bug_head'>{$version}</td><td class='bug_bg1'>{$row["quant"]}</td></tr>\n";
152}
153if ($last_date) {
154    echo "</table>\n";
155}
156echo "</div>\n";
157
158response_footer();
159
160// Helper functions
161
162function bugstats($status, $name)
163{
164    global $pkg, $all, $bug_type;
165
166    if ($name == 'all') {
167        if (isset($all[$status])) {
168            return '<a href="search.php?cmd=display&amp;' .
169                   'bug_type='.$bug_type.'&amp;status=' .$status .
170                   '&amp;by=Any&amp;limit=30">' .
171                   $all[$status] . "</a>\n";
172        }
173    } else {
174        if (empty($pkg[$status][$name])) {
175            return '&nbsp;';
176        } else {
177            return '<a href="search.php?cmd=display&amp;'.
178                   'bug_type='.$bug_type.'&amp;status=' .
179                   $status .
180                   '&amp;package_name%5B%5D=' . urlencode($name) .
181                   '&amp;by=Any&amp;limit=30">' .
182                   $pkg[$status][$name] . "</a>\n";
183        }
184    }
185}
186
187function sort_url($name)
188{
189    global $sort_by, $rev, $titles;
190
191    if ($name == $sort_by) {
192        $reve = (int) !$rev;
193    } else {
194        $reve = 1;
195    }
196    if ($sort_by != $name) {
197        $attr = 'class="bug_stats"';
198    } else {
199        $attr = 'class="bug_stats_choosen"';
200    }
201    return '<a href="stats.php?sort_by=' . urlencode($name) .
202           '&amp;rev=' . $reve . '" ' . $attr . '>' .
203           $titles[$name] . '</a>';
204}
205
206function display_stat_header($total, $grandtotal = true)
207{
208    global $titles;
209
210    $stat_head  = " <tr class='bug_header'>\n";
211    if ($grandtotal) {
212        $stat_head .= "  <th>Name</th>\n";
213    } else {
214        $stat_head .= "  <th>&nbsp;</th>\n";
215    }
216    $stat_head .= "  <th>&nbsp;</th>\n";
217
218    foreach ($titles as $key => $val) {
219        $stat_head .= '  <th>' . sort_url($key) . "</th>\n";
220    }
221
222    $stat_head .= '</tr>' . "\n";
223    return $stat_head;
224}
225