Skip to content

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

Usage analytics update v57 (#65055)

Ngoc Khuat9f9368351852
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 8804b65284a..9783490990f 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -560,6 +560,7 @@
{:team "Gadget"
:api #{metabase.glossary.api}
:uses #{api
+ events
models
util}}
@@ -1509,6 +1510,7 @@
:uses #{actions
api
driver
+ events
lib
models
query-processor
@@ -1916,6 +1918,7 @@
settings
startup
task
+ task-history
util
enterprise/serialization}}
diff --git a/enterprise/backend/src/metabase_enterprise/action_v2/api.clj b/enterprise/backend/src/metabase_enterprise/action_v2/api.clj
index 111fcb4608b..de1cefb517f 100644
--- a/enterprise/backend/src/metabase_enterprise/action_v2/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/action_v2/api.clj
@@ -7,6 +7,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
+ [metabase.events.core :as events]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]))
@@ -150,6 +151,11 @@
;; This check should be redundant in practice with the permission checks within perform-action!
;; Since test coverage is light and the logic is so simple, we've decided to be extra cautious for now.
(api/check-superuser)
+ (events/publish-event! :event/action-v2-execute
+ {:details {:action action
+ :scope scope
+ :input_count 1}
+ :user-id api/*current-user-id*})
{:outputs (execute!* action scope params [input])})
(api.macros/defendpoint :post "/execute-bulk"
@@ -179,6 +185,11 @@
;; This check should be redundant in practice with the permission checks within perform-action!
;; Since test coverage is light and the logic is so simple, we've decided to be extra cautious for now.
(api/check-superuser)
+ (events/publish-event! :event/action-v2-execute
+ {:details {:action action
+ :scope scope
+ :input_count (count inputs)}
+ :user-id api/*current-user-id*})
{:outputs (execute!* action scope params inputs)})
(api.macros/defendpoint :post "/execute-form"
diff --git a/enterprise/backend/src/metabase_enterprise/comments/api.clj b/enterprise/backend/src/metabase_enterprise/comments/api.clj
index db1c34a6f26..65cb36e6fba 100644
--- a/enterprise/backend/src/metabase_enterprise/comments/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/comments/api.clj
@@ -181,6 +181,9 @@
;; New comments always have empty reactions map
(assoc :reactions []))]
(notify-comment! comment {:entity entity :parent parent})
+ (events/publish-event! :event/comment-create
+ {:object comment
+ :user-id api/*current-user-id*})
comment))
(api.macros/defendpoint :put "/:comment-id"
@@ -210,8 +213,12 @@
not-empty)]
(t2/update! :model/Comment comment-id updates))
- (-> (t2/select-one :model/Comment :id comment-id)
- (t2/hydrate :creator :reactions))))
+ (let [updated-comment (-> (t2/select-one :model/Comment :id comment-id)
+ (t2/hydrate :creator :reactions))]
+ (events/publish-event! :event/comment-update
+ {:object updated-comment
+ :user-id api/*current-user-id*})
+ updated-comment)))
(api.macros/defendpoint :delete "/:comment-id"
"Soft delete a comment"
@@ -231,6 +238,10 @@
;; Soft delete the comment
(t2/update! :model/Comment comment-id {:deleted_at [:now]})
+ (events/publish-event! :event/comment-delete
+ {:object comment
+ :user-id api/*current-user-id*})
+
;; Return 204 No Content
api/generic-204-no-content))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj
index f908daefc48..6f670627242 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj
@@ -11,6 +11,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
+ [metabase.events.core :as events]
[metabase.util.log :as log]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
@@ -34,7 +35,12 @@
[:force {:optional true} :boolean]]]
(api/check-superuser)
(api/check-400 (settings/remote-sync-enabled) "Remote sync is not configured.")
- (let [task-id (impl/async-import! (or branch (settings/remote-sync-branch)) force {})]
+ (let [branch-name (or branch (settings/remote-sync-branch))
+ {task-id :id :as task} (impl/async-import! branch-name force {})]
+ (events/publish-event! :event/remote-sync-import
+ {:object task
+ :details {:branch branch-name}
+ :user-id api/*current-user-id*})
{:status :success
:task_id task-id
:message (when-not task-id "No changes since last import")}))
@@ -76,10 +82,16 @@
(api/check-superuser)
(api/check-400 (settings/remote-sync-enabled) "Remote sync is not configured.")
(api/check-400 (= (settings/remote-sync-type) :development) "Exports are only allowed when remote-sync-type is set to 'development'")
- {:message "Export task started"
- :task_id (impl/async-export! (or branch (settings/remote-sync-branch))
- (or force false)
- (or message "Exported from Metabase"))})
+ (let [branch-name (or branch (settings/remote-sync-branch))
+ {task-id :id :as task} (impl/async-export! branch-name
+ (or force false)
+ (or message "Exported from Metabase"))]
+ (events/publish-event! :event/remote-sync-export
+ {:object task
+ :details {:branch branch-name}
+ :user-id api/*current-user-id*})
+ {:message "Export task started"
+ :task_id task-id}))
(api.macros/defendpoint :get "/current-task" :- [:maybe remote-sync.schema/SyncTask]
"Get the current sync task"
@@ -114,6 +126,9 @@
(throw (ex-info (or (ex-message e) "Invalid settings")
{:error (ex-message e)
:status-code 400} e))))
+ (events/publish-event! :event/remote-sync-settings-update
+ {:details {:remote-sync-type remote-sync-type}
+ :user-id api/*current-user-id*})
(let [task-id (impl/finish-remote-config!)]
(if task-id
{:success true
@@ -153,6 +168,10 @@
(try
(source.p/create-branch source name base-branch)
(settings/remote-sync-branch! name)
+ (events/publish-event! :event/remote-sync-create-branch
+ {:details {:branch_name name
+ :base_branch base-branch}
+ :user-id api/*current-user-id*})
{:status "success"
:message (format "Branch '%s' created from '%s'" name base-branch)}
(catch Exception e
@@ -173,9 +192,14 @@
(api/check-400 source "Source not configured")
(try
(source.p/create-branch source new-branch (settings/remote-sync-branch))
- {:status "success"
- :message (str "Stashing to " new-branch)
- :task_id (impl/async-export! new-branch false message)}
+ (let [{task-id :id :as task} (impl/async-export! new-branch false message)]
+ (events/publish-event! :event/remote-sync-stash
+ {:object task
+ :details {:branch new-branch}
+ :user-id api/*current-user-id*})
+ {:status "success"
+ :message (str "Stashing to " new-branch)
+ :task_id task-id})
(catch Exception e
(throw (ex-info (format "Failed to stash changes to branch: %s" (ex-message e))
{:status-code 400}))))))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
index c6c5e46d604..1c9fdd24d59 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
@@ -221,13 +221,13 @@
(defn create-task-with-lock!
"Takes a cluster-wide lock and either returns an existing in-progress RemoteSyncTask ID or creates a new one.
- Takes a task-type string (either 'import' or 'export'). Returns a map with :id and optionally :existing? keys.
- If a task is already running, returns {:existing? true :id <existing-task-id>}. Otherwise creates a new task and
- returns {:id <new-task-id>}."
+ Takes a task-type string (either 'import' or 'export'). Returns a RemoteSyncTask with an optional :existing? key.
+ If a task is already running, returns (assoc existing-task :existing? true). Otherwise creates a new task and
+ returns it."
[task-type]
(cluster-lock/with-cluster-lock ::remote-sync-task
- (if-let [{id :id} (remote-sync.task/current-task)]
- {:existing? true :id id}
+ (if-let [task (remote-sync.task/current-task)]
+ (assoc task :existing? true)
(remote-sync.task/create-sync-task! task-type api/*current-user-id*))))
(defn handle-task-result!
@@ -253,15 +253,15 @@
sync-fn function that takes a task-id and performs the sync operation. Creates a new task (or errors if one is
already running), then executes the sync function in a virtual thread with a timeout.
- Returns the task ID. Throws ExceptionInfo with status 400 if a sync task is already in progress."
+ Returns a RemoteSyncTask. Throws ExceptionInfo with status 400 if a sync task is already in progress."
[task-type branch sync-fn]
- (let [{task-id :id existing? :existing?} (create-task-with-lock! task-type)]
+ (let [{task-id :id existing? :existing? :as task} (create-task-with-lock! task-type)]
(api/check-400 (not existing?) "Remote sync in progress")
(u.jvm/in-virtual-thread*
(dh/with-timeout {:interrupt? true
:timeout-ms (* (settings/remote-sync-task-time-limit-ms) 10)}
(handle-task-result! (sync-fn task-id) task-id branch)))
- task-id))
+ task))
(defn async-import!
"Imports remote-synced collections from a remote source repository asynchronously.
@@ -270,7 +270,7 @@
import-args map of additional arguments to pass to the import function. Checks for dirty changes and throws an
exception if force? is false and changes exist.
- Returns the task ID of the created import task. Throws ExceptionInfo with status 400 and :conflicts true if there
+ Returns a RemoteSyncTask. Throws ExceptionInfo with status 400 and :conflicts true if there
are unsaved changes and force? is false."
[branch force? import-args]
(let [source (source/source-from-settings branch)
@@ -289,7 +289,7 @@
branch), and a commit message string. Checks if the remote branch has changed since the last sync and throws an
exception if force? is false and changes exist.
- Returns the task ID of the created export task. Throws ExceptionInfo with status 400 and :conflicts true if there
+ Returns a RemoteSyncTask. Throws ExceptionInfo with status 400 and :conflicts true if there
are new remote changes and force? is false."
[branch force? message]
(let [source (source/source-from-settings branch)
@@ -312,6 +312,6 @@
(when (str/blank? (setting/get :remote-sync-branch))
(setting/set! :remote-sync-branch (source.p/default-branch (source/source-from-settings))))
(when (or (nil? (collection/remote-synced-collection)) (= :production (settings/remote-sync-type)))
- (async-import! (settings/remote-sync-branch) true {})))
+ (:id (async-import! (settings/remote-sync-branch) true {}))))
(u/prog1 nil
(collection/clear-remote-synced-collection!))))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/task/import.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/task/import.clj
index 333b24c1c57..d16815c7a40 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/task/import.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/task/import.clj
@@ -11,6 +11,7 @@
[metabase-enterprise.remote-sync.source :as source]
[metabase-enterprise.remote-sync.source.git :as git]
[metabase-enterprise.remote-sync.source.protocol :as source.p]
+ [metabase.task-history.core :as task-history]
[metabase.task.core :as task]
[metabase.util.log :as log]))
@@ -31,10 +32,12 @@
(let [{task-id :id existing? :existing?} (impl/create-task-with-lock! "import")]
(if existing?
(log/info "Remote sync already in progress, not auto-importing")
- (dh/with-timeout {:interrupt? true
- :timeout-ms (* (settings/remote-sync-task-time-limit-ms) 10)}
- (log/info "Auto-importing remote-sync collections")
- (impl/handle-task-result! (impl/import! source task-id) task-id))))))))
+ (task-history/with-task-history {:task "remote-sync-auto-import"
+ :task_details {:task-id task-id}}
+ (dh/with-timeout {:interrupt? true
+ :timeout-ms (* (settings/remote-sync-task-time-limit-ms) 10)}
+ (log/info "Auto-importing remote-sync collections")
+ (impl/handle-task-result! (impl/import! source task-id) task-id)))))))))
(task/defjob ^{:doc "Auto-imports any remote collections."} AutoImport [_]
(auto-import!))
diff --git a/enterprise/backend/src/metabase_enterprise/transforms/api.clj b/enterprise/backend/src/metabase_enterprise/transforms/api.clj
index 263d165b77e..72837d46d60 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms/api.clj
@@ -232,7 +232,11 @@
[{:keys [id]} :- [:map
[:id ms/PositiveInt]]]
(api/check-superuser)
- (t2/delete! :model/Transform id)
+ (let [transform (api/check-404 (t2/select-one :model/Transform id))]
+ (t2/delete! :model/Transform id)
+ (events/publish-event! :event/transform-delete
+ {:object transform
+ :user-id api/*current-user-id*}))
nil)
(api.macros/defendpoint :delete "/:id/table"
diff --git a/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.clj b/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.clj
index 1c0278b9477..777c6e9dc61 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms/models/transform_run.clj
@@ -3,6 +3,7 @@
[medley.core :as m]
[metabase-enterprise.transforms.models.transform-run-cancelation :as cancel]
[metabase.app-db.core :as mdb]
+ [metabase.events.core :as events]
[metabase.models.interface :as mi]
[metabase.query-processor.parameters.dates :as params.dates]
[metabase.util :as u]
@@ -58,11 +59,13 @@
([transform-id]
(start-run! transform-id {}))
([transform-id properties]
- (t2/insert-returning-instance! :model/TransformRun
- (assoc properties
- :transform_id transform-id
- :status :started
- :is_active true))))
+ (let [run (t2/insert-returning-instance! :model/TransformRun
+ (assoc properties
+ :transform_id transform-id
+ :status :started
+ :is_active true))]
+ (events/publish-event! :event/transform-run-start {:object run})
+ run)))
(defn succeed-started-run!
"Mark a started run as successfully completed."
diff --git a/enterprise/backend/src/metabase_enterprise/transforms/schedule.clj b/enterprise/backend/src/metabase_enterprise/transforms/schedule.clj
index 736671ff32e..7ed11c7d82c 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms/schedule.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms/schedule.clj
@@ -85,7 +85,9 @@
(let [job-id (-> (conversion/from-job-data context)
(get "job-id"))]
(log/info "Executing scheduled run of transform job" job-id)
- (task-history/with-task-history {:task "run-transforms"}
+ (task-history/with-task-history {:task "run-transforms"
+ :task_details {:job-id job-id
+ :run-method :cron}}
(transforms.jobs/run-job! job-id {:run-method :cron}))))
(defn initialize-job!
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
index e71a4f4f9ee..b1c2c4e3929 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj
@@ -570,12 +570,12 @@
remote-sync-token "test-token"
remote-sync-branch "main"
remote-sync-type :development]
- (mt/with-temp [:model/RemoteSyncObject _ {:model_type "Card"
- :model_id 1
- :status "updated"
- :status_changed_at (java.time.OffsetDateTime/now)}]
+ (mt/with-temp [:model/RemoteSyncObject remote-sync {:model_type "Card"
+ :model_id 1
+ :status "updated"
+ :status_changed_at (java.time.OffsetDateTime/now)}]
(with-redefs [source/source-from-settings (constantly mock-source)
- impl/async-export! (fn [_ _ _] (swap! export-calls inc))]
+ impl/async-export! (fn [_ _ _] (assoc remote-sync :calls (swap! export-calls inc)))]
(is (=? {:status "success"
:message "Stashing to feature-branch"}
(mt/user-http-request :crowberto :post 200 "ee/remote-sync/stash"
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
index b32ea1cdd84..e79e7bee0ef 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
@@ -375,7 +375,7 @@
impl/async-import! (fn [branch force? args]
(reset! import-called? true)
(reset! import-args {:branch branch :force? force? :args args})
- 123)
+ {:id 123})
collection/remote-synced-collection (constantly nil)]
(let [task-id (impl/finish-remote-config!)]
(is (= 123 task-id)
@@ -397,7 +397,7 @@
remote-sync-branch "main"
remote-sync-type :production]
(with-redefs [source/source-from-settings (constantly mock-source)
- impl/async-import! (fn [& _args] (reset! import-called? true) 123)]
+ impl/async-import! (fn [& _args] (reset! import-called? true) {:id 123})]
(let [task-id (impl/finish-remote-config!)]
(is (= 123 task-id)
"Should return task ID from async-import!")
diff --git a/resources/migrations/057_update_migrations.yaml b/resources/migrations/057_update_migrations.yaml
index 93e7c3fd31b..2b42cf3f32a 100644
--- a/resources/migrations/057_update_migrations.yaml
+++ b/resources/migrations/057_update_migrations.yaml
@@ -2387,6 +2387,36 @@ databaseChangeLog:
columnName: source_type
columnDataType: varchar(32)
+ - changeSet:
+ id: v57.2025-10-27T19:01:00
+ author: qnkhuat
+ comment: Usage analytics; Add documents to content view
+ changes:
+ - sqlFile:
+ dbms: postgresql
+ path: instance_analytics_views/content/v5/postgres-content.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: mysql,mariadb
+ path: instance_analytics_views/content/v5/mysql-content.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: h2
+ path: instance_analytics_views/content/v5/h2-content.sql
+ relativeToChangelogFile: true
+ rollback:
+ - sqlFile:
+ dbms: postgresql
+ path: instance_analytics_views/content/v3/postgres-content.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: mysql,mariadb
+ path: instance_analytics_views/content/v4/mysql-content.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: h2
+ path: instance_analytics_views/content/v3/h2-content.sql
+ relativeToChangelogFile: true
# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<
diff --git a/resources/migrations/instance_analytics_views/content/v5/h2-content.sql b/resources/migrations/instance_analytics_views/content/v5/h2-content.sql
new file mode 100644
index 00000000000..5b6bc9c9414
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/content/v5/h2-content.sql
@@ -0,0 +1,186 @@
+drop view if exists v_content;
+
+create or replace view v_content as
+select
+ action.id as entity_id,
+ 'action_' || action.id as entity_qualified_id,
+ 'action' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ cast(null as int) as collection_id,
+ made_public_by_id as made_public_by_user,
+ cast(null as boolean) as is_embedding_enabled,
+ cast(null as boolean) as is_verified,
+ archived,
+ type as action_type,
+ model_id as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ cast(null as boolean) as question_is_native,
+ cast(null as timestamp) as event_timestamp
+ from action
+union
+select
+ collection.id as entity_id,
+ 'collection_' || collection.id as entity_qualified_id,
+ 'collection' as entity_type,
+ created_at,
+ null as updated_at,
+ null as creator_id,
+ name,
+ description,
+ null as collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ cast(null as boolean) as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ case when authority_level='official' then true else false end as collection_is_official,
+ case when personal_owner_id is not null then true else false end as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from collection
+union
+select
+ report_card.id as entity_id,
+ 'card_' || report_card.id as entity_qualified_id,
+ type AS entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ collection_id as collection_id,
+ made_public_by_id as made_public_by_user,
+ enable_embedding as is_embedding_enabled,
+ case when is_verified then true else false end as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ display as question_viz_type,
+ 'database_' || database_id as question_database_id,
+ case when query_type='native' then true else false end as question_is_native,
+ null as event_timestamp
+ from report_card
+ left join (
+ select
+ moderated_item_type || '_' || moderated_item_id as entity_qualified_id,
+ case when status = 'verified' then true else false end as is_verified
+ from moderation_review
+ where most_recent
+ ) as moderation on 'card_' || report_card.id = moderation.entity_qualified_id
+
+union
+select
+ report_dashboard.id as entity_id,
+ 'dashboard_' || report_dashboard.id as entity_qualified_id,
+ 'dashboard' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ collection_id as collection_id,
+ made_public_by_id as made_public_by_user,
+ enable_embedding as is_embedding_enabled,
+ case when is_verified then true else false end as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from report_dashboard
+ left join (
+ select
+ moderated_item_type || '_' || moderated_item_id as entity_qualified_id,
+ case when status = 'verified' then true else false end as is_verified
+ from moderation_review
+ where most_recent
+ ) as moderation on 'dashboard_' || report_dashboard.id = moderation.entity_qualified_id
+union
+select
+ document.id as entity_id,
+ 'document_' || document.id as entity_qualified_id,
+ 'document' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ cast(null as varchar) as description,
+ collection_id as collection_id,
+ cast(null as int) as made_public_by_user,
+ cast(null as boolean) as is_embedding_enabled,
+ cast(null as boolean) as is_verified,
+ archived,
+ null as action_type,
+ cast(null as int) as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ cast(null as boolean) as question_is_native,
+ cast(null as timestamp) as event_timestamp
+ from document
+union
+select
+ event.id as entity_id,
+ 'event_' || event.id as entity_qualified_id,
+ 'event' as entity_type,
+ event.created_at,
+ event.updated_at,
+ event.creator_id,
+ event.name,
+ event.description,
+ timeline.collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ cast(null as boolean) as is_verified,
+ event.archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ timestamp as event_timestamp
+ from timeline_event event
+ left join timeline on event.timeline_id = timeline.id
+union
+select
+ transform.id as entity_id,
+ 'transform_' || transform.id as entity_qualified_id,
+ 'transform' as entity_type,
+ created_at,
+ updated_at,
+ cast(null as int) as creator_id,
+ name,
+ description,
+ cast(null as int) as collection_id,
+ cast(null as int) as made_public_by_user,
+ cast(null as boolean) as is_embedding_enabled,
+ cast(null as boolean) as is_verified,
+ false as archived,
+ null as action_type,
+ cast(null as int) as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ cast(null as boolean) as question_is_native,
+ cast(null as timestamp) as event_timestamp
+ from transform
diff --git a/resources/migrations/instance_analytics_views/content/v5/mysql-content.sql b/resources/migrations/instance_analytics_views/content/v5/mysql-content.sql
new file mode 100644
index 00000000000..7fdee92ba67
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/content/v5/mysql-content.sql
@@ -0,0 +1,187 @@
+drop view if exists v_content;
+
+create or replace
+SQL SECURITY INVOKER
+view v_content as
+select
+ action.id as entity_id,
+ concat('action_', action.id) as entity_qualified_id,
+ 'action' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ null as collection_id,
+ made_public_by_id as made_public_by_user,
+ null as is_embedding_enabled,
+ null as is_verified,
+ archived,
+ type as action_type,
+ model_id as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from action
+union
+select
+ collection.id as entity_id,
+ concat('collection_', collection.id) as entity_qualified_id,
+ 'collection' as entity_type,
+ created_at,
+ null as updated_at,
+ null as creator_id,
+ name,
+ description,
+ null as collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ null as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ case when authority_level='official' then true else false end as collection_is_official,
+ case when personal_owner_id is not null then true else false end as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from collection
+union
+select
+ report_card.id as entity_id,
+ concat('card_', report_card.id) as entity_qualified_id,
+ type as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ collection_id as collection_id,
+ made_public_by_id as made_public_by_user,
+ enable_embedding as is_embedding_enabled,
+ case when is_verified then true else false end as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ display as question_viz_type,
+ concat('database_', database_id) as question_database_id,
+ case when query_type='native' then true else false end as question_is_native,
+ null as event_timestamp
+ from report_card
+ left join (
+ select
+ concat(moderated_item_type, '_', moderated_item_id) as entity_qualified_id,
+ case when status = 'verified' then true else false end as is_verified
+ from moderation_review
+ where most_recent
+ ) as moderation on concat('card_', report_card.id) = moderation.entity_qualified_id
+union
+select
+ report_dashboard.id as entity_id,
+ concat('dashboard_', report_dashboard.id) as entity_qualified_id,
+ 'dashboard' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ collection_id as collection_id,
+ made_public_by_id as made_public_by_user,
+ enable_embedding as is_embedding_enabled,
+ case when is_verified then true else false end as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from report_dashboard
+ left join (
+ select
+ concat(moderated_item_type, '_', moderated_item_id) as entity_qualified_id,
+ case when status = 'verified' then true else false end as is_verified
+ from moderation_review
+ where most_recent
+ ) as moderation on concat('dashboard_', report_dashboard.id) = moderation.entity_qualified_id
+union
+select
+ document.id as entity_id,
+ concat('document_', document.id) as entity_qualified_id,
+ 'document' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ null as description,
+ collection_id as collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ null as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from document
+union
+select
+ event.id as entity_id,
+ concat('event_', event.id) as entity_qualified_id,
+ 'event' as entity_type,
+ event.created_at,
+ event.updated_at,
+ event.creator_id,
+ event.name,
+ event.description,
+ timeline.collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ null as is_verified,
+ event.archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ timestamp as event_timestamp
+ from timeline_event event
+ left join timeline on event.timeline_id = timeline.id
+union
+select
+ transform.id as entity_id,
+ concat('transform_', transform.id) as entity_qualified_id,
+ 'transform' as entity_type,
+ created_at,
+ updated_at,
+ null as creator_id,
+ name,
+ description,
+ null as collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ null as is_verified,
+ false as archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from transform
diff --git a/resources/migrations/instance_analytics_views/content/v5/postgres-content.sql b/resources/migrations/instance_analytics_views/content/v5/postgres-content.sql
new file mode 100644
index 00000000000..150ec2f9df7
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/content/v5/postgres-content.sql
@@ -0,0 +1,185 @@
+drop view if exists v_content;
+
+create or replace view v_content as
+select
+ action.id as entity_id,
+ 'action_' || action.id as entity_qualified_id,
+ 'action' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ cast(null as int) as collection_id,
+ made_public_by_id as made_public_by_user,
+ cast(null as boolean) as is_embedding_enabled,
+ null::bool as is_verified,
+ archived,
+ type as action_type,
+ model_id as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ cast(null as boolean) as question_is_native,
+ cast(null as timestamp) as event_timestamp
+ from action
+union
+select
+ collection.id as entity_id,
+ 'collection_' || collection.id as entity_qualified_id,
+ 'collection' as entity_type,
+ created_at,
+ null as updated_at,
+ null as creator_id,
+ name,
+ description,
+ null as collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ null::bool as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ case when authority_level='official' then true else false end as collection_is_official,
+ case when personal_owner_id is not null then true else false end as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from collection
+union
+select
+ report_card.id as entity_id,
+ 'card_' || report_card.id as entity_qualified_id,
+ type as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ collection_id as collection_id,
+ made_public_by_id as made_public_by_user,
+ enable_embedding as is_embedding_enabled,
+ case when is_verified then true else false end as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ display as question_viz_type,
+ 'database_' || database_id as question_database_id,
+ case when query_type='native' then true else false end as question_is_native,
+ null as event_timestamp
+ from report_card
+ left join (
+ select
+ moderated_item_type || '_' || moderated_item_id as entity_qualified_id,
+ case when status = 'verified' then true else false end as is_verified
+ from moderation_review
+ where most_recent
+ ) as moderation on 'card_' || report_card.id = moderation.entity_qualified_id
+union
+select
+ report_dashboard.id as entity_id,
+ 'dashboard_' || report_dashboard.id as entity_qualified_id,
+ 'dashboard' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ description,
+ collection_id as collection_id,
+ made_public_by_id as made_public_by_user,
+ enable_embedding as is_embedding_enabled,
+ case when is_verified then true else false end as is_verified,
+ archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ null as event_timestamp
+ from report_dashboard
+ left join (
+ select
+ moderated_item_type || '_' || moderated_item_id as entity_qualified_id,
+ case when status = 'verified' then true else false end as is_verified
+ from moderation_review
+ where most_recent
+ ) as moderation on 'dashboard_' || report_dashboard.id = moderation.entity_qualified_id
+union
+select
+ document.id as entity_id,
+ 'document_' || document.id as entity_qualified_id,
+ 'document' as entity_type,
+ created_at,
+ updated_at,
+ creator_id,
+ name,
+ cast(null as text) as description,
+ collection_id as collection_id,
+ cast(null as int) as made_public_by_user,
+ cast(null as boolean) as is_embedding_enabled,
+ null::bool as is_verified,
+ archived,
+ null as action_type,
+ cast(null as int) as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ cast(null as boolean) as question_is_native,
+ cast(null as timestamp) as event_timestamp
+ from document
+union
+select
+ event.id as entity_id,
+ 'event_' || event.id as entity_qualified_id,
+ 'event' as entity_type,
+ event.created_at,
+ event.updated_at,
+ event.creator_id,
+ event.name,
+ event.description,
+ timeline.collection_id,
+ null as made_public_by_user,
+ null as is_embedding_enabled,
+ null::bool as is_verified,
+ event.archived,
+ null as action_type,
+ null as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ null as question_is_native,
+ timestamp as event_timestamp
+ from timeline_event event
+ left join timeline on event.timeline_id = timeline.id
+union
+select
+ transform.id as entity_id,
+ 'transform_' || transform.id as entity_qualified_id,
+ 'transform' as entity_type,
+ created_at,
+ updated_at,
+ cast(null as int) as creator_id,
+ name,
+ description,
+ cast(null as int) as collection_id,
+ cast(null as int) as made_public_by_user,
+ cast(null as boolean) as is_embedding_enabled,
+ null::bool as is_verified,
+ false as archived,
+ null as action_type,
+ cast(null as int) as action_model_id,
+ null as collection_is_official,
+ null as collection_is_personal,
+ null as question_viz_type,
+ null as question_database_id,
+ cast(null as boolean) as question_is_native,
+ cast(null as timestamp) as event_timestamp
+ from transform
diff --git a/src/metabase/audit_app/events/audit_log.clj b/src/metabase/audit_app/events/audit_log.clj
index 955c02b2d32..6f10d0d2fac 100644
--- a/src/metabase/audit_app/events/audit_log.clj
+++ b/src/metabase/audit_app/events/audit_log.clj
@@ -274,3 +274,58 @@
(methodical/defmethod events/publish-event! ::cloud-add-on-event
[topic event]
(audit-log/record-event! topic event))
+
+(derive ::document-event ::event)
+(derive :event/document-create ::document-event)
+(derive :event/document-update ::document-event)
+(derive :event/document-delete ::document-event)
+
+(methodical/defmethod events/publish-event! ::document-event
+ [topic event]
+ (audit-log/record-event! topic event))
+
+(derive ::comment-event ::event)
+(derive :event/comment-create ::comment-event)
+(derive :event/comment-update ::comment-event)
+(derive :event/comment-delete ::comment-event)
+
+(methodical/defmethod events/publish-event! ::comment-event
+ [topic event]
+ (audit-log/record-event! topic event))
+
+(derive ::transform-event ::event)
+(derive :event/transform-create ::transform-event)
+(derive :event/update-transform ::transform-event)
+(derive :event/transform-delete ::transform-event)
+(derive :event/transform-run-start ::transform-event)
+
+(methodical/defmethod events/publish-event! ::transform-event
+ [topic event]
+ (audit-log/record-event! topic event))
+
+(derive ::glossary-event ::event)
+(derive :event/glossary-create ::glossary-event)
+(derive :event/glossary-update ::glossary-event)
+(derive :event/glossary-delete ::glossary-event)
+
+(methodical/defmethod events/publish-event! ::glossary-event
+ [topic event]
+ (audit-log/record-event! topic event))
+
+(derive ::remote-sync-event ::event)
+(derive :event/remote-sync-import ::remote-sync-event)
+(derive :event/remote-sync-export ::remote-sync-event)
+(derive :event/remote-sync-settings-update ::remote-sync-event)
+(derive :event/remote-sync-create-branch ::remote-sync-event)
+(derive :event/remote-sync-stash ::remote-sync-event)
+
+(methodical/defmethod events/publish-event! ::remote-sync-event
+ [topic event]
+ (audit-log/record-event! topic event))
+
+(derive ::action-v2-event ::event)
+(derive :event/action-v2-execute ::action-v2-event)
+
+(methodical/defmethod events/publish-event! ::action-v2-event
+ [topic event]
+ (audit-log/record-event! topic event))
diff --git a/src/metabase/audit_app/models/audit_log.clj b/src/metabase/audit_app/models/audit_log.clj
index 0c7c552ed20..074326929c6 100644
--- a/src/metabase/audit_app/models/audit_log.clj
+++ b/src/metabase/audit_app/models/audit_log.clj
@@ -1,5 +1,5 @@
(ns metabase.audit-app.models.audit-log
- "Model defenition for the Metabase Audit Log, which tracks actions taken by users across the Metabase app. This is
+ "Model definition for the Metabase Audit Log, which tracks actions taken by users across the Metabase app. This is
distinct from the View Log model, which predates this namespace, and which powers specific API endpoints used for
in-app functionality, such as the recently-viewed items displayed on the homepage."
(:require
@@ -121,6 +121,34 @@
:table_id table-id
:database_id db-id)))
+(defmethod model-details :model/Document
+ [document _event-type]
+ (select-keys document [:name :collection_id]))
+
+(defmethod model-details :model/Comment
+ [comment _event-type]
+ (select-keys comment [:target_type :target_id :child_target_id :parent_comment_id]))
+
+(defmethod model-details :model/Transform
+ [{:keys [source] :as transform} _event-type]
+ (merge (select-keys transform [:name :description :target :run_trigger])
+ {:source
+ (-> source
+ (dissoc :body)
+ (update :query #(select-keys % [:database])))}))
+
+(defmethod model-details :model/TransformRun
+ [transform-run _event-type]
+ (select-keys transform-run [:transform_id :status :run_method]))
+
+(defmethod model-details :model/Glossary
+ [glossary _event-type]
+ (select-keys glossary [:term]))
+
+(defmethod model-details :model/RemoteSyncTask
+ [task _event-type]
+ (select-keys task [:sync_task_type :version]))
+
(def ^:private model-name->audit-logged-name
{"RootCollection" "Collection"})
@@ -190,7 +218,7 @@
- `:user-id`: the user ID that initiated the event (defaults: `api/*current-user-id*`)
- `:model`: the name of the model the event is acting on, e.g. `:model/Card` or \"Card\" (default: model of `:object`)
- `:model-id`: the ID of the model the event is acting on (default: ID of `:object`)
- - `:details`: a map of arbitrary details relavent to the event, which is recorded as-is (default: {})
+ - `:details`: a map of arbitrary details relevant to the event, which is recorded as-is (default: {})
`:object` and `:previous-object` both have `model-details` called on them to determine which fields should be audited,
then they are added to `:details` before the event is recorded. `:previous-object` is only included if any audited fields
diff --git a/src/metabase/glossary/api.clj b/src/metabase/glossary/api.clj
index 454a72f7db5..fb55eff2e7c 100644
--- a/src/metabase/glossary/api.clj
+++ b/src/metabase/glossary/api.clj
@@ -3,6 +3,7 @@
(:require
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
+ [metabase.events.core :as events]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
@@ -24,7 +25,11 @@
{:keys [term definition]} :- [:map
[:term ms/NonBlankString]
[:definition ms/NonBlankString]]]
- (t2/insert-returning-instance! :model/Glossary {:term term :definition definition}))
+ (let [glossary (t2/insert-returning-instance! :model/Glossary {:term term :definition definition})]
+ (events/publish-event! :event/glossary-create
+ {:object glossary
+ :user-id api/*current-user-id*})
+ glossary))
(api.macros/defendpoint :put "/:id"
"Update an existing glossary entry."
@@ -33,13 +38,21 @@
{:keys [term definition]} :- [:map
[:term ms/NonBlankString]
[:definition ms/NonBlankString]]]
- (api/check-404 (t2/select-one :model/Glossary :id id))
- (t2/update! :model/Glossary id {:term term :definition definition})
- (t2/select-one :model/Glossary :id id))
+ (let [previous-glossary (api/check-404 (t2/select-one :model/Glossary :id id))]
+ (t2/update! :model/Glossary id {:term term :definition definition})
+ (let [glossary (t2/select-one :model/Glossary :id id)]
+ (events/publish-event! :event/glossary-update
+ {:object glossary
+ :previous-object previous-glossary
+ :user-id api/*current-user-id*})
+ glossary)))
(api.macros/defendpoint :delete "/:id"
"Delete a glossary entry."
[{:keys [id]} :- [:map [:id ms/PositiveInt]]]
- (api/check-404 (t2/select-one :model/Glossary :id id))
- (t2/delete! :model/Glossary :id id)
+ (let [glossary (api/check-404 (t2/select-one :model/Glossary :id id))]
+ (t2/delete! :model/Glossary :id id)
+ (events/publish-event! :event/glossary-delete
+ {:object glossary
+ :user-id api/*current-user-id*}))
api/generic-204-no-content)
diff --git a/test/metabase/audit_app/events/audit_log_test.clj b/test/metabase/audit_app/events/audit_log_test.clj
index 27683500ece..6008d9b7353 100644
--- a/test/metabase/audit_app/events/audit_log_test.clj
+++ b/test/metabase/audit_app/events/audit_log_test.clj
@@ -5,6 +5,7 @@
the test code for the feature."
(:require
[clojure.test :refer :all]
+ [java-time.api :as t]
[metabase.audit-app.events.audit-log :as events.audit-log]
[metabase.events.core :as events]
[metabase.lib.schema.id :as lib.schema.id]
@@ -508,3 +509,253 @@
:topic :channel-update
:model "Channel"}
(mt/latest-audit-log-entry :channel-update (:id channel))))))))
+
+(deftest transform-run-start-event-test
+ (mt/when-ee-evailable
+ (testing :event/transform-run-start
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-temp [:model/Transform transform {:name "Test Transform"}
+ :model/TransformRun transform-run {:transform_id (:id transform)
+ :status :started
+ :run_method :manual
+ :is_active true}]
+ (is (= {:object transform-run}
+ (events/publish-event! :event/transform-run-start {:object transform-run})))
+ (is (= {:model_id (:id transform-run)
+ :user_id (mt/user->id :rasta)
+ :details {:transform_id (:id transform)
+ :status "started"
+ :run_method "manual"}
+ :topic :transform-run-start
+ :model "TransformRun"}
+ (mt/latest-audit-log-entry :transform-run-start (:id transform-run)))))))))
+
+(deftest document-events-test
+ (mt/when-ee-evailable
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-temp [:model/Document document {:name "Test Document"
+ :collection_id nil}]
+ (testing :event/document-create
+ (is (= {:object document}
+ (events/publish-event! :event/document-create {:object document})))
+ (is (= {:model_id (:id document)
+ :user_id (mt/user->id :rasta)
+ :details {:name "Test Document"
+ :collection_id nil}
+ :topic :document-create
+ :model "Document"}
+ (mt/latest-audit-log-entry :document-create (:id document)))))
+
+ (testing :event/document-update
+ (let [updated-doc (assoc document :name "Updated Document")]
+ (is (= {:object updated-doc
+ :previous-object document}
+ (events/publish-event! :event/document-update {:object updated-doc
+ :previous-object document})))
+ (is (= {:model_id (:id document)
+ :user_id (mt/user->id :rasta)
+ :details {:previous {:name "Test Document"}
+ :new {:name "Updated Document"}}
+ :topic :document-update
+ :model "Document"}
+ (mt/latest-audit-log-entry :document-update (:id document))))))
+
+ (testing :event/document-delete
+ (is (= {:object document}
+ (events/publish-event! :event/document-delete {:object document})))
+ (is (= {:model_id (:id document)
+ :user_id (mt/user->id :rasta)
+ :details {:name "Test Document"
+ :collection_id nil}
+ :topic :document-delete
+ :model "Document"}
+ (mt/latest-audit-log-entry :document-delete (:id document)))))))))
+
+(deftest comment-events-test
+ (mt/when-ee-evailable
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-temp [:model/Document {doc-id :id} {}
+ :model/Comment comment {:target_id doc-id
+ :content "{}"}]
+ (testing :event/comment-create
+ (is (= {:object comment}
+ (events/publish-event! :event/comment-create {:object comment})))
+ (is (= {:model_id (:id comment)
+ :user_id (mt/user->id :rasta)
+ :details {:target_id doc-id
+ :target_type "document"
+ :child_target_id nil
+ :parent_comment_id nil}
+ :topic :comment-create
+ :model "Comment"}
+ (mt/latest-audit-log-entry :comment-create (:id comment)))))
+
+ (testing :event/comment-update
+ (let [updated-comment (assoc comment :content "Updated comment")]
+ (is (= {:object updated-comment
+ :previous-object comment}
+ (events/publish-event! :event/comment-update {:object updated-comment
+ :previous-object comment})))
+ (is (=? {:model_id (:id comment)
+ :user_id (mt/user->id :rasta)
+ :details {}
+ :topic :comment-update
+ :model "Comment"}
+ (mt/latest-audit-log-entry :comment-update (:id comment))))))
+
+ (testing :event/comment-delete
+ (is (= {:object comment}
+ (events/publish-event! :event/comment-delete {:object comment})))
+ (is (= {:model_id (:id comment)
+ :user_id (mt/user->id :rasta)
+ :details {:target_type "document"
+ :target_id doc-id
+ :child_target_id nil
+ :parent_comment_id nil}
+ :topic :comment-delete
+ :model "Comment"}
+ (mt/latest-audit-log-entry :comment-delete (:id comment)))))))))
+
+(deftest transform-events-test
+ (mt/when-ee-evailable
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-temp [:model/Transform transform {:name "Test Transform"
+ :description "Test description"}]
+ (testing :event/transform-create
+ (is (= {:object transform}
+ (events/publish-event! :event/transform-create {:object transform})))
+ (is (partial=
+ {:model_id (:id transform)
+ :user_id (mt/user->id :rasta)
+ :details {:name "Test Transform"
+ :description "Test description"}
+ :topic :transform-create
+ :model "Transform"}
+ (mt/latest-audit-log-entry :transform-create (:id transform)))))
+
+ (testing :event/update-transform
+ (let [updated-transform (assoc transform :name "Updated Transform")]
+ (is (= {:object updated-transform
+ :previous-object transform}
+ (events/publish-event! :event/update-transform {:object updated-transform
+ :previous-object transform})))
+ (is (= {:model_id (:id transform)
+ :user_id (mt/user->id :rasta)
+ :details {:previous {:name "Test Transform"}
+ :new {:name "Updated Transform"}}
+ :topic :update-transform
+ :model "Transform"}
+ (mt/latest-audit-log-entry :update-transform (:id transform))))))
+
+ (testing :event/transform-delete
+ (is (= {:object transform}
+ (events/publish-event! :event/transform-delete {:object transform})))
+ (is (partial=
+ {:model_id (:id transform)
+ :user_id (mt/user->id :rasta)
+ :details {:name "Test Transform"
+ :description "Test description"}
+ :topic :transform-delete
+ :model "Transform"}
+ (mt/latest-audit-log-entry :transform-delete (:id transform)))))))))
+
+(deftest glossary-events-test
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-temp [:model/Glossary glossary {:term "Test Term" :definition "Definition"}]
+ (testing :event/glossary-create
+ (is (= {:object glossary}
+ (events/publish-event! :event/glossary-create {:object glossary})))
+ (is (= {:model_id (:id glossary)
+ :user_id (mt/user->id :rasta)
+ :details {:term "Test Term"}
+ :topic :glossary-create
+ :model "Glossary"}
+ (mt/latest-audit-log-entry :glossary-create (:id glossary)))))
+
+ (testing :event/glossary-update
+ (let [updated-glossary (assoc glossary :term "Updated Term")]
+ (is (= {:object updated-glossary
+ :previous-object glossary}
+ (events/publish-event! :event/glossary-update {:object updated-glossary
+ :previous-object glossary})))
+ (is (= {:model_id (:id glossary)
+ :user_id (mt/user->id :rasta)
+ :details {:previous {:term "Test Term"}
+ :new {:term "Updated Term"}}
+ :topic :glossary-update
+ :model "Glossary"}
+ (mt/latest-audit-log-entry :glossary-update (:id glossary))))))
+
+ (testing :event/glossary-delete
+ (is (= {:object glossary}
+ (events/publish-event! :event/glossary-delete {:object glossary})))
+ (is (= {:model_id (:id glossary)
+ :user_id (mt/user->id :rasta)
+ :details {:term "Test Term"}
+ :topic :glossary-delete
+ :model "Glossary"}
+ (mt/latest-audit-log-entry :glossary-delete (:id glossary))))))))
+
+(deftest remote-sync-events-test
+ (mt/when-ee-evailable
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-temp [:model/RemoteSyncTask {task-id :id :as task}
+ {:sync_task_type "import"
+ :version "v1"
+ :initiated_by (mt/user->id :rasta)
+ :started_at (t/offset-date-time 2025 9 30 14 0 0)
+ :ended_at (t/offset-date-time 2025 9 30 14 0 0)}]
+ (testing :event/remote-sync-import
+ (is (= {:object task}
+ (events/publish-event! :event/remote-sync-import {:object task})))
+ (is (= {:model_id task-id
+ :user_id (mt/user->id :rasta)
+ :details {:sync_task_type "import"
+ :version "v1"}
+ :topic :remote-sync-import
+ :model "RemoteSyncTask"}
+ (mt/latest-audit-log-entry :remote-sync-import task-id))))
+
+ (testing :event/remote-sync-export
+ (is (= {:object task}
+ (events/publish-event! :event/remote-sync-export {:object task})))
+ (is (= {:model_id task-id
+ :user_id (mt/user->id :rasta)
+ :details {:sync_task_type "import"
+ :version "v1"}
+ :topic :remote-sync-export
+ :model "RemoteSyncTask"}
+ (mt/latest-audit-log-entry :remote-sync-export task-id))))
+
+ (testing :event/remote-sync-settings-update
+ (is (= {:object task}
+ (events/publish-event! :event/remote-sync-settings-update {:object task})))
+ (is (= {:model_id task-id
+ :user_id (mt/user->id :rasta)
+ :details {:sync_task_type "import"
+ :version "v1"}
+ :topic :remote-sync-settings-update
+ :model "RemoteSyncTask"}
+ (mt/latest-audit-log-entry :remote-sync-settings-update task-id))))
+
+ (testing :event/remote-sync-create-branch
+ (is (= {:object task}
+ (events/publish-event! :event/remote-sync-create-branch {:object task})))
+ (is (= {:model_id task-id
+ :user_id (mt/user->id :rasta)
+ :details {:sync_task_type "import"
+ :version "v1"}
+ :topic :remote-sync-create-branch
+ :model "RemoteSyncTask"}
+ (mt/latest-audit-log-entry :remote-sync-create-branch task-id))))
+
+ (testing :event/remote-sync-stash
+ (is (= {:object task}
+ (events/publish-event! :event/remote-sync-stash {:object task})))
+ (is (= {:model_id task-id
+ :user_id (mt/user->id :rasta)
+ :details {:sync_task_type "import"
+ :version "v1"}
+ :topic :remote-sync-stash
+ :model "RemoteSyncTask"}
+ (mt/latest-audit-log-entry :remote-sync-stash task-id))))))))