Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Alerts management: failing tab includes abandoned + query failures (#75189)

Ngoc Khuat6884aea553b6
diff --git a/frontend/src/metabase-types/api/notification.ts b/frontend/src/metabase-types/api/notification.ts
index 43cc3526f57..abd9dbd6a8f 100644
--- a/frontend/src/metabase-types/api/notification.ts
+++ b/frontend/src/metabase-types/api/notification.ts
@@ -219,6 +219,7 @@ export type AdminNotificationListParams = {
recipient_email?: string;
channel?: NotificationChannelType | NotificationChannelType[];
last_send_status?: NotificationRunStatus;
+ last_check_status?: NotificationRunStatus;
query?: string;
sort_column?: AdminNotificationSortColumn;
sort_direction?: SortDirection;
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.tsx b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.tsx
index eb03f8d5701..878a35bd791 100644
--- a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.tsx
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.tsx
@@ -87,7 +87,7 @@ export const NotificationsAdminPage = ({
limit: 1,
offset: 0,
active: true,
- last_send_status: "failing",
+ last_check_status: "failing",
});
const { data: ownerlessData, isLoading: isOwnerlessLoading } =
useAdminListNotificationsQuery({
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.unit.spec.tsx b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.unit.spec.tsx
index b49c2b5f4b9..2b16d876c3f 100644
--- a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.unit.spec.tsx
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/NotificationsAdminPage.unit.spec.tsx
@@ -78,7 +78,7 @@ const setup = ({
const params = new URL(call.url).searchParams;
if (
params.get("limit") === "1" &&
- params.get("last_send_status") === "failing"
+ params.get("last_check_status") === "failing"
) {
return { data: [], total: failingCount, limit: 1, offset: 0 };
}
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/constants.ts b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/constants.ts
index 47db4b04655..7f6a56f5a5f 100644
--- a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/constants.ts
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/constants.ts
@@ -26,7 +26,9 @@ export const SORT_COLUMN_VALUES: AdminNotificationSortColumn[] = [
export const TAB_FILTERS: Record<NotificationsTab, TabFilters> = {
all: {},
- failing: { last_send_status: "failing" },
+ // The Failing tab filters on run health (last_check) — the whole-run rollup — so it catches
+ // query failures and abandoned runs, not just channel-send delivery failures.
+ failing: { last_check_status: "failing" },
ownerless: { creatorless: true },
};
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/types.ts b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/types.ts
index 73997a27b81..3a1375cf82e 100644
--- a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/types.ts
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/types.ts
@@ -26,7 +26,7 @@ export type NotificationsUrlState = {
};
export type TabFilters = Partial<
- Pick<AdminNotificationListParams, "last_send_status" | "creatorless">
+ Pick<AdminNotificationListParams, "last_check_status" | "creatorless">
>;
export type FilterDraft = {
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.ts b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.ts
index 7998e732e8b..d27f68da538 100644
--- a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.ts
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.ts
@@ -182,8 +182,8 @@ export const buildListParams = (
active: state.active ?? undefined,
query: state.query || undefined,
channel: state.channel.length === 0 ? undefined : state.channel,
- last_send_status:
- tabFilters.last_send_status ?? state.last_send_status ?? undefined,
+ last_send_status: state.last_send_status ?? undefined,
+ last_check_status: tabFilters.last_check_status ?? undefined,
creatorless: tabFilters.creatorless,
creator_active:
state.tab === "ownerless"
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.unit.spec.ts b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.unit.spec.ts
index 2b65fd10483..7ebb1da90f4 100644
--- a/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.unit.spec.ts
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsAdminPage/utils.unit.spec.ts
@@ -184,12 +184,13 @@ describe("NotificationsAdminPage/utils", () => {
).toBeUndefined();
});
- it("lets the failing tab override last_send_status", () => {
+ it("filters the failing tab on run health (last_check), not delivery (last_send)", () => {
const params = buildListParams(
- { ...DEFAULT_STATE, tab: "failing", last_send_status: "successful" },
+ { ...DEFAULT_STATE, tab: "failing" },
PAGE_SIZE,
);
- expect(params.last_send_status).toBe("failing");
+ expect(params.last_check_status).toBe("failing");
+ expect(params.last_send_status).toBeUndefined();
});
it("pins creatorless on the ownerless tab and suppresses creator_active", () => {
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsTabs/NotificationsTabs.tsx b/frontend/src/metabase/admin/tools/notifications/NotificationsTabs/NotificationsTabs.tsx
index e22dd6c265c..7966b9d9073 100644
--- a/frontend/src/metabase/admin/tools/notifications/NotificationsTabs/NotificationsTabs.tsx
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsTabs/NotificationsTabs.tsx
@@ -49,7 +49,15 @@ export const NotificationsTabs = ({
value: "failing",
icon: "warning_round",
label: t`Failing`,
- patch: { tab: "failing", last_send_status: null },
+ // The Failing tab filters on last_check, and abandoned / query-failed runs have no last_send
+ // (the default sort), so they'd sort to the bottom. Default this tab to last_check so the
+ // alerts it exists to surface land at the top.
+ patch: {
+ tab: "failing",
+ last_send_status: null,
+ sort_column: "last_check",
+ sort_direction: "desc",
+ },
});
}
diff --git a/frontend/src/metabase/admin/tools/notifications/NotificationsTabs/NotificationsTabs.unit.spec.tsx b/frontend/src/metabase/admin/tools/notifications/NotificationsTabs/NotificationsTabs.unit.spec.tsx
new file mode 100644
index 00000000000..4cf2ec16b22
--- /dev/null
+++ b/frontend/src/metabase/admin/tools/notifications/NotificationsTabs/NotificationsTabs.unit.spec.tsx
@@ -0,0 +1,33 @@
+import userEvent from "@testing-library/user-event";
+
+import { renderWithProviders, screen } from "__support__/ui";
+
+import { NotificationsTabs } from "./NotificationsTabs";
+
+jest.mock("../analytics", () => ({
+ trackAlertsManagementTabClicked: jest.fn(),
+}));
+
+describe("NotificationsTabs", () => {
+ it("defaults the Failing tab to sort by last_check so newly surfaced failures aren't buried at the bottom", async () => {
+ const onChange = jest.fn();
+ renderWithProviders(
+ <NotificationsTabs
+ tab="all"
+ failingCount={3}
+ ownerlessCount={0}
+ onChange={onChange}
+ />,
+ );
+
+ await userEvent.click(screen.getByRole("tab", { name: /Failing/ }));
+
+ expect(onChange).toHaveBeenCalledWith(
+ expect.objectContaining({
+ tab: "failing",
+ sort_column: "last_check",
+ sort_direction: "desc",
+ }),
+ );
+ });
+});
diff --git a/src/metabase/notification/api/admin.clj b/src/metabase/notification/api/admin.clj
index 3b35c370952..2eb38d35b92 100644
--- a/src/metabase/notification/api/admin.clj
+++ b/src/metabase/notification/api/admin.clj
@@ -34,6 +34,7 @@
[metabase.request.core :as request]
[metabase.util :as u]
[metabase.util.honey-sql-2 :as h2x]
+ [metabase.util.i18n :refer [tru]]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]
@@ -288,7 +289,7 @@
"Optional WHERE clauses for the list/detail query, returned as data (a seq with inactive filters
elided) so `base-list-query` can `(reduce sql.helpers/where ...)` them onto the query."
[{:keys [active creator_id creator_active creatorless card_id recipient_email channel
- last_send_status query]}]
+ last_send_status last_check_status query]}]
(keep
identity
[(when (some? active) [:= :notification.active active])
@@ -307,6 +308,13 @@
(case last_send_status
:successful [:= :ls.has_failure false]
:failing [:= :ls.has_failure true]))
+ ;; `last_check` is the whole-run rollup (success/failed/abandoned), so it's a superset of
+ ;; `last_send`: it catches query failures and heartbeat-abandoned runs that never reached the
+ ;; channel-send step, not just delivery failures. The Failing tab filters on this.
+ (when last_check_status
+ (case last_check_status
+ :successful [:= :lc.status "success"]
+ :failing [:in :lc.status ["failed" "abandoned"]]))
(when recipient_email
(let [ids (notification-ids-with-recipient-email recipient_email)]
;; No recipient matched → an always-false predicate so the page comes back empty. We use
@@ -412,7 +420,13 @@
(when (and status at)
(let [run-status (coerce-run-status status)]
{:at at
- :error (when (= run-status :failing) error)
+ ;; Abandoned runs are heartbeat-killed mid-flight, so they usually have no task_history
+ ;; message. Without a synthesized reason the Failing tab would show them as failing with a
+ ;; blank "why", which is the first question an admin asks. Fall back to an explanation.
+ :error (when (= run-status :failing)
+ (or error
+ (when (= status :abandoned)
+ (tru "The run was abandoned, likely because the instance restarted while it was queued."))))
:status run-status})))
(defn- coerce-status
@@ -522,7 +536,12 @@
`last_send_status` filter operates on the latest channel-send task_history row for the
notification's card (`successful` = latest channel-send succeeded; `failing` = latest
- channel-send failed). The Failing tab uses `?last_send_status=failing`.
+ channel-send failed).
+
+ `last_check_status` filter operates on the latest terminal TaskRun for the card — the whole-run
+ rollup (`successful` = the run succeeded; `failing` = it failed or was abandoned). This is a
+ superset of `last_send_status`: it also catches query failures and heartbeat-abandoned runs that
+ never reached the send step. The Failing tab uses `?last_check_status=failing`.
`creatorless=true` selects notifications with no creator or a deactivated creator (the Ownerless
tab). `creatorless=false` selects the inverse.
@@ -530,7 +549,7 @@
`channel` accepts a single string or a repeated query param for multi-select (OR logic)."
[_route
{:keys [active creator_id creator_active creatorless card_id recipient_email channel last_send_status
- query sort_column sort_direction]} :-
+ last_check_status query sort_column sort_direction]} :-
[:map
[:active {:optional true} [:maybe ms/BooleanValue]]
[:creator_id {:optional true} ms/PositiveInt]
@@ -540,23 +559,25 @@
[:recipient_email {:optional true} ms/NonBlankString]
[:channel {:optional true} [:maybe [:or ms/NonBlankString [:sequential ms/NonBlankString]]]]
[:last_send_status {:optional true} ::run-status]
+ [:last_check_status {:optional true} ::run-status]
[:query {:optional true} ms/NonBlankString]
[:sort_column {:default :last_send} ::sort-column]
[:sort_direction {:default :desc} ::sort-direction]]]
(api/check-superuser)
- (list-notifications {:limit (or (request/limit) 50)
- :offset (or (request/offset) 0)
- :active active
- :creator_id creator_id
- :creator_active creator_active
- :creatorless creatorless
- :card_id card_id
- :recipient_email recipient_email
- :channel channel
- :last_send_status last_send_status
- :query query
- :sort_column sort_column
- :sort_direction sort_direction}))
+ (list-notifications {:limit (or (request/limit) 50)
+ :offset (or (request/offset) 0)
+ :active active
+ :creator_id creator_id
+ :creator_active creator_active
+ :creatorless creatorless
+ :card_id card_id
+ :recipient_email recipient_email
+ :channel channel
+ :last_send_status last_send_status
+ :last_check_status last_check_status
+ :query query
+ :sort_column sort_column
+ :sort_direction sort_direction}))
;; ---------------------------------------------------------------------------
;; Detail endpoint helpers
diff --git a/test/metabase/notification/api/admin_test.clj b/test/metabase/notification/api/admin_test.clj
index e84da6c4c1a..02e5d846301 100644
--- a/test/metabase/notification/api/admin_test.clj
+++ b/test/metabase/notification/api/admin_test.clj
@@ -534,7 +534,10 @@
:ended_at (t/instant)}]
(let [{:keys [data]} (mt/user-http-request :crowberto :get 200 "notification/admin")]
(is (=? {:last_check {:status "failing"}}
- (find-row-by-id data nid)))))))
+ (find-row-by-id data nid)))
+ (testing "abandoned runs have no task_history message, so we synthesize a reason"
+ (is (re-find #"abandoned"
+ (get-in (find-row-by-id data nid) [:last_check :error]))))))))
(deftest last-check-skips-in-flight-runs-test
(testing "an :started in-flight run is invisible to the admin list — last_check stays nil"
@@ -623,6 +626,76 @@
(is (not (contains? ids (:id success-n))))
(is (not (contains? ids (:id no-send-n)))))))))
+(deftest filter-by-last-check-status-test
+ (testing "?last_check_status=failing catches run failures the send filter misses (abandoned, query failures)"
+ (mt/with-temp [:model/Card {ok-card :id} {:archived false}
+ :model/Card {abandoned-card :id} {:archived false}
+ :model/Card {query-fail-card :id} {:archived false}
+ :model/NotificationCard {nc-ok :id} {:card_id ok-card}
+ :model/NotificationCard {nc-abandoned :id} {:card_id abandoned-card}
+ :model/NotificationCard {nc-query-fail :id} {:card_id query-fail-card}
+ :model/Notification ok-n {:payload_type :notification/card
+ :payload_id nc-ok
+ :creator_id (mt/user->id :crowberto)}
+ :model/Notification abandoned-n {:payload_type :notification/card
+ :payload_id nc-abandoned
+ :creator_id (mt/user->id :crowberto)}
+ :model/Notification query-fail-n {:payload_type :notification/card
+ :payload_id nc-query-fail
+ :creator_id (mt/user->id :crowberto)}
+ :model/TaskRun {ok-run :id} {:run_type :alert
+ :entity_type :card
+ :entity_id ok-card
+ :status :success
+ :started_at (t/instant)
+ :ended_at (t/instant)}
+ ;; heartbeat-killed run that never reached the channel-send step
+ :model/TaskRun _abandoned-run {:run_type :alert
+ :entity_type :card
+ :entity_id abandoned-card
+ :status :abandoned
+ :started_at (t/instant)
+ :ended_at (t/instant)}
+ ;; query failure: notification-send failed, never produced a channel-send row
+ :model/TaskRun {qf-run :id} {:run_type :alert
+ :entity_type :card
+ :entity_id query-fail-card
+ :status :failed
+ :started_at (t/instant)
+ :ended_at (t/instant)}
+ :model/TaskHistory _ok-th {:task "channel-send"
+ :run_id ok-run
+ :status :success
+ :started_at (t/instant)
+ :ended_at (t/instant)
+ :task_details {:channel_type "channel/email"}}
+ :model/TaskHistory _qf-th {:task "notification-send"
+ :run_id qf-run
+ :status :failed
+ :started_at (t/instant)
+ :ended_at (t/instant)
+ :task_details {:message "Query timed out"}}]
+ (testing "last_check_status=failing surfaces abandoned + query failures"
+ (let [{:keys [data]} (mt/user-http-request :crowberto :get 200 "notification/admin"
+ :last_check_status "failing")
+ ids (set (map :id data))]
+ (is (contains? ids (:id abandoned-n)))
+ (is (contains? ids (:id query-fail-n)))
+ (is (not (contains? ids (:id ok-n))))))
+ (testing "last_send_status=failing misses them — no channel-send failure exists (the original bug)"
+ (let [{:keys [data]} (mt/user-http-request :crowberto :get 200 "notification/admin"
+ :last_send_status "failing")
+ ids (set (map :id data))]
+ (is (not (contains? ids (:id abandoned-n))))
+ (is (not (contains? ids (:id query-fail-n))))))
+ (testing "last_check_status=successful matches only the healthy run"
+ (let [{:keys [data]} (mt/user-http-request :crowberto :get 200 "notification/admin"
+ :last_check_status "successful")
+ ids (set (map :id data))]
+ (is (contains? ids (:id ok-n)))
+ (is (not (contains? ids (:id abandoned-n))))
+ (is (not (contains? ids (:id query-fail-n)))))))))
+
(deftest filter-compound-test
(testing "multiple filters AND together (active + owner_id)"
(mt/with-temp [:model/User {target-user :id} {}