pierrejeambrun commented on code in PR #51264:
URL: https://github.com/apache/airflow/pull/51264#discussion_r2137776555
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -346,6 +358,55 @@ def patch_dags(
)
+@dags_router.post(
+ "/{dag_id}/favorite",
+ responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+ dependencies=[Depends(requires_access_dag(method="POST")),
Depends(action_logging())],
+)
+def favorite_dag(
+ dag_id: str,
+ session: SessionDep,
+ user: GetUserDep,
+) -> DAGResponse:
+ """Mark the DAG as favorite."""
+ dag = session.get(DagModel, dag_id)
+ if not dag:
+ raise HTTPException(status.HTTP_404_NOT_FOUND, detail=f"DAG with id
'{dag_id}' not found")
+
+ user_id = user.get_id()
+ session.execute(insert(DagFavorite).values(dag_id=dag_id, user_id=user_id))
+ session.commit()
Review Comment:
No need to commit, SessionDep will do that for you when the request end.
##########
airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_dags.py:
##########
@@ -392,6 +420,74 @@ def test_patch_dags_should_response_403(self,
unauthorized_test_client):
assert response.status_code == 403
+class TestFavoriteDag(TestDagEndpoint):
+ """Unit tests for favoriting a DAG."""
+
+ @pytest.mark.parametrize(
+ "dag_id, expected_status_code, expected_exist_in_favorites",
+ [
+ ("fake_dag_id", 404, None),
+ (DAG1_ID, 200, True),
+ ],
+ )
+ def test_favorite_dag(
+ self, test_client, dag_id, expected_status_code,
expected_exist_in_favorites, session
+ ):
+ response = test_client.post(f"/dags/{dag_id}/favorite")
+ assert response.status_code == expected_status_code
+
+ if expected_status_code == 200:
+ result = session.execute(
+ select(DagFavorite).where(DagFavorite.dag_id == dag_id,
DagFavorite.user_id == "test")
+ ).first()
+ assert result is not None if expected_exist_in_favorites else
result is None
+ check_last_log(session, dag_id=dag_id, event="favorite_dag",
logical_date=None)
+
+ def test_favorite_dag_should_response_401(self,
unauthenticated_test_client):
+ response =
unauthenticated_test_client.post(f"/dags/{DAG1_ID}/favorite")
+ assert response.status_code == 401
+
+ def test_favorite_dag_should_response_403(self, unauthorized_test_client):
+ response = unauthorized_test_client.post(f"/dags/{DAG1_ID}/favorite")
+ assert response.status_code == 403
+
Review Comment:
Can we get a test for favoriting a dag that is already a favorite ? This
should raise a 409 conflict error.
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -346,6 +358,55 @@ def patch_dags(
)
+@dags_router.post(
+ "/{dag_id}/favorite",
+ responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+ dependencies=[Depends(requires_access_dag(method="POST")),
Depends(action_logging())],
Review Comment:
Maybe we can consider that any user that can `read a dag` can create its own
`favorite` of it? And therefore we only need the `GET` permission on the DAG to
be able to create a favorite of it?
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -113,9 +115,19 @@ def get_dags(
],
readable_dags_filter: ReadableDagsFilterDep,
session: SessionDep,
+ user: GetUserDep,
+ favorites: Annotated[bool | None, Query()] = None,
Review Comment:
```suggestion
is_favorite: Annotated[bool | None, Query()] = None,
```
I think we should create a reusable param out of this. Just to not clutter
the `route` file, and also because it will be reusable.
Basically create a parameter, you can take a look at `common/parameters.py`.
This will be more or less the `to_orm` method. Also I should be able to pass
`False` to retrieve non favorited dag only. (if a value is defined for the
filter, do the join and the filter, if it's None, don't do anything)
```python
.join(DagFavorite, DagModel.dag_id == DagFavorite.dag_id)
.where(DagFavorite.user_id == user_id)
```
##########
airflow-core/src/airflow/ui/src/pages/Dashboard/FavoriteDags/FavoriteDags.tsx:
##########
@@ -0,0 +1,96 @@
+/*!
+ * 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.
+ */
+import { Box, Flex, Heading, SimpleGrid, Text } from "@chakra-ui/react";
+import { useQueryClient } from "@tanstack/react-query";
+import { useEffect, useMemo, useState } from "react";
+import { useTranslation } from "react-i18next";
+import { FiStar } from "react-icons/fi";
+import { useLocation } from "react-router-dom";
+
+import { useDagServiceGetDags } from "openapi/queries";
+
+import { FavoriteDagCard } from "./FavoriteDagCard";
+
+const MAX_VISIBLE = 5;
+
+export const FavoriteDags = () => {
+ const queryClient = useQueryClient();
+ const location = useLocation();
+
+ const { t: translate } = useTranslation("dashboard");
+ const { data: favorites } = useDagServiceGetDags({ favorites: true });
Review Comment:
The rest of the UI is using `useDagServiceGetDagsUi` to fetch dags. The UI
should probably only use this, and we should also update the `ui/get_dags`
endpoint to be able to do the same filtering on favorites. (therefore a common
`parameters.py` will be helpful)
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -346,6 +358,55 @@ def patch_dags(
)
+@dags_router.post(
+ "/{dag_id}/favorite",
+ responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+ dependencies=[Depends(requires_access_dag(method="POST")),
Depends(action_logging())],
+)
+def favorite_dag(
+ dag_id: str,
+ session: SessionDep,
+ user: GetUserDep,
+) -> DAGResponse:
+ """Mark the DAG as favorite."""
+ dag = session.get(DagModel, dag_id)
+ if not dag:
+ raise HTTPException(status.HTTP_404_NOT_FOUND, detail=f"DAG with id
'{dag_id}' not found")
+
+ user_id = user.get_id()
+ session.execute(insert(DagFavorite).values(dag_id=dag_id, user_id=user_id))
+ session.commit()
+
+ return DAGResponse.model_validate(dag)
+
+
+@dags_router.post(
+ "/{dag_id}/unfavorite",
+ responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+ dependencies=[Depends(requires_access_dag(method="POST")),
Depends(action_logging())],
+)
+def unfavorite_dag(
+ dag_id: str,
+ session: SessionDep,
+ user: GetUserDep,
+) -> DAGResponse:
+ """Unmark the DAG as favorite."""
+ dag = session.get(DagModel, dag_id)
+ if not dag:
+ raise HTTPException(status.HTTP_404_NOT_FOUND, detail=f"DAG with id
'{dag_id}' not found")
+
+ user_id = user.get_id()
+ session.execute(
+ delete(DagFavorite).where(
+ DagFavorite.dag_id == dag_id,
+ DagFavorite.user_id == user_id,
+ )
+ )
+ session.commit()
+
+ return DAGResponse.model_validate(dag)
Review Comment:
We return a dag response, but this does not hold any information about the
`favorite/non favorite` state of the Dag, not sure if it's relevant.
##########
airflow-core/src/airflow/api_fastapi/core_api/datamodels/dags.py:
##########
@@ -121,6 +121,12 @@ class DAGPatchBody(StrictBaseModel):
is_paused: bool
+class DAGFavoriteBody(StrictBaseModel):
+ """Dag Serializer for updatable bodies."""
Review Comment:
Docstring is not up to date, also this seems unused and should be deleted.
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -346,6 +358,55 @@ def patch_dags(
)
+@dags_router.post(
+ "/{dag_id}/favorite",
+ responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+ dependencies=[Depends(requires_access_dag(method="POST")),
Depends(action_logging())],
+)
+def favorite_dag(
+ dag_id: str,
+ session: SessionDep,
+ user: GetUserDep,
+) -> DAGResponse:
+ """Mark the DAG as favorite."""
+ dag = session.get(DagModel, dag_id)
+ if not dag:
+ raise HTTPException(status.HTTP_404_NOT_FOUND, detail=f"DAG with id
'{dag_id}' not found")
+
+ user_id = user.get_id()
+ session.execute(insert(DagFavorite).values(dag_id=dag_id, user_id=user_id))
+ session.commit()
+
+ return DAGResponse.model_validate(dag)
+
+
+@dags_router.post(
+ "/{dag_id}/unfavorite",
+ responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
+ dependencies=[Depends(requires_access_dag(method="POST")),
Depends(action_logging())],
+)
+def unfavorite_dag(
+ dag_id: str,
+ session: SessionDep,
+ user: GetUserDep,
+) -> DAGResponse:
+ """Unmark the DAG as favorite."""
+ dag = session.get(DagModel, dag_id)
+ if not dag:
+ raise HTTPException(status.HTTP_404_NOT_FOUND, detail=f"DAG with id
'{dag_id}' not found")
+
+ user_id = user.get_id()
+ session.execute(
+ delete(DagFavorite).where(
+ DagFavorite.dag_id == dag_id,
+ DagFavorite.user_id == user_id,
+ )
+ )
+ session.commit()
Review Comment:
same about commit.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]