Skip to content

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

[GDGT-2503] add sorting by duration to transform runs list (#75102)

YB31fb67c2703a
diff --git a/frontend/src/metabase-types/api/transform.ts b/frontend/src/metabase-types/api/transform.ts
index 81c6c86afc7..9a3af385ddc 100644
--- a/frontend/src/metabase-types/api/transform.ts
+++ b/frontend/src/metabase-types/api/transform.ts
@@ -182,6 +182,7 @@ export const TRANSFORM_RUN_SORT_COLUMNS = [
"status",
"run-method",
"transform-tags",
+ "duration",
] as const;
export type TransformRunSortColumn =
(typeof TRANSFORM_RUN_SORT_COLUMNS)[number];
diff --git a/frontend/src/metabase/query_builder/components/view/ExecutionTime/ExecutionTime.tsx b/frontend/src/metabase/query_builder/components/view/ExecutionTime/ExecutionTime.tsx
index a4e781c52e5..63f80a0f33d 100644
--- a/frontend/src/metabase/query_builder/components/view/ExecutionTime/ExecutionTime.tsx
+++ b/frontend/src/metabase/query_builder/components/view/ExecutionTime/ExecutionTime.tsx
@@ -1,8 +1,7 @@
import { t } from "ttag";
import { FixedSizeIcon, Flex, Tooltip } from "metabase/ui";
-
-import { formatDuration } from "./utils";
+import { formatDurationLong } from "metabase/utils/formatting/time";
/**
* `time` can most likely never be `null`
@@ -28,7 +27,7 @@ export const ExecutionTime = ({ time }: Props) => {
aria-label={label}
>
<FixedSizeIcon name="bolt" />
- {formatDuration(time)}
+ {formatDurationLong(time)}
</Flex>
</Tooltip>
);
diff --git a/frontend/src/metabase/query_builder/components/view/ExecutionTime/utils.ts b/frontend/src/metabase/query_builder/components/view/ExecutionTime/utils.ts
deleted file mode 100644
index 931e9c5aca6..00000000000
--- a/frontend/src/metabase/query_builder/components/view/ExecutionTime/utils.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import { t } from "ttag";
-
-export const formatDuration = (time: number): string => {
- if (time < 1000) {
- return t`${time}ms`;
- }
-
- return t`${(time / 1000).toFixed(1)}s`;
-};
diff --git a/frontend/src/metabase/query_builder/components/view/ExecutionTime/utils.unit.spec.ts b/frontend/src/metabase/query_builder/components/view/ExecutionTime/utils.unit.spec.ts
deleted file mode 100644
index 1f62b2a551f..00000000000
--- a/frontend/src/metabase/query_builder/components/view/ExecutionTime/utils.unit.spec.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { formatDuration } from "./utils";
-
-describe("formatDuration", () => {
- it("formats duration correctly", () => {
- expect(formatDuration(100)).toBe("100ms");
- expect(formatDuration(999)).toBe("999ms");
- expect(formatDuration(1000)).toBe("1.0s");
- expect(formatDuration(1425)).toBe("1.4s");
- expect(formatDuration(1475)).toBe("1.5s");
- expect(formatDuration(1500)).toBe("1.5s");
- expect(formatDuration(120000)).toBe("120.0s");
- });
-});
diff --git a/frontend/src/metabase/transforms/pages/RunListPage/RunListPage.unit.spec.tsx b/frontend/src/metabase/transforms/pages/RunListPage/RunListPage.unit.spec.tsx
index 39a2b83eac8..c1f32dfe854 100644
--- a/frontend/src/metabase/transforms/pages/RunListPage/RunListPage.unit.spec.tsx
+++ b/frontend/src/metabase/transforms/pages/RunListPage/RunListPage.unit.spec.tsx
@@ -85,4 +85,75 @@ describe("RunListPage", () => {
const infoSection = screen.getByRole("region", { name: "Info" });
expect(within(infoSection).getByText("Failed")).toBeInTheDocument();
});
+
+ describe("Duration column", () => {
+ it("renders a Duration column header when the table is populated", async () => {
+ const transform = createMockTransform({ name: "Some Transform" });
+ const run = createMockTransformRun({ status: "succeeded", transform });
+ setup({ runs: [run] });
+ expect(
+ await screen.findByRole("columnheader", { name: /duration/i }),
+ ).toBeInTheDocument();
+ });
+
+ it("renders the formatted duration for completed runs", async () => {
+ // 8m 42s duration: start at 00:00:00, end at 00:08:42.
+ const transform = createMockTransform({ name: "Long batch" });
+ const run = createMockTransformRun({
+ status: "succeeded",
+ transform,
+ start_time: "2026-01-01T00:00:00.000Z",
+ end_time: "2026-01-01T00:08:42.000Z",
+ });
+ setup({ runs: [run] });
+ expect(await screen.findByText("8m 42s")).toBeInTheDocument();
+ });
+
+ it("renders the placeholder for runs still in progress (no end_time)", async () => {
+ const transform = createMockTransform({ name: "In flight" });
+ const run = createMockTransformRun({
+ status: "started",
+ transform,
+ start_time: "2026-01-01T00:00:00.000Z",
+ end_time: null,
+ });
+ setup({ runs: [run] });
+
+ // Resolve the Duration column's index from the headers (robust to
+ // column reordering); then scope the placeholder assertion to that
+ // specific gridcell in the in-progress row.
+ const headers = await screen.findAllByRole("columnheader");
+ const durationIndex = headers.findIndex((h) =>
+ /duration/i.test(h.textContent ?? ""),
+ );
+ expect(durationIndex).toBeGreaterThan(-1);
+
+ const row = await screen.findByRole("row", { name: /In flight/ });
+ const cells = within(row).getAllByRole("gridcell");
+ expect(cells[durationIndex]).toHaveTextContent("—");
+ });
+ });
+
+ describe("Ended at column null handling", () => {
+ it("renders the placeholder for runs still in progress (no end_time)", async () => {
+ const transform = createMockTransform({ name: "Mid flight" });
+ const run = createMockTransformRun({
+ status: "started",
+ transform,
+ start_time: "2026-01-01T00:00:00.000Z",
+ end_time: null,
+ });
+ setup({ runs: [run] });
+
+ const headers = await screen.findAllByRole("columnheader");
+ const endedAtIndex = headers.findIndex((h) =>
+ /ended at/i.test(h.textContent ?? ""),
+ );
+ expect(endedAtIndex).toBeGreaterThan(-1);
+
+ const row = await screen.findByRole("row", { name: /Mid flight/ });
+ const cells = within(row).getAllByRole("gridcell");
+ expect(cells[endedAtIndex]).toHaveTextContent("—");
+ });
+ });
});
diff --git a/frontend/src/metabase/transforms/pages/RunListPage/RunTable/utils.tsx b/frontend/src/metabase/transforms/pages/RunListPage/RunTable/utils.tsx
index cde2bd2bfac..ba68f493033 100644
--- a/frontend/src/metabase/transforms/pages/RunListPage/RunTable/utils.tsx
+++ b/frontend/src/metabase/transforms/pages/RunListPage/RunTable/utils.tsx
@@ -1,6 +1,14 @@
import type { SortingState } from "@tanstack/react-table";
import { t } from "ttag";
+import {
+ formatRunMethod,
+ formatStatus,
+ getRunDurationMs,
+ getTransformRunName,
+ isErrorStatus,
+ parseTimestampWithTimezone,
+} from "metabase/transforms/utils";
import type { TreeTableColumnDef } from "metabase/ui";
import {
Box,
@@ -10,6 +18,7 @@ import {
Text,
Tooltip,
} from "metabase/ui";
+import { formatDurationLong } from "metabase/utils/formatting/time";
import {
TRANSFORM_RUN_SORT_COLUMNS,
type TransformRun,
@@ -18,18 +27,13 @@ import {
type TransformTagId,
} from "metabase-types/api";
-import {
- formatRunMethod,
- formatStatus,
- getTransformRunName,
- isErrorStatus,
- parseTimestampWithTimezone,
-} from "../../../utils";
import type { TransformRunSortOptions } from "../types";
import { TagList } from "./TagList";
import { TimezoneIndicator } from "./TimezoneIndicator";
+const EMPTY_CELL_PLACEHOLDER = "—";
+
function getTransformColumn(): TreeTableColumnDef<TransformRun> {
return {
id: "transform-name" satisfies TransformRunSortColumn,
@@ -117,8 +121,33 @@ function getEndedAtColumn(
: null;
},
cell: ({ getValue }) => {
- const value = getValue();
- return value != null ? <Ellipsified>{String(value)}</Ellipsified> : null;
+ const value = getValue<string | null>();
+ if (value == null) {
+ return EMPTY_CELL_PLACEHOLDER;
+ }
+ return <Ellipsified>{String(value)}</Ellipsified>;
+ },
+ };
+}
+
+function getDurationColumn(): TreeTableColumnDef<TransformRun> {
+ return {
+ id: "duration" satisfies TransformRunSortColumn,
+ header: ({ header }) => (
+ <SortableHeaderPill
+ name={t`Duration`}
+ sort={header.column.getIsSorted() || undefined}
+ />
+ ),
+ width: 120,
+ enableSorting: true,
+ accessorFn: (run) => getRunDurationMs(run),
+ cell: ({ getValue }) => {
+ const ms = getValue<number | null>();
+ if (ms == null) {
+ return EMPTY_CELL_PLACEHOLDER;
+ }
+ return <Ellipsified>{formatDurationLong(ms)}</Ellipsified>;
},
};
}
@@ -200,6 +229,7 @@ export function getColumns(
getTransformColumn(),
getStartedAtColumn(systemTimezone),
getEndedAtColumn(systemTimezone),
+ getDurationColumn(),
getStatusColumn(),
getRunMethodColumn(),
getTransformTagsColumn(tagsById),
diff --git a/frontend/src/metabase/transforms/utils.ts b/frontend/src/metabase/transforms/utils.ts
index 959e37f80fd..d4091e11cf4 100644
--- a/frontend/src/metabase/transforms/utils.ts
+++ b/frontend/src/metabase/transforms/utils.ts
@@ -273,3 +273,21 @@ export const getRootCollectionItem = ({
}
return null;
};
+
+/**
+ * Returns the duration in ms of a transform run, or null when it cannot be
+ * measured (run still in progress, missing timestamps, unparseable values).
+ */
+export function getRunDurationMs(
+ run: Pick<TransformRun, "start_time" | "end_time"> | null | undefined,
+): number | null {
+ if (run == null || run.end_time == null) {
+ return null;
+ }
+ const start = Date.parse(run.start_time);
+ const end = Date.parse(run.end_time);
+ if (Number.isNaN(start) || Number.isNaN(end)) {
+ return null;
+ }
+ return end - start;
+}
diff --git a/frontend/src/metabase/transforms/utils.unit.spec.ts b/frontend/src/metabase/transforms/utils.unit.spec.ts
index 5acf3aff54b..31b99df58ec 100644
--- a/frontend/src/metabase/transforms/utils.unit.spec.ts
+++ b/frontend/src/metabase/transforms/utils.unit.spec.ts
@@ -1,7 +1,11 @@
import type { Database } from "metabase-types/api";
import { createMockDatabase } from "metabase-types/api/mocks";
-import { parseTimestampWithTimezone, validateDatabase } from "./utils";
+import {
+ getRunDurationMs,
+ parseTimestampWithTimezone,
+ validateDatabase,
+} from "./utils";
describe("parseTimestamp", () => {
const value = "2025-09-04T16:25:03.000Z";
@@ -81,3 +85,36 @@ describe("validateDatabase", () => {
expect(result.message).toBe(message);
});
});
+
+describe("getRunDurationMs", () => {
+ const mkRun = (
+ overrides: Partial<{ start_time: string; end_time: string | null }>,
+ ) => ({
+ start_time: overrides.start_time ?? "2026-01-01T00:00:00.000Z",
+ end_time: overrides.end_time ?? null,
+ });
+
+ it("returns null when end_time is null (run in progress)", () => {
+ expect(getRunDurationMs(mkRun({ end_time: null }))).toBeNull();
+ });
+
+ it("returns the difference in ms when both timestamps are present", () => {
+ expect(
+ getRunDurationMs(
+ mkRun({
+ start_time: "2026-01-01T00:00:00.000Z",
+ end_time: "2026-01-01T00:00:08.500Z",
+ }),
+ ),
+ ).toBe(8_500);
+ });
+
+ it("returns null when either timestamp is unparseable", () => {
+ expect(getRunDurationMs(mkRun({ end_time: "not-a-date" }))).toBeNull();
+ });
+
+ it("returns null when the input itself is null/undefined", () => {
+ expect(getRunDurationMs(null)).toBeNull();
+ expect(getRunDurationMs(undefined)).toBeNull();
+ });
+});
diff --git a/frontend/src/metabase/utils/formatting/time.ts b/frontend/src/metabase/utils/formatting/time.ts
index f5641054eab..f6e504a8429 100644
--- a/frontend/src/metabase/utils/formatting/time.ts
+++ b/frontend/src/metabase/utils/formatting/time.ts
@@ -1,5 +1,5 @@
-import type { Dayjs } from "dayjs";
-import { msgid, ngettext } from "ttag";
+import dayjs, { type Dayjs } from "dayjs";
+import { msgid, ngettext, t } from "ttag";
import type { TimeOnlyOptions } from "metabase/utils/formatting/types";
import { parseTime, parseTimestamp } from "metabase/utils/time-dayjs";
@@ -29,6 +29,47 @@ export function duration(milliseconds: number) {
return ngettext(msgid`${seconds} second`, `${seconds} seconds`, seconds);
}
+/**
+ * Human-readable duration formatter.
+ *
+ * Ladder:
+ * < 1s → "Nms" (e.g. "750ms")
+ * < 1min → "N.Ms" (e.g. "8.4s")
+ * < 1hr → "Xm Ys" (e.g. "8m 42s")
+ * ≥ 1hr → "Xh Ym" (e.g. "2h 17m")
+ *
+ * Negative inputs (defensive — e.g. clock skew between FE and DB) clamp to 0ms.
+ *
+ * Uses dayjs.duration internally for component decomposition (the duration
+ * plugin is already extended globally at frontend/src/metabase/utils/dayjs.ts),
+ * but NOT its .format("HH:mm:ss") because that produces clock-style output
+ * rather than the readable ladder we want.
+ */
+export function formatDurationLong(durationMs: number): string {
+ if (durationMs <= 0) {
+ return t`0ms`;
+ }
+ if (durationMs < 1_000) {
+ const ms = Math.round(durationMs);
+ return t`${ms}ms`;
+ }
+ if (durationMs < 60_000) {
+ const seconds = (durationMs / 1_000).toFixed(1);
+ return t`${seconds}s`;
+ }
+ const d = dayjs.duration(durationMs);
+ if (durationMs < 3_600_000) {
+ const minutes = d.minutes();
+ const seconds = d.seconds();
+ return t`${minutes}m ${seconds}s`;
+ }
+ // Hour-plus: use TOTAL hours (asHours can exceed 24 for very long
+ // backfills) plus the leftover minutes within the hour.
+ const hours = Math.floor(d.asHours());
+ const minutes = d.minutes();
+ return t`${hours}h ${minutes}m`;
+}
+
interface TimeWithUnitType {
local?: boolean;
time_enabled?: "minutes" | "milliseconds" | "seconds" | boolean;
diff --git a/frontend/src/metabase/utils/formatting/time.unit.spec.ts b/frontend/src/metabase/utils/formatting/time.unit.spec.ts
index 3061d25223f..84929ec4814 100644
--- a/frontend/src/metabase/utils/formatting/time.unit.spec.ts
+++ b/frontend/src/metabase/utils/formatting/time.unit.spec.ts
@@ -1,4 +1,4 @@
-import { formatTime, formatTimeWithUnit } from "./time";
+import { formatDurationLong, formatTime, formatTimeWithUnit } from "./time";
describe("time", () => {
describe("formatTime", () => {
@@ -74,4 +74,36 @@ describe("time", () => {
expect(formatTimeWithUnit(14.4, "hour-of-day", options)).toEqual("14");
});
});
+
+ describe("formatDurationLong", () => {
+ it("formats 0 and sub-second durations in ms", () => {
+ expect(formatDurationLong(0)).toBe("0ms");
+ expect(formatDurationLong(1)).toBe("1ms");
+ expect(formatDurationLong(999)).toBe("999ms");
+ });
+
+ it("formats sub-minute durations as seconds with one decimal", () => {
+ expect(formatDurationLong(1_000)).toBe("1.0s");
+ expect(formatDurationLong(1_500)).toBe("1.5s");
+ expect(formatDurationLong(59_900)).toBe("59.9s");
+ });
+
+ it("formats sub-hour durations as Xm Ys", () => {
+ expect(formatDurationLong(60_000)).toBe("1m 0s");
+ expect(formatDurationLong(90_000)).toBe("1m 30s");
+ expect(formatDurationLong(522_000)).toBe("8m 42s");
+ expect(formatDurationLong(3_599_000)).toBe("59m 59s");
+ });
+
+ it("formats hour-plus durations as Xh Ym", () => {
+ expect(formatDurationLong(3_600_000)).toBe("1h 0m");
+ expect(formatDurationLong(5_400_000)).toBe("1h 30m");
+ expect(formatDurationLong(7_320_000)).toBe("2h 2m");
+ });
+
+ it("clamps negative values to 0ms (defends against clock skew)", () => {
+ expect(formatDurationLong(-1)).toBe("0ms");
+ expect(formatDurationLong(-99_999)).toBe("0ms");
+ });
+ });
});
diff --git a/src/metabase/transforms/models/transform_run.clj b/src/metabase/transforms/models/transform_run.clj
index 875222a879f..7784bef7c2b 100644
--- a/src/metabase/transforms/models/transform_run.clj
+++ b/src/metabase/transforms/models/transform_run.clj
@@ -338,6 +338,13 @@
:status [[(translate-status-clause) sort-direction]]
:run-method [[(translate-run-method-clause) sort-direction]]
:transform-tags [[(first-tag-name-subquery) sort-direction nulls-sort]]
+ ;; In-progress runs (end_time = nil) sink to the bottom in BOTH
+ ;; directions — null means "no measurable duration yet," not
+ ;; "longest duration."
+ :duration [[[:is :end_time nil] :asc]
+ [(h2x/calculate-interval-honeysql-form
+ (mdb/db-type) :end_time :start_time)
+ sort-direction]]
[[:start_time sort-direction]
[:end_time sort-direction nulls-sort]])
[:transform_run.id sort-direction])))
diff --git a/src/metabase/transforms_rest/api/transform.clj b/src/metabase/transforms_rest/api/transform.clj
index 03d2afd4b7a..c53551e457b 100644
--- a/src/metabase/transforms_rest/api/transform.clj
+++ b/src/metabase/transforms_rest/api/transform.clj
@@ -203,7 +203,7 @@
[_route-params
query-params :-
[:map
- [:sort-column {:optional true} [:enum "transform-name" "start-time" "end-time" "status" "run-method" "transform-tags"]]
+ [:sort-column {:optional true} [:enum "transform-name" "start-time" "end-time" "status" "run-method" "transform-tags" "duration"]]
[:sort-direction {:optional true} [:enum "asc" "desc"]]
[:transform-ids {:optional true} [:maybe (ms/QueryVectorOf ms/IntGreaterThanOrEqualToZero)]]
[:statuses {:optional true} [:maybe (ms/QueryVectorOf [:enum "started" "succeeded" "failed" "timeout"])]]
diff --git a/src/metabase/util/honey_sql_2.clj b/src/metabase/util/honey_sql_2.clj
index 92acd45437a..436f4083a16 100644
--- a/src/metabase/util/honey_sql_2.clj
+++ b/src/metabase/util/honey_sql_2.clj
@@ -542,3 +542,50 @@
:hsql-form hsql-form
:amount amount
:unit unit})))
+
+(defmulti calculate-interval-honeysql-form
+ "Return a HoneySQL form representing the temporal interval `end-form` minus `start-form`.
+
+ Inverse of [[add-interval-honeysql-form]]. The return value is monotonic in the actual duration but
+ its absolute units differ per DB: Postgres returns an `interval`, MySQL returns microseconds, H2
+ returns milliseconds. Suitable for ORDER BY purposes; do NOT rely on the units when the value is
+ exposed to callers.
+
+ (calculate-interval-honeysql-form :my-driver hsql-end-form hsql-start-form)
+ -> [:- hsql-end-form hsql-start-form]
+
+ This multimethod is intended for use in app DB queries; other drivers should extend
+ metabase.driver.sql.query-processor/datetime-diff instead."
+ {:arglists '([db-type end-form start-form])}
+ (fn [db-type _end-form _start-form]
+ (keyword db-type)))
+
+(defmethod calculate-interval-honeysql-form :postgres
+ [_db-type end-form start-form]
+ ;; Postgres timestamp subtraction returns an interval that orders correctly.
+ [:- end-form start-form])
+
+(defmethod calculate-interval-honeysql-form :postgres-mbql5
+ [db-type end-form start-form]
+ ((get-method calculate-interval-honeysql-form :postgres) db-type end-form start-form))
+
+(defmethod calculate-interval-honeysql-form :mysql
+ [_db-type end-form start-form]
+ [:timestampdiff [:raw "MICROSECOND"] start-form end-form])
+
+(defmethod calculate-interval-honeysql-form :h2
+ [_db-type end-form start-form]
+ [:datediff (literal "MILLISECOND") start-form end-form])
+
+(defmethod calculate-interval-honeysql-form :h2-mbql5
+ [db-type end-form start-form]
+ ((get-method calculate-interval-honeysql-form :h2) db-type end-form start-form))
+
+(defmethod calculate-interval-honeysql-form :default
+ [db-type end-form start-form]
+ (throw (ex-info (clojure.core/format
+ "metabase.util.honey-sql-2/calculate-interval-honeysql-form not implemented for db-type %s. You might want to be calling metabase.driver.sql.query-processor/datetime-diff instead."
+ db-type)
+ {:db-type db-type
+ :end-form end-form
+ :start-form start-form})))
diff --git a/test/metabase/transforms_rest/api/transform_test.clj b/test/metabase/transforms_rest/api/transform_test.clj
index 4977e2110f6..fbe14472438 100644
--- a/test/metabase/transforms_rest/api/transform_test.clj
+++ b/test/metabase/transforms_rest/api/transform_test.clj
@@ -1780,3 +1780,45 @@
(search.tu/with-new-search-and-legacy-search
(is (= #{native-id mbql-id}
(search-transform-ids search-term))))))))))
+
+(deftest get-runs-sort-by-duration-test
+ (testing "GET /api/transform/run supports sort-column=duration"
+ (mt/with-premium-features #{}
+ (mt/with-temp [:model/Transform {tid :id} {}
+ ;; 1s short run
+ :model/TransformRun {short-id :id} {:transform_id tid
+ :status :succeeded
+ :start_time #t "2026-01-01T00:00:00Z"
+ :end_time #t "2026-01-01T00:00:01Z"
+ :run_method "cron"}
+ ;; 10s longer run
+ :model/TransformRun {long-id :id} {:transform_id tid
+ :status :succeeded
+ :start_time #t "2026-01-01T00:00:00Z"
+ :end_time #t "2026-01-01T00:00:10Z"
+ :run_method "cron"}
+ ;; in-progress run (no end_time, no measurable duration)
+ :model/TransformRun {running-id :id} {:transform_id tid
+ :status :started
+ :start_time #t "2026-01-01T00:00:00Z"
+ :end_time nil
+ :is_active true
+ :run_method "cron"}]
+ (let [run-id? #{short-id long-id running-id}
+ seed-runs (fn [data] (filter (comp run-id? :id) data))]
+ (testing "desc — longest completed runs first; in-progress (null duration) sinks to the bottom"
+ (let [resp (mt/user-http-request :crowberto :get 200 "transform/run"
+ :sort-column "duration"
+ :sort-direction "desc")
+ ids (map :id (seed-runs (:data resp)))]
+ (is (= [long-id short-id running-id] ids))))
+ (testing "asc — shortest completed runs first; in-progress run still last"
+ (let [resp (mt/user-http-request :crowberto :get 200 "transform/run"
+ :sort-column "duration"
+ :sort-direction "asc")
+ ids (map :id (seed-runs (:data resp)))]
+ (is (= [short-id long-id running-id] ids))))
+ (testing "unknown sort-column is rejected with a 400"
+ (mt/user-http-request :crowberto :get 400 "transform/run"
+ :sort-column "bogus"
+ :sort-direction "asc")))))))