SqlQueryString   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 92
dl 0
loc 140
rs 9.76
c 0
b 0
f 0
wmc 33

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addeq() 0 9 3
A __construct() 0 9 2
A addgt() 0 9 4
A addeq_not_CHOOSE_ALL() 0 12 4
A get_url() 0 3 1
A addsort() 0 18 5
A count() 0 8 2
A addclause() 0 6 2
A get_select_query() 0 9 3
A process_form_items() 0 24 5
A add() 0 6 2
1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2017 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
// functions to query various entities, and display them,
20
// in admin web pages
21
// TODO: most of this code shouldn't exist; use the standard code instead
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
22
23
require_once("../inc/util_basic.inc");
24
require_once("../inc/util_ops.inc");
25
require_once("../inc/result.inc");
26
require_once("../inc/boinc_db.inc");
27
28
class BoincAssignment {
29
    static function enum($where_clause = null) {
30
        $db = BoincDb::get();
31
        return $db->enum('assignment', 'BoincAssignment', $where_clause);
32
    }
33
}
34
35
define("NVALIDATE_STATES", 6);
36
37
// Converts a mysql-Timestamp to a user readable format
38
// @return String A user readable DateTime-String in UTC
39
// @param Integer $x The mysql-Timestamp to convert
40
function mysqltime_str($x) {
41
    if(strpos($x,"-")==4) {
42
        // Syntax of supplied mysql-timestamp is YYYY-MM-DD HH:MM:SS
43
        $year = substr($x,0,4);
44
        $month = substr($x,5,2);
45
        $day = substr($x,8,2);
46
        $hour = substr($x,11,2);
47
        $minute = substr($x,14,2);
48
        $second = substr($x,17,2);
49
    } else {
50
        // Syntax of supplied mysql-timestamp is YYYYMMDDHHMMSS
51
        $year = substr($x,0,4);
52
        $month = substr($x,4,2);
53
        $day = substr($x,6,2);
54
        $hour = substr($x,8,2);
55
        $minute = substr($x,10,2);
56
        $second = substr($x,12,2);
57
58
    }
59
    //make a Unix-Timestamp
60
    // echo "Time string is " . "$x";
61
    $time = mktime($hour,$minute,$second,$month,$day,$year);
0 ignored issues
show
Bug introduced by
$hour of type string is incompatible with the type integer expected by parameter $hour of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
    $time = mktime(/** @scrutinizer ignore-type */ $hour,$minute,$second,$month,$day,$year);
Loading history...
Bug introduced by
$second of type string is incompatible with the type integer expected by parameter $second of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
    $time = mktime($hour,$minute,/** @scrutinizer ignore-type */ $second,$month,$day,$year);
Loading history...
Bug introduced by
$month of type string is incompatible with the type integer expected by parameter $month of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
    $time = mktime($hour,$minute,$second,/** @scrutinizer ignore-type */ $month,$day,$year);
Loading history...
Bug introduced by
$year of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
    $time = mktime($hour,$minute,$second,$month,$day,/** @scrutinizer ignore-type */ $year);
Loading history...
Bug introduced by
$day of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
    $time = mktime($hour,$minute,$second,$month,/** @scrutinizer ignore-type */ $day,$year);
Loading history...
Bug introduced by
$minute of type string is incompatible with the type integer expected by parameter $minute of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
    $time = mktime($hour,/** @scrutinizer ignore-type */ $minute,$second,$month,$day,$year);
Loading history...
62
    return time_str($time);
63
}
64
65
// translate type_id into humand readable text
66
// http://php.net/manual/de/mysqli-result.fetch-field-direct.php
67
//
68
function mysql_fieldtype_str($type_id) {
69
    static $types;
70
    // maps php constants to well known mysql definitions
71
    static $mysql_data_type_hash = array(
72
        MYSQLI_TYPE_TINY=>'tinyint',
73
        MYSQLI_TYPE_SHORT=>'smallint',
74
        MYSQLI_TYPE_LONG=>'int',
75
        MYSQLI_TYPE_FLOAT=>'float',
76
        MYSQLI_TYPE_DOUBLE=>'double',
77
        MYSQLI_TYPE_TIMESTAMP=>'timestamp',
78
        MYSQLI_TYPE_LONGLONG=>'bigint',
79
        MYSQLI_TYPE_INT24=>'mediumint',
80
        MYSQLI_TYPE_DATE=>'date',
81
        MYSQLI_TYPE_TIME=>'time',
82
        MYSQLI_TYPE_DATETIME=>'datetime',
83
        MYSQLI_TYPE_YEAR=>'year',
84
        MYSQLI_TYPE_BIT=>'bit',
85
        MYSQLI_TYPE_BLOB=>'blob',
86
        MYSQLI_TYPE_VAR_STRING=>'varchar',
87
        MYSQLI_TYPE_STRING=>'char',
88
        MYSQLI_TYPE_DECIMAL=>'decimal'
89
    );
90
    if (array_key_exists($type_id, $mysql_data_type_hash)) {
91
        return $mysql_data_type_hash[$type_id];
92
    }
93
    // $type_id is not well known or new so get the constant name and return that
94
    if (!isset($types)) {
95
        $types = array();
96
        $constants = get_defined_constants(true);
97
        foreach ($constants['mysqli'] as $c => $n) if (preg_match('/^MYSQLI_TYPE_(.*)/', $c, $m)) $types[$n] = $m[1];
98
    }
99
    return array_key_exists($type_id, $types)? strtolower($types[$type_id]) : "unknown";
100
}
101
102
// Function prints a description of $table
103
//
104
function print_describe_table_onecol($table, $which, $columns) {
105
    $db = BoincDb::get(true);
106
    $result = $db->do_query("SELECT * from $table LIMIT 1");
107
    $fields = $result->field_count;
108
109
    $avgnum=(int)($fields/$columns);
110
    if ($avgnum*$columns<$fields) {
111
        $avgnum++;
112
    }
113
114
    $actualcolumns=0;
115
    while ($avgnum*$actualcolumns<$fields) {
116
        $actualcolumns++;
117
    }
118
119
    if ($which>$actualcolumns) {
120
        return 0;
121
    }
122
123
    $bot=($which-1)*$avgnum;
124
    $top=$which*$avgnum;
125
126
    $width=100.0/$actualcolumns;
127
128
    echo "<td>";
129
    start_table('table-striped');
130
    echo "<tr><th align=\"left\">NAME</th><th align=\"left\">Type</th><th align=\"left\">Bytes</th>\n";
131
    for ($count=$bot; $count<$top; $count++) {
132
        if ($count<$fields) {
133
            $x = $result->fetch_field_direct($count);
134
            $name = $x->name;
135
            $type = mysql_fieldtype_str($x->type);
136
            $length = $x->length;
137
        } else {
138
            $name="<br/> ";
139
            $type="<br/>";
140
            $length="<br/>";
141
        }
142
        echo "\t<tr><td><b>$name</b></td><td>$type</td><td>$length</td></tr>\n";
143
    }
144
    end_table();
145
    echo "</td>";
146
    $result->free();
147
    return 0;
148
}
149
150
function print_describe_table($table, $how_many_columns) {
151
    // Number of columns for showing table description
152
    echo "<h2>Description of <b>$table</b> table fields:</h2>\n";
153
    start_table();
154
    echo "<tr>";
155
    for ($i=1; $i<=$how_many_columns; $i++) {
156
        print_describe_table_onecol($table, $i, $how_many_columns);
157
    }
158
    echo "</tr>";
159
    end_table();
160
    return 0;
161
}
162
163
function print_detail_field() {
164
    echo "<tr><td align=\"right\">Detail level</td><td>";
165
    echo "<select name=\"detail\">
166
        <option value=\"low\">low
167
        <option value=\"high\">high
168
        </select>
169
        </td></tr>
170
    ";
171
}
172
173
function print_query_field() {
174
    $currenttime=time();
175
    $hourago=$currenttime-3600;
176
    $dayago=$currenttime-24*3600;
177
    $weekago=$currenttime-7*24*3600;
178
    echo "
179
        <tr>
180
        <td align=\"right\">Additional clauses</td>
181
        <td><input name=\"clauses\" size=\"100\"></td>
182
        </tr><tr>
183
        <td align=\"right\">Unix times</td>
184
        <td>Now:<b> $currenttime</b> Hour ago: $hourago Day ago: $dayago Week ago: $weekago</td>
185
        </tr>
186
    ";
187
}
188
189
function join_query_string($s1, $s2) {
190
    if ($s1) {
191
        if ($s2) {
192
            return "$s1&s2";
193
        } else {
194
            return $s1;
195
        }
196
    } else {
197
        return $s2;
198
    }
199
}
200
201
function append_sql_query($original,$addition,$first) {
202
    if ($first == 1) {
203
        return $original . " where " . $addition;
204
    } else {
205
        return $original . " and " . $addition;
206
    }
207
}
208
209
// SqlQueryString maps a bunch of form items to a SQL query
210
//
211
// The items are
212
// table        (name of table)
213
// id
214
// platformid
215
// appid
216
// workunitid
217
// hostid
218
// userid
219
// teamid
220
// nsecs            (modified_time > now - nsecs)
221
// received_time    (> x)
222
// server_state     (== x if z nonzero)
223
// outcome          (== x if z nonzero)
224
// client_state     (== x if z nonzero)
225
// exit_status      (== x if z nonzero)
226
// clauses          (literals added after the above)
227
// sort_by          (order by x desc added after all else)
228
//
229
// Once you've parsed the items (using parse_form_items()):
230
//
231
// get_select_query(n, m) returns the SQL query to get items from m to m+n
232
// get_url() returns the URL-encoded version of everything
233
// count() returns the number of records satisfying the query
234
235
class SqlQueryString {
236
    var $table;
237
    var $query;
238
    var $urlquery;
239
240
    function __construct() {
241
        if (isset($_GET['table'])) {
242
            $this->table = $_GET['table'];
243
        } else {
244
            $this->table = "";
245
        }
246
        //$this->query = $_GET['query'];
247
        $this->query = "";
248
        $this->urlquery = "";
249
    }
250
    function add($clause) {
251
        //$clause=boinc_real_escape_string($clause);
252
        if (!$this->query) {
253
            $this->query .= "where $clause";
254
        } else {
255
            $this->query .= " and $clause";
256
        }
257
    }
258
    function addclause($clause) {
259
        if ($clause) {
260
            $c = urldecode($clause);
261
            $this->add("( $c )");
262
            $clause = urlencode($clause);
263
            $this->urlquery .= "&clauses=$clause";
264
        }
265
    }
266
    function addeq($name) {
267
        if (isset($_GET[$name])) {
268
            $value = $_GET[$name];
269
        } else {
270
            $value = "";
271
        }
272
        if (strlen($value)) {
273
            $this->add("$name = '$value'");
274
            $this->urlquery .= "&$name=".urlencode($value);
275
        }
276
    }
277
    function addeq_not_CHOOSE_ALL($name) {
278
        if (isset($_GET[$name])) {
279
            $value = $_GET[$name];
280
        } else {
281
            $value = "";
282
        }
283
        // On the selection menu, the ALL selection criteria sets (for example)
284
        // outcome=='CHOOSE_ALL' rather than a numeric value.  This means that
285
        // we enter no condition for this case.
286
        if (strlen($value) && strcmp("CHOOSE_ALL", $value)) {
287
            $this->add("$name = '$value'");
288
            $this->urlquery .= "&$name=".urlencode($value);
289
        }
290
    }
291
    function addgt($name) {
292
        if (isset($_GET[$name])) {
293
            $value = $_GET[$name];
294
        } else {
295
            $value = '';
296
        }
297
        if (strlen($value) && $value > 0) {
298
            $this->add("$name > '$value'");
299
            $this->urlquery .= "&$name=".urlencode($value);
300
        }
301
    }
302
    function addsort($name, $order) {
303
        if (isset($_GET[$name])) {
304
            $value = $_GET[$name];
305
        } else {
306
            $value=null;
307
        }
308
        if (isset($_GET[$order])) {
309
             $order = $_GET[$order];
310
        } else {
311
             $order = null;
312
        }
313
        if ($value) {
314
            if ($order == 'asc') {
315
                $this->query .= " order by $value asc";
316
                $this->urlquery .= "&sort_by_order=".urlencode($order);
317
            } else {
318
                $this->query .= " order by $value desc";
319
                $this->urlquery .= "&$name=".urlencode($value);
320
            }
321
        }
322
    }
323
324
    function count() {
325
        $count_query = "select count(*) as cnt from $this->table $this->query";
326
        $db = BoincDb::get(true);
327
        $result = $db->do_query($count_query);
328
        if (!$result) return 0;
329
        $res = $result->fetch_object();
330
        $result->free();
331
        return $res->cnt;
332
    }
333
334
    function get_select_query($entries_to_show, $start_at) {
335
        if ($entries_to_show) {
336
            if ($start_at) {
337
                return "select * from $this->table $this->query limit $start_at,$entries_to_show";
338
            } else {
339
                return "select * from $this->table $this->query limit $entries_to_show";
340
            }
341
        } else {
342
            return "select * from $this->table $this->query";
343
        }
344
    }
345
346
    function get_url($base = "db_action.php") {
347
        $s = $base . "?table=$this->table$this->urlquery";
348
        return $s;
349
    }
350
351
    function process_form_items() {
352
        $this->addeq('id');
353
        $this->addeq('platformid');
354
        $this->addeq('appid');
355
        $this->addeq('workunitid');
356
        $this->addeq('hostid');
357
        $this->addeq('userid');
358
        $this->addeq('teamid');
359
        $this->addeq('app_version_id');
360
        $this->addeq('exit_status');
361
        if (isset($_GET['nsecs'])) {
362
            $_GET['mod_time'] = date("YmdHis",time() - $_GET['nsecs']);
363
        }
364
        $this->addgt('mod_time');
365
        $this->addeq_not_CHOOSE_ALL('server_state');
366
        $this->addeq_not_CHOOSE_ALL('outcome');
367
        $this->addeq_not_CHOOSE_ALL('client_state');
368
        $this->addeq_not_CHOOSE_ALL('validate_state');
369
        $clauses = get_str("clauses", true);
370
        if ($clauses && strstr($clauses, ";")) error_page("bad clause");
371
        if ($clauses) {
372
            $this->addclause($clauses);
373
        }
374
        $this->addsort('sort_by', 'sort_by_order');
375
    }
376
}
377
378
379
function link_results($n, $mq, $query, $clauses) {
380
    if ($n == '0') { // intentional compare by string
381
        return "0";
382
    } else {
383
        if(strlen($clauses)) {
384
            return "<a href=\"db_action.php?table=result&query=$mq&$query&clauses=".urlencode($clauses)."&sort_by=mod_time&detail=low\">$n</a>";
385
        }
386
        else {
387
            return "<a href=\"db_action.php?table=result&query=$mq&$query&sort_by=mod_time&detail=low\">$n</a>";
388
        }
389
390
    }
391
}
392
393
// Determines if in stderr_out is an error reported and prints as human readable String
394
// @return String A human readable string if error otherwise FALSE
395
// @param String $stderr_out the stderr_out value to parse
396
function stderr_error_string($stderr_out){
397
    $y = parse_element($stderr_out, "<error_code>");
398
    $x = 0;
399
    if ($y) {
400
        $x = (int)$y;
401
    }
402
    if (0<=$x && $x<=9) {
403
        return FALSE;
404
    } else {
405
        return "$x ".error_code_str($x);
406
    }
407
}
408
409
function admin_show_result_summary() {
410
    $ntotal =0;     // TODO: how to count $result?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
411
    $nvalid = 0;    // for SUCCESS results
412
    $ninvalid = 0;
413
    $nfile_deleted = 0;
414
415
    $server_state = array();
416
    $outcome = array();
417
    $client_state = array();
418
419
    for ($ss=1; $ss<6; $ss++) {
420
        $server_state[$ss] = 0;
421
    }
422
    for ($ro=0; $ro<8; $ro++) {
423
        $outcome[$ro] = 0;
424
    }
425
    for ($cs=1; $cs<7; $cs++) {
426
        $client_state[$cs] = 0;
427
    }
428
    for ($fds=0; $fds<4; $fds++) {
429
        $delete_state[$fds] = 0;
430
    }
431
    for ($vs=0; $vs<NVALIDATE_STATES; $vs++) {
432
        $validate_state[$vs]=0;
433
    }
434
435
    $_GET['table'] = 'result';
436
    $_GET['sort_by'] = ''; // ignore sort
437
438
    if (isset($_GET['appid'])) {
439
        $query_appid = $_GET['appid'];
440
    } else {
441
        $query_appid = "";
442
    }
443
    $query_mod_time = 0;
444
    if (isset($_GET['nsecs'])) {
445
        //$query_mod_time = time() - $_GET['nsecs'];
446
        $query_mod_time = $_GET['nsecs'];
447
    }
448
    if (isset($_GET['workunitid'])) {
449
        $query_wuid = $_GET['workunitid'];
450
    } else {
451
        $query_wuid = null;
452
    }
453
    $q = new SqlQueryString();
454
    $q->process_form_items();
455
456
// Important: these need to be kept consistent with db/boinc_db.h and lib/result_state.h
457
    $main_query = "
458
SELECT COUNT(id) AS nTotal,
459
       SUM(case when server_state = '1' then 1 else 0 end) AS serverstate_inactive,
460
       SUM(case when server_state = '2' then 1 else 0 end) AS serverstate_unset,
461
       SUM(case when server_state = '3' then 1 else 0 end) AS serverstate_unset_seq,
462
       SUM(case when server_state = '4' then 1 else 0 end) AS serverstate_inprogress,
463
       SUM(case when server_state = '5' then 1 else 0 end) AS serverstate_over,
464
       SUM(case when server_state = '5' and outcome = '0' then 1 else 0 end) AS outcome_init,
465
       SUM(case when server_state = '5' and outcome = '1' then 1 else 0 end) AS outcome_success,
466
       SUM(case when server_state = '5' and outcome = '2' then 1 else 0 end) AS outcome_couldntsend,
467
       SUM(case when server_state = '5' and outcome = '3' then 1 else 0 end) AS outcome_failure,
468
       SUM(case when server_state = '5' and outcome = '4' then 1 else 0 end) AS outcome_noreply,
469
       SUM(case when server_state = '5' and outcome = '5' then 1 else 0 end) AS outcome_didntneed,
470
       SUM(case when server_state = '5' and outcome = '6' then 1 else 0 end) AS outcome_validateerror,
471
       SUM(case when server_state = '5' and outcome = '7' then 1 else 0 end) AS outcome_clientdetached,
472
       SUM(case when server_state = '5' and outcome = '1' and validate_state = '0' then 1 else 0 end) AS validate_init,
473
       SUM(case when server_state = '5' and outcome = '1' and validate_state = '1' then 1 else 0 end) AS validate_valid,
474
       SUM(case when server_state = '5' and outcome = '1' and validate_state = '2' then 1 else 0 end) AS validate_invalid,
475
       SUM(case when server_state = '5' and outcome = '1' and validate_state = '3' then 1 else 0 end) AS validate_nocheck,
476
       SUM(case when server_state = '5' and outcome = '1' and validate_state = '4' then 1 else 0 end) AS validate_inconclusive,
477
       SUM(case when server_state = '5' and outcome = '1' and validate_state = '5' then 1 else 0 end) AS validate_too_late,
478
       SUM(case when server_state = '5' and outcome = '1' and file_delete_state = '0' then 1 else 0 end) AS filedeletestate_init,
479
       SUM(case when server_state = '5' and outcome = '1' and file_delete_state = '1' then 1 else 0 end) AS filedeletestate_ready,
480
       SUM(case when server_state = '5' and outcome = '1' and file_delete_state = '2' then 1 else 0 end) AS filedeletestate_done,
481
       SUM(case when server_state = '5' and outcome = '1' and file_delete_state = '3' then 1 else 0 end) AS filedeletestate_error,
482
       SUM(case when server_state = '5' and outcome = '3' and client_state = '0' then 1 else 0 end) AS clientstate_init,
483
       SUM(case when server_state = '5' and outcome = '3' and client_state = '1' then 1 else 0 end) AS clientstate_downloading,
484
       SUM(case when server_state = '5' and outcome = '3' and client_state = '2' then 1 else 0 end) AS clientstate_downloaded,
485
       SUM(case when server_state = '5' and outcome = '3' and client_state = '3' then 1 else 0 end) AS clientstate_computedone,
486
       SUM(case when server_state = '5' and outcome = '3' and client_state = '4' then 1 else 0 end) AS clientstate_uploading,
487
       SUM(case when server_state = '5' and outcome = '3' and client_state = '5' then 1 else 0 end) AS clientstate_uploaded,
488
       SUM(case when server_state = '5' and outcome = '3' and client_state = '6' then 1 else 0 end) AS clientstate_aborted
489
FROM result WHERE true
490
    ";
491
492
    if ($query_appid) {
493
        $main_query .= " and appid=$query_appid";
494
    }
495
    if ($query_wuid) {
496
        $main_query .= " and workunitid=$query_wuid";
497
    }
498
    if ($query_mod_time) {
499
        $start = time() - $query_mod_time;
500
501
        // If file deletion is delayed by X,
502
        // subtract X from mod time of  file-deleted results.
503
        // Otherwise we'll show lots of irrelevant results
504
        //
505
        $delay = parse_config(get_config(), "<delete_delay_hours>");
506
        if ($delay) {
507
            $start2 = $start - $delay*3600;;
508
            $main_query .= " and ((file_delete_state>1 and mod_time>FROM_UNIXTIME($start2)) or (mod_time>FROM_UNIXTIME($start)))";
509
        } else {
510
            $main_query .= " and mod_time > FROM_UNIXTIME($start)";
511
        }
512
    }
513
514
    $urlquery = $q->urlquery;
515
    $db = BoincDb::get(true);
516
    $result = $db->do_query($main_query);
517
518
    // echo "Main query was $main_query<br/>";
519
520
    if ($result) {
521
522
        $res = $result->fetch_object();
523
        $ntotal          = $res->nTotal;
524
525
        $server_state[1] = $res->serverstate_inactive;
526
        $server_state[2] = $res->serverstate_unset;
527
        $server_state[3] = $res->serverstate_unset_seq;
528
        $server_state[4] = $res->serverstate_inprogress;
529
        $server_state[5] = $res->serverstate_over;
530
531
        $outcome[0]      = $res->outcome_init;
532
        $outcome[1]      = $res->outcome_success;
533
        $outcome[2]      = $res->outcome_couldntsend;
534
        $outcome[3]      = $res->outcome_failure;
535
        $outcome[4]      = $res->outcome_noreply;
536
        $outcome[5]      = $res->outcome_didntneed;
537
        $outcome[6]      = $res->outcome_validateerror;
538
        $outcome[7]      = $res->outcome_clientdetached;
539
540
        $client_state[1] = $res->clientstate_downloading;
541
        $client_state[2] = $res->clientstate_downloaded;
542
        $client_state[3] = $res->clientstate_computedone;
543
        $client_state[4] = $res->clientstate_uploading;
544
        $client_state[5] = $res->clientstate_uploaded;
545
        $client_state[6] = $res->clientstate_aborted;
546
547
        $validate_state[0] = $res->validate_init;
548
        $validate_state[1] = $res->validate_valid;
549
        $validate_state[2] = $res->validate_invalid;
550
        $validate_state[3] = $res->validate_nocheck;
551
        $validate_state[4] = $res->validate_inconclusive;
552
        $validate_state[5] = $res->validate_too_late;
553
554
        $file_delete[0]  = $res->filedeletestate_init;
555
        $file_delete[1]  = $res->filedeletestate_ready;
556
        $file_delete[2]  = $res->filedeletestate_done;
557
        $file_delete[3]  = $res->filedeletestate_error;
558
559
        $nfile_deleted   = $res->filedeletestate_ready + $res->filedeletestate_done + $res->filedeletestate_error;
560
        $result->free();
561
    }
562
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
563
564
    start_table();
565
    echo "<tr valign=\"top\">";
566
    echo "<td><h2>" . link_results("$ntotal results", $urlquery, '', '') . "</h2></td>";
567
    echo "<td><h2>" . link_results("'Over' results", $urlquery, "server_state=5", '') . "</h2></td>";
568
    echo "<td><h2>" . link_results("'Success' results", $urlquery, "outcome=1", '') . "</h2></td>";
569
    echo "<td><h2>" . link_results("'Client error' results", $urlquery, "outcome=3", '') . "</h2></td>";
570
    echo "</tr>";
571
    echo "<tr valign=\"top\">";
572
    echo "<td>";
573
    start_table('table-striped');
574
    echo "<tr><th>Server state</th><th># results</th></tr>\n";
575
    for ($ss=1; $ss<6; $ss++) {
576
        $res = new StdClass;
577
        $res->server_state = $ss;
578
        row2(result_server_state_string($res),
579
             link_results("$server_state[$ss]",  $urlquery,"server_state=$ss", '')
580
        );
581
    }
582
    end_table();
583
    echo "</td>";
584
585
    echo "<td>";
586
    start_table('table-striped');
587
    echo "<tr><th>Outcome</th><th># results</th></tr>\n";
588
589
    for ($ro=0; $ro<8; $ro++) {
590
        $res = new StdClass;
591
        $res->outcome = $ro;
592
        $res->exit_status = 0;
593
        c_row2($outcome[$ro]?outcome_color($ro):'', result_outcome_string($res),
594
            link_results("$outcome[$ro]", $urlquery, "outcome=$ro", '')
595
        );
596
    }
597
    end_table();
598
    echo "</td>";
599
600
    echo "<td>";
601
    start_table('table-striped');
602
    echo "<tr><th>Validate state</th><th># results</th></tr>\n";
603
    for ($vs=0; $vs<NVALIDATE_STATES; $vs++) {
604
        $res = new StdClass;
605
        $res->validate_state = $vs;
606
        $res->exit_status = 0;
607
        c_row2($validate_state[$vs]?validate_color($vs):'', validate_state_str($res),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $validate_state does not seem to be defined for all execution paths leading up to this point.
Loading history...
608
        link_results("$validate_state[$vs]", $urlquery, "validate_state=$vs", "outcome=1"));
609
    }
610
    end_table();
611
    start_table('table-striped');
612
    echo "<tr><th>File Delete state</th><th># results</th></tr>\n";
613
614
    for ($fds=0; $fds<4; $fds++) {
615
        row2(file_delete_state_str($fds),
616
        link_results("$file_delete[$fds]", $urlquery, "outcome=1", "file_delete_state=$fds"));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $file_delete does not seem to be defined for all execution paths leading up to this point.
Loading history...
617
    }
618
    row2("Total files deleted",
619
    link_results("$nfile_deleted", $urlquery, "outcome=1", "(file_delete_state=1 or file_delete_state=2 or file_delete_state=3)"));
620
    end_table();
621
    echo "</td>";
622
623
    echo "<td>";
624
    start_table('table-striped');
625
    echo "<tr><th>Client state</th><th># results</th></tr>\n";
626
    for ($cs=1; $cs<7; $cs++) {
627
        $res = new StdClass;
628
        $res->client_state = $cs;
629
        $res->exit_status = 0;
630
        row2(result_client_state_string($res),
631
            link_results("$client_state[$cs]", $urlquery, "client_state=$cs", "outcome=3")
632
        );
633
    }
634
    end_table();
635
    echo "</td>";
636
    end_table();
637
638
}
639
640
function server_state_select() {
641
    echo "
642
        <select name=\"server_state\">
643
        <option value=\"CHOOSE_ALL\" selected=\"selected\"> Any </option>
644
    ";
645
    for ($i=1; $i<6; $i++) {
646
        $res = new StdClass;
647
        $res->server_state=$i;
648
        echo "<option value=\"$i\"> "."[$i]&nbsp;&nbsp;".'   '.result_server_state_string($res)."</option>\n";
649
    }
650
    echo "</select>\n";
651
}
652
653
function outcome_select() {
654
    echo "
655
        <select name=\"outcome\">
656
        <option value=\"CHOOSE_ALL\" selected=\"selected\"> Any </option>
657
    ";
658
    for ($i=0; $i<8; $i++) {
659
        $res = new StdClass;
660
        $res->outcome = $i;
661
        $res->exit_status = 0;
662
        echo "<option value=\"$i\"> "."[$i]&nbsp;&nbsp;".'   '.result_outcome_string($res)."</option>\n";
663
    }
664
    echo "</select>\n";
665
}
666
667
function validate_state_select() {
668
    echo "
669
        <select name=\"validate_state\">
670
        <option value=\"CHOOSE_ALL\" selected=\"selected\"> Any </option>
671
        ";
672
    for ($vs=0; $vs<NVALIDATE_STATES; $vs++) {
673
        $res = new StdClass;
674
        $res->validate_state = $vs;
675
        $res->exit_status = 0;
676
        echo "<option value=\"$vs\"> "."[$vs]&nbsp;&nbsp;".'   '.validate_state_str($res)."</option>\n";
677
    }
678
    echo "</select>\n";
679
}
680
681
function client_state_select() {
682
    echo "
683
        <select name=\"client_state\">
684
        <option value=\"CHOOSE_ALL\" selected=\"selected\"> Any </option>
685
    ";
686
    for ($i=0; $i<7; $i++) {
687
        $res = new StdClass;
688
        $res->client_state = $i;
689
        $res->exit_status = 0;
690
        echo "<option value=\"$i\"> "."[$i]&nbsp;&nbsp;".result_client_state_string($res)."</option>\n";
691
    }
692
    echo "</select>\n";
693
}
694
695
function result_sort_select() {
696
    echo "
697
        <select name=\"sort_by\">
698
        <option value=\"\">None
699
        <option value=\"id\">ID
700
        <option value=\"sent_time\">Sent time
701
        <option value=\"mod_time\">Modification time
702
        <option value=\"received_time\">Received time
703
        <option value=\"exit_status\">Exit status
704
        <option value=\"hostid\">Host ID
705
        <option value=\"userid\">User ID
706
        <option value=\"app_version_num\">App Version Number
707
        <option value=\"cpu_time\">CPU time
708
        <option value=\"workunitid\">Work Unit ID
709
        </select>
710
    ";
711
}
712
713
function sort_order_select() {
714
    echo "
715
        <select name=\"sort_by_order\">
716
        <option value=\"asc\">Ascending
717
        <option value=\"desc\" selected>Descending
718
        </select>
719
    ";
720
}
721
722
function table_title($table) {
723
    switch($table) {
724
    case "platform": return "Platforms";
725
    case "app": return "Applications";
726
    case "app_version": return "Application Versions";
727
    case "host": return "Hosts";
728
    case "workunit": return "Workunits";
729
    case "result": return "Results";
730
    case "team": return "Teams";
731
    case "user": return "Users";
732
    case "profile": return "Profiles";
733
    default: return "????";
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
Blank lines are not allowed after DEFAULT statements
Loading history...
734
    }
735
}
736
737
function admin_show_platform($platform) {
738
    start_table();
739
    row("ID", $platform->id);
740
    row("Created", time_str($platform->create_time));
741
    row("Name", $platform->name);
742
    row("User friendly name", $platform->user_friendly_name);
743
    row("","<a href=\"db_action.php?table=app_version&platformid=$platform->id\">App versions for this platform</a>");
744
    end_table();
745
}
746
747
function admin_show_app($app) {
748
    start_table();
749
    row("ID", $app->id);
750
    row("Created", time_str($app->create_time));
751
    row("Name", $app->name);
752
    row("User-friendly name", $app->user_friendly_name);
753
    row("Deprecated", $app->deprecated);
754
    row("Homogeneous redundancy", $app->homogeneous_redundancy);
755
    row("","<a href=\"db_action.php?table=app_version&appid=$app->id\">App Versions for this application</a>");
756
    row("","<a href=\"db_action.php?table=workunit&appid=$app->id&detail=low\">Workunits for this application</a>");
757
    end_table();
758
}
759
760
function admin_show_app_version($app_version) {
761
    start_table();
762
    row("ID", $app_version->id);
763
    row("Created", time_str($app_version->create_time));
764
    row("Application", "<a href=\"db_action.php?table=app&id=$app_version->appid\">" . app_name_by_id($app_version->appid) . "</a>");
765
    row("Version num", $app_version->version_num);
766
    row("Platform", "<a href=\"db_action.php?table=platform&id=$app_version->platformid\">" . platform_name_by_id($app_version->platformid) . "</a>" );
767
    row("Plan Class", $app_version->plan_class);
768
    row("XML doc", "<pre>".htmlspecialchars($app_version->xml_doc)."</pre>");
769
    row("min_core_version", $app_version->min_core_version);
770
    row("max_core_version", $app_version->max_core_version);
771
    row("deprecated", $app_version->deprecated);
772
    end_table();
773
}
774
775
function app_version_short_header() {
776
    echo "
777
        <tr>
778
        <th>ID</th>
779
        <th>Application</th>
780
        <th>Version</th>
781
        <th>Platform</th>
782
        <th>Plan Class</th>
783
        </tr>
784
    ";
785
}
786
787
function admin_show_app_version_short($app_version) {
788
    $x = app_name_by_id($app_version->appid);
789
    $y = platform_name_by_id($app_version->platformid);
790
    echo "
791
        <tr>
792
        <td><a href=\"db_action.php?table=app_version&id=$app_version->id\">$app_version->id</a></td>
793
        <td><a href=\"db_action.php?table=app&id=$app_version->appid\">$x</a></td>
794
        <td>$app_version->version_num</td>
795
        <td><a href=\"db_action.php?table=platform&id=$app_version->platformid\">$y</a></td>
796
        <td>$app_version->plan_class</td>
797
        </tr>
798
    ";
799
}
800
801
function host_short_header() {
802
    echo "
803
        <tr>
804
        <th>host ID</th>
805
        <th>IP address</th>
806
        <th>name</th>
807
        <th>RAC</th>
808
        <th>total credit</th>
809
        <th>CPU</th>
810
        <th>OS</th>
811
        </tr>
812
    ";
813
}
814
815
function admin_show_host_short($host) {
816
    echo "
817
        <tr>
818
        <td><a href=\"db_action.php?table=host&id=$host->id\">$host->id</a></td>
819
        <td>$host->last_ip_addr</td>
820
        <td>$host->domain_name</td>
821
    ";
822
    printf("<td>%.2f</td>", $host->expavg_credit);
823
    printf("<td>%.1f</td>", $host->total_credit);
824
    echo "<td>$host->p_vendor $host->p_model</td>
825
        <td>$host->os_name $host->os_version</td>
826
        </tr>
827
    ";
828
}
829
830
function days_string($x) {
831
    return number_format($x/86400, 2)." days";
832
}
833
834
function resource_name($x) {
835
    switch ($x) {
836
    case 2: return "CPU";
837
    case 3: return "NVIDIA";
838
    case 4: return "AMD";
839
    }
840
    return "Unknown resource: $x";
841
}
842
function hav_app_version_string($avid) {
843
    if ($avid > 1000000) {
844
        $appid = (int)($avid/1000000);
845
        $platform_name = "Anonymous platform";
846
        $version_info = resource_name($avid%1000000);
847
    } else {
848
        $av = BoincAppVersion::lookup("id=$avid");
849
        if (!$av) return "Missing app version $avid";
850
        $appid = $av->appid;
851
        $platform = BoincPlatform::lookup_id($av->platformid);
852
        if (!$platform) return "Missing platform $av->platformid";
853
        $platform_name = $platform->user_friendly_name;
854
        $version_info = "$av->version_num $av->plan_class
855
            <br>PFC: $av->pfc_avg ($av->pfc_n)
856
            <br>scale: $av->pfc_scale
857
        ";
858
    }
859
    $app = BoincApp::lookup_id($appid);
860
    if (!$app) return "Missing app $appid";
861
    return "$app->user_friendly_name
862
        <br>$platform_name
863
        <br>$version_info
864
    ";
865
}
866
867
function admin_show_host_app_versions($hostid) {
868
    start_table();
869
    table_header("app version", "PFC", "Elapsed", "Turnaround");
870
    $havs = BoincHostAppVersion::enum("host_id=$hostid");
871
    foreach ($havs as $hav) {
872
        table_row(
873
            hav_app_version_string($hav->app_version_id),
874
            "$hav->pfc_avg ($hav->pfc_n)",
875
            "$hav->et_avg ($hav->et_n)",
876
            days_string($hav->turnaround_avg)." ($hav->turnaround_n)"
877
        );
878
    }
879
    end_table();
880
}
881
882
function admin_show_host($host) {
883
    start_table();
884
885
    row("ID", $host->id);
886
    row("Created", time_str($host->create_time));
887
    row("User",
888
        "<a href=\"db_action.php?table=user&id=$host->userid\">".user_name_by_id($host->userid)."($host->userid)</a>"
889
    );
890
891
    row("Venue", $host->venue);
892
    row("Info", $host->serialnum);
893
    row("Total credit", $host->total_credit);
894
    row("Average credit", $host->expavg_credit);
895
    row("Average update time", time_str($host->expavg_time));
896
    row("IP address", "$host->last_ip_addr<br>(same the last $host->nsame_ip_addr times)");
897
    row("External IP address", "$host->external_ip_addr<br>");
898
    row("Domain name", $host->domain_name);
899
    $x = $host->timezone/3600;
900
    if ($x >= 0) $x="+$x";
901
    row("Local Standard Time", "UTC $x hours");
902
    row("Number of CPUs", $host->p_ncpus);
903
    row("CPU", "$host->p_vendor $host->p_model");
904
    row("GFLOPS", number_format($host->p_fpops/1e9, 2));
905
    row("GIOPS", number_format($host->p_iops/1e9, 2));
906
907
    row("Number of GPUs", $host->p_ngpus);
908
    row("GPU GFLOPS", number_format($host->p_gpu_fpops/1e9, 2));
909
910
    $x = $host->p_membw/(1024*1024);
911
    $y = number_format($x, 2);
912
    row("Memory bandwidth", "$y MB/sec");
913
    row("Operating System", "$host->os_name $host->os_version");
914
    $x = $host->m_nbytes/(1024*1024);
915
    $y = number_format($x, 2);
916
    row("Memory", "$y MB");
917
    $x = $host->m_cache/1024;
918
    $y = number_format($x, 2);
919
    row("Cache", "$y KB");
920
    $x = $host->m_swap/(1024*1024);
921
    $y = number_format($x, 2);
922
    row("Swap Space", "$y MB");
923
    $x = $host->d_total/(1024*1024*1024);
924
    $y = number_format($x, 2);
925
    row("Total Disk Space", "$y GB");
926
    $x = $host->d_free/(1024*1024*1024);
927
    $y = number_format($x, 2);
928
    row("Free Disk Space", "$y GB");
929
    $x = number_format($host->n_bwup/1024);
930
    row("Avg network bandwidth (upstream)", "$x kB/sec");
931
    $x = number_format($host->n_bwdown/1024);
932
    row("Avg network bandwidth (downstream)", "$x kB/sec");
933
    row("Average turnaround", days_string($host->avg_turnaround));
934
    row("Number of RPCs", $host->rpc_seqno);
935
    row("Last RPC", time_str($host->rpc_time));
936
    row("% of time client on", 100*$host->on_frac." %");
937
    row("% of time host connected", 100*$host->connected_frac." %");
938
    row("% of time user active", 100*$host->active_frac." %");
939
    row("# of results today", $host->nresults_today);
940
    row("Results", "<a href=\"db_action.php?table=result&detail=low&hostid=$host->id&sort_by=sent_time\">click here</a>");
941
    end_table();
942
    admin_show_host_app_versions($host->id);
943
}
944
945
function admin_show_workunit($wu) {
946
    $_GET = array('workunitid' => $wu->id);
947
    admin_show_result_summary();
948
949
    start_table();
950
    row("Created", time_str($wu->create_time));
951
    row("Transition Time", time_str($wu->transition_time));
952
    row("Last time modified",mysqltime_str($wu->mod_time));
953
    row("Name", $wu->name);
954
    row("XML doc", "<pre>".htmlspecialchars($wu->xml_doc)."</pre>");
955
    row("Application", "<a href=\"db_action.php?table=app&id=$wu->appid\">" . app_name_by_id($wu->appid) . " [".$wu->appid."]</a>");
956
    row("Application version number", $wu->app_version_num);
957
    row("Batch", $wu->batch);
958
    $x = number_format($wu->rsc_fpops_est/1e9, 2);
959
    row("Estimated FP Operations", "$x GFLOPS");
960
    $x = number_format($wu->rsc_fpops_bound/1e9, 2);
961
    row("Max FP Operations", "$x GFLOPS");
962
    $x = $wu->rsc_memory_bound/(1024*1024);
963
    $y = number_format($x, 2);
964
    row("Max Memory Usage", "$y MB");
965
    $x = $wu->rsc_disk_bound/(1024*1024);
966
    $y = number_format($x, 2);
967
    row("Max Disk Usage", "$y MB");
968
    row("Need validate?", ($wu->need_validate?"yes [":"no [").$wu->need_validate."]");
969
    row("Canonical resultid",
970
            "<a href=\"db_action.php?table=result&id=$wu->canonical_resultid\">".$wu->canonical_resultid."</a>");
971
    row("Canonical credit", $wu->canonical_credit);
972
    //row("Timeout check time", time_str($wu->timeout_check_time));
973
    row("Delay bound", "$wu->delay_bound" . " =  " . time_diff($wu->delay_bound) );
974
    row("Error mask", wu_error_mask_str($wu->error_mask, true));
975
    row("File delete state", file_delete_state_str($wu->file_delete_state)." [".$wu->file_delete_state."]");
976
    row("Assimilation state", assimilate_state_str($wu->assimilate_state)." [".$wu->assimilate_state."]");
977
    // row("","<a href=db_action.php?table=result&workunitid=$wu->id&detail=low>Show associated results</a>");
978
    row("min quorum", $wu->min_quorum);
979
    row("target results", $wu->target_nresults);
980
    row("max error results", $wu->max_error_results);
981
    row("max total results", $wu->max_total_results);
982
    row("max success results", $wu->max_success_results);
983
    row("result template file",$wu->result_template_file);
984
    row("hr_class", $wu->hr_class);
985
    row("opaque", $wu->opaque);
986
    row("Priority", $wu->priority);
987
    row("Keywords", ($wu->keywords ? $wu->keywords : "<em>empty</em>"));
988
    end_table();
989
    echo "<div align=\"center\">";
990
    echo "<a href=\"show_log.php?s=$wu->name\">GREP LOGS FOR THIS WORKUNIT</a>";
991
    echo "</div>";
992
    echo "<p>";
993
}
994
995
function workunit_short_header() {
996
    echo "
997
        <tr>
998
        <th>ID</th>
999
        <th>name</th>
1000
        <th>canonical result</th>
1001
        <th>error_mask</th>
1002
        <th>file delete</th>
1003
        <th>assimilate</th>
1004
        </tr>
1005
    ";
1006
}
1007
1008
function admin_show_workunit_short($wu) {
1009
    if ($wu->canonical_resultid) {
1010
        $cr = sprintf('<a href="db_action.php?table=result&id=%d">%d</a>',
1011
            $wu->canonical_resultid,
1012
            $wu->canonical_resultid
1013
        );
1014
    } else {
1015
        $cr = "none";
1016
    }
1017
    $cr .= sprintf(
1018
        ' <a href="db_action.php?table=result&workunitid=%d&detail=low">all</a>',
1019
        $wu->id
1020
    );
1021
    $wu_link = sprintf(
1022
        '<a href="db_action.php?table=workunit&id=%d&detail=high">%d</a>',
1023
        $wu->id,
1024
        $wu->id
1025
    );
1026
    $e = wu_error_mask_str($wu->error_mask, true);
1027
    $f = file_delete_state_str($wu->file_delete_state);
1028
    $a = assimilate_state_str($wu->assimilate_state);
1029
    echo "
1030
        <tr>
1031
        <td>$wu_link</td>
1032
        <td>$wu->name</td>
1033
        <td>$cr</td>
1034
        <td>$e</td>
1035
        <td>$f</td>
1036
        <td>$a</td>
1037
        </tr>
1038
    ";
1039
}
1040
1041
function host_user_link($hostid) {
1042
    if (!$hostid) return '---';
1043
1044
    $h = "<a href=\"db_action.php?table=host&id=$hostid\">$hostid</a>";
1045
    $host = BoincHost::enum_fields("userid", "id=".$hostid, "limit 1");
1046
    if (!$host) return $h;
1047
    $user = BoincUser::enum_fields("id, name", "id=".$host[0]->userid, "limit 1");
1048
    if (!$user) return $h;
1049
    return "$h<br><small>(<a href=\"db_action.php?table=user&id=".$user[0]->id."\">".$user[0]->name."</a>)</small>";
1050
}
1051
1052
function validate_color($validate_state) {
1053
    switch ($validate_state) {
1054
   case 1: return '33cc33'; // valid, green
1055
   case 2: return 'ffa500'; // invalid result, orange
1056
    }
1057
    return '';
1058
}
1059
1060
function outcome_color($outcome) {
1061
    switch($outcome) {
1062
    case 0: return '9900cc'; // "Init", purple
1063
    case 1: return '33cc33'; // "Success", green
1064
    case 3: return 'ff3333'; // "Client error", red
1065
    case 4: return 'ff6699'; // "No reply", pink
1066
    case 6: return 'ffff33'; // "Validate error", yellow
1067
    }
1068
    return '';
1069
}
1070
1071
function credit_str($c) {
1072
    if ($c) {
1073
        return sprintf("%.3f", $c);
1074
    } else {
1075
        return '---';
1076
    }
1077
}
1078
1079
function admin_show_result($result) {
1080
    $wu_name = wu_name_by_id($result->workunitid);
1081
1082
    start_table();
1083
1084
    row("Created", time_str($result->create_time));
1085
    row("Sent", time_str($result->sent_time));
1086
    row("Report deadline", time_str($result->report_deadline));
1087
    row("Received", time_str($result->received_time));
1088
    row("Last time modified",mysqltime_str($result->mod_time));
1089
    row("Name", $result->name);
1090
    row("Workunit", "<a href=\"db_action.php?table=workunit&id=$result->workunitid\">" . wu_name_by_id($result->workunitid) . "</a> [$result->workunitid]" );
1091
    row("Server state", result_server_state_string($result)." [$result->server_state]");
1092
    row("Outcome", result_outcome_string($result)." [$result->outcome]");
1093
    row("Client state", result_client_state_string($result)." [$result->client_state]");
1094
    row("Exit status", exit_status_string($result->exit_status));
1095
    row("Host ID", "<a href=\"db_action.php?table=host&id=$result->hostid\">" . host_name_by_id($result->hostid) . "</a> [$result->hostid]");
1096
    row("User ID", "<a href=\"db_action.php?table=user&id=$result->userid\">" . user_name_by_id($result->userid) . "</a> [$result->userid]");
1097
    row("CPU time", $result->cpu_time);
1098
    row("Elapsed time", $result->elapsed_time);
1099
    if($error=stderr_error_string($result->stderr_out)) {
1100
        row("error in stderr out", $error);
1101
    }
1102
    row("Batch", $result->batch);
1103
    row("File delete state", file_delete_state_str($result->file_delete_state)." [$result->file_delete_state]");
1104
    row("Validate state", validate_state_str($result)." [$result->validate_state]");
1105
    row("Granted credit", $result->granted_credit);
1106
    row("Application", "<a href=\"db_action.php?table=app&id=$result->appid\">".app_name_by_id($result->appid)."</a>");
1107
    if ($result->app_version_id > 0) {
1108
        $x1 = "<a href=\"db_action.php?table=app_version&amp;id=$result->app_version_id\">";
1109
        $x2 = "</a>";
1110
    } else {
1111
        $x1 = $x2 = "";
1112
    }
1113
    row("App version", $x1.app_version_string($result).$x2);
1114
    row("App version ID", $result->app_version_id);
1115
    row("Estimated GFLOPS", number_format($result->flops_estimate/1e9, 2));
1116
    row("Random",$result->random);
1117
    row("Opaque",$result->opaque);
1118
    row("Teamid",$result->teamid);
1119
    row("Priority",$result->priority);
1120
    row("XML doc in", "<pre>".htmlspecialchars($result->xml_doc_in)."</pre>");
1121
    row("XML doc out", "<pre>".htmlspecialchars($result->xml_doc_out)."</pre>");
1122
    row("stderr out", "<pre>"
1123
        .htmlspecialchars(
1124
            $result->stderr_out,
1125
            ENT_QUOTES | (defined('ENT_SUBSTITUTE')?ENT_SUBSTITUTE:0),
1126
            'utf-8'
1127
        )
1128
        ."</pre>"
1129
    );
1130
    end_table();
1131
    echo "
1132
        <center>
1133
        <a href=\"show_log.php?s=$result->name\">GREP LOGS FOR THIS RESULT</a>
1134
        </center>
1135
        <p>
1136
    ";
1137
}
1138
1139
function result_short_header() {
1140
    echo "
1141
        <tr>
1142
        <th>result ID</th>
1143
        <th>WU ID</th>
1144
        <th>server<br>state</th>
1145
        <th>outcome</th>
1146
        <th>client<br>state</th>
1147
        <th>validate<br>state</th>
1148
        <th>delete<br>state</th>
1149
        <th>exit<br>status</th>
1150
        <th>host<br>(user)</th>
1151
        <th>app<br>ver</th>
1152
        <th>received <br><i>or</i> <font color=\"#ff3333\">dead</font><font color=\"#33cc33\">line</font> <br><i>or</i> <font color=\"#9900cc\">created</font></th>
1153
        <th>CPU<br>hours</th>
1154
        <th>granted<br>credit</th>
1155
        </tr>
1156
    ";
1157
}
1158
1159
function admin_show_result_short($result) {
1160
    $ss = result_server_state_string($result)." [$result->server_state]";
1161
    $oc = result_outcome_string($result)." [$result->outcome]";
1162
    $vs = validate_state_str($result)." [$result->validate_state]";
1163
    $cs2 = result_client_state_string($result)." [$result->client_state]";
1164
    if ($result->outcome == 3) {
1165
        $cs = result_client_state_string($result);
1166
        $oc = "$oc ($cs)";
1167
    }
1168
    if ($result->received_time) {
1169
        $received = time_str($result->received_time);
1170
    } else {
1171
        // result has not been received yet, so show report deadline either
1172
        // in green if in the future or in red if in the past.
1173
        $timenow=time();
1174
        if ($result->report_deadline==0)  {
1175
            // not sent -- show create time in purple
1176
            $received = "<font color=\"9900cc\">". time_str($result->create_time) . "</font>";
1177
        } else if ($result->report_deadline>=$timenow) {
1178
            // overdue -- show deadline in red
1179
            $received = "<font color=\"#33cc33\">". time_str($result->report_deadline) . "</font>";
1180
        } else {
1181
            // in progress and not overdue -- show deadline in green
1182
            $received = "<font color=\"#ff3333\">". time_str($result->report_deadline) . "</font>";
1183
        }
1184
    }
1185
    $version = app_version_string($result)." (<a href=\"db_action.php?table=app_version&id=$result->app_version_id\">$result->app_version_id</a>)";
1186
    $outcome_color = outcome_color($result->outcome);
1187
    $validate_color = validate_color($result->validate_state);
1188
    $host_user = host_user_link($result->hostid);
1189
    $cpu_hours = sprintf("%.1f",$result->cpu_time / 3600);
1190
    $granted_credit = "<a href=credit.php?wu_id=$result->workunitid>".credit_str($result->granted_credit)."</a>";
1191
    $delete_state = file_delete_state_str($result->file_delete_state);
1192
1193
    echo "
1194
        <tr>
1195
        <td><a href=\"db_action.php?table=result&id=$result->id\">$result->id</a></td>
1196
        <td><a href=\"db_action.php?table=workunit&id=$result->workunitid\">$result->workunitid</a></td>
1197
        <td>$ss</td>
1198
        <td bgcolor=$outcome_color>$oc</td>
1199
        <td>$cs2</td>
1200
        <td bgcolor=$validate_color>$vs</td>
1201
        <td>$delete_state</td>
1202
        <td>", exit_status_string($result->exit_status), "</td>
1203
        <td>$host_user</td>
1204
        <td>$version</td>
1205
        <td>$received</td>
1206
        <td>$cpu_hours</td>
1207
        <td>$granted_credit</td>
1208
        </tr>
1209
    ";
1210
}
1211
1212
function admin_show_user($user) {
1213
    start_table();
1214
    row("ID", $user->id);
1215
    row("Created", time_str($user->create_time));
1216
    row("Name", $user->name);
1217
    if(!in_rops()) {
1218
        row("Authenticator", $user->authenticator);
1219
    }
1220
    row("Email address", $user->email_addr);
1221
    row("Previous email address", $user->previous_email_addr);
1222
    row("Email change time", time_str($user->email_addr_change_time));
1223
    row("OK to send Email?", $user->send_email);
1224
    row("Country", $user->country);
1225
    row("Postal code", $user->postal_code);
1226
    row("Total credit", $user->total_credit);
1227
    row("Average credit", $user->expavg_credit);
1228
    row("Last average time", time_str($user->expavg_time));
1229
    row("Default venue", $user->venue);
1230
    row("Hosts", "<a href=\"db_action.php?table=host&userid=$user->id&detail=low\">click</a>");
1231
    row("Cross project ID", $user->cross_project_id);
1232
    if(!in_rops()) {
1233
        row("Password Hash", $user->passwd_hash);
1234
    }
1235
    row("Donated", $user->donated);
1236
    end_table();
1237
}
1238
1239
function admin_show_user_summary($maxuser) {
1240
    $top_country = array();
1241
    $top_language = array();
1242
    $db = BoincDb::get(true);
1243
    $stats_res = $db->do_query("select max(id) as maxuser,
1244
        SUM(case when has_profile = '1' then 1 else 0 end) as profuser,
1245
        SUM(case when teamid != '0' then 1 else 0 end) as teamuser
1246
        from user"
1247
    );
1248
    $stats = $stats_res->fetch_assoc();
1249
    if ($maxuser > $stats['maxuser']) {
1250
        $maxuser = $stats['maxuser'];
1251
    }
1252
    // TODO: what is in profile.posts? A user can post in the forums without a profile.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
1253
    $users = BoincUser::enum(null, "order by posts desc limit ".$maxuser);
1254
    $profiles = BoincProfile::enum(null, "order by posts desc limit ".$maxuser);
1255
    foreach ($users as $user) {
1256
        $top10poster[$i] = $user;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $i seems to be never defined.
Loading history...
1257
        $top_country[$user->country] += 1;
1258
    }
1259
    foreach ($profiles as $profile) {
1260
        if ($profile->language != '') {
1261
            $top_language[$profile->language] += 1;
1262
        }
1263
    }
1264
    $stats_res->free();
1265
    echo "<table>
1266
          <tr valign=\"top\">
1267
            <td><h2>General</h2></td>
1268
            <td><h2>top10 Poster</h2></td>
1269
            <td><h2>top$maxuser Countries</h2></td>
1270
            <td><h2>top$maxuser Languages</h2></td>
1271
          </tr>
1272
    ";
1273
    echo "<tr valign=\"top\">";
1274
    echo "<td><table border=\"1\">
1275
          <tr><th>&nbsp;</th><th>&nbsp;</th></tr>
1276
    ";
1277
    row2_plain("Users:", $stats['maxuser']);
1278
    row2_plain("Profiles:", $stats['profuser']);
1279
    row2_plain("Team members:", $stats['teamuser']);
1280
    echo "</table></td>";
1281
    echo "<td><table border=\"2\">\n";
1282
    echo "<tr><th>User</th><th># posts</th></tr>\n";
1283
    for ($p=1; $p<=10; $p++) {
1284
        row2_plain(user_links_ops($top10poster[$p]), $top10poster[$p]->posts);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $top10poster seems to be defined by a foreach iteration on line 1255. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
1285
    }
1286
    echo "</table></td>";
1287
    echo "<td><table border=\"2\">\n";
1288
    echo "<tr><th>Country</th><th># users</th></tr>\n";
1289
    arsort($top_country);
1290
1291
    foreach ($top_country as $key => $value) {
1292
        row2_plain($key, $value);
1293
    }
1294
    echo "</table></td>";
1295
    echo "<td><table border=\"2\">\n";
1296
    echo "<tr><th>Language</th><th># users</th></tr>\n";
1297
    arsort($top_language);
1298
    foreach ($top_language as $key => $value) {
1299
        row2_plain($key, $value);
1300
    }
1301
    echo "</table></td>";
1302
1303
    echo "</tr></table></td></tr>";
1304
}
1305
1306
function team_type_string($s) {
1307
    switch ($s) {
1308
    case 1: return "Unclassified";
1309
    case 2: return "Company";
1310
    case 3: return "Primary school";
1311
    case 4: return "Secondary school";
1312
    case 5: return "Junior college";
1313
    case 6: return "University or department";
1314
    case 7: return "Government agency";
1315
    default: return "Unknown";
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
Blank lines are not allowed after DEFAULT statements
Loading history...
1316
    }
1317
}
1318
1319
function admin_show_team($team) {
1320
    start_table();
1321
    row("ID", $team->id);
1322
    row("Team Founder", "<a href=\"db_action.php?table=user&id=$team->userid\">" . user_name_by_id($team->userid) . "</a>");
1323
    row("Name", $team->name);
1324
    row("Name (HTML Formatted)", "<pre>" . htmlspecialchars($team->name_html) . "</pre>" );
1325
    row("Url", "<a href=\"http://$team->url\">" . $team->url . "</a>");
1326
    row("Type", team_type_string($team->type));
1327
    row("Description", $team->description);
1328
    row("", "<a href=\"db_action.php?table=user&teamid=$team->id\">List All Members</a>");
1329
    end_table();
1330
}
1331
1332
function team_name_by_id($teamid) {
1333
    $team = BoincTeam::lookup_id($teamid);
1334
    if (!$team) return "No team";
1335
    return $team->name;
1336
}
1337
1338
function user_name_by_id($user_id) {
1339
    $user = BoincUser::lookup_id($user_id);
1340
    if (!$user) return "No user";
1341
    return $user->name;
1342
}
1343
1344
function app_name_by_id($appid) {
1345
    $app = BoincApp::lookup_id($appid);
1346
    if (!$app) return "No app";
1347
    return $app->name;
1348
}
1349
1350
function wu_name_by_id($workunitid) {
1351
    $wu = BoincWorkunit::lookup_id($workunitid);
1352
    if (!$wu) return "Missing workunit";
1353
    return $wu->name;
1354
}
1355
1356
function platform_name_by_id($platformid) {
1357
    $plat = BoincPlatform::lookup_id($platformid);
1358
    if (!$plat) return "Missing platform";
1359
    return $plat->name;
1360
}
1361
1362
function host_name_by_id($hostid) {
1363
    $host = BoincHost::lookup_id($hostid);
1364
    if (!$host) return "No host";
1365
    if (!strlen($host->domain_name) && !strlen($host->last_ip_addr)) {
1366
        return "(blank)";
1367
    } else {
1368
        return $host->domain_name . " (" . $host->last_ip_addr . ")";
1369
    }
1370
}
1371
1372
?>
1373