Skip to content

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

Add BE endpoints for job run history (#75926)

Nicola Momettof0fc22bb2749
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 76755037eb6..c7e6a2fb662 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -2345,6 +2345,7 @@
enterprise/transforms-python}
:model-exports #{:model/Transform
:model/TransformJob
+ :model/TransformJobRun
:model/TransformJobTransformTag
:model/TransformRun
:model/TransformTag}
@@ -2398,6 +2399,7 @@
util}
:model-imports #{:model/Transform
:model/TransformJob
+ :model/TransformJobRun
:model/TransformJobTransformTag
:model/TransformRun
:model/TransformTag}}
diff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/execute.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/execute.clj
index e31f563ec2f..76fccf49dc3 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms_python/execute.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/execute.clj
@@ -97,7 +97,7 @@
"Execute a Python transform by calling the python runner.
Blocks until the transform returns."
- [transform {:keys [run-method on-start user-id]}]
+ [transform {:keys [run-method on-start user-id job-run-id]}]
(try
(let [message-log (base/empty-message-log)
{:keys [target owner_user_id creator_id] transform-id :id} transform
@@ -105,7 +105,7 @@
run-user-id (if (and (= run-method :manual) user-id)
user-id
(or owner_user_id creator_id))
- {run-id :id} (transforms.u/try-start-unless-already-running transform-id run-method run-user-id)]
+ {run-id :id} (transforms.u/try-start-unless-already-running transform-id run-method run-user-id :job-run-id job-run-id)]
(when on-start (on-start run-id))
(with-open [_ (open-python-message-update-future! run-id message-log)]
(driver.conn/with-write-connection
diff --git a/resources/migrations/063/20260616_transform_run_job_run_id.yaml b/resources/migrations/063/20260616_transform_run_job_run_id.yaml
new file mode 100644
index 00000000000..f9ec1858f84
--- /dev/null
+++ b/resources/migrations/063/20260616_transform_run_job_run_id.yaml
@@ -0,0 +1,97 @@
+## Add job_run_id to transform_run to link each transform execution to its parent job run
+
+databaseChangeLog:
+
+ - changeSet:
+ id: v63.2026-06-16T00:00:00
+ author: bronsa
+ comment: Add job_run_id to transform_run to link each transform execution to its parent job run
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ - columnExists:
+ tableName: transform_run
+ columnName: job_run_id
+ changes:
+ - addColumn:
+ tableName: transform_run
+ columns:
+ - column:
+ name: job_run_id
+ type: bigint
+ remarks: FK to transform_job_run; links each transform execution to its parent job run
+ constraints:
+ nullable: true
+ rollback:
+ - dropColumn:
+ tableName: transform_run
+ columnName: job_run_id
+
+ - changeSet:
+ id: v63.2026-06-16T00:00:01
+ author: bronsa
+ comment: Index transform_run on job_run_id for efficient drill-down queries
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ - indexExists:
+ tableName: transform_run
+ indexName: idx_transform_run_job_run_id
+ changes:
+ - createIndex:
+ tableName: transform_run
+ indexName: idx_transform_run_job_run_id
+ columns:
+ - column:
+ name: job_run_id
+ rollback:
+ - dropIndex:
+ tableName: transform_run
+ indexName: idx_transform_run_job_run_id
+
+ - changeSet:
+ id: v63.2026-06-16T00:00:02
+ author: bronsa
+ comment: >-
+ Backfill job_run_id on transform_run by matching each run to the job run whose time window fully contains it.
+ This is best-effort for historical data. Going forward job_run_id is set directly at execution time.
+ changes:
+ - sql:
+ dbms: postgresql
+ sql: >-
+ WITH matches AS (
+ SELECT DISTINCT ON (run.id)
+ run.id AS transform_run_id,
+ job_run.id AS job_run_id
+ FROM transform_run run
+ INNER JOIN transform_transform_tag tag ON tag.transform_id = run.transform_id
+ INNER JOIN transform_job_transform_tag job_tag ON job_tag.tag_id = tag.tag_id
+ INNER JOIN transform_job_run job_run ON job_run.job_id = job_tag.job_id
+ WHERE run.job_run_id IS NULL
+ AND job_run.run_method = run.run_method
+ AND job_run.start_time <= run.start_time
+ AND (job_run.end_time IS NULL OR job_run.end_time >= COALESCE(run.end_time, run.start_time))
+ ORDER BY run.id, job_run.start_time DESC
+ )
+ UPDATE transform_run
+ SET job_run_id = matches.job_run_id
+ FROM matches
+ WHERE transform_run.id = matches.transform_run_id
+ - sql:
+ dbms: mysql,h2
+ sql: >-
+ UPDATE transform_run
+ SET job_run_id = (
+ SELECT job_run.id
+ FROM transform_job_run job_run
+ INNER JOIN transform_job_transform_tag job_tag ON job_tag.job_id = job_run.job_id
+ INNER JOIN transform_transform_tag tag ON tag.tag_id = job_tag.tag_id
+ WHERE tag.transform_id = transform_run.transform_id
+ AND job_run.run_method = transform_run.run_method
+ AND job_run.start_time <= transform_run.start_time
+ AND (job_run.end_time IS NULL OR job_run.end_time >= COALESCE(transform_run.end_time, transform_run.start_time))
+ ORDER BY job_run.start_time DESC
+ LIMIT 1
+ )
+ WHERE transform_run.job_run_id IS NULL
+ rollback:
diff --git a/src/metabase/transforms/core.clj b/src/metabase/transforms/core.clj
index 600afea641d..64e6c172653 100644
--- a/src/metabase/transforms/core.clj
+++ b/src/metabase/transforms/core.clj
@@ -7,6 +7,7 @@
[metabase.transforms.crud]
[metabase.transforms.execute]
[metabase.transforms.jobs]
+ [metabase.transforms.models.job-run]
[metabase.transforms.models.transform]
[metabase.transforms.models.transform-job]
[metabase.transforms.models.transform-run]
@@ -71,6 +72,9 @@
timeout-run!
paged-runs
running-run-for-transform-id]
+ [metabase.transforms.models.job-run
+ paged-job-runs
+ transform-runs-for-job-run]
[metabase.transforms.models.transform-run-cancelation
mark-cancel-started-run!]
[metabase.transforms.models.transform-job
diff --git a/src/metabase/transforms/jobs.clj b/src/metabase/transforms/jobs.clj
index e96997e1d1b..6dd15f0a04a 100644
--- a/src/metabase/transforms/jobs.clj
+++ b/src/metabase/transforms/jobs.clj
@@ -120,6 +120,7 @@
(log/info "Executing job transform" (pr-str transform-id))
(transforms.execute/execute! transform {:run-method run-method
:user-id user-id
+ :job-run-id run-id
;; lets the coordinator cancel exactly the run
;; this worker started (see [[cancel-worker!]])
:on-start #(deliver started-run-id %)})
diff --git a/src/metabase/transforms/models/job_run.clj b/src/metabase/transforms/models/job_run.clj
index 71ed3b2da76..2e04451af2b 100644
--- a/src/metabase/transforms/models/job_run.clj
+++ b/src/metabase/transforms/models/job_run.clj
@@ -2,6 +2,7 @@
(:require
[metabase.models.interface :as mi]
[metabase.run-tracking.core :as rt]
+ [metabase.transforms.models.util :as transforms.models.u]
[methodical.core :as methodical]
[toucan2.core :as t2]
[toucan2.realize :as t2.realize]))
@@ -102,39 +103,40 @@
:job_id id
:is_active true))
-(defn paged-runs
- "Return a page of the list of the runs.
+(defn paged-job-runs
+ "Return a page of the list of job runs.
Follows the conventions used by the FE."
- [{:keys [offset
- limit
- sort_column
- sort_direction
- job_id
- status]}]
- (let [offset (or offset 0)
- limit (or limit 20)
- sort-direction (or (keyword sort_direction) :desc)
- nulls-sort (if (= sort-direction :asc)
- :nulls-last
- :nulls-first)
- sort-column (keyword sort_column)
- order-by (case sort_column
- :started_at [[sort-column sort-direction]]
- :ended_at [[sort-column sort-direction nulls-sort]]
- [[:start_time sort-direction]
- [:end_time sort-direction nulls-sort]])
- conditions (concat (when job_id
- [:job_id job_id])
- (when status
- [:= :status status])
- (when (= status "started")
- [:is_active true]))
- conditions-with-sort-and-pagination (concat conditions [{:order-by order-by
- :offset offset
- :limit limit}])
- runs (apply t2/select :model/TransformJobRun conditions-with-sort-and-pagination)]
- {:data (t2/hydrate runs :transform)
- :limit limit
+ [{:keys [offset limit sort-column sort-direction job-id status run-method start-time]}]
+ (let [offset (or offset 0)
+ limit (or limit 20)
+ sort-direction (or (keyword sort-direction) :desc)
+ nulls-sort (if (= sort-direction :asc) :nulls-last :nulls-first)
+ sort-column (keyword sort-column)
+ order-by (case sort-column
+ :start_time [[sort-column sort-direction]]
+ :end_time [[sort-column sort-direction nulls-sort]]
+ [[:start_time sort-direction]
+ [:end_time sort-direction nulls-sort]])
+ where-cond (cond-> []
+ job-id (conj [:= :job_id job-id])
+ status (conj [:= :status status])
+ (= status "started") (conj [:= :is_active true])
+ run-method (conj [:= :run_method run-method])
+ start-time (conj (transforms.models.u/timestamp-constraint :start_time start-time)))
+ where (when (seq where-cond) (into [:and] where-cond))
+ query-opts (cond-> {:order-by order-by :offset offset :limit limit}
+ where (assoc :where where))
+ count-opts (if where {:where where} {})
+ runs (t2/select :model/TransformJobRun query-opts)]
+ {:data runs
+ :limit limit
:offset offset
- :total (apply t2/count :model/TransformJobRun conditions)}))
+ :total (t2/count :model/TransformJobRun count-opts)}))
+
+(defn transform-runs-for-job-run
+ "Return transform runs that were part of the given job run, ordered by start time."
+ [job-run-id]
+ (t2/select :model/TransformRun
+ {:where [:= :job_run_id job-run-id]
+ :order-by [[:start_time :asc]]}))
diff --git a/src/metabase/transforms/models/transform_run.clj b/src/metabase/transforms/models/transform_run.clj
index c91394ff095..fbb61e714ed 100644
--- a/src/metabase/transforms/models/transform_run.clj
+++ b/src/metabase/transforms/models/transform_run.clj
@@ -7,11 +7,10 @@
[metabase.events.core :as events]
[metabase.models.interface :as mi]
[metabase.premium-features.core :as premium-features]
- [metabase.query-processor.parameters.dates :as params.dates]
[metabase.run-tracking.core :as rt]
[metabase.transforms.models.transform-run-cancelation :as cancel]
+ [metabase.transforms.models.util :as transforms.models.u]
[metabase.util :as u]
- [metabase.util.date-2 :as u.date]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
@@ -248,23 +247,6 @@
[:= :status [:inline "succeeded"]]]
:group-by [:transform_id]}))))
-(defn- timestamp-constraint
- [field-name date-string]
- (let [{:keys [start end]}
- (try
- (params.dates/date-string->range date-string {:inclusive-end? false})
- (catch Exception e
- (throw (ex-info (tru "Failed to parse datetime value: {0}" date-string)
- {:status-code 400}
- e))))
- start (some-> start u.date/parse)
- end (some-> end u.date/parse)]
- (into [:and] (remove nil?)
- [(when start
- [:>= field-name start])
- (when end
- [:< field-name end])])))
-
(defn- paged-runs-join-clause
"Returns a `:left-join` clause for transform runs sort columns that require joining other tables."
[{:keys [sort-column]}]
@@ -277,10 +259,10 @@
[{:keys [start-time end-time run-methods transform-ids transform-tag-ids statuses user-id]}]
(let [where-cond (cond-> []
(some? start-time)
- (conj (timestamp-constraint :start_time start-time))
+ (conj (transforms.models.u/timestamp-constraint :start_time start-time))
(some? end-time)
- (conj (timestamp-constraint :end_time end-time))
+ (conj (transforms.models.u/timestamp-constraint :end_time end-time))
(seq run-methods)
(conj [:in :run_method (set run-methods)])
diff --git a/src/metabase/transforms/models/util.clj b/src/metabase/transforms/models/util.clj
new file mode 100644
index 00000000000..04846a9eefc
--- /dev/null
+++ b/src/metabase/transforms/models/util.clj
@@ -0,0 +1,24 @@
+(ns metabase.transforms.models.util
+ (:require
+ [metabase.query-processor.parameters.dates :as params.dates]
+ [metabase.util.date-2 :as u.date]
+ [metabase.util.i18n :refer [tru]]))
+
+(set! *warn-on-reflection* true)
+
+(defn timestamp-constraint
+ "Build a HoneySQL `[:and ...]` clause constraining `field-name` to the range expressed by `date-string`.
+ `date-string` uses the same format as the query-processor date parameter (e.g. \"2025-01\", \"past7days\")."
+ [field-name date-string]
+ (let [{:keys [start end]}
+ (try
+ (params.dates/date-string->range date-string {:inclusive-end? false})
+ (catch Exception e
+ (throw (ex-info (tru "Failed to parse datetime value: {0}" date-string)
+ {:status-code 400}
+ e))))
+ start (some-> start u.date/parse)
+ end (some-> end u.date/parse)]
+ (into [:and] (remove nil?)
+ [(when start [:>= field-name start])
+ (when end [:< field-name end])])))
diff --git a/src/metabase/transforms/query_impl.clj b/src/metabase/transforms/query_impl.clj
index bc56ba21bc8..6c7089509e4 100644
--- a/src/metabase/transforms/query_impl.clj
+++ b/src/metabase/transforms/query_impl.clj
@@ -32,7 +32,7 @@
(defn- run-mbql-transform!
([transform] (run-mbql-transform! transform nil))
([{:keys [id source target owner_user_id creator_id] :as transform}
- {:keys [run-method on-start user-id]}]
+ {:keys [run-method on-start user-id job-run-id]}]
;; `:target` is already workspace-rewritten — `resolve-transform-target` runs in
;; `metabase.transforms.execute/execute!` before dispatch.
(try
@@ -42,7 +42,7 @@
run-user-id (if (and (= run-method :manual) user-id)
user-id
(or owner_user_id creator_id))
- {run-id :id} (transforms.u/try-start-unless-already-running id run-method run-user-id)]
+ {run-id :id} (transforms.u/try-start-unless-already-running id run-method run-user-id :job-run-id job-run-id)]
(when on-start (on-start run-id))
(driver.conn/with-write-connection
(log/info "Executing transform" id "with target" (pr-str target)
diff --git a/src/metabase/transforms/util.clj b/src/metabase/transforms/util.clj
index 762632c7ce1..58a1dc015db 100644
--- a/src/metabase/transforms/util.clj
+++ b/src/metabase/transforms/util.clj
@@ -115,11 +115,13 @@
(defn try-start-unless-already-running
"Start a transform run. Throws ex-info with {:error :already-running} if another
run is already active (duplicate key violation). Other errors are rethrown.
- If `user-id` is provided, it will be stored with the run for attribution purposes."
- [id run-method user-id]
+ If `user-id` is provided, it will be stored with the run for attribution purposes.
+ If `job-run-id` is provided, it will be stored with the run to link it to its parent job run."
+ [id run-method user-id & {:keys [job-run-id]}]
(try
(transform-run/start-run! id (cond-> {:run_method run-method}
- user-id (assoc :user_id user-id)))
+ user-id (assoc :user_id user-id)
+ job-run-id (assoc :job_run_id job-run-id)))
(catch Exception e
(if (duplicate-key-violation? e)
(throw (ex-info "Transform is already running"
diff --git a/src/metabase/transforms_rest/api/transform.clj b/src/metabase/transforms_rest/api/transform.clj
index c3458eb9902..69a9628acc9 100644
--- a/src/metabase/transforms_rest/api/transform.clj
+++ b/src/metabase/transforms_rest/api/transform.clj
@@ -67,6 +67,7 @@
[:user_id [:maybe pos-int?]]
[:transform_name {:optional true} [:maybe :string]]
[:transform_entity_id {:optional true} [:maybe :string]]
+ [:job_run_id {:optional true} [:maybe pos-int?]]
[:checkpoint_filter_field_id {:optional true} [:maybe pos-int?]]
[:checkpoint_lo_value {:optional true} [:maybe :string]]
[:checkpoint_hi_value {:optional true} [:maybe :string]]
@@ -115,6 +116,7 @@
[:user_id [:maybe pos-int?]]
[:transform_name {:optional true} [:maybe :string]]
[:transform_entity_id {:optional true} [:maybe :string]]
+ [:job_run_id {:optional true} [:maybe pos-int?]]
[:checkpoint_filter_field_id {:optional true} [:maybe pos-int?]]
[:checkpoint_lo_value {:optional true} [:maybe :string]]
[:checkpoint_hi_value {:optional true} [:maybe :string]]
diff --git a/src/metabase/transforms_rest/api/transform_job.clj b/src/metabase/transforms_rest/api/transform_job.clj
index 330d2dfd0d5..82ab09d0462 100644
--- a/src/metabase/transforms_rest/api/transform_job.clj
+++ b/src/metabase/transforms_rest/api/transform_job.clj
@@ -5,6 +5,7 @@
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
[metabase.models.interface :as mi]
+ [metabase.request.core :as request]
[metabase.transforms-base.util :as transforms-base.u]
[metabase.transforms.core :as transforms.core]
[metabase.transforms.util :as transforms.u]
@@ -285,6 +286,71 @@
(map #(update % :next_run transforms-base.u/present-run)))
(t2/hydrate jobs :tag_ids :last_run))))
+(def ^:private JobRunResponse
+ [:map {:closed true}
+ [:id pos-int?]
+ [:job_id pos-int?]
+ [:run_method :keyword]
+ [:status [:enum :started :succeeded :failed :timeout]]
+ [:is_active [:maybe :boolean]]
+ [:start_time :any]
+ [:end_time {:optional true} [:maybe :any]]
+ [:message [:maybe :string]]
+ [:created_at :any]
+ [:updated_at :any]])
+
+(def ^:private TransformRunForJobRunResponse
+ [:map {:closed true}
+ [:id pos-int?]
+ [:transform_id [:maybe pos-int?]]
+ [:job_run_id [:maybe pos-int?]]
+ [:run_method :keyword]
+ [:status [:enum :started :succeeded :failed :timeout :canceled :canceling]]
+ [:is_active [:maybe :boolean]]
+ [:start_time :any]
+ [:end_time {:optional true} [:maybe :any]]
+ [:message [:maybe :string]]
+ [:user_id [:maybe pos-int?]]
+ [:transform_name {:optional true} [:maybe :string]]
+ [:transform_entity_id {:optional true} [:maybe :string]]
+ [:transform {:optional true} [:maybe :map]]
+ [:metered_as {:optional true} [:maybe :string]]
+ [:checkpoint_filter_field_id {:optional true} [:maybe pos-int?]]
+ [:checkpoint_lo_value {:optional true} [:maybe :string]]
+ [:checkpoint_hi_value {:optional true} [:maybe :string]]])
+
+(api.macros/defendpoint :get "/:job-id/runs" :- [:map {:closed true}
+ [:data [:sequential JobRunResponse]]
+ [:limit pos-int?]
+ [:offset :int]
+ [:total :int]]
+ "Get paginated run history for a transform job."
+ [{:keys [job-id]} :- [:map [:job-id ms/PositiveInt]]
+ query-params :- [:map
+ [:status {:optional true} [:maybe [:enum "started" "succeeded" "failed" "timeout"]]]
+ [:run-method {:optional true} [:maybe [:enum "manual" "cron"]]]
+ [:start-time {:optional true} [:maybe ms/NonBlankString]]
+ [:sort-column {:optional true} [:maybe [:enum "start_time" "end_time"]]]
+ [:sort-direction {:optional true} [:maybe [:enum "asc" "desc"]]]]]
+ (api/read-check :model/TransformJob job-id)
+ (-> (transforms.core/paged-job-runs (assoc query-params
+ :job-id job-id
+ :offset (request/offset)
+ :limit (request/limit)))
+ (update :data #(map transforms-base.u/present-run %))))
+
+(api.macros/defendpoint :get "/:job-id/runs/:run-id/transform-runs" :- [:sequential TransformRunForJobRunResponse]
+ "Get the transform runs that made up a specific job run."
+ [{:keys [job-id run-id]} :- [:map
+ [:job-id ms/PositiveInt]
+ [:run-id ms/PositiveInt]]
+ _query-params]
+ (api/read-check :model/TransformJob job-id)
+ (api/check-404 (t2/select-one :model/TransformJobRun :id run-id :job_id job-id))
+ (let [runs (transforms.core/transform-runs-for-job-run run-id)]
+ (->> (t2/hydrate runs [:transform :collection :transform_tag_ids])
+ (map transforms-base.u/present-run))))
+
(def ^{:arglists '([request respond raise])} routes
"`/api/transform-job` routes."
(api.macros/ns-handler *ns* +auth))
diff --git a/test/metabase/transforms/jobs_test.clj b/test/metabase/transforms/jobs_test.clj
index 3795c6f6311..a96f02e668e 100644
--- a/test/metabase/transforms/jobs_test.clj
+++ b/test/metabase/transforms/jobs_test.clj
@@ -555,10 +555,10 @@
;; root swap is seen by every thread.
#_{:clj-kondo/ignore [:metabase/prefer-with-dynamic-fn-redefs]}
(with-redefs [transforms.u/try-start-unless-already-running
- (fn [transform-id run-method user-id]
+ (fn [transform-id run-method user-id & kwargs]
(on-enter)
(let [[ret ex] (try
- [(original-insert transform-id run-method user-id)]
+ [(apply original-insert transform-id run-method user-id kwargs)]
(catch Throwable t [nil t]))]
(on-exit)
(if ex (throw ex) ret)))]
diff --git a/test/metabase/transforms/models_test.clj b/test/metabase/transforms/models_test.clj
index e0620e691b1..b64834a4fdb 100644
--- a/test/metabase/transforms/models_test.clj
+++ b/test/metabase/transforms/models_test.clj
@@ -6,6 +6,7 @@
[metabase.config.core :as config]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
+ [metabase.transforms.models.job-run :as transforms.job-run]
[metabase.transforms.models.transform :as transform.model]
[metabase.transforms.models.transform-job :as transform-job]
[metabase.transforms.models.transform-run :as transform-run]
@@ -371,3 +372,53 @@
"only the active job's schedule is included")
(is (= {} (transform-tag/schedules-for-transforms #{(:id untagged)}))
"a transform with no scheduling job is absent"))))
+
+(deftest start-run-stores-job-run-id-test
+ (testing "start-run! stores job_run_id when provided in properties"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temp [:model/TransformJob {job-id :id} {:name "Test Job" :schedule "0 0 0 * * ?"}
+ :model/TransformJobRun {job-run-id :id} {:job_id job-id
+ :status "started"
+ :run_method "cron"
+ :start_time #t "2025-09-01T10:00:00Z"
+ :is_active true}
+ :model/Transform {transform-id :id} {}]
+ (let [run (transform-run/start-run! transform-id {:run_method "cron"
+ :job_run_id job-run-id})]
+ (is (= job-run-id (:job_run_id run))))))))
+
+(deftest paged-job-runs-test
+ (testing "paged-job-runs returns paginated runs for a job"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temp [:model/TransformJob {j1-id :id} {:name "Job 1" :schedule "0 0 0 * * ?"}
+ :model/TransformJob {j2-id :id} {:name "Job 2" :schedule "0 0 0 * * ?"}
+ :model/TransformJobRun {r1-id :id} {:job_id j1-id :status "succeeded" :run_method "cron"
+ :start_time #t "2025-09-01T10:00:00Z"}
+ :model/TransformJobRun {r2-id :id} {:job_id j1-id :status "failed" :run_method "manual"
+ :start_time #t "2025-09-02T10:00:00Z"}
+ :model/TransformJobRun _ {:job_id j2-id :status "succeeded" :run_method "cron"
+ :start_time #t "2025-09-03T10:00:00Z"}]
+ (let [result (transforms.job-run/paged-job-runs {:job-id j1-id})]
+ (is (= #{r1-id r2-id} (into #{} (map :id) (:data result))))
+ (is (= 2 (:total result))))
+ (testing "filters by status"
+ (let [result (transforms.job-run/paged-job-runs {:job-id j1-id :status "failed"})]
+ (is (= [r2-id] (map :id (:data result))))
+ (is (= 1 (:total result)))))
+ (testing "filters by run-method"
+ (let [result (transforms.job-run/paged-job-runs {:job-id j1-id :run-method "manual"})]
+ (is (= [r2-id] (map :id (:data result))))
+ (is (= 1 (:total result)))))
+ (testing "filters by start-time (date string range)"
+ (testing "exact date matches only runs on that day"
+ (let [result (transforms.job-run/paged-job-runs {:job-id j1-id :start-time "2025-09-01"})]
+ (is (= [r1-id] (map :id (:data result))))
+ (is (= 1 (:total result)))))
+ (testing "range string matches both runs"
+ (let [result (transforms.job-run/paged-job-runs {:job-id j1-id :start-time "2025-09-01~2025-09-03"})]
+ (is (= #{r1-id r2-id} (into #{} (map :id) (:data result))))
+ (is (= 2 (:total result))))))
+ (testing "honors offset/limit"
+ (let [result (transforms.job-run/paged-job-runs {:job-id j1-id :limit 1 :offset 0})]
+ (is (= 1 (count (:data result))))
+ (is (= 2 (:total result)))))))))
diff --git a/test/metabase/transforms_rest/api/transform_job_test.clj b/test/metabase/transforms_rest/api/transform_job_test.clj
index 6d9b7711d0d..3ac8c39d9d3 100644
--- a/test/metabase/transforms_rest/api/transform_job_test.clj
+++ b/test/metabase/transforms_rest/api/transform_job_test.clj
@@ -3,6 +3,7 @@
[clojure.test :refer :all]
[metabase.test :as mt]
[metabase.transforms.models.transform-job]
+ [metabase.transforms.models.transform-run]
[metabase.transforms.models.transform-tag]
[metabase.transforms.schedule :as transforms.schedule]
[metabase.transforms.test-util :refer [parse-instant
@@ -329,3 +330,179 @@
{:name "Updated"})
(mt/user-http-request :rasta :delete 403 (str "transform-job/" (:id job)))
(mt/user-http-request :rasta :post 403 (str "transform-job/" (:id job) "/run"))))))
+
+;;; +------------------------------------------------------------------------------------------------------------------+
+;;; | GET /api/transform-job/:job-id/runs |
+;;; +------------------------------------------------------------------------------------------------------------------+
+
+(deftest get-job-runs-test
+ (testing "GET /api/transform-job/:job-id/runs"
+ (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temp [:model/TransformJob {job-id :id} {:name "Run History Job" :schedule "0 0 0 * * ?"}
+ :model/TransformJobRun {r1-id :id} {:job_id job-id
+ :status "succeeded"
+ :run_method "cron"
+ :start_time (parse-instant "2025-09-01T10:00:00")
+ :end_time (parse-instant "2025-09-01T10:05:00")}
+ :model/TransformJobRun {r2-id :id} {:job_id job-id
+ :status "failed"
+ :run_method "manual"
+ :start_time (parse-instant "2025-09-02T10:00:00")
+ :end_time (parse-instant "2025-09-02T10:01:00")
+ :message "Something broke"}
+ :model/TransformJobRun _ {:job_id job-id
+ :status "started"
+ :run_method "cron"
+ :start_time (parse-instant "2025-09-03T10:00:00")
+ :is_active true}]
+ (testing "returns paginated runs for a job"
+ (let [response (mt/user-http-request :lucky :get 200 (str "transform-job/" job-id "/runs"))
+ ids (into #{} (map :id) (:data response))]
+ (is (contains? ids r1-id))
+ (is (contains? ids r2-id))
+ (is (= 3 (:total response)))
+ (is (= 3 (count (:data response))))
+ (is (pos? (:limit response)))
+ (is (integer? (:offset response)))))
+ (testing "filters by status"
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs")
+ :status "failed")]
+ (is (= [r2-id] (map :id (:data response))))
+ (is (= 1 (:total response)))))
+ (testing "filters by run-method"
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs")
+ :run-method "manual")]
+ (is (= [r2-id] (map :id (:data response))))
+ (is (= 1 (:total response)))))
+ (testing "filters by start-time"
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs")
+ :start-time "2025-09-01")]
+ (is (= [r1-id] (map :id (:data response))))
+ (is (= 1 (:total response)))))
+ (testing "sorts by start_time asc"
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs")
+ :sort-column "start_time"
+ :sort-direction "asc")]
+ (is (= r1-id (-> response :data first :id)))))
+ (testing "response shape"
+ (let [run (first (filter #(= r1-id (:id %))
+ (:data (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs")))))]
+ (is (= job-id (:job_id run)))
+ (is (= "cron" (:run_method run)))
+ (is (= "succeeded" (:status run)))
+ (is (= (utc-timestamp "2025-09-01T10:00:00") (:start_time run)))
+ (is (= (utc-timestamp "2025-09-01T10:05:00") (:end_time run)))))
+ (testing "returns 404 for non-existent job"
+ (mt/user-http-request :lucky :get 404 "transform-job/999999/runs"))
+ (testing "does not return runs from other jobs"
+ (mt/with-temp [:model/TransformJob {other-job-id :id} {:name "Other Job" :schedule "0 0 0 * * ?"}
+ :model/TransformJobRun _ {:job_id other-job-id
+ :status "succeeded"
+ :run_method "cron"
+ :start_time (parse-instant "2025-09-04T10:00:00")}]
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs"))]
+ (is (every? #(= job-id (:job_id %)) (:data response)))))))))))
+
+(deftest get-job-runs-requires-auth-test
+ (testing "GET /api/transform-job/:job-id/runs requires data-analyst role"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temp [:model/TransformJob {job-id :id} {:name "Auth Test" :schedule "0 0 0 * * ?"}]
+ (mt/user-http-request :rasta :get 403 (str "transform-job/" job-id "/runs"))))))
+
+;;; +------------------------------------------------------------------------------------------------------------------+
+;;; | GET /api/transform-job/:job-id/runs/:run-id/transform-runs |
+;;; +------------------------------------------------------------------------------------------------------------------+
+
+(deftest get-job-run-transform-runs-test
+ (testing "GET /api/transform-job/:job-id/runs/:run-id/transform-runs"
+ (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (mt/with-premium-features #{:transforms-basic}
+ (let [lucky-id (mt/user->id :lucky)]
+ (mt/with-temp [:model/TransformJob {job-id :id} {:name "Drill-down Job" :schedule "0 0 0 * * ?"}
+ :model/TransformJobRun {run-id :id} {:job_id job-id
+ :status "failed"
+ :run_method "cron"
+ :start_time (parse-instant "2025-09-01T10:00:00")}
+ :model/Transform {t1-id :id} {:name "TR1" :creator_id lucky-id}
+ :model/Transform {t2-id :id} {:name "TR2" :creator_id lucky-id}
+ :model/TransformRun {tr1-id :id} {:transform_id t1-id
+ :job_run_id run-id
+ :status "succeeded"
+ :run_method "cron"
+ :transform_name "TR1"
+ :start_time (parse-instant "2025-09-01T10:00:00")
+ :end_time (parse-instant "2025-09-01T10:02:00")}
+ :model/TransformRun {tr2-id :id} {:transform_id t2-id
+ :job_run_id run-id
+ :status "failed"
+ :run_method "cron"
+ :transform_name "TR2"
+ :start_time (parse-instant "2025-09-01T10:02:00")
+ :end_time (parse-instant "2025-09-01T10:03:00")
+ :message "Query failed"}
+ :model/TransformRun _ {:transform_id t1-id
+ :job_run_id nil
+ :status "succeeded"
+ :run_method "manual"
+ :transform_name "TR1"
+ :start_time (parse-instant "2025-08-01T10:00:00")}]
+ (testing "returns transform runs for the job run"
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs/" run-id "/transform-runs"))]
+ (is (= #{tr1-id tr2-id} (into #{} (map :id) response)))
+ (is (= 2 (count response)))))
+ (testing "response shape"
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs/" run-id "/transform-runs"))
+ tr1 (first (filter #(= tr1-id (:id %)) response))]
+ (is (= t1-id (:transform_id tr1)))
+ (is (= run-id (:job_run_id tr1)))
+ (is (= "cron" (:run_method tr1)))
+ (is (= "succeeded" (:status tr1)))
+ (is (= "TR1" (:transform_name tr1)))))
+ (testing "orders by start_time ascending"
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs/" run-id "/transform-runs"))]
+ (is (= [tr1-id tr2-id] (map :id response)))))
+ (testing "does not include transform runs from other job runs"
+ (mt/with-temp [:model/TransformJobRun {other-run-id :id} {:job_id job-id
+ :status "succeeded"
+ :run_method "cron"
+ :start_time (parse-instant "2025-09-02T10:00:00")}
+ :model/TransformRun _ {:transform_id t1-id
+ :job_run_id other-run-id
+ :status "succeeded"
+ :run_method "cron"
+ :transform_name "TR1"
+ :start_time (parse-instant "2025-09-02T10:00:00")}]
+ (let [response (mt/user-http-request :lucky :get 200
+ (str "transform-job/" job-id "/runs/" run-id "/transform-runs"))]
+ (is (every? #(= run-id (:job_run_id %)) response)))))
+ (testing "returns 404 for non-existent job"
+ (mt/user-http-request :lucky :get 404
+ (str "transform-job/999999/runs/" run-id "/transform-runs")))
+ (testing "returns 404 for non-existent run"
+ (mt/user-http-request :lucky :get 404
+ (str "transform-job/" job-id "/runs/999999/transform-runs")))
+ (testing "returns 404 when run does not belong to job"
+ (mt/with-temp [:model/TransformJob {other-job-id :id} {:name "Other" :schedule "0 0 0 * * ?"}]
+ (mt/user-http-request :lucky :get 404
+ (str "transform-job/" other-job-id "/runs/" run-id "/transform-runs"))))))))))
+
+(deftest get-job-run-transform-runs-requires-auth-test
+ (testing "GET /api/transform-job/:job-id/runs/:run-id/transform-runs requires data-analyst role"
+ (mt/with-premium-features #{:transforms-basic}
+ (mt/with-temp [:model/TransformJob {job-id :id} {:name "Auth Test" :schedule "0 0 0 * * ?"}
+ :model/TransformJobRun {run-id :id} {:job_id job-id
+ :status "succeeded"
+ :run_method "cron"
+ :start_time (parse-instant "2025-09-01T10:00:00")}]
+ (mt/user-http-request :rasta :get 403
+ (str "transform-job/" job-id "/runs/" run-id "/transform-runs"))))))