You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "omkar-foss (via GitHub)" <gi...@apache.org> on 2024/08/07 12:36:26 UTC
[PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
omkar-foss opened a new pull request, #41307:
URL: https://github.com/apache/airflow/pull/41307
closes #41283.
This adds db-level sorting with order_by query param to the following TI listing APIs:
1. List task instances - `/api/v1/dags/~/dagRuns/~/taskInstances`
2. List task instances (batch) - `/api/v1/dags/~/dagRuns/~/taskInstances/list`
order_by defaults to sorting by start_date (ascending) for above mentioned 2 APIs. Please note that this does NOT change the default sorting param for the List mapped task instances API.
This also adds corresponding unit tests.
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1722832082
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -297,6 +279,31 @@ def _apply_range_filter(query: Select, key: ClauseElement, value_range: tuple[T,
return query
+def _get_order_by_params(order_by: str | None = None) -> tuple:
+ """Return a tuple with the order by params to be used in the query."""
+ if order_by is None:
+ return (TI.map_index.asc(),)
+ if order_by == "state":
+ return (TI.state.asc(), TI.map_index.asc())
+ if order_by == "-state":
+ return (TI.state.desc(), TI.map_index.asc())
+ if order_by == "duration":
+ return (TI.duration.asc(), TI.map_index.asc())
+ if order_by == "-duration":
+ return (TI.duration.desc(), TI.map_index.asc())
+ if order_by == "start_date":
+ return (TI.start_date.asc(), TI.map_index.asc())
+ if order_by == "-start_date":
+ return (TI.start_date.desc(), TI.map_index.asc())
+ if order_by == "end_date":
+ return (TI.end_date.asc(), TI.map_index.asc())
+ if order_by == "-end_date":
+ return (TI.end_date.desc(), TI.map_index.asc())
+ if order_by == "-map_index":
+ return (TI.map_index.desc(),)
+ return ()
Review Comment:
If we receive an invalid value here, I think an error should just be raised directly inside this function. Returning nothing is too easy for the call site to not handle correctly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1722839752
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -378,11 +386,15 @@ def get_task_instances(
)
.add_columns(SlaMiss)
.options(joinedload(TI.rendered_task_instance_fields))
- .offset(offset)
- .limit(limit)
)
+
+ order_by_params = _get_order_by_params(order_by or "start_date")
Review Comment:
This default should be mentioned somewhere. I’m not sure this is a good default though? I’d probably expect this to be ordered naturally (the current behaviour in main), or by `(dag_id, dag_run_id, task_id, map_idnex)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "pierrejeambrun (via GitHub)" <gi...@apache.org>.
pierrejeambrun merged PR #41307:
URL: https://github.com/apache/airflow/pull/41307
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "omkar-foss (via GitHub)" <gi...@apache.org>.
omkar-foss commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1722916682
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -378,11 +386,15 @@ def get_task_instances(
)
.add_columns(SlaMiss)
.options(joinedload(TI.rendered_task_instance_fields))
- .offset(offset)
- .limit(limit)
)
+
+ order_by_params = _get_order_by_params(order_by or "start_date")
Review Comment:
Adding this to the REST API docs. Thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "jscheffl (via GitHub)" <gi...@apache.org>.
jscheffl commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1722936745
##########
airflow/api_connexion/openapi/v1.yaml:
##########
@@ -5193,6 +5194,11 @@ components:
items:
type: string
description: The value can be repeated to retrieve multiple matching values (OR condition).
+ order_by:
+ type: string
+ description: |
+ The name of the field to order the results by. Prefix a field name
+ with `-` to reverse the sort order.
Review Comment:
I think this should also mention the version it was added.
```suggestion
The name of the field to order the results by. Prefix a field name
with `-` to reverse the sort order.
*New in version 3.0.0*
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "omkar-foss (via GitHub)" <gi...@apache.org>.
omkar-foss commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1722915931
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -297,6 +279,31 @@ def _apply_range_filter(query: Select, key: ClauseElement, value_range: tuple[T,
return query
+def _get_order_by_params(order_by: str | None = None) -> tuple:
+ """Return a tuple with the order by params to be used in the query."""
+ if order_by is None:
+ return (TI.map_index.asc(),)
+ if order_by == "state":
+ return (TI.state.asc(), TI.map_index.asc())
+ if order_by == "-state":
+ return (TI.state.desc(), TI.map_index.asc())
+ if order_by == "duration":
+ return (TI.duration.asc(), TI.map_index.asc())
+ if order_by == "-duration":
+ return (TI.duration.desc(), TI.map_index.asc())
+ if order_by == "start_date":
+ return (TI.start_date.asc(), TI.map_index.asc())
+ if order_by == "-start_date":
+ return (TI.start_date.desc(), TI.map_index.asc())
+ if order_by == "end_date":
+ return (TI.end_date.asc(), TI.map_index.asc())
+ if order_by == "-end_date":
+ return (TI.end_date.desc(), TI.map_index.asc())
+ if order_by == "-map_index":
+ return (TI.map_index.desc(),)
+ return ()
Review Comment:
Yes makes sense, I'll raise a `ValueError` in this function on invalid value. Thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "omkar-foss (via GitHub)" <gi...@apache.org>.
omkar-foss commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1723332790
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -297,6 +279,31 @@ def _apply_range_filter(query: Select, key: ClauseElement, value_range: tuple[T,
return query
+def _get_order_by_params(order_by: str | None = None) -> tuple:
+ """Return a tuple with the order by params to be used in the query."""
+ if order_by is None:
+ return (TI.map_index.asc(),)
+ if order_by == "state":
+ return (TI.state.asc(), TI.map_index.asc())
+ if order_by == "-state":
+ return (TI.state.desc(), TI.map_index.asc())
+ if order_by == "duration":
+ return (TI.duration.asc(), TI.map_index.asc())
+ if order_by == "-duration":
+ return (TI.duration.desc(), TI.map_index.asc())
+ if order_by == "start_date":
+ return (TI.start_date.asc(), TI.map_index.asc())
+ if order_by == "-start_date":
+ return (TI.start_date.desc(), TI.map_index.asc())
+ if order_by == "end_date":
+ return (TI.end_date.asc(), TI.map_index.asc())
+ if order_by == "-end_date":
+ return (TI.end_date.desc(), TI.map_index.asc())
+ if order_by == "-map_index":
+ return (TI.map_index.desc(),)
+ return ()
Review Comment:
PR updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "omkar-foss (via GitHub)" <gi...@apache.org>.
omkar-foss commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1723331913
##########
airflow/api_connexion/openapi/v1.yaml:
##########
@@ -5193,6 +5194,11 @@ components:
items:
type: string
description: The value can be repeated to retrieve multiple matching values (OR condition).
+ order_by:
+ type: string
+ description: |
+ The name of the field to order the results by. Prefix a field name
+ with `-` to reverse the sort order.
Review Comment:
Updated docs, thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "omkar-foss (via GitHub)" <gi...@apache.org>.
omkar-foss commented on PR #41307:
URL: https://github.com/apache/airflow/pull/41307#issuecomment-2299246662
Fixed all linting issues and added all your suggestions from comments above. Please review when you get a chance, thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1722839752
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -378,11 +386,15 @@ def get_task_instances(
)
.add_columns(SlaMiss)
.options(joinedload(TI.rendered_task_instance_fields))
- .offset(offset)
- .limit(limit)
)
+
+ order_by_params = _get_order_by_params(order_by or "start_date")
Review Comment:
This default should be mentioned somewhere.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "omkar-foss (via GitHub)" <gi...@apache.org>.
omkar-foss commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1723347207
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -378,11 +386,15 @@ def get_task_instances(
)
.add_columns(SlaMiss)
.options(joinedload(TI.rendered_task_instance_fields))
- .offset(offset)
- .limit(limit)
)
+
+ order_by_params = _get_order_by_params(order_by or "start_date")
Review Comment:
Keeping `order_by` to default to `map_index` (same as it has been for Get Mapped Instances) for consistency. User can specify the `order_by` param as required by referring mentioned to the docs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(rest-api): Add order_by query param to TI listing APIs (#41283) [airflow]
Posted by "omkar-foss (via GitHub)" <gi...@apache.org>.
omkar-foss commented on code in PR #41307:
URL: https://github.com/apache/airflow/pull/41307#discussion_r1723347207
##########
airflow/api_connexion/endpoints/task_instance_endpoint.py:
##########
@@ -378,11 +386,15 @@ def get_task_instances(
)
.add_columns(SlaMiss)
.options(joinedload(TI.rendered_task_instance_fields))
- .offset(offset)
- .limit(limit)
)
+
+ order_by_params = _get_order_by_params(order_by or "start_date")
Review Comment:
Keeping `order_by` to default to `map_index` (same as it has been for Get Mapped Instances) for consistency. User can specify the `order_by` param as required by referring to the docs.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org