Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REST API query sort and order to some endpoints #14895

Merged
merged 21 commits into from
Mar 31, 2021

Conversation

ephraimbuddy
Copy link
Contributor

@ephraimbuddy ephraimbuddy commented Mar 19, 2021

This change adds ordering to the REST API endpoints.

The get taskinstances and dags endpoint are excluded in this PR

Part of #14803


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.

@boring-cyborg boring-cyborg bot added the area:API Airflow's REST/HTTP API label Mar 19, 2021
@ryanahamilton ryanahamilton added the AIP-38 Modern Web Application label Mar 19, 2021
@ephraimbuddy ephraimbuddy force-pushed the sort-and-order-endpoints branch 2 times, most recently from 00178a2 to 4a20594 Compare March 22, 2021 08:25
airflow/api_connexion/openapi/v1.yaml Outdated Show resolved Hide resolved
tests/api_connexion/endpoints/test_dag_endpoint.py Outdated Show resolved Hide resolved
airflow/api_connexion/endpoints/dag_endpoint.py Outdated Show resolved Hide resolved
airflow/api_connexion/endpoints/dag_run_endpoint.py Outdated Show resolved Hide resolved
Copy link
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a common enough pattern that I think it should be extracted out to something like this:

def apply_sorting(model, query, sort, order_by):
    if order_by not in (i.name for i in model.__table__.columns):
         raise BadRequest(
             detail=f"{type(model).__name__} has no attribute '{order_by}' specified in order_by parameter",
        )

    if sort == "asc":
        return = query.order_by(asc(order_by))
    else:
        return query.order_by(desc(order_by))

(I have used the param names you did, but see my previous comment.)

@ephraimbuddy ephraimbuddy force-pushed the sort-and-order-endpoints branch 2 times, most recently from 9a367c8 to fa4fed7 Compare March 23, 2021 16:35
airflow/api_connexion/parameters.py Outdated Show resolved Hide resolved
airflow/api_connexion/parameters.py Outdated Show resolved Hide resolved
airflow/api_connexion/parameters.py Outdated Show resolved Hide resolved
total_entries = session.query(func.count(Connection.id)).scalar()
query = session.query(Connection)
connections = query.order_by(Connection.id).offset(offset).limit(limit).all()
query = apply_sorting(Connection, query, order_by, to_replace, allowed_filter_attrs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel there should be a way to avoid repeating Connection here. Same goes for other calls below.

@github-actions
Copy link

The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.

@github-actions
Copy link

The Workflow run is cancelling this PR. Building image for the PR has been cancelled

Copy link
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_dag_runs_batch in dag_run_endpoint should have ordering too.

@ephraimbuddy ephraimbuddy force-pushed the sort-and-order-endpoints branch 2 times, most recently from 5fdf19d to 142457f Compare March 24, 2021 15:47
@github-actions
Copy link

The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.

@github-actions
Copy link

The Workflow run is cancelling this PR. Building image for the PR has been cancelled

@github-actions
Copy link

The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*.

Co-authored-by: Kamil Breguła <mik-laj@users.noreply.github.com>
@ashb ashb merged commit a2d9929 into apache:master Mar 31, 2021
@ashb ashb deleted the sort-and-order-endpoints branch March 31, 2021 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AIP-38 Modern Web Application area:API Airflow's REST/HTTP API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants