bbovenzi commented on code in PR #76567:
URL: https://github.com/apache/airflow/pull/76567#discussion_r4530459443
##########
airflow-core/src/airflow/ui/public/i18n/locales/en/browse.json:
##########
@@ -60,6 +60,51 @@
},
"title": "Deadlines"
},
+ "triggers": {
+ "columns": {
+ "dagsAffected": "Dags Affected",
+ "failures": "Failures",
+ "kind": "Kind",
+ "lastError": "Last Error",
+ "lastEvent": "Last Event",
+ "owner": "Owner",
+ "status": "Status",
+ "triggerer": "Triggerer"
+ },
+ "detail": {
+ "firstFailedAt": "Failing Since",
+ "lastFailedAt": "Last Failed At",
+ "noReadableScheduledDags": "None you can read",
+ "scheduledDags": "Scheduled Dags"
+ },
+ "filters": {
+ "triggererHostname": "Triggerer Hostname"
+ },
+ "forbidden": {
+ "description": "Triggers are shown to whoever can see what they run for:
a Dag's task instances or Dag runs, or an asset they watch. You can see none of
these.",
+ "title": "No triggers to show"
+ },
+ "kind": {
+ "asset_watcher": "Asset Watcher",
+ "callback": "Callback",
+ "task": "Task"
+ },
+ "notFound": {
+ "backToList": "Back to Triggers",
+ "description": "Triggers are removed once they fire or what they run for
is done, so this one has most likely finished.",
+ "title": "This trigger no longer exists"
+ },
+ "status": {
+ "failing": "Failing",
+ "orphaned": "Orphaned"
+ },
+ "statusHint": {
+ "failing": "It has failed since it last recovered. It clears once it
fires an event or stays up for 2 minutes after it was last started. The last
error says why it failed.",
Review Comment:
Is `stays up for 2 minutes` always true? Or should we pass a number to the
translation file?
##########
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:
Were doing a separate query per asset. Let's try to pull all the asset
events in one query instead.
##########
airflow-core/src/airflow/ui/src/components/FailingWatchersChip.tsx:
##########
@@ -0,0 +1,79 @@
+/*!
+ * 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 { useDisclosure } from "@chakra-ui/react";
+import { useTranslation } from "react-i18next";
+import { FiZap } from "react-icons/fi";
+
+import { useDagServiceGetDagDetails, useTriggerServiceGetTriggers } from
"openapi/queries";
+import type { TriggerAssetWatcherReferenceResponse, TriggerDetailResponse }
from "openapi/requests/types.gen";
+
+import { StatusChip } from "src/components/StatusChip";
+
+import { FailingWatchersModal } from "./FailingWatchersModal";
+
+type Props = {
+ /** Whether a watcher of a failing trigger is one this chip speaks for. */
+ readonly isCounted: (watcher: TriggerAssetWatcherReferenceResponse) =>
boolean;
+ readonly triggers?: Array<TriggerDetailResponse>;
+};
+
+/** Failing watchers, as a red status chip that lists them in a modal, each
with its trigger's last error. */
+export const FailingWatchersChip = ({ isCounted, triggers }: Props) => {
+ const { t: translate } = useTranslation("dag");
+ const { onClose, onOpen, open } = useDisclosure();
+ const failing = (triggers ?? []).flatMap((trigger) =>
+ trigger.asset_watchers.filter(isCounted).map((watcher) => ({ trigger,
watcher })),
+ );
+
+ if (failing.length === 0) {
+ return undefined;
+ }
+
+ return (
+ <>
+ <StatusChip
+ icon={<FiZap />}
+ label={translate("failingWatchers", { count: failing.length })}
+ onClick={onOpen}
+ severity="error"
+ />
+ <FailingWatchersModal onClose={onClose} open={open} watchers={failing} />
+ </>
+ );
+};
+
+/** Failing watchers on the assets the Dag is scheduled on, which keep it from
running. */
+export const DagFailingWatchersChip = ({ dagId }: { readonly dagId: string })
=> {
+ // The details the breadcrumb already loads: only a Dag scheduled on assets
asks for its watchers.
+ const { data: dag } = useDagServiceGetDagDetails({ dagId }, undefined, {
enabled: dagId !== "" });
+ // Narrowed to this Dag on the server, so the page limit is per Dag.
+ const { data } = useTriggerServiceGetTriggers(
+ { kind: ["asset_watcher"], limit: 100, scheduledDagId: dagId, status:
["failing"] },
Review Comment:
Why did we hardcode limit:100 here?
##########
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:
`dag_ids` here is coming from the auth manager which may need a check per
dag id for access, and then we are chaining each dag_id as a param here, and
finally in the UI we are calling this query constantly with auto refresh. For
large deployments this can be thousands of dags.
Then let's pass the readable dag ids in into the query at
`_get_scheduled_dag_ids()`. Query assets and then check the dag_ids of those
assets with our list of authorized dag ids.
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
Review Comment:
Why do we need to edit our security file for this feature?
##########
airflow-core/src/airflow/ui/src/components/FailingWatchersChip.tsx:
##########
@@ -0,0 +1,79 @@
+/*!
+ * 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 { useDisclosure } from "@chakra-ui/react";
+import { useTranslation } from "react-i18next";
+import { FiZap } from "react-icons/fi";
+
+import { useDagServiceGetDagDetails, useTriggerServiceGetTriggers } from
"openapi/queries";
+import type { TriggerAssetWatcherReferenceResponse, TriggerDetailResponse }
from "openapi/requests/types.gen";
+
+import { StatusChip } from "src/components/StatusChip";
+
+import { FailingWatchersModal } from "./FailingWatchersModal";
+
+type Props = {
+ /** Whether a watcher of a failing trigger is one this chip speaks for. */
+ readonly isCounted: (watcher: TriggerAssetWatcherReferenceResponse) =>
boolean;
+ readonly triggers?: Array<TriggerDetailResponse>;
+};
+
+/** Failing watchers, as a red status chip that lists them in a modal, each
with its trigger's last error. */
+export const FailingWatchersChip = ({ isCounted, triggers }: Props) => {
+ const { t: translate } = useTranslation("dag");
+ const { onClose, onOpen, open } = useDisclosure();
+ const failing = (triggers ?? []).flatMap((trigger) =>
+ trigger.asset_watchers.filter(isCounted).map((watcher) => ({ trigger,
watcher })),
+ );
+
+ if (failing.length === 0) {
+ return undefined;
+ }
+
+ return (
+ <>
+ <StatusChip
+ icon={<FiZap />}
+ label={translate("failingWatchers", { count: failing.length })}
+ onClick={onOpen}
+ severity="error"
+ />
+ <FailingWatchersModal onClose={onClose} open={open} watchers={failing} />
+ </>
+ );
+};
+
+/** Failing watchers on the assets the Dag is scheduled on, which keep it from
running. */
+export const DagFailingWatchersChip = ({ dagId }: { readonly dagId: string })
=> {
+ // The details the breadcrumb already loads: only a Dag scheduled on assets
asks for its watchers.
+ const { data: dag } = useDagServiceGetDagDetails({ dagId }, undefined, {
enabled: dagId !== "" });
+ // Narrowed to this Dag on the server, so the page limit is per Dag.
+ const { data } = useTriggerServiceGetTriggers(
+ { kind: ["asset_watcher"], limit: 100, scheduledDagId: dagId, status:
["failing"] },
Review Comment:
This will need pagination.
##########
airflow-core/src/airflow/ui/src/constants/stateOptions.ts:
##########
@@ -81,3 +81,14 @@ export const dagRunTypeOptions = createListCollection({
{ label: "common:runTypes.asset_triggered", value: "asset_triggered" },
],
});
+
+export const triggerKinds: Array<TriggerKind> = ["task", "callback",
"asset_watcher"];
+
+export const triggerStatuses: Array<TriggerStatus> = ["pending", "running",
"failing", "orphaned"];
Review Comment:
This is just copying the kind and statuses. If the API changes then this
fails silently.
We already have `const taskStates: Record<TriggerStatus, TaskInstanceState>
= {` elsewhere
--
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]