Exemple #1
0
def get_dag_runs(dag_id, state=None, run_url_route='Airflow.graph'):
    # type: (str, Optional[str], str) -> List[Dict[str, Any]]
    """
    Returns a list of Dag Runs for a specific DAG ID.
    :param dag_id: String identifier of a DAG
    :param state: queued|running|success...
    :return: List of DAG runs of a DAG with requested state,
    or all runs if the state is not specified
    """
    check_and_get_dag(dag_id=dag_id)

    dag_runs = list()
    state = state.lower() if state else None
    for run in DagRun.find(dag_id=dag_id, state=state):
        dag_runs.append({
            'id':
            run.id,
            'run_id':
            run.run_id,
            'state':
            run.state,
            'dag_id':
            run.dag_id,
            'execution_date':
            run.execution_date.isoformat(),
            'start_date': ((run.start_date or '')
                           and run.start_date.isoformat()),
            'dag_run_url':
            url_for(run_url_route,
                    dag_id=run.dag_id,
                    execution_date=run.execution_date)
        })

    return dag_runs
Exemple #2
0
def get_lineage(dag_id: str,
                execution_date: datetime.datetime,
                session=None) -> Dict[str, Dict[str, Any]]:
    """
    Gets the lineage information for dag specified
    """
    dag = check_and_get_dag(dag_id)
    check_and_get_dagrun(dag, execution_date)

    inlets: List[XCom] = XCom.get_many(dag_ids=dag_id,
                                       execution_date=execution_date,
                                       key=PIPELINE_INLETS,
                                       session=session).all()
    outlets: List[XCom] = XCom.get_many(dag_ids=dag_id,
                                        execution_date=execution_date,
                                        key=PIPELINE_OUTLETS,
                                        session=session).all()

    lineage: Dict[str, Dict[str, Any]] = {}
    for meta in inlets:
        lineage[meta.task_id] = {'inlets': meta.value}

    for meta in outlets:
        lineage[meta.task_id]['outlets'] = meta.value

    return {'task_ids': lineage}
Exemple #3
0
def get_task_instance(dag_id: str, task_id: str, execution_date: datetime) -> TaskInstance:
    """Return the task instance identified by the given dag_id, task_id and execution_date."""
    dag = check_and_get_dag(dag_id, task_id)

    dagrun = check_and_get_dagrun(dag=dag, execution_date=execution_date)
    # Get task instance object and check that it exists
    task_instance = dagrun.get_task_instance(task_id)
    if not task_instance:
        error_message = f'Task {task_id} instance for date {execution_date} not found'
        raise TaskInstanceNotFound(error_message)

    return task_instance
Exemple #4
0
def get_dag_run_state(dag_id: str, execution_date: datetime) -> Dict[str, str]:
    """Return the Dag Run state identified by the given dag_id and execution_date.

    :param dag_id: DAG id
    :param execution_date: execution date
    :return: Dictionary storing state of the object
    """
    dag = check_and_get_dag(dag_id=dag_id)

    dagrun = check_and_get_dagrun(dag, execution_date)

    return {'state': dagrun.get_state()}
Exemple #5
0
def get_code(dag_id: str) -> str:
    """Return python code of a given dag_id.

    :param dag_id: DAG id
    :return: code of the DAG
    """
    dag = check_and_get_dag(dag_id=dag_id)

    try:
        return DagCode.get_code_by_fileloc(dag.fileloc)
    except (OSError, DagCodeNotFound) as exception:
        error_message = f"Error {str(exception)} while reading Dag id {dag_id} Code"
        raise AirflowException(error_message, exception)
Exemple #6
0
def get_dag_run_state(
        dag_id, execution_date):  # type: (str, datetime) -> Dict[str, str]
    """Return the task object identified by the given dag_id and task_id.

    :param dag_id: DAG id
    :param execution_date: execution date
    :return: Dictionary storing state of the object
    """

    dag = check_and_get_dag(dag_id=dag_id)

    dagrun = check_and_get_dagrun(dag, execution_date)

    return {'state': dagrun.get_state()}
Exemple #7
0
def get_code(dag_id: str) -> str:
    """Return python code of a given dag_id.

    :param dag_id: DAG id
    :return: code of the DAG
    """
    dag = check_and_get_dag(dag_id=dag_id)

    try:
        with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as file:
            code = file.read()
            return code
    except IOError as exception:
        error_message = "Error {} while reading Dag id {} Code".format(str(exception), dag_id)
        raise AirflowException(error_message)
Exemple #8
0
def get_lineage(dag_id: str, execution_date: datetime.datetime, *, session) -> Dict[str, Dict[str, Any]]:
    """Gets the lineage information for dag specified."""
    dag = check_and_get_dag(dag_id)
    dagrun = check_and_get_dagrun(dag, execution_date)

    inlets = XCom.get_many(dag_ids=dag_id, run_id=dagrun.run_id, key=PIPELINE_INLETS, session=session)
    outlets = XCom.get_many(dag_ids=dag_id, run_id=dagrun.run_id, key=PIPELINE_OUTLETS, session=session)

    lineage: Dict[str, Dict[str, Any]] = collections.defaultdict(dict)
    for meta in inlets:
        lineage[meta.task_id]["inlets"] = meta.value
    for meta in outlets:
        lineage[meta.task_id]["outlets"] = meta.value

    return {"task_ids": {k: v for k, v in lineage.items()}}
Exemple #9
0
def get_task_instance(
        dag_id, task_id,
        execution_date):  # type: (str, str, datetime) -> TaskInstance
    """Return the task object identified by the given dag_id and task_id."""
    dag = check_and_get_dag(dag_id, task_id)

    dagrun = check_and_get_dagrun(dag=dag, execution_date=execution_date)
    # Get task instance object and check that it exists
    task_instance = dagrun.get_task_instance(task_id)
    if not task_instance:
        error_message = ('Task {} instance for date {} not found'.format(
            task_id, execution_date))
        raise TaskInstanceNotFound(error_message)

    return task_instance
Exemple #10
0
def get_task(dag_id: str, task_id: str) -> TaskInstance:
    """Return the task object identified by the given dag_id and task_id."""
    dag = check_and_get_dag(dag_id, task_id)

    # Return the task.
    return dag.get_task(task_id)