A Metric is a saved MBQL 'macro' expanding to a combination of | (ns metabase.models.legacy-metric (:require [medley.core :as m] [metabase.api.common :as api] [metabase.models.audit-log :as audit-log] [metabase.models.data-permissions :as data-perms] [metabase.models.database :as database] [metabase.models.interface :as mi] [metabase.models.revision :as revision] [metabase.util :as u] [metabase.util.i18n :refer [tru]] [methodical.core :as methodical] [toucan2.core :as t2])) |
Used to be the toucan1 model name defined using [[toucan.models/defmodel]], not it's a reference to the toucan2 model name. We'll keep this till we replace all these symbols in our codebase. | (def LegacyMetric :model/LegacyMetric) |
(methodical/defmethod t2/table-name :model/LegacyMetric [_model] :metric) | |
(doto :model/LegacyMetric (derive :metabase/model) (derive :hook/timestamped?) (derive ::mi/write-policy.superuser) (derive ::mi/create-policy.superuser)) | |
(defmethod mi/can-read? :model/LegacyMetric ([instance] (let [table (:table (t2/hydrate instance :table))] (data-perms/user-has-permission-for-table? api/*current-user-id* :perms/manage-table-metadata :yes (:db_id table) (u/the-id table)))) ([model pk] (mi/can-read? (t2/select-one model pk)))) | |
(t2/deftransforms :model/LegacyMetric {:definition mi/transform-legacy-metric-segment-definition}) | |
(t2/define-before-update :model/LegacyMetric [{:keys [creator_id id], :as metric}] (u/prog1 (t2/changes metric) ;; throw an Exception if someone tries to update creator_id (when (contains? <> :creator_id) (when (not= (:creator_id <>) (t2/select-one-fn :creator_id LegacyMetric :id id)) (throw (UnsupportedOperationException. (tru "You cannot update the creator_id of a Metric."))))))) | |
(t2/define-before-delete :model/LegacyMetric [{:keys [id] :as _metric}] (t2/delete! :model/Revision :model "Metric" :model_id id)) | |
(defmethod mi/perms-objects-set LegacyMetric [metric read-or-write] (let [table (or (:table metric) (t2/select-one ['Table :db_id :schema :id] :id (u/the-id (:table_id metric))))] (mi/perms-objects-set table read-or-write))) | |
--------------------------------------------------- REVISIONS ---------------------------------------------------- | |
(defmethod revision/serialize-instance LegacyMetric [_model _id instance] (dissoc instance :created_at :updated_at)) | |
(defmethod revision/diff-map LegacyMetric [model metric1 metric2] (if-not metric1 ;; model is the first version of the metric (m/map-vals (fn [v] {:after v}) (select-keys metric2 [:name :description :definition])) ;; do our diff logic (let [base-diff ((get-method revision/diff-map :default) model (select-keys metric1 [:name :description :definition]) (select-keys metric2 [:name :description :definition]))] (cond-> (merge-with merge (m/map-vals (fn [v] {:after v}) (:after base-diff)) (m/map-vals (fn [v] {:before v}) (:before base-diff))) (or (get-in base-diff [:after :definition]) (get-in base-diff [:before :definition])) (assoc :definition {:before (get metric1 :definition) :after (get metric2 :definition)}))))) | |
------------------------------------------------ Audit Log -------------------------------------------------------- | |
(defmethod audit-log/model-details :model/LegacyMetric [metric _event-type] (let [table-id (:table_id metric) db-id (database/table-id->database-id table-id)] (assoc (select-keys metric [:name :description :revision_message]) :table_id table-id :database_id db-id))) | |