summaryrefslogtreecommitdiffstats
path: root/index.php
blob: 321b4772a19d8461c0ad2c32561ba82065931a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
 * Mageia build-system quick status report script.
 *
 * @copyright Copyright (C) 2011 Mageia.Org
 *
 * @author Olivier Blin
 * @author Pascal Terjan
 * @author Romain d'Alverny
 *
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License aspublished by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 *
 * Shows submitted packages in the past $max_modified 24 hours and their
 * status (built & uploaded, failed build, rejected, etc.).
 *
 * This was written anew in Jan. 2011 because existing Mandriva build-system
 * web report code was not clearly licensed at this very time.
*/

error_reporting(E_ALL);

/**
 * @param array $pkg
 *
 * @return string
*/
function pkg_gettype($pkg) {
    if (array_key_exists("rejected", $pkg["status"]))
        return "rejected";
    if (array_key_exists("youri", $pkg["status"])) {
        if (array_key_exists("src", $pkg["status"]))
            return "youri";
        else
            return "uploaded";
    }
    if (array_key_exists("failure", $pkg["status"]))
        return "failure";
    if (array_key_exists("done", $pkg["status"]))
        return "partial";
    if (array_key_exists("build", $pkg["status"]))
        return "building";
    if (array_key_exists("todo", $pkg["status"]))
        return "todo";
    return "unknown";
}

/**
 * @param integer $num
 *
 * @return string
*/
function plural($num) {
    if ($num > 1)
        return "s";
}

/**
 * Return timestamp from package key
 * @param string $key package submission key
 *
 * @return integer
*/

function key2timestamp($key) {
    global $tz;

    $date = DateTime::createFromFormat("YmdHis", $key+0, $tz);
    if ($date <= 0)
        return null;

    return $date->getTimestamp();
}

function timediff($start, $end) {
/**
 * Return human-readable time difference
 *
 * @param integer $start timestamp
 * @param integer $end timestamp, defaults to now
 *
 * @return string
*/
    if (is_null($end)) {
	$end = time();
    }
    $diff = $end - $start;
    if ($diff<60)
       return $diff . " second" . plural($diff);
    $diff = round($diff/60);
    if ($diff<60)
       return $diff . " minute" . plural($diff);
    $diff = round($diff/60);
    if ($diff<24)
       return $diff . " hour" . plural($diff);
    $diff = round($diff/24);

    return $diff . " day" . plural($diff);
}

$g_user = isset($_GET['user']) ? htmlentities(strip_tags($_GET['user'])) : null;

$upload_dir = '/home/schedbot/uploads';
$max_modified = 2;
$title = '<a href="http://mageia.org/">Mageia</a> build system status';
$robots = 'index,nofollow,nosnippet,noarchive';
if ($g_user) {
    $title .= ' for ' . $g_user . "'s packages";
    $robots = 'no' . $robots;
}
$tz = new DateTimeZone('UTC');
$date_gen = date('c');

# Temporary until initial mirror is ready
chdir("data");
$missing_deps = file("missing-deps.i586.txt");
#########################################

chdir($upload_dir);

$all_files = shell_exec("find \( -name '*.rpm' -o -name '*.src.rpm.info' -o -name '*.youri' -o -name '*.lock' -o -name '*.done' \) -ctime -$max_modified -printf \"%p\t%T@\\n\"");
$re = "!^\./(\w+)/((\w+)/(\w+)/(\w+)/(\d+)\.(\w+)\.(\w+)\.(\d+))_?(.*)(\.src\.rpm(?:\.info)?|\.youri|\.lock|\.done)\s+(\d+\.\d+)$!m";
$r = preg_match_all($re,
    $all_files,
    $matches,
    PREG_SET_ORDER);

$pkgs = array();
foreach ($matches as $val) {

    if ($_GET['user'] && ($_GET['user'] != $val[7])) {
        continue;
    }
    $key = $val[6] . $val[7];
    if (!is_array($pkgs[$key])) {

        $pkgs[$key] = array(
            'status'  => array(),
            'path'    => $val[2],
            'version' => $val[3],
            'media'   => $val[4],
            'section' => $val[5],
            'user'    => $val[7],
            'host'    => $val[8],
            'job'     => $val[9]
        );
    }
    $status = $val[1];
    $data = $val[10];
    if (preg_match("/@(\d+):/", $data, $revision)) {
        $pkgs[$key]['revision'] = $revision[1];
    }
    $pkgs[$key]['status'][$status] = 1;
    $ext = $val[11];
    if ($ext == '.src.rpm.info') {
        preg_match("!^(?:@\d+:)?(.*)!", $data, $name);
        $pkgs[$key]['package'] = $name[1];
    } else if ($ext == '.src') {
        $pkgs[$key]['status']['src'] = 1;
    } else if ($ext == '.youri') {
        $pkgs[$key]['status']['youri'] = 1;
    } else if ($ext == '.lock') {
        // parse build bot from $data
        $pkgs[$key]['status']['build'] = 1;
    } else if ($ext == '.done') {
        $pkgs[$key]['buildtime']['start'] = key2timestamp($val[6]);
        $pkgs[$key]['buildtime']['end'] = round($val[12]);
        $pkgs[$key]['buildtime']['diff'] = $pkgs[$key]['buildtime']['end'] - $pkgs[$key]['buildtime']['start'];
    }
}
// sort by key in reverse order to have more recent pkgs first
krsort($pkgs);

// count all packages statuses
$stats = array(
    'uploaded' => 0,
    'failure'  => 0,
    'todo'     => 0,
    'building' => 0,
    'partial'  => 0,
    'built'    => 0,
);
$total = count($pkgs);

// count users' packages
$users = array();

if ($total > 0) {
    foreach ($pkgs as $key => $p) {
        $pkgs[$key]['type'] = pkg_gettype($p);

        $stats[$pkgs[$key]['type']] += 1;

        if (!array_key_exists($p['user'], $users))
            $users[$p['user']] = 1;
        else
            $users[$p['user']] += 1;
    }
}

// feedback labels
$badges = array(
    'uploaded' => 'Congrats %s! \o/',
    'failure'  => 'Booooo! /o\\',
    'todo'     => '',
    'building' => '',
    'partial'  => '',
    'built'    => ''
);

foreach ($stats as $k => $v) {
    Header("X-BS-Queue-$k: $v");
}

$w = $stats['todo'] - 10;
if($w < 0)
    $w = 0;
$w = $w * 60;
Header("X-BS-Throttle: $w");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?php echo strip_tags($title); ?></title>
    <meta name="robots" content="<?php echo $robots; ?>">
    <link rel="shortcut icon" type="image/ico" href="https://bugs.mageia.org/images/favicon.ico" />
    <style type="text/css">
    .clear { clear: both; }
    table { 
        border-spacing: 0;
        font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 80%;
        border: 1px solid #ccc;
        float: left;
    }
    table tr { padding: 0; margin: 0; }
    table th { padding: 0.2em 0.5em; margin: 0; border-bottom: 2px solid #ccc; border-right: 1px solid #ccc; }
    table td { padding: 0; margin: 0; padding: 0.2em 0.5em; border-bottom: 1px solid #ccc; }
 
    tr { background: transparent; }
    tr.uploaded { background: #bbffbb; }
    tr.failure, tr.rejected { background: #ffbbbb; }
    tr.todo { background: white; }
    tr.building { background: #ffff99; }
    tr.partial { background: #bbbbff; }
    tr.built { background: #cceeff; }
    tr.youri { background: #aacc66; }

    td.status-box { width: 1em; height: 1em; }
    tr.uploaded td.status-box { background: green; }
    tr.failure td.status-box, tr.rejected td.status-box { background: red; }
    tr.todo td.status-box { background: white; }
    tr.building td.status-box { background: yellow; }
    tr.partial td.status-box { background: blue; }
    tr.built td.status-box { background: #00ccff; }
    tr.youri td.status-box { background: olive; }
    
    #stats { float: right; }
    #score { margin-bottom: 2em; font-family: Helvetica, Verdana, Arial, sans-serif; }
    #score-box { width: 100px; height: 100px; background: #faa; }
    #score-meter { width: 100px; background: #afa; }
    </style>
</head>
<body>
    <h1><?php echo $title ?></h1>

<?php
if (!is_null($g_user))
    echo '<a href="/">&laquo;&nbsp;Back to full list</a>';

# Temporary until initial mirror is ready
echo sprintf(
    '<p><a href="%s">%d broken dependencies</a>. <strong><a href="%s">You can help!</a></strong></p>',
    'data/missing-deps.i586.txt', count($missing_deps),
    'http://www.mageia.org/wiki/doku.php?id=packaging#starting_package_import'
);

#########################################

$s = '';
$tmpl = <<<T
<tr class="%s">
    <td>%s</td>
    <td><a href="?user=%s">%s</a></td>
    <td><a href="http://svnweb.mageia.org/packages?view=revision&revision=%d">%s</a></td>
    <td>%s</td>
    <td>%s/%s</td>
    <td class="status-box"></td>
T;

if ($total > 0) {
    foreach ($pkgs as $key => $p) {
        $s .= sprintf($tmpl,
            $p['type'],
            timediff(key2timestamp($key)) . ' ago',
            $p['user'], $p['user'],
            $p['revision'],
            $p['package'],
            $p['version'],
            $p['media'], $p['section']
        );
    
        $typelink = '';
        if ($p['type'] == 'failure') {
           $typelink = '/uploads/' . $p['type'] . '/' . $p['path'];
        } elseif ($p['type'] == 'rejected') {
           $typelink = '/uploads/' . $p['type'] . '/' . $p['path'] . '.youri';
        }

        $s .= '<td>';
        $s .= ($typelink != '') ?
            sprintf('<a href="%s">%s</a>', $typelink, $p['type']) :
            $p['type'];

        $s .= '</td><td>';
        if ($p['type'] == 'uploaded')
            $s .= timediff($p['buildtime']['start'], $p['buildtime']['end']);
        $s .= '</td>';
        //$s .= '<td>' . sprintf($badges[$p['type']], $p['user']) . '</td>';
        $s .= '</tr>';
    }
    // Table
    echo '<table>',
        '<caption>', $total, ' packages submitted in the past ', $max_modified * 24, '&nbsp;hours.</caption>',
        '<tr><th>Submitted</th><th>User</th>
            <th>Package</th><th>Target</th><th>Media</th>
            <th colspan="2">Status</th><th>Build time</th></tr>',
        $s,
        '</table>';

    // Stats
    $s = '<div id="stats">';
    $score = round($stats['uploaded']/$total * 100);
    $s .= sprintf('<div id="score"><h3>Score: %d/100</h3>
        <div id="score-box"><div id="score-meter" style="height: %dpx;"></div></div></div>',
        $score, $score);

    $s .= '<table><caption>Stats.</caption><tr><th colspan="2">Status</th><th>Count</th><th>%</th></tr>';
    foreach ($stats as $k => $v) {
        $s .= sprintf('<tr class="%s"><td class="status-box"></td><td>%s</td><td>%d</td><td>%d%%</td></tr>',
            $k, $k, $v, round($v/$total*100));
    }

    $s .= '</table><br /><br />';

    $s .= '<table><caption>Packagers</caption><tr><th>User</th><th>Packages</th></tr>';
    foreach ($users as $k => $v)
        $s .= sprintf('<tr><td><a href="/?user=%s">%s</a></td><td>%d</td></tr>',
            $k, $k, $v);

    $s .= '</table>';
    $s .= '</div>';

    echo $s;
}
else
{
    echo sprintf('<p>No package has been submitted in the past %d&nbsp;hours.</p>',
        $max_modified * 24);
}

?>
    <div class="clear"></div>
    <hr />
    <p>Generated at <?php echo $date_gen; ?>.</p>
</body>
</html>