Skip to content
Snippets Groups Projects
Unverified Commit 27808a5e authored by Cam Saul's avatar Cam Saul
Browse files

Cleanup for DashboardCard util fns :shower:

parent 55ba22d7
Branches
Tags
No related merge requests found
......@@ -20,8 +20,8 @@
(declare series)
(defn- perms-objects-set
"Return the set of permissions required to READ-OR-WRITE this `DashboardCard`.
If `:card` and `:series` are already hydrated this method doesn't need to make any DB calls."
"Return the set of permissions required to `read-or-write` this DashboardCard. If `:card` and `:series` are already
hydrated this method doesn't need to make any DB calls."
[dashcard read-or-write]
(let [card (or (:card dashcard)
(db/select-one [Card :dataset_query] :id (u/get-id (:card_id dashcard))))
......@@ -58,14 +58,14 @@
;;; --------------------------------------------------- HYDRATION ----------------------------------------------------
(defn dashboard
"Return the `Dashboard` associated with the `DashboardCard`."
"Return the Dashboard associated with the DashboardCard."
[{:keys [dashboard_id]}]
{:pre [(integer? dashboard_id)]}
(db/select-one 'Dashboard, :id dashboard_id))
(defn ^:hydrate series
"Return the `Cards` associated as additional series on this `DashboardCard`."
"Return the `Cards` associated as additional series on this DashboardCard."
[{:keys [id]}]
(db/select [Card :id :name :description :display :dataset_query :visualization_settings :collection_id]
(mdb/join [Card :id] [DashboardCardSeries :card_id])
......@@ -75,25 +75,22 @@
;;; ---------------------------------------------------- CRUD FNS ----------------------------------------------------
(defn retrieve-dashboard-card
"Fetch a single `DashboardCard` by its ID value."
[id]
{:pre [(integer? id)]}
(s/defn retrieve-dashboard-card
"Fetch a single DashboardCard by its ID value."
[id :- su/IntGreaterThanZero]
(-> (DashboardCard id)
(hydrate :series)))
(defn update-dashboard-card-series!
"Update the `DashboardCardSeries` for a given `DashboardCard`.
CARD-IDS should be a definitive collection of *all* IDs of cards for the dashboard card in the desired order.
(s/defn update-dashboard-card-series!
"Update the DashboardCardSeries for a given DashboardCard.
`card-ids` should be a definitive collection of *all* IDs of cards for the dashboard card in the desired order.
* If an ID in CARD-IDS has no corresponding existing `DashboardCardSeries` object, one will be created.
* If an existing `DashboardCardSeries` has no corresponding ID in CARD-IDs, it will be deleted.
* All cards will be updated with a `position` according to their place in the collection of CARD-IDS"
* If an ID in `card-ids` has no corresponding existing DashboardCardSeries object, one will be created.
* If an existing DashboardCardSeries has no corresponding ID in `card-ids`, it will be deleted.
* All cards will be updated with a `position` according to their place in the collection of `card-ids`"
{:arglists '([dashboard-card card-ids])}
[{:keys [id]} card-ids]
{:pre [(integer? id)
(sequential? card-ids)
(every? integer? card-ids)]}
[{:keys [id]} :- {:id su/IntGreaterThanZero, s/Keyword s/Any}
card-ids :- [su/IntGreaterThanZero]]
;; first off, just delete all series on the dashboard card (we add them again below)
(db/delete! DashboardCardSeries :dashboardcard_id id)
;; now just insert all of the series that were given to us
......@@ -103,14 +100,18 @@
card-ids)]
(db/insert-many! DashboardCardSeries cards))))
(defn update-dashboard-card!
"Update an existing `DashboardCard`, including all `DashboardCardSeries`.
Returns the updated `DashboardCard` or throws an Exception."
[{:keys [id series parameter_mappings visualization_settings] :as dashboard-card}]
{:pre [(integer? id)
(u/maybe? u/sequence-of-maps? parameter_mappings)
(u/maybe? map? visualization_settings)
(every? integer? series)]}
(def ^:private DashboardCardUpdates
{:id su/IntGreaterThanZero
(s/optional-key :parameter_mappings) (s/maybe [su/Map])
(s/optional-key :visualization_settings) (s/maybe su/Map)
;; series is a sequence of IDs of additional cards after the first to include as "additional serieses"
(s/optional-key :series) (s/maybe [su/IntGreaterThanZero])
s/Keyword s/Any})
(s/defn update-dashboard-card!
"Update an existing DashboardCard` including all DashboardCardSeries.
Returns the updated DashboardCard or throws an Exception."
[{:keys [id series parameter_mappings visualization_settings] :as dashboard-card} :- DashboardCardUpdates]
(let [{:keys [sizeX sizeY row col series]} (merge {:series []} dashboard-card)]
(db/transaction
;; update the dashcard itself (positional attributes)
......@@ -123,9 +124,9 @@
:parameter_mappings parameter_mappings
:visualization_settings visualization_settings))
;; update series (only if they changed)
(when (not= series (map :card_id (db/select [DashboardCardSeries :card_id]
:dashboardcard_id id
{:order-by [[:position :asc]]})))
(when-not (= series (map :card_id (db/select [DashboardCardSeries :card_id]
:dashboardcard_id id
{:order-by [[:position :asc]]})))
(update-dashboard-card-series! dashboard-card series))
;; fetch the fully updated dashboard card then return it (and fire off an event)
(->> (retrieve-dashboard-card id)
......@@ -140,8 +141,8 @@
s/Keyword s/Any})
(s/defn create-dashboard-card!
"Create a new `DashboardCard` by inserting it into the database along with all associated pieces of data such as
`DashboardCardSeries`. Returns the newly created `DashboardCard` or throws an Exception."
"Create a new DashboardCard by inserting it into the database along with all associated pieces of data such as
DashboardCardSeries. Returns the newly created DashboardCard or throws an Exception."
[dashboard-card :- NewDashboardCard]
(let [{:keys [dashboard_id card_id creator_id parameter_mappings visualization_settings sizeX sizeY row col series]
:or {sizeX 2, sizeY 2, series []}} dashboard-card]
......@@ -164,7 +165,7 @@
(dissoc dashcard :actor_id))))))
(defn delete-dashboard-card!
"Delete a `DashboardCard`."
"Delete a DashboardCard`"
[dashboard-card user-id]
{:pre [(map? dashboard-card)
(integer? user-id)]}
......
......@@ -15,7 +15,7 @@
[metabase.util.i18n :refer [trs tru]]
[ring.util.codec :as codec])
(:import [java.io BufferedReader Reader]
[java.net InetAddress InetSocketAddress Socket URL]
[java.net InetAddress InetSocketAddress Socket]
[java.text Normalizer Normalizer$Form]
java.util.concurrent.TimeoutException
java.util.Locale
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment