bbovenzi commented on code in PR #76567:
URL: https://github.com/apache/airflow/pull/76567#discussion_r4604898337


##########
airflow-core/src/airflow/ui/src/components/FailingWatchersChip.tsx:
##########
@@ -0,0 +1,56 @@
+/*!
+ * 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 { StatusChip } from "src/components/StatusChip";
+
+import { FailingWatchersModal, getFailingWatchersParams } from 
"./FailingWatchersModal";
+
+/** Failing watchers on the assets the Dag is scheduled on, which keep it from 
running. */
+export const DagFailingWatchersChip = ({ dagId }: { readonly dagId: string }) 
=> {
+  const { t: translate } = useTranslation("dag");
+  const { onClose, onOpen, open } = useDisclosure();
+  // The details the breadcrumb already loads: only a Dag scheduled on assets 
asks for its watchers.
+  const { data: dag } = useDagServiceGetDagDetails({ dagId }, undefined, { 
enabled: dagId !== "" });
+  // Only the count is needed: the modal reads them a page at a time.
+  const { data } = useTriggerServiceGetTriggers({ 
...getFailingWatchersParams(dagId), limit: 0 }, undefined, {
+    enabled: Boolean(dag?.asset_expression),
+  });

Review Comment:
   Let's use auto refresh here.



##########
airflow-core/src/airflow/ui/src/components/Graph/AssetWatcherNode.tsx:
##########
@@ -0,0 +1,61 @@
+/*!
+ * 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, HStack, LinkOverlay } from "@chakra-ui/react";
+import type { NodeProps, Node as NodeType } from "@xyflow/react";
+import { FiEye } from "react-icons/fi";
+import { Link as RouterLink } from "react-router-dom";

Review Comment:
   Let's try to use RouterLink from src/system-components instead.



##########
airflow-core/src/airflow/ui/src/pages/Trigger/UsedBy.tsx:
##########
@@ -0,0 +1,187 @@
+/*!
+ * 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 { useState, type PropsWithChildren } from "react";
+
+import { Table } from "@chakra-ui/react";
+import type { ColumnDef } from "@tanstack/react-table";
+import type { TFunction } from "i18next";
+import { useTranslation } from "react-i18next";
+import { useParams } from "react-router-dom";
+
+import {
+  useAssetServiceGetAssetEvents,
+  useAssetServiceGetAssets,
+  useTriggerServiceGetTrigger,
+} from "openapi/queries";
+import type { AssetResponse, CallbackState, TaskInstanceState } from 
"openapi/requests/types.gen";
+
+import { RouterLink } from "src/system-components";
+
+import { TriggerUsedBy } from "src/pages/Triggers/TriggerUsedBy";
+
+import { AssetEvents } from "src/components/Assets/AssetEvents";
+import { DataTable } from "src/components/DataTable";
+import type { DataTableFeatures } from "src/components/DataTable/features";
+import { StateBadge } from "src/components/StateBadge";
+import Time from "src/components/Time";
+
+import { useConfig } from "src/queries/useConfig";
+
+const RECENT_EVENTS = 5;
+
+type AssetRow = { row: { original: AssetResponse } };
+
+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 createColumns = (
+  translate: TFunction,
+  triggerId: number,
+): Array<ColumnDef<DataTableFeatures, AssetResponse>> => [
+  {
+    accessorKey: "name",
+    cell: ({ row: { original } }: AssetRow) => (
+      <RouterLink to={`/assets/${original.id}`}>{original.name}</RouterLink>
+    ),
+    enableSorting: false,
+    header: translate("asset_one"),
+  },
+  {
+    accessorKey: "watchers",
+    cell: ({ row: { original } }: AssetRow) =>
+      original.watchers
+        .filter((watcher) => watcher.trigger_id === triggerId)
+        .map((watcher) => watcher.name)
+        .join(", "),
+    enableSorting: false,
+    header: translate("watcher_one"),
+  },
+  {
+    accessorKey: "last_asset_event",
+    cell: ({ row: { original } }: AssetRow) => <Time 
datetime={original.last_asset_event?.timestamp} />,
+    enableSorting: false,
+    header: translate("lastAssetEvent"),
+  },
+];
+
+/**
+ * Each watched asset with its own latest event, so a busy asset cannot hide a 
quiet one's, then the
+ * latest events of them all. Both requests name the trigger, not its assets, 
so neither grows with them.
+ */
+const WatchedAssets = ({
+  hasWatchers,
+  triggerId,
+}: {
+  readonly hasWatchers: boolean;
+  readonly triggerId: number;
+}) => {
+  const { t: translate } = useTranslation("assets");
+  const pageSize = useConfig("fallback_page_limit") as number;
+  const [pageIndex, setPageIndex] = useState(0);
+  const { data: assets, isLoading: isLoadingAssets } = 
useAssetServiceGetAssets(
+    { limit: pageSize, offset: pageIndex * pageSize, triggerId },
+    undefined,
+    { enabled: hasWatchers, placeholderData: (prev) => prev },
+  );
+  const { data: events, isLoading: isLoadingEvents } = 
useAssetServiceGetAssetEvents(
+    { limit: RECENT_EVENTS, orderBy: ["-timestamp"], triggerId },
+    undefined,
+    { enabled: hasWatchers },
+  );
+
+  return (
+    <>
+      <DataTable
+        columns={createColumns(translate, triggerId)}
+        data={assets?.assets ?? []}
+        hideRowCountHeading
+        initialState={{ pagination: { pageIndex, pageSize }, sorting: [] }}
+        isLoading={isLoadingAssets}
+        modelName="common:asset"
+        onStateChange={(state) => setPageIndex(state.pagination.pageIndex)}
+        total={assets?.total_entries ?? 0}
+      />
+      <AssetEvents data={events} isLoading={isLoadingEvents} px={0} />
+    </>
+  );
+};
+
+export const UsedBy = () => {
+  const { t: translate } = useTranslation(["common", "dag"]);
+  const { triggerId = "" } = useParams();
+  const { data: trigger } = useTriggerServiceGetTrigger({ triggerId: 
Number(triggerId) });
+
+  if (trigger === undefined) {
+    return undefined;
+  }
+
+  const { asset_watchers: watchers, callback, task_instance: ti } = trigger;
+
+  if (ti !== null) {
+    return (
+      <Table.Root striped>
+        <Table.Body>
+          <Row label={translate("taskInstance_one")}>
+            <TriggerUsedBy trigger={trigger} />
+          </Row>
+          <Row label={translate("dagId")}>{ti.dag_id}</Row>
+          <Row label={translate("runId")}>{ti.dag_run_id}</Row>
+          <Row label={translate("taskId")}>{ti.task_id}</Row>
+          {ti.map_index >= 0 ? <Row 
label={translate("mapIndex")}>{ti.map_index}</Row> : undefined}
+          <Row label={translate("tryNumber")}>{ti.try_number}</Row>
+          <Row label={translate("state")}>
+            <StateBadge state={ti.state as TaskInstanceState | null}>

Review Comment:
   We shouldn't need to type-cast the state. This should be handled by the 
datamodel on the API-level



##########
airflow-core/src/airflow/ui/tests/e2e/specs/triggers.spec.ts:
##########
@@ -0,0 +1,74 @@
+/*!
+ * 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 { expect, test } from "tests/e2e/fixtures";
+import { waitForDagReady } from "tests/e2e/utils/api/dag-runs";
+import { baseUrl } from "tests/e2e/utils/shared";
+
+const WATCHER_DAG_ID = "example_asset_with_watchers";
+
+test.describe("Triggers page", () => {
+  test.setTimeout(120_000);
+
+  test.beforeAll(async ({ authenticatedRequest }) => {
+    test.setTimeout(300_000);
+    const response = await 
authenticatedRequest.get(`${baseUrl}/api/v2/triggers?limit=1`);
+
+    test.skip(response.status() === 404, "This Airflow has no GET 
/api/v2/triggers");
+
+    // A watcher's trigger exists only while its Dag is unpaused; the global 
teardown pauses it again.
+    await waitForDagReady(authenticatedRequest, WATCHER_DAG_ID);
+    await 
authenticatedRequest.patch(`${baseUrl}/api/v2/dags/${WATCHER_DAG_ID}`, {
+      data: { is_paused: false },
+    });
+
+    await expect
+      .poll(
+        async () => {
+          const running = await authenticatedRequest.get(
+            
`${baseUrl}/api/v2/triggers?kind=asset_watcher&status=running&limit=1`,
+          );
+
+          return running.ok() ? ((await running.json()) as { total_entries: 
number }).total_entries : 0;
+        },
+        { intervals: [5000], timeout: 240_000 },
+      )
+      .toBeGreaterThan(0);
+  });
+
+  test("filters to running watchers and opens one", async ({ page, 
triggersPage }) => {
+    await triggersPage.navigate();
+    await triggersPage.addFilter("Kind", "Asset Watcher");
+    await triggersPage.addFilter("Status", "Running");
+
+    await expect(page).toHaveURL(/kind=asset_watcher/u);
+    await expect(page).toHaveURL(/status=running/u);
+    // Retried: the table shows placeholder rows with empty cells while it 
loads.
+    await expect(async () => {
+      expect(new Set(await triggersPage.columnTexts("Kind"))).toEqual(new 
Set(["Asset Watcher"]));
+      expect(new Set(await triggersPage.columnTexts("Status"))).toEqual(new 
Set(["Running"]));
+    }).toPass({ timeout: 30_000 });
+
+    await triggersPage.tableRows.first().getByRole("link").first().click();

Review Comment:
   Let's add some data-testid's instead of using `first()`. That would be much 
more reliable.



##########
airflow-core/src/airflow/ui/src/pages/Trigger/UsedBy.tsx:
##########
@@ -0,0 +1,187 @@
+/*!
+ * 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 { useState, type PropsWithChildren } from "react";
+
+import { Table } from "@chakra-ui/react";
+import type { ColumnDef } from "@tanstack/react-table";
+import type { TFunction } from "i18next";
+import { useTranslation } from "react-i18next";
+import { useParams } from "react-router-dom";
+
+import {
+  useAssetServiceGetAssetEvents,
+  useAssetServiceGetAssets,
+  useTriggerServiceGetTrigger,
+} from "openapi/queries";
+import type { AssetResponse, CallbackState, TaskInstanceState } from 
"openapi/requests/types.gen";
+
+import { RouterLink } from "src/system-components";
+
+import { TriggerUsedBy } from "src/pages/Triggers/TriggerUsedBy";
+
+import { AssetEvents } from "src/components/Assets/AssetEvents";
+import { DataTable } from "src/components/DataTable";
+import type { DataTableFeatures } from "src/components/DataTable/features";
+import { StateBadge } from "src/components/StateBadge";
+import Time from "src/components/Time";
+
+import { useConfig } from "src/queries/useConfig";
+
+const RECENT_EVENTS = 5;
+
+type AssetRow = { row: { original: AssetResponse } };
+
+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 createColumns = (
+  translate: TFunction,
+  triggerId: number,
+): Array<ColumnDef<DataTableFeatures, AssetResponse>> => [
+  {
+    accessorKey: "name",
+    cell: ({ row: { original } }: AssetRow) => (
+      <RouterLink to={`/assets/${original.id}`}>{original.name}</RouterLink>
+    ),
+    enableSorting: false,
+    header: translate("asset_one"),
+  },
+  {
+    accessorKey: "watchers",
+    cell: ({ row: { original } }: AssetRow) =>
+      original.watchers
+        .filter((watcher) => watcher.trigger_id === triggerId)
+        .map((watcher) => watcher.name)
+        .join(", "),
+    enableSorting: false,
+    header: translate("watcher_one"),
+  },
+  {
+    accessorKey: "last_asset_event",
+    cell: ({ row: { original } }: AssetRow) => <Time 
datetime={original.last_asset_event?.timestamp} />,
+    enableSorting: false,
+    header: translate("lastAssetEvent"),
+  },
+];
+
+/**
+ * Each watched asset with its own latest event, so a busy asset cannot hide a 
quiet one's, then the
+ * latest events of them all. Both requests name the trigger, not its assets, 
so neither grows with them.
+ */
+const WatchedAssets = ({
+  hasWatchers,
+  triggerId,
+}: {
+  readonly hasWatchers: boolean;
+  readonly triggerId: number;
+}) => {
+  const { t: translate } = useTranslation("assets");
+  const pageSize = useConfig("fallback_page_limit") as number;
+  const [pageIndex, setPageIndex] = useState(0);
+  const { data: assets, isLoading: isLoadingAssets } = 
useAssetServiceGetAssets(
+    { limit: pageSize, offset: pageIndex * pageSize, triggerId },
+    undefined,
+    { enabled: hasWatchers, placeholderData: (prev) => prev },
+  );
+  const { data: events, isLoading: isLoadingEvents } = 
useAssetServiceGetAssetEvents(
+    { limit: RECENT_EVENTS, orderBy: ["-timestamp"], triggerId },
+    undefined,
+    { enabled: hasWatchers },
+  );
+
+  return (
+    <>
+      <DataTable
+        columns={createColumns(translate, triggerId)}
+        data={assets?.assets ?? []}
+        hideRowCountHeading
+        initialState={{ pagination: { pageIndex, pageSize }, sorting: [] }}
+        isLoading={isLoadingAssets}
+        modelName="common:asset"
+        onStateChange={(state) => setPageIndex(state.pagination.pageIndex)}
+        total={assets?.total_entries ?? 0}
+      />
+      <AssetEvents data={events} isLoading={isLoadingEvents} px={0} />
+    </>
+  );
+};
+
+export const UsedBy = () => {
+  const { t: translate } = useTranslation(["common", "dag"]);
+  const { triggerId = "" } = useParams();
+  const { data: trigger } = useTriggerServiceGetTrigger({ triggerId: 
Number(triggerId) });
+
+  if (trigger === undefined) {
+    return undefined;
+  }
+
+  const { asset_watchers: watchers, callback, task_instance: ti } = trigger;

Review Comment:
   Nit. Let's not call variables `ti`, use `taskInstance`



-- 
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]

Reply via email to