pierrejeambrun commented on code in PR #51264:
URL: https://github.com/apache/airflow/pull/51264#discussion_r2164045609
##########
airflow-core/src/airflow/api_fastapi/common/parameters.py:
##########
@@ -112,6 +114,35 @@ def depends(cls, offset: NonNegativeInt = 0) ->
OffsetFilter:
return cls().set_value(offset)
+class _FavoriteFilter(BaseParam[bool]):
+ """Filter DAGs by favorite status."""
+
+ user_id: str
+
+ def to_orm(self, select_stmt: Select) -> Select:
+ if self.value is None and self.skip_none:
+ return select_stmt
+
+ if self.value:
+ select_stmt = select_stmt.join(DagFavorite, DagFavorite.dag_id ==
DagModel.dag_id).where(
+ DagFavorite.user_id == self.user_id
+ )
+ else:
+ select_stmt = select_stmt.where(
+ ~select(DagFavorite)
+ .where((DagFavorite.dag_id == DagModel.dag_id) &
(DagFavorite.user_id == self.user_id))
+ .exists()
+ )
Review Comment:
Please don't use bitwise operator, sqlalchemy exposes "not" "and" "or" which
are clearer.
##########
airflow-core/src/airflow/ui/src/pages/Dashboard/FavoriteDags/FavoriteDags.tsx:
##########
@@ -0,0 +1,72 @@
+/*!
+ * 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 } from "react";
+import { useTranslation } from "react-i18next";
+import { FiStar } from "react-icons/fi";
+import { useLocation } from "react-router-dom";
+
+import { useDagServiceGetDagsUi } from "openapi/queries";
+
+import { FavoriteDagCard } from "./FavoriteDagCard";
+
+export const FavoriteDags = () => {
+ const queryClient = useQueryClient();
+ const location = useLocation();
+
+ const { t: translate } = useTranslation("dashboard");
+ const { data: favorites } = useDagServiceGetDagsUi({ isFavorite: true,
limit: 10 });
+
+ useEffect(() => {
+ void queryClient.refetchQueries({ queryKey: ["DagServiceGetDagsUi"] });
+ }, [location.key, queryClient]);
+
+ if (!favorites) {
+ return undefined;
+ }
+
+ return (
+ <Box>
+ <Flex color="fg.muted" my={2}>
+ <FiStar />
+ <Heading ml={1} size="xs">
+ {translate("favorite.favoriteDags")}
Review Comment:
Translation need to be changed to `First 10 favorite dags" or something like
this. To explicit the fact that not all of the favorite dags will be displayed
there but just the first 10 .
##########
airflow-core/src/airflow/api_fastapi/core_api/openapi/v2-rest-api-generated.yaml:
##########
@@ -3140,6 +3148,104 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/HTTPValidationError'
+ /api/v2/dags/{dag_id}/favorite:
+ post:
+ tags:
+ - DAG
+ summary: Favorite Dag
+ description: Mark the DAG as favorite.
+ operationId: favorite_dag
+ security:
+ - OAuth2PasswordBearer: []
+ parameters:
+ - name: dag_id
+ in: path
+ required: true
+ schema:
+ type: string
+ title: Dag Id
+ responses:
+ '200':
+ description: Successful Response
+ content:
+ application/json:
+ schema: {}
Review Comment:
Spec is wrong that should be `204`
##########
airflow-core/src/airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -317,6 +322,64 @@ 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="GET")),
Depends(action_logging())],
+)
+def favorite_dag(
+ dag_id: str,
+ session: SessionDep,
+ user: GetUserDep,
+) -> Response:
+ """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))
+
+ return Response(status_code=status.HTTP_204_NO_CONTENT)
+
Review Comment:
Please check at other endpoints to see how we achieve `204` no response
endpoint.
(no return + declare the response status code + no type in the return
annotation, etc.)
--
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]