seanghaeli commented on code in PR #76567: URL: https://github.com/apache/airflow/pull/76567#discussion_r4546629560
########## airflow-core/src/airflow/api_fastapi/core_api/security.py: ########## Review Comment: It was just handing the trigger a cached lookup of the caller's readable Dags instead of making a fresh query every time unnecessarily. But I moved this logic over to `parameters/trigger.py` now. After this change, the only thing updated in `security.py` is renaming instances of the word "owner" to "used by". See this comment by Niko https://github.com/apache/airflow/pull/76567#pullrequestreview-5849994193 ########## airflow-core/src/airflow/api_fastapi/common/parameters/trigger.py: ########## @@ -19,16 +19,58 @@ from typing import TYPE_CHECKING, Annotated from fastapi import Depends, Query -from sqlalchemy import select as sql_select +from sqlalchemy import select as sql_select, union_all +from airflow.api_fastapi.common.db.common import SessionDep from airflow.api_fastapi.common.parameters.base import BaseParam from airflow.api_fastapi.common.parameters.filter import FilterOptionEnum, FilterParam, filter_param_factory +from airflow.api_fastapi.core_api.security import ReadableTriggersFilterDep from airflow.jobs.job import Job -from airflow.models.asset import AssetWatcherModel +from airflow.models.asset import ( + AssetModel, + AssetWatcherModel, + DagScheduleAssetNameReference, + DagScheduleAssetReference, + DagScheduleAssetUriReference, +) from airflow.models.trigger import Trigger, TriggerKind, TriggerStatus if TYPE_CHECKING: - from sqlalchemy.sql import Select + from collections.abc import Collection + + from sqlalchemy.sql import CompoundSelect, Select + + +def select_scheduled_dag_ids(*, asset_ids: Collection[int], dag_ids: Collection[str]) -> CompoundSelect: + """ + Select ``(asset_id, dag_id)`` for each of the Dags scheduled on each of the assets. + + As an event of the asset queues them: scheduled on the asset itself, or on ``Asset.ref(name=...)`` / + ``Asset.ref(uri=...)`` of it. + """ + by_ref = [ + sql_select(AssetModel.id, ref.dag_id) + .join(AssetModel, getattr(AssetModel, column) == getattr(ref, column)) + .where(AssetModel.id.in_(asset_ids), ref.dag_id.in_(dag_ids)) Review Comment: Nice idea, I tested it with 5,000 Dags and it took the query time from about 220 ms to 0.5 ms. ########## airflow-core/src/airflow/ui/src/pages/Trigger/Owner.tsx: ########## @@ -0,0 +1,144 @@ +/*! + * 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 type { PropsWithChildren } from "react"; + +import { Box, Heading, Table, Text, VStack } from "@chakra-ui/react"; +import { useTranslation } from "react-i18next"; +import { useParams } from "react-router-dom"; + +import { useAssetServiceGetAssetEvents, useTriggerServiceGetTrigger } from "openapi/queries"; +import type { + CallbackState, + TaskInstanceState, + TriggerAssetWatcherReferenceResponse, +} from "openapi/requests/types.gen"; + +import { RouterLink } from "src/system-components"; + +import { TriggerOwner } from "src/pages/Triggers/TriggerOwner"; + +import { AssetEvents } from "src/components/Assets/AssetEvents"; +import { StateBadge } from "src/components/StateBadge"; + +const RECENT_EVENTS = 5; + +const Row = ({ children, label }: PropsWithChildren<{ readonly label: string }>) => ( + <Table.Row> + <Table.Cell whiteSpace="nowrap">{label}</Table.Cell> + <Table.Cell>{children}</Table.Cell> + </Table.Row> +); + +const WatchedAsset = ({ + assetId, + watchers, +}: { + readonly assetId: number; + readonly watchers: Array<TriggerAssetWatcherReferenceResponse>; +}) => { + const { data, isLoading } = useAssetServiceGetAssetEvents({ Review Comment: Good idea, built in now. On a setup with 5,000 assets on a trigger, I measured the query time to go from about 4 minutes to 0.7 seconds -- 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]
