summaryrefslogtreecommitdiffstats
path: root/lib.php
blob: 5d1cfded7536d083ba25f4bb111a572ca738f291 (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
<?php
/**
 * Mageia build-system quick status report script.
 *
 * @copyright Copyright (C) 2011 Mageia.Org
 *
 * @author Olivier Blin
 * @author Pascal Terjan
 * @author Romain d'Alverny
 * @author Michael Scherer
 *
 * @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.
 *
*/

/**
 * Return a human-readable label for this package build status.
 *
 * @param array $pkg package information
 *
 * @return string
*/
function pkg_gettype($pkg)
{
    $labels = array(
        'rejected' => 'rejected',
        'upload'   => 'uploaded',
        'failure'  => 'failure',
        'done'     => 'partial',
        'build'    => 'building',
        'todo'     => 'todo'
    );

    foreach ($labels as $k => $v) {
        if (array_key_exists($k, $pkg['status'])) {
            return $v;
        }
    }

    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();
}

/**
 * Return human-readable time difference
 *
 * @param integer $start timestamp
 * @param integer $end timestamp, defaults to now
 *
 * @return string
*/
function timediff($start, $end)
{
    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);
}


/**
 * Compare two duration strings
 *
 * @param string $a "1 hour" or "23 mins"
 * @param string $b
 *
 * @return integer
*/
function timesort($a, $b)
{
    $a = explode(' ', trim($a));
    $b = explode(' ', trim($b));

    if ($a[1] == 'hour' || $a[1] == 'hours') {
        $a[0] *= 3600;
    }

    if ($b[1] == 'hour' || $a[1] == 'hours') {
        $b[0] *= 3600;
    }

    if ($a[0] > $b[0]) {
        return 1;
    } elseif ($a[0] < $b[0]) {
        return -1;
    } else {
        return 0;
    }
}


/**
 * Publish BS stats as HTTP headers for remote services to adapt.
 *
 * @param array $stats
 * @param integer $buildtime_total
 * @param integer $build_count
 * @param array $last_package
 *
 * @return void
*/
function publish_stats_headers($stats, $buildtime_total, $build_count, $last_package = null)
{
    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");

    if (!is_null($last_package)) {
        header("X-BS-Package-Status: ".$last_package['type']);
    }

    $buildtime_total = $buildtime_total / 60;
    header(sprintf('X-BS-Buildtime: %d', round($buildtime_total)));


    $buildtime_avg = ($build_count == 0) ?
        0 :
        round($buildtime_total / $build_count, 2);
    header(sprintf('X-BS-Buildtime-Average: %5.2f', $buildtime_avg));
}

/**
 * check if emi is running
 *
 * @return
*/
function get_upload_time()
{
    if (file_exists('/var/lib/schedbot/tmp/upload')) {
        $stat = stat('/var/lib/schedbot/tmp/upload');
        if ($stat) {
            return $stat['mtime'];
        }
    }

    return null;
}

/**
 * Build and return stats about all packages.
 *
 * @todo should not alter/return $pkgs
 *
 * @param array $pkgs
 *
 * @return array (array, array, integer, array)
*/
function build_stats($pkgs)
{
    // count all packages statuses
    $stats = array(
        'todo'     => 0,
        'building' => 0,
        'partial'  => 0,
        'uploaded' => 0,
        'rejected' => 0,
        'failure'  => 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;
            }
        }
    }

    return array(
        $stats,
        $users,
        $total,
        $pkgs
    );
}


class mga_bs_charts
{

    public static function js_init()
    {
        return <<<S
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

  // Load the Visualization API and the piechart package.
  google.load('visualization', '1.0', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.setOnLoadCallback(drawChart);
</script>
S;
    }

    public static function js_draw_charts()
    {
        return <<<S
function drawChart() {
    draw_status_chart();
    draw_buildtime_chart();
    draw_buildschedule_chart();
}
S;
    }

    public static function js_draw_status_chart($stats, $id)
    {
        $rows = array();
        foreach ($stats as $status => $count) {
            $rows[] = sprintf("['%s', %d]", $status, $count);
        }
        $rows = implode(', ', $rows);
        $s = <<<S
function draw_status_chart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Status');
    data.addColumn('number', 'Packages');
    data.addRows([{$rows}]);

    var options = {'title':'Packages status',
                   'width':300,
                   'height':300};

    var chart = new google.visualization.PieChart(document.getElementById('{$id}'));
    chart.draw(data, options);
}
S;
        return $s;
    }

    public static function js_draw_buildtime_chart($data, $id)
    {
        $d = print_r($data, true);
        $rows  = array("['Duration', 'Builds']", '[1, 3]');
        foreach ($data as $duration => $count) {
            $rows[] = sprintf("['%s', %d]", $duration, $count);
        }
        $rows = implode(', ', $rows);
        return <<<S
function draw_buildtime_chart() {
    /*
    {$d}
    */
    var data = google.visualization.arrayToDataTable([{$rows}]);
    var options = {
        title: 'Builds duration',
        hAxis: {title: ''},
        'width':600,
        'height':300
    };
    var chart = new google.visualization.ColumnChart(document.getElementById('{$id}'));
    chart.draw(data, options);
}
S;
    }

    public static function js_draw_buildschedule_chart($data, $id)
    {
        $d = print_r($data, true);
        $rows = array("['Hour', 'Builds']", '[1, 3]');
        foreach ($data as $hour => $count) {
            $rows[] = sprintf("['%s', %d]", $hour, $count);
        }
        $rows = implode(', ', $rows);
        return <<<S
function draw_buildschedule_chart() {
    /*
    {$d}
    */
    var data = google.visualization.arrayToDataTable([{$rows}]);
    var options = {
        title: 'Builds layout',
        hAxis: {title: ''},
       'width':600,
       'height':300
    };
    var chart = new google.visualization.ColumnChart(document.getElementById('{$id}'));
    chart.draw(data, options);
}
S;
    }

}