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

AIP-84 Fix: Allow Null Values for end_date Field in Dashboard Endpint in FastAPI #44043

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bugraoz93
Copy link
Collaborator

@bugraoz93 bugraoz93 commented Nov 14, 2024

related: #43846

  • Allow safe_date_time to return None if the input is None
  • Handle None in queries/endpoints
  • Simplify unit tests

I have created a separate PR because write permissions don't allow me to push to the existing PR.
cc: @bbovenzi @pierrejeambrun @tirkarthi


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an 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 a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

@bugraoz93 bugraoz93 changed the title Allow safe_date_time to return none if input none, handle None in que… AIP-84 Fix: Allow Null Values for Dashboard Endpint in FastAPI Nov 14, 2024
@bugraoz93 bugraoz93 changed the title AIP-84 Fix: Allow Null Values for Dashboard Endpint in FastAPI AIP-84 Fix: Allow Null Values for end_date Field in Dashboard Endpint in FastAPI Nov 14, 2024
# DagRuns
dag_run_types = session.execute(
select(DagRun.run_type, func.count(DagRun.run_id))
.where(
DagRun.start_date >= start_date,
func.coalesce(DagRun.end_date, timezone.utcnow()) <= end_date,
func.coalesce(DagRun.end_date, timezone.utcnow()) <= func.coalesce(end_date, timezone.utcnow()),
Copy link
Contributor

Choose a reason for hiding this comment

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

timezone.utcnow() can be just defined once at the top of the function to avoid different values due to multiple calls.

Copy link
Member

@pierrejeambrun pierrejeambrun left a comment

Choose a reason for hiding this comment

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

Thanks a few suggestions

@@ -365,14 +365,16 @@ def depends(self, tag_name_pattern: str | None = None) -> _DagTagNamePatternSear
return self.set_value(tag_name_pattern)


def _safe_parse_datetime(date_to_check: str) -> datetime:
def _safe_parse_datetime(date_to_check: str) -> datetime | None:
Copy link
Member

Choose a reason for hiding this comment

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

argument type need to be adjusted

@@ -49,12 +49,18 @@ def historical_metrics(
session: Annotated[Session, Depends(get_session)],
) -> HistoricalMetricDataResponse:
"""Return cluster activity historical metrics."""
if start_date is None:
Copy link
Member

Choose a reason for hiding this comment

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

DateTimeQuery need to be adjusted, because by definition, it cannot return str | None, only str:

DateTimeQuery = Annotated[str, AfterValidator(_safe_parse_datetime)]

Maybe create a OptionalDateTimeQuery = Annotated[str | None, AfterValidator(_safe_parse_datetime)], or maybe DateTimeQuery | None already works

Comment on lines +53 to +57
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="start_date parameter is required in the request",
)

Copy link
Member

Choose a reason for hiding this comment

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

This should be handled automatically by FastAPI. If the Query parameter is properly defined, we shouldn't allow None. No need to handle that manually here. (Also that would be a 422 and not a 400, because this is how fastapi handles validation errors)

@tirkarthi
Copy link
Contributor

The following were added in #43934 . The changes broke dashboard endpoint probably due str changed to datetime . During resolving merge conflicts maybe OptionalDateTimeQuery could be used for end_date which hopefully resolves this PR's use case.

DateTimeQuery = Annotated[datetime, AfterValidator(_safe_parse_datetime)]
OptionalDateTimeQuery = Annotated[Union[datetime, None], AfterValidator(_safe_parse_datetime_optional)]

https://github.com/apache/airflow/pull/43934/files#diff-183a76763667e545ddd8c2aadf6858fdd7a80fecdd04b71e61eaa4ff344239cdR638-R639

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants