|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from fastapi import Depends, status |
| 20 | +from sqlalchemy import select |
| 21 | +from sqlalchemy.orm import Session |
| 22 | +from typing_extensions import Annotated |
| 23 | + |
| 24 | +from airflow.api_fastapi.common.db.common import ( |
| 25 | + get_session, |
| 26 | + paginated_select, |
| 27 | +) |
| 28 | +from airflow.api_fastapi.common.parameters import ( |
| 29 | + QueryLimit, |
| 30 | + QueryOffset, |
| 31 | + RangeFilter, |
| 32 | + SortParam, |
| 33 | + datetime_range_filter_factory, |
| 34 | +) |
| 35 | +from airflow.api_fastapi.common.router import AirflowRouter |
| 36 | +from airflow.api_fastapi.core_api.datamodels.job import ( |
| 37 | + JobCollectionResponse, |
| 38 | + JobResponse, |
| 39 | +) |
| 40 | +from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc |
| 41 | +from airflow.jobs.job import Job |
| 42 | +from airflow.utils.state import JobState |
| 43 | + |
| 44 | +job_router = AirflowRouter(tags=["Job"], prefix="/jobs") |
| 45 | + |
| 46 | + |
| 47 | +@job_router.get( |
| 48 | + "/", |
| 49 | + responses=create_openapi_http_exception_doc( |
| 50 | + [status.HTTP_400_BAD_REQUEST, status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN] |
| 51 | + ), |
| 52 | +) |
| 53 | +def get_jobs( |
| 54 | + start_date_range: Annotated[ |
| 55 | + RangeFilter, |
| 56 | + Depends(datetime_range_filter_factory("start_date", Job)), |
| 57 | + ], |
| 58 | + end_date_range: Annotated[ |
| 59 | + RangeFilter, |
| 60 | + Depends(datetime_range_filter_factory("end_date", Job)), |
| 61 | + ], |
| 62 | + limit: QueryLimit, |
| 63 | + offset: QueryOffset, |
| 64 | + order_by: Annotated[ |
| 65 | + SortParam, |
| 66 | + Depends( |
| 67 | + SortParam( |
| 68 | + [ |
| 69 | + "id", |
| 70 | + "dag_id", |
| 71 | + "state", |
| 72 | + "job_type", |
| 73 | + "start_date", |
| 74 | + "end_date", |
| 75 | + "latest_heartbeat", |
| 76 | + "executor_class", |
| 77 | + "hostname", |
| 78 | + "unixname", |
| 79 | + ], |
| 80 | + Job, |
| 81 | + ).dynamic_depends(default="id") |
| 82 | + ), |
| 83 | + ], |
| 84 | + session: Annotated[Session, Depends(get_session)], |
| 85 | + state: str | None = None, |
| 86 | + job_type: str | None = None, |
| 87 | + hostname: str | None = None, |
| 88 | + executor_class: str | None = None, |
| 89 | + is_alive: bool | None = None, |
| 90 | +) -> JobCollectionResponse: |
| 91 | + """Get all jobs.""" |
| 92 | + base_select = select(Job).where(Job.state == JobState.RUNNING).order_by(Job.latest_heartbeat.desc()) |
| 93 | + # TODO: Refactor using the `FilterParam` class in commit `574b72e41cc5ed175a2bbf4356522589b836bb11` |
| 94 | + if state: |
| 95 | + base_select = base_select.where(Job.state == state) |
| 96 | + if job_type: |
| 97 | + base_select = base_select.where(Job.job_type == job_type) |
| 98 | + if hostname: |
| 99 | + base_select = base_select.where(Job.hostname == hostname) |
| 100 | + if executor_class: |
| 101 | + base_select = base_select.where(Job.executor_class == executor_class) |
| 102 | + |
| 103 | + jobs_select, total_entries = paginated_select( |
| 104 | + base_select, |
| 105 | + [ |
| 106 | + start_date_range, |
| 107 | + end_date_range, |
| 108 | + ], |
| 109 | + order_by, |
| 110 | + limit, |
| 111 | + offset, |
| 112 | + session, |
| 113 | + ) |
| 114 | + jobs = session.scalars(jobs_select).all() |
| 115 | + |
| 116 | + if is_alive is not None: |
| 117 | + jobs = [job for job in jobs if job.is_alive()] |
| 118 | + |
| 119 | + return JobCollectionResponse( |
| 120 | + jobs=[ |
| 121 | + JobResponse.model_validate( |
| 122 | + job, |
| 123 | + from_attributes=True, |
| 124 | + ) |
| 125 | + for job in jobs |
| 126 | + ], |
| 127 | + total_entries=total_entries, |
| 128 | + ) |
0 commit comments