Passed
Push — master ( 124629...ee6189 )
by Vitalii
09:10
created

fill_in_app_and_user_names()   B

Complexity

Conditions 7
Paths 25

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 23
c 3
b 0
f 0
nc 25
nop 1
dl 0
loc 29
rs 8.6186
1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2024 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// remote job submission functions.
21
//
22
// A 'remote app' is one where jobs are submitted remotely:
23
// - via web RPCs
24
// - via forms on the project web site
25
//
26
// In both cases only users with permission can submit; either
27
// - a user_submit record with submit_all set
28
// - a user_submit_app record
29
//
30
// These apps are described in $remote_apps in project.inc
31
//
32
// This page provides several functions involving remote apps
33
// ('me' means logged-in user)
34
//
35
//  show_all_batches
36
//      show list of batches visible to me, possibly filtered by
37
//          submitting user
38
//          app
39
//          state (in progress, completed etc.)
40
//  show_user_batches
41
//      show my batches
42
//  show_batches_admin_app
43
//      show all batches for a given app to admin
44
//  admin_all
45
//      show all batches to admin
46
//  batch_stats
47
//      show WSS, disk usage stats for a batch
48
//  query_batch
49
//      show list of jobs in a batch
50
//  query_job
51
//      show job details and instances
52
//  retire_batch
53
//      retire a batch
54
//  retire_multi
55
//      retire multiple batches
56
//  abort_batch
57
//      abort a batch
58
//  admin
59
//      show index of admin functions
60
//  update_only_own
61
//      control whether my jobs should run only on my computers
62
63
require_once("../inc/submit_db.inc");
64
require_once("../inc/util.inc");
65
require_once("../inc/result.inc");
66
require_once("../inc/submit_util.inc");
67
require_once("../project/project.inc");
68
require_once('../project/remote_apps.inc');
69
70
display_errors();
71
72
// in general, if there can be lots of something,
73
// show this many and a link to show all.
74
// TODO: jobs in a batch
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...
75
//
76
define("PAGE_SIZE", 20);
77
78
function return_link() {
79
    echo "<p><a href=submit.php?action=show_user_batches>Return to batches page</a>\n";
80
}
81
82
// return subset of batches in given state
83
//
84
function batches_in_state($all_batches, $state) {
85
    $batches = [];
86
    foreach ($all_batches as $batch) {
87
        if ($batch->state != $state) continue;
88
        $batches[] = $batch;
89
    }
90
    return $batches;
91
}
92
93
function sort_batches(&$batches, $order) {
94
    switch ($order) {
95
    case 'sub_asc':
96
        $f = function($a, $b) {
97
            return (int)($a->create_time - $b->create_time);
98
        };
99
        break;
100
    case 'sub_desc':
101
        $f = function($a, $b) {
102
            return (int)($b->create_time - $a->create_time);
103
        };
104
        break;
105
    case 'comp_asc':
106
        $f = function($a, $b) {
107
            return (int)($a->completion_time - $b->completion_time);
108
        };
109
        break;
110
    case 'comp_desc':
111
        $f = function($a, $b) {
112
            return (int)($b->completion_time - $a->completion_time);
113
        };
114
        break;
115
    }
116
    usort($batches, $f);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $f does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
}
118
119
// in progress batches don't have completion time
120
//
121
function in_progress_order($order) {
122
    switch ($order) {
123
    case 'comp_asc': return 'sub_asc';
124
    case 'comp_desc': return 'sub_desc';
125
    }
126
    return $order;
127
}
128
129
// show order options
130
// sub_asc, sub_desc: submission time ( = ID order)
131
// comp_asc, comp_desc: completion time
132
133
function order_options($url_args, $order) {
134
    $url = "submit.php?$url_args";
135
    echo sprintf(
136
        'Order by: submission time (%s, %s) or completion time (%s, %s)',
137
        order_item($url, $order, 'sub_asc', 'ascending'),
138
        order_item($url, $order, 'sub_desc', 'descending'),
139
        order_item($url, $order, 'comp_asc', 'ascending'),
140
        order_item($url, $order, 'comp_desc', 'descending')
141
    );
142
}
143
144
function order_item($url, $cur_order, $order, $label) {
145
    if ($cur_order == $order) {
146
        return $label;
147
    } else {
148
        $url .= "&order=$order";
149
        return "<a href=$url>$label</a>";
150
    }
151
}
152
153
function get_order() {
154
    $order = get_str('order', true);
155
    if (!$order) $order = 'sub_desc';
156
    return $order;
157
}
158
159
function state_count($batches, $state) {
160
    $n = 0;
161
    foreach ($batches as $batch) {
162
        if ($batch->state == $state) $n++;
163
    }
164
    return $n;
165
}
166
167
function show_all_batches_link($batches, $state, $limit, $user, $app) {
168
    $n = state_count($batches, $state);
169
    if ($n > $limit) {
170
        if ($user) $userid = $user->id;
171
        else $userid = 0;
172
        if ($app) $appid = $app->id;
173
        else $appid = 0;
174
175
        echo "Showing the most recent $limit of $n batches.
176
            <a href=submit.php?action=show_all_batches&state=$state&userid=$userid&appid=$appid>Show all $n</a>
177
            <p>
178
        ";
179
    }
180
}
181
182
// show in-progress batches.
183
//
184
function show_in_progress($all_batches, $order, $limit, $user, $app) {
185
    $batches = batches_in_state($all_batches, BATCH_STATE_IN_PROGRESS);
186
    sort_batches($batches, in_progress_order($order));
187
    echo sprintf('<h3>Batches in progress (%d)</h3>', count($batches));
188
    $first = true;
189
    $n = 0;
190
    foreach ($batches as $batch) {
191
        if ($limit && $n == $limit) break;
192
        $n++;
193
        if ($first) {
194
            $first = false;
195
            if ($limit) {
196
                show_all_batches_link(
197
                    $batches, BATCH_STATE_IN_PROGRESS, $limit, $user, $app
198
                );
199
            }
200
            form_start('submit.php');
201
            form_input_hidden('action', 'abort_selected');
202
            start_table('table-striped');
203
            $x = [
204
                "Name",
205
                "ID",
206
                "User",
207
                "App",
208
                "# jobs",
209
                "Progress",
210
                "Submitted"
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
211
            ];
212
            row_heading_array($x);
213
        }
214
        $pct_done = (int)($batch->fraction_done*100);
215
        $x = [
216
            "<a href=submit.php?action=query_batch&batch_id=$batch->id>$batch->name</a>",
217
            "<a href=submit.php?action=query_batch&batch_id=$batch->id>$batch->id</a>",
218
            $batch->user_name,
219
            $batch->app_name,
220
            $batch->njobs,
221
            "$pct_done%",
222
            local_time_str($batch->create_time)
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
223
        ];
224
        row_array($x);
225
    }
226
    if ($first) {
227
        echo "<p>None.\n";
228
    } else {
229
        end_table();
230
        form_end();
231
    }
232
}
233
234
function show_complete($all_batches, $order, $limit, $user, $app) {
235
    $batches = batches_in_state($all_batches, BATCH_STATE_COMPLETE);
236
    sort_batches($batches, $order);
237
    echo sprintf('<h3>Completed batches (%d)</h3>', count($batches));
238
    $first = true;
239
    $n = 0;
240
    foreach ($batches as $batch) {
241
        if ($limit && $n == $limit) break;
242
        $n++;
243
        if ($first) {
244
            $first = false;
245
            if ($limit) {
246
                show_all_batches_link($batches, BATCH_STATE_COMPLETE, $limit, $user, $app);
247
            }
248
            form_start('submit.php', 'get');
249
            form_input_hidden('action', 'retire_multi');
250
            start_table('table-striped');
251
            table_header(
252
                "Name", "ID", "User", "App", "# Jobs", "Submitted", "Completed", "Select"
253
            );
254
        }
255
        table_row(
256
            "<a href=submit.php?action=query_batch&batch_id=$batch->id>$batch->name</a>",
257
            "<a href=submit.php?action=query_batch&batch_id=$batch->id>$batch->id</a>",
258
            $batch->user_name,
259
            $batch->app_name,
260
            $batch->njobs,
261
            local_time_str($batch->create_time),
262
            local_time_str($batch->completion_time),
263
            sprintf('<input type=checkbox name=retire_%d>', $batch->id)
264
        );
265
    }
266
    if ($first) {
267
        echo "<p>None.\n";
268
    } else {
269
        end_table();
270
        form_submit('Retire selected batches');
271
        form_end();
272
    }
273
}
274
275
function show_aborted($all_batches, $order, $limit, $user, $app) {
276
    $batches = batches_in_state($all_batches, BATCH_STATE_ABORTED);
277
    if (!$batches) return;
278
    sort_batches($batches, $order);
279
    echo sprintf('<h3>Aborted batches (%d)</h3>', count($batches));
280
    $first = true;
281
    $n = 0;
282
    foreach ($batches as $batch) {
283
        if ($limit && $n == $limit) break;
284
        $n++;
285
        if ($first) {
286
            $first = false;
287
            if ($limit) {
288
                show_all_batches_link($batches, BATCH_STATE_ABORTED, $limit, $user, $app);
289
            }
290
            form_start('submit.php', 'get');
291
            form_input_hidden('action', 'retire_multi');
292
            start_table();
293
            table_header("Name", "ID", "User", "App", "# Jobs", "Submitted", "Aborted", 'Select');
294
        }
295
        table_row(
296
            "<a href=submit.php?action=query_batch&batch_id=$batch->id>$batch->name</a>",
297
            "<a href=submit.php?action=query_batch&batch_id=$batch->id>$batch->id</a>",
298
            $batch->user_name,
299
            $batch->app_name,
300
            $batch->njobs,
301
            local_time_str($batch->create_time),
302
            local_time_str($batch->completion_time),
303
            sprintf('<input type=checkbox name=retire_%d>', $batch->id)
304
        );
305
    }
306
    if (!$first) {
307
        end_table();
308
        form_submit('Retire selected batches');
309
        form_end();
310
    }
311
}
312
313
// fill in the app and user names in list of batches
314
//
315
function fill_in_app_and_user_names(&$batches) {
316
    $apps = [];
317
    $users = [];
318
    foreach ($batches as $batch) {
319
        if (array_key_exists($batch->app_id, $apps)) {
320
            $app = $apps[$batch->app_id];
321
        } else {
322
            $app = BoincApp::lookup_id($batch->app_id);
323
            $apps[$batch->app_id] = $app;
324
        }
325
        if ($app) {
326
            $batch->app_name = $app->name;
327
            if ($batch->description) {
328
                $batch->app_name .= " ($batch->description)";
329
            }
330
        } else {
331
            $batch->app_name = "unknown";
332
        }
333
334
        if (array_key_exists($batch->user_id, $users)) {
335
            $user = $users[$batch->user_id];
336
        } else {
337
            $user = BoincUser::lookup_id($batch->user_id);
338
            $users[$batch->user_id] = $user;
339
        }
340
        if ($user) {
341
            $batch->user_name = $user->name;
342
        } else {
343
            $batch->user_name = "missing user $batch->user_id";
344
        }
345
    }
346
}
347
348
// show a set of batches: in progress, then completed, then aborted
349
//
350
function show_batches($batches, $order, $limit, $user, $app) {
351
    fill_in_app_and_user_names($batches);
352
    $batches = get_batches_params($batches);
353
    show_in_progress($batches, $order, $limit, $user, $app);
354
    show_complete($batches, $order, $limit, $user, $app);
355
    show_aborted($batches, $order, $limit, $user, $app);
356
}
357
358
// show links to per-app job submission forms
359
//
360
function show_submit_links($user) {
361
    global $remote_apps;
362
    $user_submit = BoincUserSubmit::lookup_userid($user->id);
363
    if (!$user_submit) {
364
        error_page("Ask the project admins for permission to submit jobs");
365
    }
366
367
    page_head("Submit jobs");
368
369
    // show links to per-app job submission pages
370
    //
371
    foreach ($remote_apps as $area => $apps) {
372
        panel($area,
373
            function() use ($apps) {
374
                foreach ($apps as $app) {
375
                    if (empty($app->form)) continue;
376
                    // show app logo if available
377
                    if (!empty($app->logo)) {
378
                        echo sprintf(
379
                            '<a href=%s><img width=100 src=%s></a>&nbsp;',
380
                            $app->form, $app->logo
381
                        );
382
                    } else {
383
                        echo sprintf(
384
                            '<a href=%s>%s</a><p>',
385
                            $app->form, $app->long_name
386
                        );
387
                    }
388
                }
389
            }
390
        );
391
    }
392
393
    form_start('submit.php');
394
    form_input_hidden('action', 'update_only_own');
395
    form_radio_buttons(
396
        'Jobs you submit can run', 'only_own',
397
        [
398
            [0, 'on any computer'],
399
            [1, 'only on your computers']
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
400
        ],
401
        $user->seti_id
402
    );
403
    form_submit('Update');
404
    form_end();
405
    page_tail();
406
}
407
408
// show batches of logged in user.
409
// They have manage access to these batches.
410
//
411
function handle_show_user_batches($user) {
412
    page_head("Your batches");
413
    $order = get_order();
414
    order_options('action=show_user_batches', $order);
415
    $batches = BoincBatch::enum("user_id = $user->id");
416
    show_batches($batches, $order, PAGE_SIZE, $user, null);
417
418
    page_tail();
419
}
420
421
function handle_update_only_own($user) {
422
    if (!parse_bool(get_config(), 'enable_assignment')) {
423
        error_page(
424
            'Job assignment is not enabled in the project config file.
425
            Please ask the project admins to enable it.'
426
        );
427
        return;
428
    }
429
    $val = get_int('only_own');
430
    $user->update("seti_id=$val");
431
    header("Location: submit.php");
432
}
433
434
// get list of app names of remote apps
435
//
436
function get_remote_app_names() {
437
    global $remote_apps;
438
    $x = [];
439
    foreach ($remote_apps as $category => $apps) {
440
        foreach ($apps as $app) {
441
            $x[] = $app->app_name;
442
        }
443
    }
444
    return array_unique($x);
445
}
446
447
// show links for everything the user has admin access to
448
//
449
function handle_admin($user) {
450
    $user_submit = BoincUserSubmit::lookup_userid($user->id);
451
    if (!$user_submit) error_page('no access');
452
    if ($user_submit->manage_all) {
453
        // user can administer all apps
454
        //
455
        page_head("Job submission: manage all apps");
456
        echo '<ul>';
457
        echo "<li> <a href=submit.php?action=admin_all>View/manage all batches</a>
458
        ";
459
        $app_names = get_remote_app_names();
460
        foreach ($app_names as $app_name) {
461
            $app_name = BoincDb::escape_string($app_name);
462
            $app = BoincApp::lookup("name='$app_name'");
463
            echo "
464
                <li>$app->user_friendly_name<br>
465
                <ul>
466
                <li><a href=submit.php?action=show_batches_admin_app&app_id=$app->id>View/manage batches</a>
467
            ";
468
            if ($app_name == 'buda') {
469
                echo "
470
                    <li> <a href=buda.php>Manage BUDA apps and variants</a>
471
                ";
472
            } else {
473
                echo "
474
                    <li> <a href=manage_app.php?app_id=$app->id&amp;action=app_version_form>Manage app versions</a>
475
                ";
476
            }
477
            echo "
478
                </ul>
479
            ";
480
        }
481
    } else {
482
        // see if user can administer specific apps
483
        //
484
        page_head("Job submission: manage apps");
485
        echo '<ul>';
486
        $usas = BoincUserSubmitApp::enum("user_id=$user->id");
487
        foreach ($usas as $usa) {
488
            $app = BoincApp::lookup_id($usa->app_id);
489
            echo "<li>$app->user_friendly_name<br>
490
                <a href=submit.php?action=show_batches_admin_app&app_id=$app->id>Batches</a>
491
            ";
492
            if ($usa->manage) {
493
                echo "&middot;
494
                    <a href=manage_app.php?app_id=$app->id&action=app_version_form>Versions</a>
495
                ";
496
            }
497
        }
498
    }
499
    echo "</ul>\n";
500
    page_tail();
501
}
502
503
// show all batches for given app to administrator
504
//
505
function show_batches_admin_app($user) {
506
    $app_id = get_int("app_id");
507
    $app = BoincApp::lookup_id($app_id);
508
    if (!$app) error_page("no such app");
509
    if (!has_manage_access($user, $app_id)) {
510
        error_page('no access');
511
    }
512
513
    $order = get_order();
514
515
    page_head("Manage batches for $app->user_friendly_name");
516
    order_options("action=show_batches_admin_app&app_id=$app_id", $order);
517
    $batches = BoincBatch::enum("app_id = $app_id");
518
    show_batches($batches, $order, PAGE_SIZE, null, $app);
519
    page_tail();
520
}
521
522
function handle_admin_all($user) {
523
    $order = get_order();
524
    page_head("Administer batches (all apps and users)");
525
    order_options("action=admin_all", $order);
526
    $batches = BoincBatch::enum('');
527
    show_batches($batches, $order, PAGE_SIZE, null, null);
528
    page_tail();
529
}
530
531
532
// show the statics of mem/disk usage of jobs in a batch
533
//
534
function handle_batch_stats($user) {
535
    $batch_id = get_int('batch_id');
536
    $batch = BoincBatch::lookup_id($batch_id);
537
    $results = BoincResult::enum_fields(
538
        'peak_working_set_size, peak_swap_size, peak_disk_usage',
539
        sprintf('batch = %d and outcome=%d',
540
            $batch->id, RESULT_OUTCOME_SUCCESS
541
        )
542
    );
543
    page_head("Statistics for batch $batch_id");
544
    $n = 0;
545
    $wss_sum = 0;
546
    $swap_sum = 0;
547
    $disk_sum = 0;
548
    $wss_max = 0;
549
    $swap_max = 0;
550
    $disk_max = 0;
551
    foreach ($results as $r) {
552
        // pre-7.3.16 clients don't report usage info
553
        //
554
        if ($r->peak_working_set_size == 0) {
555
            continue;
556
        }
557
        $n++;
558
        $wss_sum += $r->peak_working_set_size;
559
        if ($r->peak_working_set_size > $wss_max) {
560
            $wss_max = $r->peak_working_set_size;
561
        }
562
        $swap_sum += $r->peak_swap_size;
563
        if ($r->peak_swap_size > $swap_max) {
564
            $swap_max = $r->peak_swap_size;
565
        }
566
        $disk_sum += $r->peak_disk_usage;
567
        if ($r->peak_disk_usage > $disk_max) {
568
            $disk_max = $r->peak_disk_usage;
569
        }
570
    }
571
    if ($n == 0) {
572
        echo "No qualifying results.";
573
        page_tail();
574
        return;
575
    }
576
    text_start(800);
577
    start_table('table-striped');
578
    row2("qualifying results", $n);
579
    row2("mean WSS", size_string($wss_sum/$n));
580
    row2("max WSS", size_string($wss_max));
581
    row2("mean swap", size_string($swap_sum/$n));
582
    row2("max swap", size_string($swap_max));
583
    row2("mean disk usage", size_string($disk_sum/$n));
584
    row2("max disk usage", size_string($disk_max));
585
    end_table();
586
    text_end();
587
    page_tail();
588
}
589
590
define('COLOR_SUCCESS', 'green');
591
define('COLOR_FAIL', 'red');
592
define('COLOR_IN_PROGRESS', 'deepskyblue');
593
define('COLOR_UNSENT', 'gray');
594
595
// return HTML for a color-coded batch progress bar
596
//
597
function progress_bar($batch, $wus, $width) {
598
    $nsuccess = $batch->njobs_success;
599
    $nerror = $batch->nerror_jobs;
600
    $nin_prog = $batch->njobs_in_prog;
601
    $nunsent = $batch->njobs - $nsuccess - $nerror - $nin_prog;
602
    $w_success = $width*$nsuccess/$batch->njobs;
603
    $w_fail = $width*$nerror/$batch->njobs;
604
    $w_prog = $width*$nin_prog/$batch->njobs;
605
    $w_unsent = $width*$nunsent/$batch->njobs;
606
    $x = '<table height=20><tr>';
607
    if ($w_fail) {
608
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_fail, COLOR_FAIL);
609
    }
610
    if ($w_success) {
611
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_success, COLOR_SUCCESS);
612
    }
613
    if ($w_prog) {
614
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_prog, COLOR_IN_PROGRESS);
615
    }
616
    if ($w_unsent) {
617
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_unsent, COLOR_UNSENT);
618
    }
619
    $x .= sprintf('</tr></table>
620
        <strong>
621
        <font color=%s>%d failed</font> &middot;
622
        <font color=%s>%d completed</font> &middot;
623
        <font color=%s>%d in progress</font> &middot;
624
        <font color=%s>%d unsent</font>
625
        </strong>',
626
        COLOR_FAIL, $nerror,
627
        COLOR_SUCCESS, $nsuccess,
628
        COLOR_IN_PROGRESS, $nin_prog,
629
        COLOR_UNSENT, $nunsent
630
    );
631
    return $x;
632
}
633
634
// show the details of an existing batch.
635
// $user has access to abort/retire the batch
636
// and to get its output files
637
//
638
function handle_query_batch($user) {
639
    $batch_id = get_int('batch_id');
640
    $status = get_int('status', true);
641
    $batch = BoincBatch::lookup_id($batch_id);
642
    if (!$batch) error_page('no batch');
643
    $app = BoincApp::lookup_id($batch->app_id);
644
    $wus = BoincWorkunit::enum_fields(
645
        'id, name, rsc_fpops_est, canonical_credit, canonical_resultid, error_mask',
646
        "batch = $batch->id"
647
    );
648
    $batch = get_batch_params($batch, $wus);
649
    if ($batch->user_id == $user->id) {
650
        $owner = $user;
651
    } else {
652
        $owner = BoincUser::lookup_id($batch->user_id);
653
    }
654
655
    $is_assim_move = is_assim_move($app);
656
657
    page_head("Batch $batch_id");
658
    text_start(800);
659
    start_table();
660
    row2("name", $batch->name);
661
    if ($batch->description) {
662
        row2('description', $batch->description);
663
    }
664
    if ($owner) {
665
        row2('submitter', $owner->name);
666
    }
667
    row2("application", $app?$app->name:'---');
668
    row2("state", batch_state_string($batch->state));
669
    //row2("# jobs", $batch->njobs);
670
    //row2("# error jobs", $batch->nerror_jobs);
671
    //row2("logical end time", time_str($batch->logical_end_time));
672
    if ($batch->expire_time) {
673
        row2("expiration time", time_str($batch->expire_time));
674
    }
675
    if ($batch->njobs) {
676
        row2('progress', progress_bar($batch, $wus, 600));
677
    }
678
    if ($batch->completion_time) {
679
        row2("completed", local_time_str($batch->completion_time));
680
    }
681
    row2("GFLOP/hours, estimated", number_format(credit_to_gflop_hours($batch->credit_estimate), 2));
682
    row2("GFLOP/hours, actual", number_format(credit_to_gflop_hours($batch->credit_canonical), 2));
683
    if (!$is_assim_move) {
684
        row2("Total size of output files",
685
            size_string(batch_output_file_size($batch->id))
686
        );
687
    }
688
    end_table();
689
    echo "<p>";
690
691
    if ($is_assim_move) {
692
        $url = "get_output3.php?action=get_batch&batch_id=$batch->id";
693
    } else {
694
        $url = "get_output2.php?cmd=batch&batch_id=$batch->id";
695
    }
696
    echo "<p>";
697
    show_button($url, "Get zipped output files");
698
    echo "<p>";
699
    switch ($batch->state) {
700
    case BATCH_STATE_IN_PROGRESS:
701
        show_button(
702
            "submit.php?action=abort_batch&batch_id=$batch_id",
703
            "Abort batch"
704
        );
705
        break;
706
    case BATCH_STATE_COMPLETE:
707
    case BATCH_STATE_ABORTED:
708
        show_button(
709
            "submit.php?action=retire_batch&batch_id=$batch_id",
710
            "Retire batch"
711
        );
712
        break;
713
    }
714
    echo "<p>
715
        <h3>Completed jobs</h3>
716
        <ul>
717
        <li>
718
        <a href=submit_stats.php?action=flops_graph&batch_id=$batch_id>Job runtimes</a>
719
        <li>
720
        <a href=submit.php?action=batch_stats&batch_id=$batch_id>Memory/disk usage</a>
721
        <li>
722
        <a href=submit_stats.php?action=show_hosts&batch_id=$batch_id>Grouped by host</a>
723
        </ul>
724
        <h3>Failed jobs</h3>
725
        <ul>
726
        <li>
727
        <a href=submit_stats.php?action=err_host&batch_id=$batch_id>Grouped by host</a>
728
        <li>
729
        <a href=submit_stats.php?action=err_code&batch_id=$batch_id>Grouped by exit code</a>
730
        </ul>
731
    ";
732
733
    echo "<h2>Jobs</h2>\n";
734
    $url = "submit.php?action=query_batch&batch_id=$batch_id";
735
    echo "Show: ";
736
    echo sprintf('
737
        <a href=%s&status=%d>failed</a> &middot;
738
        <a href=%s&status=%d>completed</a> &middot;
739
        <a href=%s&status=%d>in progress</a> &middot;
740
        <a href=%s&status=%d>unsent</a> &middot;
741
        <a href=%s>all</a>
742
        <p>',
743
        $url, WU_ERROR,
744
        $url, WU_SUCCESS,
745
        $url, WU_IN_PROGRESS,
746
        $url, WU_UNSENT,
747
        $url
748
    );
749
750
    start_table();
751
    $x = [
752
        "Name <br><small>click for details</small>",
753
        "status",
754
        "GFLOPS-hours"
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
755
    ];
756
    row_heading_array($x);
757
    foreach($wus as $wu) {
758
        if ($status && $wu->status != $status) continue;
759
        $y = '';
760
        $c = '---';
761
        switch($wu->status) {
762
        case WU_SUCCESS:
763
            $resultid = $wu->canonical_resultid;
764
            $y = sprintf('<font color="%s">completed</font>', COLOR_SUCCESS);
765
            $c = number_format(
766
                credit_to_gflop_hours($wu->canonical_credit), 2
767
            );
768
            break;
769
        case WU_ERROR:
770
            $y = sprintf('<font color="%s">failed</font>', COLOR_FAIL);
771
            break;
772
        case WU_IN_PROGRESS:
773
            $y = sprintf('<font color="%s">in progress</font>', COLOR_IN_PROGRESS);
774
            break;
775
        case WU_UNSENT:
776
            $y = sprintf('<font color="%s">unsent</font>', COLOR_UNSENT);
777
            break;
778
        }
779
        $x = [
780
            "<a href=submit.php?action=query_job&wuid=$wu->id>$wu->name</a>",
781
            $y,
782
            $c
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
783
        ];
784
        row_array($x);
785
    }
786
    end_table();
787
    return_link();
788
    text_end();
789
    page_tail();
790
}
791
792
// Does the assimilator for the given app move output files
793
// to a results/<batchid>/ directory?
794
// This info is stored in the $remote_apps data structure in project.inc
795
//
796
function is_assim_move($app) {
797
    global $remote_apps;
798
    foreach ($remote_apps as $category => $apps) {
799
        foreach ($apps as $web_app) {
800
            if ($web_app->app_name == $app->name) {
801
                return $web_app->is_assim_move;
802
            }
803
        }
804
    }
805
    return false;
806
}
807
808
// show the details of a job, including links to see the output files
809
//
810
function handle_query_job($user) {
811
    $wuid = get_int('wuid');
812
    $wu = BoincWorkunit::lookup_id($wuid);
813
    if (!$wu) error_page("no such job");
814
815
    $app = BoincApp::lookup_id($wu->appid);
816
    $is_assim_move = is_assim_move($app);
817
818
    page_head("Job '$wu->name'");
819
    text_start(800);
820
821
    echo "
822
        <ul>
823
        <li><a href=workunit.php?wuid=$wuid>Job details</a>
824
        <p>
825
        <li><a href=submit.php?action=query_batch&batch_id=$wu->batch>Batch details</a>
826
        </ul>
827
    ";
828
    $d = "<foo>$wu->xml_doc</foo>";
829
    $x = simplexml_load_string($d);
830
    $x = $x->workunit;
831
    //echo "foo: $x->command_line";
832
833
    echo "<h2>Job instances</h2>\n";
834
    start_table('table-striped');
835
    table_header(
836
        "ID<br><small>click for details and stderr</small>",
837
        "State",
838
        "Output files"
839
    );
840
    $results = BoincResult::enum("workunitid=$wuid");
841
    $upload_dir = parse_config(get_config(), "<upload_dir>");
842
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
843
    foreach ($results as $result) {
844
        $x = [
845
            "<a href=result.php?resultid=$result->id>$result->id</a>",
846
            state_string($result)
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
847
        ];
848
        if ($is_assim_move) {
849
            if ($result->id == $wu->canonical_resultid) {
850
                $log_names = get_outfile_log_names($result);
851
                $nfiles = count($log_names);
852
                for ($i=0; $i<$nfiles; $i++) {
853
                    $name = $log_names[$i];
854
                    // don't show 'view' link if it's a .zip
855
                    $y = "$name: ";
856
                    if (!strstr($name, '.zip')) {
857
                        $y .= sprintf(
858
                            '<a href=get_output3.php?action=get_file&result_id=%d&index=%d>view</a> &middot; ',
859
                            $result->id, $i
860
                        );
861
                    }
862
                    $y .= sprintf(
863
                        '<a href=get_output3.php?action=get_file&result_id=%d&index=%d&download=1>download</a>',
864
                        $result->id, $i
865
                    );
866
                    $x[] = $y;
867
                }
868
            } else {
869
                $x[] = '---';
870
            }
871
        } else {
872
            if ($result->server_state == RESULT_SERVER_STATE_OVER) {
873
                $phys_names = get_outfile_phys_names($result);
874
                $log_names = get_outfile_log_names($result);
875
                $nfiles = count($log_names);
876
                for ($i=0; $i<$nfiles; $i++) {
877
                    $path = dir_hier_path(
878
                        $phys_names[$i], $upload_dir, $fanout
879
                    );
880
                    if (file_exists($path)) {
881
                        $url = sprintf(
882
                            'get_output2.php?cmd=result&result_id=%d&file_num=%d',
883
                            $result->id, $i
884
                        );
885
                        $s = stat($path);
886
                        $size = $s['size'];
887
                        $x[] = sprintf('<a href=%s>%s</a> (%s bytes)<br/>',
888
                            $url,
889
                            $log_names[$i],
890
                            number_format($size)
891
                        );
892
                    } else {
893
                        $x[] = sprintf("file '%s' is missing", $log_names[$i]);
894
                    }
895
                }
896
            } else {
897
                $x[] = '---';
898
            }
899
        }
900
        row_array($x);
901
    }
902
    end_table();
903
904
    // show input files
905
    //
906
    echo "<h2>Input files</h2>\n";
907
    $x = "<in>".$wu->xml_doc."</in>";
908
    $x = simplexml_load_string($x);
909
    if ($x->workunit->file_ref) {
910
        start_table('table-striped');
911
        table_header("Name<br><small>(click to view)</small>", "Size (bytes)");
912
        foreach ($x->workunit->file_ref as $fr) {
913
            $pname = (string)$fr->file_name;
914
            $lname = (string)$fr->open_name;
915
            foreach ($x->file_info as $fi) {
916
                if ((string)$fi->name == $pname) {
917
                    table_row(
918
                        "<a href=$fi->url>$lname</a>",
919
                        $fi->nbytes
920
                    );
921
                    break;
922
                }
923
            }
924
        }
925
        end_table();
926
    } else {
927
        echo "The job has no input files.<p>";
928
    }
929
930
    text_end();
931
    return_link();
932
    page_tail();
933
}
934
935
// is user allowed to retire or abort this batch?
936
//
937
function has_access($user, $batch) {
938
    if ($user->id == $batch->user_id) return true;
939
    $user_submit = BoincUserSubmit::lookup_userid($user->id);
940
    if ($user_submit->manage_all) return true;
941
    $usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$batch->app_id");
942
    if ($usa->manage) return true;
943
    return false;
944
}
945
946
function handle_abort_batch($user) {
947
    $batch_id = get_int('batch_id');
948
    $batch = BoincBatch::lookup_id($batch_id);
949
    if (!$batch) error_page("no such batch");
950
    if (!has_access($user, $batch)) {
951
        error_page("no access");
952
    }
953
954
    if (get_int('confirmed', true)) {
955
        abort_batch($batch);
956
        page_head("Batch $batch_id aborted");
957
        return_link();
958
        page_tail();
959
    } else {
960
        page_head("Confirm abort batch");
961
        echo "
962
            Aborting a batch will cancel all unstarted jobs.
963
            Are you sure you want to do this?
964
            <p>
965
        ";
966
        show_button(
967
            "submit.php?action=abort_batch&batch_id=$batch_id&confirmed=1",
968
            "Yes - abort batch"
969
        );
970
        return_link();
971
        page_tail();
972
    }
973
}
974
975
function handle_retire_batch($user) {
976
    $batch_id = get_int('batch_id');
977
    $batch = BoincBatch::lookup_id($batch_id);
978
    if (!$batch) error_page("no such batch");
979
    if (!has_access($user, $batch)) {
980
        error_page("no access");
981
    }
982
983
    if (get_int('confirmed', true)) {
984
        retire_batch($batch);
985
        page_head("Batch $batch_id retired");
986
        return_link();
987
        page_tail();
988
    } else {
989
        page_head("Confirm retire batch");
990
        echo "
991
            Retiring a batch will remove all of its output files.
992
            Are you sure you want to do this?
993
            <p>
994
        ";
995
        show_button(
996
            "submit.php?action=retire_batch&batch_id=$batch_id&confirmed=1",
997
            "Yes - retire batch"
998
        );
999
        return_link();
1000
        page_tail();
1001
    }
1002
}
1003
1004
// retire multiple batches
1005
//
1006
function handle_retire_multi($user) {
1007
    $batches = BoincBatch::enum(
1008
        sprintf('state in (%d, %d)', BATCH_STATE_COMPLETE, BATCH_STATE_ABORTED)
1009
    );
1010
    page_head('Retiring batches');
1011
    foreach ($batches as $batch) {
1012
        if (!has_access($user, $batch)) {
1013
            continue;
1014
        }
1015
        $x = sprintf('retire_%d', $batch->id);
1016
        if (get_str($x, true) == 'on') {
1017
            retire_batch($batch);
1018
            echo "<p>retired batch $batch->id ($batch->name)\n";
1019
        }
1020
    }
1021
    return_link();
1022
    page_tail();
1023
}
1024
1025
// given a list of batches, show the ones in a given state
1026
//
1027
function show_batches_in_state($batches, $state, $url_args, $order) {
1028
    switch ($state) {
1029
    case BATCH_STATE_IN_PROGRESS:
1030
        page_head("Batches in progress");
1031
        order_options($url_args, $order);
1032
        show_in_progress($batches, $order, 0, null, null);
1033
        break;
1034
    case BATCH_STATE_COMPLETE:
1035
        page_head("Completed batches");
1036
        order_options($url_args, $order);
1037
        show_complete($batches, $order, 0, null, null);
1038
        break;
1039
    case BATCH_STATE_ABORTED:
1040
        page_head("Aborted batches");
1041
        order_options($url_args, $order);
1042
        show_aborted($batches, $order, 0, null, null);
1043
        break;
1044
    }
1045
    page_tail();
1046
}
1047
1048
// show all batches visible to user, possibly limited by user/app/state
1049
function handle_show_all_batches($user) {
1050
    $userid = get_int("userid");
1051
    $appid = get_int("appid");
1052
    $state = get_int("state");
1053
    $order = get_order();
1054
    $url_args = "action=show_all_batches&state=$state&userid=$userid&appid=$appid";
1055
    if ($userid) {
1056
        // user looking at their own batches
1057
        //
1058
        if ($userid != $user->id) error_page("wrong user");
1059
        $batches = BoincBatch::enum("user_id=$user->id and state=$state");
1060
        fill_in_app_and_user_names($batches);
1061
        $batches = get_batches_params($batches);
1062
        show_batches_in_state($batches, $state, $url_args, $order);
1063
    } else {
1064
        // admin looking at batches
1065
        //
1066
        if (!has_manage_access($user, $appid)) {
1067
            error_page('no access');
1068
        }
1069
        if ($appid) {
1070
            $app = BoincApp::lookup_id($appid);
1071
            if (!$app) error_page("no such app");
1072
            $batches = BoincBatch::enum("app_id=$appid and state=$state");
1073
        } else {
1074
            $batches = BoincBatch::enum("state=$state");
1075
        }
1076
        fill_in_app_and_user_names($batches);
1077
        $batches = get_batches_params($batches);
1078
        show_batches_in_state($batches, $state, $url_args, $order);
1079
    }
1080
}
1081
1082
$user = get_logged_in_user();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $user is correct as get_logged_in_user() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1083
1084
$action = get_str('action', true);
1085
1086
switch ($action) {
1087
1088
// links to job submission forms
1089
case '': show_submit_links($user); break;
1090
1091
// show lists of batches
1092
case 'show_all_batches': handle_show_all_batches($user); break;
1093
case 'show_user_batches': handle_show_user_batches($user); break;
1094
case 'show_batches_admin_app': show_batches_admin_app($user); break;
1095
case 'admin_all': handle_admin_all($user); break;
1096
1097
// show info about a batch or job
1098
case 'batch_stats': handle_batch_stats($user); break;
1099
case 'query_batch': handle_query_batch($user); break;
1100
case 'query_job': handle_query_job($user); break;
1101
1102
// operations on batches
1103
case 'retire_batch': handle_retire_batch($user); break;
1104
case 'retire_multi': handle_retire_multi($user); break;
1105
case 'abort_batch': handle_abort_batch($user); break;
1106
1107
// access control
1108
case 'admin': handle_admin($user); break;
1109
1110
// 'run jobs only on my computers' flag (stored in user.seti_id)
1111
case 'update_only_own': handle_update_only_own($user); break;
1112
1113
default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
1114
    error_page("no such action $action");
1115
}
1116
1117
?>
1118