16
16
# See the License for the specific language governing permissions and
17
17
# limitations under the License
18
18
19
+ import datetime
19
20
import uuid
20
21
21
22
import attrs
@@ -242,8 +243,9 @@ def post_process_execution(
242
243
def get_jobs (
243
244
self ,
244
245
processID : list [str ] | None = fastapi .Query (None ),
245
- status : list [ogc_api_processes_fastapi .models .StatusCode ]
246
- | None = fastapi .Query (None ),
246
+ status : (
247
+ list [ogc_api_processes_fastapi .models .StatusCode ] | None
248
+ ) = fastapi .Query (None ),
247
249
limit : int | None = fastapi .Query (10 , ge = 1 , le = 10000 ),
248
250
sortby : utils .JobSortCriterion | None = fastapi .Query (
249
251
utils .JobSortCriterion .created_at_desc
@@ -317,34 +319,38 @@ def get_jobs(
317
319
** job_filters ,
318
320
)
319
321
job_entries = compute_session .scalars (statement ).all ()
320
- if back :
321
- job_entries = reversed (job_entries )
322
- jobs = []
323
- catalogue_sessionmaker = db_utils .get_catalogue_sessionmaker (
324
- db_utils .ConnectionMode .read
325
- )
326
- for job in job_entries :
327
- with catalogue_sessionmaker () as catalogue_session :
328
- try :
329
- (dataset_title ,) = utils .get_resource_properties (
330
- resource_id = job .process_id ,
331
- properties = "title" ,
332
- table = self .process_table ,
333
- session = catalogue_session ,
322
+ if back :
323
+ job_entries = reversed (job_entries )
324
+ jobs = []
325
+ catalogue_sessionmaker = db_utils .get_catalogue_sessionmaker (
326
+ db_utils .ConnectionMode .read
327
+ )
328
+ for job in job_entries :
329
+ with catalogue_sessionmaker () as catalogue_session :
330
+ try :
331
+ (dataset_title ,) = utils .get_resource_properties (
332
+ resource_id = job .process_id ,
333
+ properties = "title" ,
334
+ table = self .process_table ,
335
+ session = catalogue_session ,
336
+ )
337
+ except ogc_api_processes_fastapi .exceptions .NoSuchProcess :
338
+ dataset_title = config .ensure_settings ().missing_dataset_title
339
+ results = utils .parse_results_from_broker_db (
340
+ job , session = compute_session
341
+ )
342
+ jobs .append (
343
+ utils .make_status_info (
344
+ job = job ,
345
+ results = results ,
346
+ dataset_metadata = {"title" : dataset_title },
347
+ qos = {
348
+ "status" : cads_broker .database .get_qos_status_from_request (
349
+ job
350
+ )
351
+ },
334
352
)
335
- except ogc_api_processes_fastapi .exceptions .NoSuchProcess :
336
- dataset_title = config .ensure_settings ().missing_dataset_title
337
- results = utils .parse_results_from_broker_db (job )
338
- jobs .append (
339
- utils .make_status_info (
340
- job = job ,
341
- results = results ,
342
- dataset_metadata = {"title" : dataset_title },
343
- qos = {
344
- "status" : cads_broker .database .get_qos_status_from_request (job )
345
- },
346
353
)
347
- )
348
354
job_list = models .JobList (
349
355
jobs = jobs ,
350
356
links = [ogc_api_processes_fastapi .models .Link (href = "" )],
@@ -367,6 +373,9 @@ def get_job(
367
373
qos : bool = fastapi .Query (False ),
368
374
request : bool = fastapi .Query (False ),
369
375
log : bool = fastapi .Query (False ),
376
+ log_start_time : datetime .datetime | None = fastapi .Query (
377
+ None , alias = "logStartTime"
378
+ ),
370
379
) -> models .StatusInfo :
371
380
"""Implement OGC API - Processes `GET /jobs/{job_id}` endpoint.
372
381
@@ -386,6 +395,8 @@ def get_job(
386
395
Whether to include the request in the response
387
396
log : bool, optional
388
397
Whether to include the job's log in the response
398
+ log_start_time: datetime.datetime, optional
399
+ Datetime of the first log message to be returned
389
400
390
401
Returns
391
402
-------
@@ -403,7 +414,16 @@ def get_job(
403
414
job_id = job_id , session = compute_session
404
415
)
405
416
if qos :
406
- job_qos_info = utils .collect_job_qos_info (job , compute_session )
417
+ job_qos_info = utils .get_job_qos_info (job , compute_session )
418
+ # These lines are inside the session context because the related fields
419
+ # are lazy loaded
420
+ if log :
421
+ job_log = utils .get_job_events (
422
+ job ,
423
+ compute_session ,
424
+ "user_visible_log" ,
425
+ log_start_time ,
426
+ )
407
427
except ogc_api_processes_fastapi .exceptions .NoSuchJob :
408
428
compute_sessionmaker = db_utils .get_compute_sessionmaker (
409
429
mode = db_utils .ConnectionMode .write
@@ -413,7 +433,16 @@ def get_job(
413
433
job_id = job_id , session = compute_session
414
434
)
415
435
if qos :
416
- job_qos_info = utils .collect_job_qos_info (job , compute_session )
436
+ job_qos_info = utils .get_job_qos_info (job , compute_session )
437
+ # These lines are inside the session context because the related fields
438
+ # are lazy loaded
439
+ if log :
440
+ job_log = utils .get_job_events (
441
+ job ,
442
+ compute_session ,
443
+ "user_visible_log" ,
444
+ log_start_time ,
445
+ )
417
446
if job .portal not in portals :
418
447
raise ogc_api_processes_fastapi .exceptions .NoSuchJob (
419
448
detail = f"job { job_id } not found"
@@ -438,13 +467,15 @@ def get_job(
438
467
request_ids , form_data
439
468
),
440
469
}
470
+ if log :
471
+ kwargs ["log" ] = [
472
+ (message [0 ].isoformat (), message [1 ]) for message in job_log
473
+ ]
441
474
if qos :
442
475
kwargs ["qos" ] = {
443
476
** job_qos_info ,
444
477
"status" : cads_broker .database .get_qos_status_from_request (job ),
445
478
}
446
- if log :
447
- kwargs ["log" ] = utils .extract_job_log (job )
448
479
status_info = utils .make_status_info (job = job , ** kwargs )
449
480
return status_info
450
481
@@ -483,8 +514,8 @@ def get_job_results(
483
514
job = utils .get_job_from_broker_db (
484
515
job_id = job_id , session = compute_session
485
516
)
517
+ results = utils .get_results_from_job (job = job , session = compute_session )
486
518
auth .verify_permission (user_uid , job )
487
- results = utils .get_results_from_job (job = job )
488
519
except (
489
520
ogc_api_processes_fastapi .exceptions .NoSuchJob ,
490
521
ogc_api_processes_fastapi .exceptions .ResultsNotReady ,
@@ -496,8 +527,8 @@ def get_job_results(
496
527
job = utils .get_job_from_broker_db (
497
528
job_id = job_id , session = compute_session
498
529
)
530
+ results = utils .get_results_from_job (job = job , session = compute_session )
499
531
auth .verify_permission (user_uid , job )
500
- results = utils .get_results_from_job (job = job )
501
532
handle_download_metrics (job .process_id , results )
502
533
return results
503
534
0 commit comments