handle_query_batch()   F
last analyzed

Complexity

Conditions 21
Paths 14336

Size

Total Lines 152
Code Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
cc 21
eloc 106
c 7
b 2
f 0
nc 14336
nop 1
dl 0
loc 152
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
// TODO: speed this up by making list of app and user IDs
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...
315
// and doing lookup just once.
316
//
317
function fill_in_app_and_user_names(&$batches) {
318
    $apps = [];
319
    foreach ($batches as $batch) {
320
        if (array_key_exists($batch->app_id, $apps)) {
321
            $app = $apps[$batch->app_id];
322
        } else {
323
            $app = BoincApp::lookup_id($batch->app_id);
324
            $apps[$batch->app_id] = $app;
325
        }
326
        if ($app) {
327
            $batch->app_name = $app->name;
328
            if ($batch->description) {
329
                $batch->app_name .= ": $batch->description";
330
            }
331
        } else {
332
            $batch->app_name = "unknown";
333
        }
334
        $user = BoincUser::lookup_id($batch->user_id);
335
        if ($user) {
336
            $batch->user_name = $user->name;
337
        } else {
338
            $batch->user_name = "missing user $batch->user_id";
339
        }
340
    }
341
}
342
343
// show a set of batches: in progress, then completed, then aborted
344
//
345
function show_batches($batches, $order, $limit, $user, $app) {
346
    fill_in_app_and_user_names($batches);
347
    $batches = get_batches_params($batches);
348
    show_in_progress($batches, $order, $limit, $user, $app);
349
    show_complete($batches, $order, $limit, $user, $app);
350
    show_aborted($batches, $order, $limit, $user, $app);
351
}
352
353
// show links to per-app job submission forms
354
//
355
function show_submit_links($user) {
356
    global $remote_apps;
357
    $user_submit = BoincUserSubmit::lookup_userid($user->id);
358
    if (!$user_submit) {
359
        error_page("Ask the project admins for permission to submit jobs");
360
    }
361
362
    page_head("Submit jobs");
363
364
    // show links to per-app job submission pages
365
    //
366
    foreach ($remote_apps as $area => $apps) {
367
        panel($area,
368
            function() use ($apps) {
369
                foreach ($apps as $app) {
370
                    if (empty($app->form)) continue;
371
                    // show app logo if available
372
                    if (!empty($app->logo)) {
373
                        echo sprintf(
374
                            '<a href=%s><img width=100 src=%s></a>&nbsp;',
375
                            $app->form, $app->logo
376
                        );
377
                    } else {
378
                        echo sprintf(
379
                            '<a href=%s>%s</a><p>',
380
                            $app->form, $app->long_name
381
                        );
382
                    }
383
                }
384
            }
385
        );
386
    }
387
388
    form_start('submit.php');
389
    form_input_hidden('action', 'update_only_own');
390
    form_radio_buttons(
391
        'Jobs you submit can run', 'only_own',
392
        [
393
            [0, 'on any computer'],
394
            [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...
395
        ],
396
        $user->seti_id
397
    );
398
    form_submit('Update');
399
    form_end();
400
    page_tail();
401
}
402
403
// show batches of logged in user.
404
// They have manage access to these batches.
405
//
406
function handle_show_user_batches($user) {
407
    page_head("Your batches");
408
    $order = get_order();
409
    order_options('action=show_user_batches', $order);
410
    $batches = BoincBatch::enum("user_id = $user->id");
411
    show_batches($batches, $order, PAGE_SIZE, $user, null);
412
413
    page_tail();
414
}
415
416
function handle_update_only_own($user) {
417
    if (!parse_bool(get_config(), 'enable_assignment')) {
418
        error_page(
419
            'Job assignment is not enabled in the project config file.
420
            Please ask the project admins to enable it.'
421
        );
422
        return;
423
    }
424
    $val = get_int('only_own');
425
    $user->update("seti_id=$val");
426
    header("Location: submit.php");
427
}
428
429
// get list of app names of remote apps
430
//
431
function get_remote_app_names() {
432
    global $remote_apps;
433
    $x = [];
434
    foreach ($remote_apps as $category => $apps) {
435
        foreach ($apps as $app) {
436
            $x[] = $app->app_name;
437
        }
438
    }
439
    return array_unique($x);
440
}
441
442
// show links for everything the user has admin access to
443
//
444
function handle_admin($user) {
445
    $user_submit = BoincUserSubmit::lookup_userid($user->id);
446
    if (!$user_submit) error_page('no access');
447
    if ($user_submit->manage_all) {
448
        // user can administer all apps
449
        //
450
        page_head("Job submission: manage all apps");
451
        echo '<ul>';
452
        echo "<li> <a href=submit.php?action=admin_all>View/manage all batches</a>
453
        ";
454
        $app_names = get_remote_app_names();
455
        foreach ($app_names as $app_name) {
456
            $app_name = BoincDb::escape_string($app_name);
457
            $app = BoincApp::lookup("name='$app_name'");
458
            echo "
459
                <li>$app->user_friendly_name<br>
460
                <ul>
461
                <li><a href=submit.php?action=show_batches_admin_app&app_id=$app->id>View/manage batches</a>
462
            ";
463
            if ($app_name == 'buda') {
464
                echo "
465
                    <li> <a href=buda.php>Manage BUDA apps and variants</a>
466
                ";
467
            } else {
468
                echo "
469
                    <li> <a href=manage_app.php?app_id=$app->id&amp;action=app_version_form>Manage app versions</a>
470
                ";
471
            }
472
            echo "
473
                </ul>
474
            ";
475
        }
476
    } else {
477
        // see if user can administer specific apps
478
        //
479
        page_head("Job submission: manage apps");
480
        echo '<ul>';
481
        $usas = BoincUserSubmitApp::enum("user_id=$user->id");
482
        foreach ($usas as $usa) {
483
            $app = BoincApp::lookup_id($usa->app_id);
484
            echo "<li>$app->user_friendly_name<br>
485
                <a href=submit.php?action=show_batches_admin_app&app_id=$app->id>Batches</a>
486
            ";
487
            if ($usa->manage) {
488
                echo "&middot;
489
                    <a href=manage_app.php?app_id=$app->id&action=app_version_form>Versions</a>
490
                ";
491
            }
492
        }
493
    }
494
    echo "</ul>\n";
495
    page_tail();
496
}
497
498
// show all batches for given app to administrator
499
//
500
function show_batches_admin_app($user) {
501
    $app_id = get_int("app_id");
502
    $app = BoincApp::lookup_id($app_id);
503
    if (!$app) error_page("no such app");
504
    if (!has_manage_access($user, $app_id)) {
505
        error_page('no access');
506
    }
507
508
    $order = get_order();
509
510
    page_head("Manage batches for $app->user_friendly_name");
511
    order_options("action=show_batches_admin_app&app_id=$app_id", $order);
512
    $batches = BoincBatch::enum("app_id = $app_id");
513
    show_batches($batches, $order, PAGE_SIZE, null, $app);
514
    page_tail();
515
}
516
517
function handle_admin_all($user) {
518
    $order = get_order();
519
    page_head("Administer batches (all apps and users)");
520
    order_options("action=admin_all", $order);
521
    $batches = BoincBatch::enum('');
522
    show_batches($batches, $order, PAGE_SIZE, null, null);
523
    page_tail();
524
}
525
526
527
// show the statics of mem/disk usage of jobs in a batch
528
//
529
function handle_batch_stats($user) {
530
    $batch_id = get_int('batch_id');
531
    $batch = BoincBatch::lookup_id($batch_id);
532
    $results = BoincResult::enum_fields(
533
        'peak_working_set_size, peak_swap_size, peak_disk_usage',
534
        sprintf('batch = %d and outcome=%d',
535
            $batch->id, RESULT_OUTCOME_SUCCESS
536
        )
537
    );
538
    page_head("Statistics for batch $batch_id");
539
    $n = 0;
540
    $wss_sum = 0;
541
    $swap_sum = 0;
542
    $disk_sum = 0;
543
    $wss_max = 0;
544
    $swap_max = 0;
545
    $disk_max = 0;
546
    foreach ($results as $r) {
547
        // pre-7.3.16 clients don't report usage info
548
        //
549
        if ($r->peak_working_set_size == 0) {
550
            continue;
551
        }
552
        $n++;
553
        $wss_sum += $r->peak_working_set_size;
554
        if ($r->peak_working_set_size > $wss_max) {
555
            $wss_max = $r->peak_working_set_size;
556
        }
557
        $swap_sum += $r->peak_swap_size;
558
        if ($r->peak_swap_size > $swap_max) {
559
            $swap_max = $r->peak_swap_size;
560
        }
561
        $disk_sum += $r->peak_disk_usage;
562
        if ($r->peak_disk_usage > $disk_max) {
563
            $disk_max = $r->peak_disk_usage;
564
        }
565
    }
566
    if ($n == 0) {
567
        echo "No qualifying results.";
568
        page_tail();
569
        return;
570
    }
571
    text_start(800);
572
    start_table('table-striped');
573
    row2("qualifying results", $n);
574
    row2("mean WSS", size_string($wss_sum/$n));
575
    row2("max WSS", size_string($wss_max));
576
    row2("mean swap", size_string($swap_sum/$n));
577
    row2("max swap", size_string($swap_max));
578
    row2("mean disk usage", size_string($disk_sum/$n));
579
    row2("max disk usage", size_string($disk_max));
580
    end_table();
581
    text_end();
582
    page_tail();
583
}
584
585
define('COLOR_SUCCESS', 'green');
586
define('COLOR_FAIL', 'red');
587
define('COLOR_IN_PROGRESS', 'deepskyblue');
588
define('COLOR_UNSENT', 'gray');
589
590
// return HTML for a color-coded batch progress bar
591
//
592
function progress_bar($batch, $wus, $width) {
593
    $nsuccess = $batch->njobs_success;
594
    $nerror = $batch->nerror_jobs;
595
    $nin_prog = $batch->njobs_in_prog;
596
    $nunsent = $batch->njobs - $nsuccess - $nerror - $nin_prog;
597
    $w_success = $width*$nsuccess/$batch->njobs;
598
    $w_fail = $width*$nerror/$batch->njobs;
599
    $w_prog = $width*$nin_prog/$batch->njobs;
600
    $w_unsent = $width*$nunsent/$batch->njobs;
601
    $x = '<table height=20><tr>';
602
    if ($w_fail) {
603
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_fail, COLOR_FAIL);
604
    }
605
    if ($w_success) {
606
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_success, COLOR_SUCCESS);
607
    }
608
    if ($w_prog) {
609
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_prog, COLOR_IN_PROGRESS);
610
    }
611
    if ($w_unsent) {
612
        $x .= sprintf('<td width=%d bgcolor=%s></td>', $w_unsent, COLOR_UNSENT);
613
    }
614
    $x .= sprintf('</tr></table>
615
        <strong>
616
        <font color=%s>%d failed</font> &middot;
617
        <font color=%s>%d completed</font> &middot;
618
        <font color=%s>%d in progress</font> &middot;
619
        <font color=%s>%d unsent</font>
620
        </strong>',
621
        COLOR_FAIL, $nerror,
622
        COLOR_SUCCESS, $nsuccess,
623
        COLOR_IN_PROGRESS, $nin_prog,
624
        COLOR_UNSENT, $nunsent
625
    );
626
    return $x;
627
}
628
629
// show the details of an existing batch.
630
// $user has access to abort/retire the batch
631
// and to get its output files
632
//
633
function handle_query_batch($user) {
634
    $batch_id = get_int('batch_id');
635
    $status = get_int('status', true);
636
    $batch = BoincBatch::lookup_id($batch_id);
637
    if (!$batch) error_page('no batch');
638
    $app = BoincApp::lookup_id($batch->app_id);
639
    $wus = BoincWorkunit::enum_fields(
640
        'id, name, rsc_fpops_est, canonical_credit, canonical_resultid, error_mask',
641
        "batch = $batch->id"
642
    );
643
    $batch = get_batch_params($batch, $wus);
644
    if ($batch->user_id == $user->id) {
645
        $owner = $user;
646
    } else {
647
        $owner = BoincUser::lookup_id($batch->user_id);
648
    }
649
650
    $is_assim_move = is_assim_move($app);
651
652
    page_head("Batch $batch_id");
653
    text_start(800);
654
    start_table();
655
    row2("name", $batch->name);
656
    if ($batch->description) {
657
        row2('description', $batch->description);
658
    }
659
    if ($owner) {
660
        row2('submitter', $owner->name);
661
    }
662
    row2("application", $app?$app->name:'---');
663
    row2("state", batch_state_string($batch->state));
664
    //row2("# jobs", $batch->njobs);
665
    //row2("# error jobs", $batch->nerror_jobs);
666
    //row2("logical end time", time_str($batch->logical_end_time));
667
    if ($batch->expire_time) {
668
        row2("expiration time", time_str($batch->expire_time));
669
    }
670
    if ($batch->njobs) {
671
        row2('progress', progress_bar($batch, $wus, 600));
672
    }
673
    if ($batch->completion_time) {
674
        row2("completed", local_time_str($batch->completion_time));
675
    }
676
    row2("GFLOP/hours, estimated", number_format(credit_to_gflop_hours($batch->credit_estimate), 2));
677
    row2("GFLOP/hours, actual", number_format(credit_to_gflop_hours($batch->credit_canonical), 2));
678
    if (!$is_assim_move) {
679
        row2("Total size of output files",
680
            size_string(batch_output_file_size($batch->id))
681
        );
682
    }
683
    end_table();
684
    echo "<p>";
685
686
    if ($is_assim_move) {
687
        $url = "get_output3.php?action=get_batch&batch_id=$batch->id";
688
    } else {
689
        $url = "get_output2.php?cmd=batch&batch_id=$batch->id";
690
    }
691
    echo "<p>";
692
    show_button($url, "Get zipped output files");
693
    echo "<p>";
694
    switch ($batch->state) {
695
    case BATCH_STATE_IN_PROGRESS:
696
        show_button(
697
            "submit.php?action=abort_batch&batch_id=$batch_id",
698
            "Abort batch"
699
        );
700
        break;
701
    case BATCH_STATE_COMPLETE:
702
    case BATCH_STATE_ABORTED:
703
        show_button(
704
            "submit.php?action=retire_batch&batch_id=$batch_id",
705
            "Retire batch"
706
        );
707
        break;
708
    }
709
    echo "<p>
710
        <h3>Completed jobs</h3>
711
        <ul>
712
        <li>
713
        <a href=submit_stats.php?action=flops_graph&batch_id=$batch_id>Job runtimes</a>
714
        <li>
715
        <a href=submit.php?action=batch_stats&batch_id=$batch_id>Memory/disk usage</a>
716
        <li>
717
        <a href=submit_stats.php?action=show_hosts&batch_id=$batch_id>Grouped by host</a>
718
        </ul>
719
        <h3>Failed jobs</h3>
720
        <ul>
721
        <li>
722
        <a href=submit_stats.php?action=err_host&batch_id=$batch_id>Grouped by host</a>
723
        <li>
724
        <a href=submit_stats.php?action=err_code&batch_id=$batch_id>Grouped by exit code</a>
725
        </ul>
726
    ";
727
728
    echo "<h2>Jobs</h2>\n";
729
    $url = "submit.php?action=query_batch&batch_id=$batch_id";
730
    echo "Show: ";
731
    echo sprintf('
732
        <a href=%s&status=%d>failed</a> &middot;
733
        <a href=%s&status=%d>completed</a> &middot;
734
        <a href=%s&status=%d>in progress</a> &middot;
735
        <a href=%s&status=%d>unsent</a> &middot;
736
        <a href=%s>all</a>
737
        <p>',
738
        $url, WU_ERROR,
739
        $url, WU_SUCCESS,
740
        $url, WU_IN_PROGRESS,
741
        $url, WU_UNSENT,
742
        $url
743
    );
744
745
    start_table();
746
    $x = [
747
        "Name <br><small>click for details</small>",
748
        "status",
749
        "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...
750
    ];
751
    row_heading_array($x);
752
    foreach($wus as $wu) {
753
        if ($status && $wu->status != $status) continue;
754
        $y = '';
755
        $c = '---';
756
        switch($wu->status) {
757
        case WU_SUCCESS:
758
            $resultid = $wu->canonical_resultid;
759
            $y = sprintf('<font color="%s">completed</font>', COLOR_SUCCESS);
760
            $c = number_format(
761
                credit_to_gflop_hours($wu->canonical_credit), 2
762
            );
763
            break;
764
        case WU_ERROR:
765
            $y = sprintf('<font color="%s">failed</font>', COLOR_FAIL);
766
            break;
767
        case WU_IN_PROGRESS:
768
            $y = sprintf('<font color="%s">in progress</font>', COLOR_IN_PROGRESS);
769
            break;
770
        case WU_UNSENT:
771
            $y = sprintf('<font color="%s">unsent</font>', COLOR_UNSENT);
772
            break;
773
        }
774
        $x = [
775
            "<a href=submit.php?action=query_job&wuid=$wu->id>$wu->name</a>",
776
            $y,
777
            $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...
778
        ];
779
        row_array($x);
780
    }
781
    end_table();
782
    return_link();
783
    text_end();
784
    page_tail();
785
}
786
787
// Does the assimilator for the given app move output files
788
// to a results/<batchid>/ directory?
789
// This info is stored in the $remote_apps data structure in project.inc
790
//
791
function is_assim_move($app) {
792
    global $remote_apps;
793
    foreach ($remote_apps as $category => $apps) {
794
        foreach ($apps as $web_app) {
795
            if ($web_app->app_name == $app->name) {
796
                return $web_app->is_assim_move;
797
            }
798
        }
799
    }
800
    return false;
801
}
802
803
// show the details of a job, including links to see the output files
804
//
805
function handle_query_job($user) {
806
    $wuid = get_int('wuid');
807
    $wu = BoincWorkunit::lookup_id($wuid);
808
    if (!$wu) error_page("no such job");
809
810
    $app = BoincApp::lookup_id($wu->appid);
811
    $is_assim_move = is_assim_move($app);
812
813
    page_head("Job '$wu->name'");
814
    text_start(800);
815
816
    echo "
817
        <ul>
818
        <li><a href=workunit.php?wuid=$wuid>Job details</a>
819
        <p>
820
        <li><a href=submit.php?action=query_batch&batch_id=$wu->batch>Batch details</a>
821
        </ul>
822
    ";
823
    $d = "<foo>$wu->xml_doc</foo>";
824
    $x = simplexml_load_string($d);
825
    $x = $x->workunit;
826
    //echo "foo: $x->command_line";
827
828
    echo "<h2>Job instances</h2>\n";
829
    start_table('table-striped');
830
    table_header(
831
        "ID<br><small>click for details and stderr</small>",
832
        "State",
833
        "Output files"
834
    );
835
    $results = BoincResult::enum("workunitid=$wuid");
836
    $upload_dir = parse_config(get_config(), "<upload_dir>");
837
    $fanout = parse_config(get_config(), "<uldl_dir_fanout>");
838
    foreach ($results as $result) {
839
        $x = [
840
            "<a href=result.php?resultid=$result->id>$result->id</a>",
841
            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...
842
        ];
843
        if ($is_assim_move) {
844
            if ($result->id == $wu->canonical_resultid) {
845
                $log_names = get_outfile_log_names($result);
846
                $nfiles = count($log_names);
847
                for ($i=0; $i<$nfiles; $i++) {
848
                    $name = $log_names[$i];
849
                    // don't show 'view' link if it's a .zip
850
                    $y = "$name: ";
851
                    if (!strstr($name, '.zip')) {
852
                        $y .= sprintf(
853
                            '<a href=get_output3.php?action=get_file&result_id=%d&index=%d>view</a> &middot; ',
854
                            $result->id, $i
855
                        );
856
                    }
857
                    $y .= sprintf(
858
                        '<a href=get_output3.php?action=get_file&result_id=%d&index=%d&download=1>download</a>',
859
                        $result->id, $i
860
                    );
861
                    $x[] = $y;
862
                }
863
            } else {
864
                $x[] = '---';
865
            }
866
        } else {
867
            if ($result->server_state == RESULT_SERVER_STATE_OVER) {
868
                $phys_names = get_outfile_phys_names($result);
869
                $log_names = get_outfile_log_names($result);
870
                $nfiles = count($log_names);
871
                for ($i=0; $i<$nfiles; $i++) {
872
                    $path = dir_hier_path(
873
                        $phys_names[$i], $upload_dir, $fanout
874
                    );
875
                    if (file_exists($path)) {
876
                        $url = sprintf(
877
                            'get_output2.php?cmd=result&result_id=%d&file_num=%d',
878
                            $result->id, $i
879
                        );
880
                        $s = stat($path);
881
                        $size = $s['size'];
882
                        $x[] = sprintf('<a href=%s>%s</a> (%s bytes)<br/>',
883
                            $url,
884
                            $log_names[$i],
885
                            number_format($size)
886
                        );
887
                    } else {
888
                        $x[] = sprintf("file '%s' is missing", $log_names[$i]);
889
                    }
890
                }
891
            } else {
892
                $x[] = '---';
893
            }
894
        }
895
        row_array($x);
896
    }
897
    end_table();
898
899
    // show input files
900
    //
901
    echo "<h2>Input files</h2>\n";
902
    $x = "<in>".$wu->xml_doc."</in>";
903
    $x = simplexml_load_string($x);
904
    if ($x->workunit->file_ref) {
905
        start_table('table-striped');
906
        table_header("Name<br><small>(click to view)</small>", "Size (bytes)");
907
        foreach ($x->workunit->file_ref as $fr) {
908
            $pname = (string)$fr->file_name;
909
            $lname = (string)$fr->open_name;
910
            foreach ($x->file_info as $fi) {
911
                if ((string)$fi->name == $pname) {
912
                    table_row(
913
                        "<a href=$fi->url>$lname</a>",
914
                        $fi->nbytes
915
                    );
916
                    break;
917
                }
918
            }
919
        }
920
        end_table();
921
    } else {
922
        echo "The job has no input files.<p>";
923
    }
924
925
    text_end();
926
    return_link();
927
    page_tail();
928
}
929
930
// is user allowed to retire or abort this batch?
931
//
932
function has_access($user, $batch) {
933
    if ($user->id == $batch->user_id) return true;
934
    $user_submit = BoincUserSubmit::lookup_userid($user->id);
935
    if ($user_submit->manage_all) return true;
936
    $usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$batch->app_id");
937
    if ($usa->manage) return true;
938
    return false;
939
}
940
941
function handle_abort_batch($user) {
942
    $batch_id = get_int('batch_id');
943
    $batch = BoincBatch::lookup_id($batch_id);
944
    if (!$batch) error_page("no such batch");
945
    if (!has_access($user, $batch)) {
946
        error_page("no access");
947
    }
948
949
    if (get_int('confirmed', true)) {
950
        abort_batch($batch);
951
        page_head("Batch $batch_id aborted");
952
        return_link();
953
        page_tail();
954
    } else {
955
        page_head("Confirm abort batch");
956
        echo "
957
            Aborting a batch will cancel all unstarted jobs.
958
            Are you sure you want to do this?
959
            <p>
960
        ";
961
        show_button(
962
            "submit.php?action=abort_batch&batch_id=$batch_id&confirmed=1",
963
            "Yes - abort batch"
964
        );
965
        return_link();
966
        page_tail();
967
    }
968
}
969
970
function handle_retire_batch($user) {
971
    $batch_id = get_int('batch_id');
972
    $batch = BoincBatch::lookup_id($batch_id);
973
    if (!$batch) error_page("no such batch");
974
    if (!has_access($user, $batch)) {
975
        error_page("no access");
976
    }
977
978
    if (get_int('confirmed', true)) {
979
        retire_batch($batch);
980
        page_head("Batch $batch_id retired");
981
        return_link();
982
        page_tail();
983
    } else {
984
        page_head("Confirm retire batch");
985
        echo "
986
            Retiring a batch will remove all of its output files.
987
            Are you sure you want to do this?
988
            <p>
989
        ";
990
        show_button(
991
            "submit.php?action=retire_batch&batch_id=$batch_id&confirmed=1",
992
            "Yes - retire batch"
993
        );
994
        return_link();
995
        page_tail();
996
    }
997
}
998
999
// retire multiple batches
1000
//
1001
function handle_retire_multi($user) {
1002
    $batches = BoincBatch::enum(
1003
        sprintf('state in (%d, %d)', BATCH_STATE_COMPLETE, BATCH_STATE_ABORTED)
1004
    );
1005
    page_head('Retiring batches');
1006
    foreach ($batches as $batch) {
1007
        if (!has_access($user, $batch)) {
1008
            continue;
1009
        }
1010
        $x = sprintf('retire_%d', $batch->id);
1011
        if (get_str($x, true) == 'on') {
1012
            retire_batch($batch);
1013
            echo "<p>retired batch $batch->id ($batch->name)\n";
1014
        }
1015
    }
1016
    return_link();
1017
    page_tail();
1018
}
1019
1020
// given a list of batches, show the ones in a given state
1021
//
1022
function show_batches_in_state($batches, $state, $url_args, $order) {
1023
    switch ($state) {
1024
    case BATCH_STATE_IN_PROGRESS:
1025
        page_head("Batches in progress");
1026
        order_options($url_args, $order);
1027
        show_in_progress($batches, $order, 0, null, null);
1028
        break;
1029
    case BATCH_STATE_COMPLETE:
1030
        page_head("Completed batches");
1031
        order_options($url_args, $order);
1032
        show_complete($batches, $order, 0, null, null);
1033
        break;
1034
    case BATCH_STATE_ABORTED:
1035
        page_head("Aborted batches");
1036
        order_options($url_args, $order);
1037
        show_aborted($batches, $order, 0, null, null);
1038
        break;
1039
    }
1040
    page_tail();
1041
}
1042
1043
// show all batches visible to user, possibly limited by user/app/state
1044
function handle_show_all_batches($user) {
1045
    $userid = get_int("userid");
1046
    $appid = get_int("appid");
1047
    $state = get_int("state");
1048
    $order = get_order();
1049
    $url_args = "action=show_all_batches&state=$state&userid=$userid&appid=$appid";
1050
    if ($userid) {
1051
        // user looking at their own batches
1052
        //
1053
        if ($userid != $user->id) error_page("wrong user");
1054
        $batches = BoincBatch::enum("user_id=$user->id and state=$state");
1055
        fill_in_app_and_user_names($batches);
1056
        show_batches_in_state($batches, $state, $url_args, $order);
1057
    } else {
1058
        // admin looking at batches
1059
        //
1060
        if (!has_manage_access($user, $appid)) {
1061
            error_page('no access');
1062
        }
1063
        if ($appid) {
1064
            $app = BoincApp::lookup_id($appid);
1065
            if (!$app) error_page("no such app");
1066
            $batches = BoincBatch::enum("app_id=$appid and state=$state");
1067
        } else {
1068
            $batches = BoincBatch::enum("state=$state");
1069
        }
1070
        fill_in_app_and_user_names($batches);
1071
        show_batches_in_state($batches, $state, $url_args, $order);
1072
    }
1073
}
1074
1075
$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...
1076
1077
$action = get_str('action', true);
1078
1079
switch ($action) {
1080
1081
// links to job submission forms
1082
case '': show_submit_links($user); break;
1083
1084
// show lists of batches
1085
case 'show_all_batches': handle_show_all_batches($user); break;
1086
case 'show_user_batches': handle_show_user_batches($user); break;
1087
case 'show_batches_admin_app': show_batches_admin_app($user); break;
1088
case 'admin_all': handle_admin_all($user); break;
1089
1090
// show info about a batch or job
1091
case 'batch_stats': handle_batch_stats($user); break;
1092
case 'query_batch': handle_query_batch($user); break;
1093
case 'query_job': handle_query_job($user); break;
1094
1095
// operations on batches
1096
case 'retire_batch': handle_retire_batch($user); break;
1097
case 'retire_multi': handle_retire_multi($user); break;
1098
case 'abort_batch': handle_abort_batch($user); break;
1099
1100
// access control
1101
case 'admin': handle_admin($user); break;
1102
1103
// 'run jobs only on my computers' flag (stored in user.seti_id)
1104
case 'update_only_own': handle_update_only_own($user); break;
1105
1106
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...
1107
    error_page("no such action $action");
1108
}
1109
1110
?>
1111