bbovenzi commented on code in PR #69148: URL: https://github.com/apache/airflow/pull/69148#discussion_r3778172260
########## airflow-core/src/airflow/ui/src/hooks/usePluginAppliesToContext.ts: ########## @@ -0,0 +1,63 @@ +/*! + * 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 { useParams } from "react-router-dom"; + +import { + useDagServiceGetDag, + useTaskInstanceServiceGetMappedTaskInstance, + useTaskServiceGetTask, +} from "openapi/queries"; +import type { AppliesToContext } from "src/utils/pluginAppliesTo"; + +/** + * Resolve the records the current route provides, for evaluating plugin `applies_to`. + * + * Each query is gated on the route params it needs, so it stays disabled where those + * params are absent and is a cache hit where the details page already fetched it — no + * extra requests. Task groups are skipped for the task query, since `groupId` is not a + * task_id and would 404. + */ +export const usePluginAppliesToContext = (): AppliesToContext => { + const { dagId = "", groupId, mapIndex = "-1", runId = "", taskId = "" } = useParams(); + + const { data: dag, isLoading: isDagLoading } = useDagServiceGetDag({ dagId }, undefined, { + enabled: Boolean(dagId), + }); + + const { data: task, isLoading: isTaskLoading } = useTaskServiceGetTask({ dagId, taskId }, undefined, { + enabled: Boolean(dagId) && Boolean(taskId) && groupId === undefined, + }); + + const { data: taskInstance, isLoading: isTaskInstanceLoading } = + useTaskInstanceServiceGetMappedTaskInstance( + { dagId, dagRunId: runId, mapIndex: parseInt(mapIndex, 10), taskId }, + undefined, + { enabled: Boolean(dagId) && Boolean(runId) && Boolean(taskId) && groupId === undefined }, + ); Review Comment: I wonder if we should make the queryKeys here a constant that can be shared between this and the individual pages. Then we can be confident that they are an exact match and we're just fetching the cache. This also means someone can't accidentally break all of our caching. ########## airflow-core/src/airflow/ui/src/hooks/usePluginTabs.tsx: ########## @@ -32,14 +34,18 @@ type TabPlugin = { export const usePluginTabs = (destination: string): Array<TabPlugin> => { const { colorMode } = useColorMode(); const { data: pluginData } = usePluginServiceGetPlugins(); + const appliesToContext = usePluginAppliesToContext(); Review Comment: We should only call this if there even are plugins for this view in the first place. then we avoid extra requests for no reason. ########## airflow-core/docs/administration-and-deployment/plugins.rst: ########## @@ -301,6 +313,59 @@ definitions in Airflow. .. seealso:: :doc:`/howto/define-extra-link` +Scoping a view to specific Dags and tasks +----------------------------------------- + +By default an external view or React app is shown on every page matching its ``destination``. +The optional ``applies_to`` block narrows that down, so a tab is only offered where it is +relevant instead of appearing on every Dag: + +.. code-block:: python + + "applies_to": { + "dag_tags": ["ml"], # Dag carries any of these tags + "dag_ids": ["train_pipeline"], # exact dag_id + "task_ids": ["train_model"], # exact task_id + "operators": ["KubernetesPodOperator"], # the task's operator + } + +All four keys are optional. ``operators`` matches either the operator class name or its +display name (``custom_operator_name``). Review Comment: I think we should split operator and operator_name since that is how our other UI filtering works today. ########## airflow-core/src/airflow/api_fastapi/core_api/datamodels/plugins.py: ########## @@ -72,6 +72,17 @@ class AppBuilderMenuItemResponse(BaseModel): BaseDestinationLiteral = Literal["nav", "dag", "dag_run", "task", "task_instance", "asset", "base"] +class PluginAppliesToResponse(BaseModel): + """Serializer for the optional Dag/task scoping criteria of a UI plugin.""" + + model_config = ConfigDict(extra="allow") Review Comment: ```suggestion model_config = ConfigDict(extra="forbid") ``` We definitely don't want to allow random other fields being added. -- 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]
