Ejemplo n.º 1
0
def get_dags(limit, offset=0):
    """Get all DAGs."""
    readable_dags = current_app.appbuilder.sm.get_readable_dags(g.user)
    dags = readable_dags.order_by(DagModel.dag_id).offset(offset).limit(limit).all()
    total_entries = readable_dags.count()

    return dags_collection_schema.dump(DAGCollection(dags=dags, total_entries=total_entries))
Ejemplo n.º 2
0
def get_dags(
    *,
    limit: int,
    offset: int = 0,
    tags: Optional[Collection[str]] = None,
    dag_id_pattern: Optional[str] = None,
    only_active: bool = True,
    session: Session = NEW_SESSION,
) -> APIResponse:
    """Get all DAGs."""
    if only_active:
        dags_query = session.query(DagModel).filter(~DagModel.is_subdag,
                                                    DagModel.is_active)
    else:
        dags_query = session.query(DagModel).filter(~DagModel.is_subdag)

    if dag_id_pattern:
        dags_query = dags_query.filter(
            DagModel.dag_id.ilike(f'%{dag_id_pattern}%'))

    readable_dags = current_app.appbuilder.sm.get_accessible_dag_ids(g.user)

    dags_query = dags_query.filter(DagModel.dag_id.in_(readable_dags))
    if tags:
        cond = [DagModel.tags.any(DagTag.name == tag) for tag in tags]
        dags_query = dags_query.filter(or_(*cond))

    total_entries = len(dags_query.all())

    dags = dags_query.order_by(
        DagModel.dag_id).offset(offset).limit(limit).all()

    return dags_collection_schema.dump(
        DAGCollection(dags=dags, total_entries=total_entries))
Ejemplo n.º 3
0
 def test_serialize(self):
     dag_model_a = DagModel(dag_id="test_dag_id_a", fileloc="/tmp/a.py")
     dag_model_b = DagModel(dag_id="test_dag_id_b", fileloc="/tmp/a.py")
     schema = DAGCollectionSchema()
     instance = DAGCollection(dags=[dag_model_a, dag_model_b], total_entries=2)
     assert {
         "dags": [
             {
                 "dag_id": "test_dag_id_a",
                 "description": None,
                 "fileloc": "/tmp/a.py",
                 "file_token": SERIALIZER.dumps("/tmp/a.py"),
                 "is_paused": None,
                 "is_subdag": None,
                 "owners": [],
                 "root_dag_id": None,
                 "schedule_interval": None,
                 "tags": [],
             },
             {
                 "dag_id": "test_dag_id_b",
                 "description": None,
                 "fileloc": "/tmp/a.py",
                 "file_token": SERIALIZER.dumps("/tmp/a.py"),
                 "is_paused": None,
                 "is_subdag": None,
                 "owners": [],
                 "root_dag_id": None,
                 "schedule_interval": None,
                 "tags": [],
             },
         ],
         "total_entries": 2,
     } == schema.dump(instance)
Ejemplo n.º 4
0
def get_dags(session, limit, offset=0):
    """
    Get all DAGs.
    """
    dags = session.query(DagModel).order_by(
        DagModel.dag_id).offset(offset).limit(limit).all()

    total_entries = session.query(func.count(DagModel.dag_id)).scalar()

    return dags_collection_schema.dump(
        DAGCollection(dags=dags, total_entries=total_entries))
Ejemplo n.º 5
0
def patch_dags(limit,
               session,
               offset=0,
               only_active=True,
               tags=None,
               dag_id_pattern=None,
               update_mask=None):
    """Patch multiple DAGs."""
    try:
        patch_body = dag_schema.load(request.json, session=session)
    except ValidationError as err:
        raise BadRequest(detail=str(err.messages))
    if update_mask:
        patch_body_ = {}
        if update_mask != ['is_paused']:
            raise BadRequest(
                detail=
                "Only `is_paused` field can be updated through the REST API")
        update_mask = update_mask[0]
        patch_body_[update_mask] = patch_body[update_mask]
        patch_body = patch_body_
    if only_active:
        dags_query = session.query(DagModel).filter(~DagModel.is_subdag,
                                                    DagModel.is_active)
    else:
        dags_query = session.query(DagModel).filter(~DagModel.is_subdag)

    if dag_id_pattern == '~':
        dag_id_pattern = '%'
    dags_query = dags_query.filter(
        DagModel.dag_id.ilike(f'%{dag_id_pattern}%'))
    editable_dags = current_app.appbuilder.sm.get_editable_dag_ids(g.user)

    dags_query = dags_query.filter(DagModel.dag_id.in_(editable_dags))
    if tags:
        cond = [DagModel.tags.any(DagTag.name == tag) for tag in tags]
        dags_query = dags_query.filter(or_(*cond))

    total_entries = dags_query.count()

    dags = dags_query.order_by(
        DagModel.dag_id).offset(offset).limit(limit).all()

    dags_to_update = {dag.dag_id for dag in dags}
    session.query(DagModel).filter(DagModel.dag_id.in_(dags_to_update)).update(
        {DagModel.is_paused: patch_body['is_paused']},
        synchronize_session='fetch')

    session.flush()

    return dags_collection_schema.dump(
        DAGCollection(dags=dags, total_entries=total_entries))
Ejemplo n.º 6
0
def get_dags(limit, session, offset=0):
    """Get all DAGs."""
    dags_query = session.query(DagModel).filter(~DagModel.is_subdag,
                                                DagModel.is_active)

    readable_dags = current_app.appbuilder.sm.get_accessible_dag_ids(g.user)

    dags_query = dags_query.filter(DagModel.dag_id.in_(readable_dags))
    total_entries = len(dags_query.all())

    dags = dags_query.order_by(
        DagModel.dag_id).offset(offset).limit(limit).all()

    return dags_collection_schema.dump(
        DAGCollection(dags=dags, total_entries=total_entries))
Ejemplo n.º 7
0
def get_dags(limit, session, offset=0, only_active=True, tags=None):
    """Get all DAGs."""
    if only_active:
        dags_query = session.query(DagModel).filter(~DagModel.is_subdag,
                                                    DagModel.is_active)
    else:
        dags_query = session.query(DagModel).filter(~DagModel.is_subdag)

    readable_dags = current_app.appbuilder.sm.get_accessible_dag_ids(g.user)

    dags_query = dags_query.filter(DagModel.dag_id.in_(readable_dags))
    if tags:
        cond = [DagModel.tags.any(DagTag.name == tag) for tag in tags]
        dags_query = dags_query.filter(or_(*cond))

    total_entries = len(dags_query.all())

    dags = dags_query.order_by(
        DagModel.dag_id).offset(offset).limit(limit).all()

    return dags_collection_schema.dump(
        DAGCollection(dags=dags, total_entries=total_entries))