Skip to content
Snippets Groups Projects
Commit e153fbde authored by Cam Saül's avatar Cam Saül
Browse files

Merge pull request #2356 from metabase/cleanup-utils

Cleanup utils :cry:
parents 44ec0f16 45012e1d
No related branches found
No related tags found
No related merge requests found
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
(check 1) (check 1)
(checkp 1) (checkp 1)
(cond-as-> 2) (cond-as-> 2)
(cond-let 0)
(conda 0)
(context 2) (context 2)
(create-database-definition 1) (create-database-definition 1)
(engine-case 0) (engine-case 0)
......
...@@ -484,10 +484,11 @@ ...@@ -484,10 +484,11 @@
"Save QueryExecution state and construct a completed (successful) query response" "Save QueryExecution state and construct a completed (successful) query response"
[query-execution query-result] [query-execution query-result]
;; record our query execution and format response ;; record our query execution and format response
(-> (u/assoc<> query-execution (-> (assoc query-execution
:status :completed :status :completed
:finished_at (u/new-sql-timestamp) :finished_at (u/new-sql-timestamp)
:running_time (- (System/currentTimeMillis) (:start_time_millis <>)) :running_time (- (System/currentTimeMillis)
(:start_time_millis query-execution))
:result_rows (get query-result :row_count 0)) :result_rows (get query-result :row_count 0))
(dissoc :start_time_millis) (dissoc :start_time_millis)
(save-query-execution) (save-query-execution)
......
...@@ -122,9 +122,7 @@ ...@@ -122,9 +122,7 @@
(let [report-tz (driver/report-timezone)] (let [report-tz (driver/report-timezone)]
(when-not (empty? report-tz) (when-not (empty? report-tz)
report-tz)))}] report-tz)))}]
(->> (u/filter-nil-values settings) (qp (assoc query :settings (m/filter-vals (complement nil?) settings))))))
(assoc query :settings)
qp))))
(defn- pre-expand-macros (defn- pre-expand-macros
......
(ns metabase.models.metric (ns metabase.models.metric
(:require [korma.core :as k] (:require [korma.core :as k]
[medley.core :as m]
[metabase.db :as db] [metabase.db :as db]
[metabase.events :as events] [metabase.events :as events]
(metabase.models [dependency :as dependency] (metabase.models [dependency :as dependency]
...@@ -30,17 +31,17 @@ ...@@ -30,17 +31,17 @@
(defn- diff-metrics [this metric1 metric2] (defn- diff-metrics [this metric1 metric2]
(if-not metric1 (if-not metric1
;; this is the first version of the metric ;; this is the first version of the metric
(u/update-values (select-keys metric2 [:name :description :definition]) (fn [v] {:after v})) (m/map-vals (fn [v] {:after v}) (select-keys metric2 [:name :description :definition]))
;; do our diff logic ;; do our diff logic
(let [base-diff (revision/default-diff-map this (let [base-diff (revision/default-diff-map this
(select-keys metric1 [:name :description :definition]) (select-keys metric1 [:name :description :definition])
(select-keys metric2 [:name :description :definition]))] (select-keys metric2 [:name :description :definition]))]
(cond-> (merge-with merge (cond-> (merge-with merge
(u/update-values (:after base-diff) (fn [v] {:after v})) (m/map-vals (fn [v] {:after v}) (:after base-diff))
(u/update-values (:before base-diff) (fn [v] {:before v}))) (m/map-vals (fn [v] {:before v}) (:before base-diff)))
(or (get-in base-diff [:after :definition]) (or (get-in base-diff [:after :definition])
(get-in base-diff [:before :definition])) (assoc :definition {:before (get-in metric1 [:definition]) (get-in base-diff [:before :definition])) (assoc :definition {:before (get-in metric1 [:definition])
:after (get-in metric2 [:definition])}))))) :after (get-in metric2 [:definition])})))))
(u/strict-extend (class Metric) (u/strict-extend (class Metric)
revision/IRevisioned revision/IRevisioned
......
(ns metabase.models.segment (ns metabase.models.segment
(:require [korma.core :as k] (:require [korma.core :as k]
[medley.core :as m]
[metabase.db :as db] [metabase.db :as db]
[metabase.events :as events] [metabase.events :as events]
(metabase.models [hydrate :refer [hydrate]] (metabase.models [hydrate :refer [hydrate]]
...@@ -29,14 +30,14 @@ ...@@ -29,14 +30,14 @@
(defn- diff-segments [this segment1 segment2] (defn- diff-segments [this segment1 segment2]
(if-not segment1 (if-not segment1
;; this is the first version of the segment ;; this is the first version of the segment
(u/update-values (select-keys segment2 [:name :description :definition]) (fn [v] {:after v})) (m/map-vals (fn [v] {:after v}) (select-keys segment2 [:name :description :definition]))
;; do our diff logic ;; do our diff logic
(let [base-diff (revision/default-diff-map this (let [base-diff (revision/default-diff-map this
(select-keys segment1 [:name :description :definition]) (select-keys segment1 [:name :description :definition])
(select-keys segment2 [:name :description :definition]))] (select-keys segment2 [:name :description :definition]))]
(cond-> (merge-with merge (cond-> (merge-with merge
(u/update-values (:after base-diff) (fn [v] {:after v})) (m/map-vals (fn [v] {:after v}) (:after base-diff))
(u/update-values (:before base-diff) (fn [v] {:before v}))) (m/map-vals (fn [v] {:before v}) (:before base-diff)))
(or (get-in base-diff [:after :definition]) (or (get-in base-diff [:after :definition])
(get-in base-diff [:before :definition])) (assoc :definition {:before (get-in segment1 [:definition]) (get-in base-diff [:before :definition])) (assoc :definition {:before (get-in segment1 [:definition])
:after (get-in segment2 [:definition])}))))) :after (get-in segment2 [:definition])})))))
......
...@@ -114,10 +114,6 @@ ...@@ -114,10 +114,6 @@
(^String [date-format date] (^String [date-format date]
(time/unparse (->DateTimeFormatter date-format) (coerce/from-long (.getTime (->Timestamp date)))))) (time/unparse (->DateTimeFormatter date-format) (coerce/from-long (.getTime (->Timestamp date))))))
(def ^{:arglists '([] [date])} date->yyyy-mm-dd
"Format DATE as a `YYYY-MM-DD` string."
(partial format-date "yyyy-MM-dd"))
(def ^{:arglists '([] [date])} date->iso-8601 (def ^{:arglists '([] [date])} date->iso-8601
"Format DATE a an ISO-8601 string." "Format DATE a an ISO-8601 string."
(partial format-date :date-time)) (partial format-date :date-time))
...@@ -250,35 +246,6 @@ ...@@ -250,35 +246,6 @@
;;; ## Etc ;;; ## Etc
(defmacro assoc<>
"Like `assoc`, but associations happen sequentially; i.e. each successive binding can build
upon the result of the previous one using `<>`.
(assoc<> {}
:a 100
:b (+ 100 (:a <>)) ; -> {:a 100 :b 200}"
{:style/indent 1}
[object & kvs]
;; wrap in a `fn` so this can be used in `->`/`->>` forms
`((fn [~'<>]
(let [~@(apply concat (for [[k v] (partition 2 kvs)]
['<> `(assoc ~'<> ~k ~v)]))]
~'<>))
~object))
(defn format-num
"format a number into a more human readable form."
[number]
{:pre [(number? number)]}
(let [decimal-type? #(or (float? %) (decimal? %))]
(cond
;; looks like this is a decimal number, format with precision of 2
(and (decimal-type? number) (not (zero? (mod number 1)))) (format "%,.2f" number)
;; this is a decimal type number with no actual decimal value, so treat it as a whole number
(decimal-type? number) (format "%,d" (long number))
;; otherwise this is a whole number
:else (format "%,d" number))))
(defprotocol ^:private IClobToStr (defprotocol ^:private IClobToStr
(jdbc-clob->str ^String [this] (jdbc-clob->str ^String [this]
"Convert a Postgres/H2/SQLServer JDBC Clob to a string.")) "Convert a Postgres/H2/SQLServer JDBC Clob to a string."))
...@@ -337,19 +304,6 @@ ...@@ -337,19 +304,6 @@
(if (pred? (first args)) [(first args) (next args)] (if (pred? (first args)) [(first args) (next args)]
[default args])) [default args]))
;; provided courtesy of Jay Fields http://blog.jayfields.com/2011/08/clojure-apply-function-to-each-value-of.html
(defn update-values
"Update the values of a map by applying the given function.
Function expects the map value as an arg and optionally accepts additional args as passed."
[m f & args]
(reduce (fn [r [k v]] (assoc r k (apply f v args))) {} m))
(defn filter-nil-values
"Remove any keys from a MAP when the value is `nil`."
[m]
(into {} (for [[k v] m
:when (not (nil? v))]
{k v})))
(defn is-email? (defn is-email?
"Is STRING a valid email address?" "Is STRING a valid email address?"
...@@ -477,13 +431,6 @@ ...@@ -477,13 +431,6 @@
([color-symb x] ([color-symb x]
((ns-resolve 'colorize.core color-symb) (pprint-to-str x)))) ((ns-resolve 'colorize.core color-symb) (pprint-to-str x))))
(defmacro cond-let
"Like `if-let` or `when-let`, but for `cond`."
[binding-form then-form & more]
`(if-let ~binding-form ~then-form
~(when (seq more)
`(cond-let ~@more))))
(defn filtered-stacktrace (defn filtered-stacktrace
"Get the stack trace associated with E and return it as a vector with non-metabase frames filtered out." "Get the stack trace associated with E and return it as a vector with non-metabase frames filtered out."
[^Throwable e] [^Throwable e]
...@@ -538,42 +485,6 @@ ...@@ -538,42 +485,6 @@
[& body] [& body]
`(try ~@body (catch Throwable ~'_))) `(try ~@body (catch Throwable ~'_)))
(defn wrap-try-catch!
"Re-intern FN-SYMB as a new fn that wraps the original with a `try-catch`. Intended for debugging.
(defn z [] (throw (Exception. \"!\")))
(z) ; -> exception
(wrap-try-catch! 'z)
(z) ; -> nil; exception logged with log/error"
[fn-symb]
{:pre [(symbol? fn-symb)
(fn? @(resolve fn-symb))]}
(let [varr (resolve fn-symb)
{nmspc :ns, symb :name} (meta varr)]
(println (format "wrap-try-catch! %s/%s" nmspc symb))
(intern nmspc symb (wrap-try-catch @varr fn-symb))))
(defn ns-wrap-try-catch!
"Re-intern all functions in NAMESPACE as ones that wrap the originals with a `try-catch`.
Defaults to the current namespace. You may optionally exclude a set of symbols using the kwarg `:exclude`.
(ns-wrap-try-catch!)
(ns-wrap-try-catch! 'metabase.driver)
(ns-wrap-try-catch! 'metabase.driver :exclude 'query-complete)
Intended for debugging."
{:arglists '([namespace? :exclude & excluded-symbs])}
[& args]
(let [[nmspc args] (optional #(try-apply the-ns [%]) args *ns*)
excluded (when (= (first args) :exclude)
(set (rest args)))]
(doseq [[symb varr] (ns-interns nmspc)]
(when (fn? @varr)
(when-not (contains? excluded symb)
(wrap-try-catch! (symbol (str (ns-name nmspc) \/ symb))))))))
(defn deref-with-timeout (defn deref-with-timeout
"Call `deref` on a FUTURE and throw an exception if it takes more than TIMEOUT-MS." "Call `deref` on a FUTURE and throw an exception if it takes more than TIMEOUT-MS."
[futur timeout-ms] [futur timeout-ms]
......
...@@ -46,17 +46,6 @@ ...@@ -46,17 +46,6 @@
(expect #inst "2015-11-01" (date-trunc :month friday-the-13th)) (expect #inst "2015-11-01" (date-trunc :month friday-the-13th))
(expect #inst "2015-10-01" (date-trunc :quarter friday-the-13th)) (expect #inst "2015-10-01" (date-trunc :quarter friday-the-13th))
;;; ## tests for ASSOC<>
(expect
{:a 100
:b 200
:c 300}
(assoc<> {}
:a 100
:b (+ 100 (:a <>))
:c (+ 100 (:b <>))))
;;; ## tests for HOST-UP? ;;; ## tests for HOST-UP?
(expect true (expect true
...@@ -71,36 +60,6 @@ ...@@ -71,36 +60,6 @@
(host-port-up? "nosuchhost" 8005)) (host-port-up? "nosuchhost" 8005))
;; ## tests for `(format-num)`
;; basic whole number case
(expect "1" (format-num 1))
(expect "1" (format-num (float 1)))
(expect "1" (format-num (double 1)))
(expect "1" (format-num (bigdec 1)))
(expect "1" (format-num (long 1)))
;; make sure we correctly format down to 2 decimal places
;; note that we are expecting a round DOWN in this case
(expect "1.23" (format-num (float 1.23444)))
(expect "1.23" (format-num (double 1.23444)))
(expect "1.23" (format-num (bigdec 1.23444)))
;; test that we always force precision of 2 on decimal places
(expect "1.20" (format-num (float 1.2)))
(expect "1.20" (format-num (double 1.2)))
(expect "1.20" (format-num (bigdec 1.2)))
;; we can take big numbers and add in commas
(expect "1,234" (format-num 1234))
(expect "1,234" (format-num (float 1234)))
(expect "1,234" (format-num (double 1234)))
(expect "1,234" (format-num (bigdec 1234)))
(expect "1,234" (format-num (long 1234)))
;; we can handle numbers with both commas and decimal places
;; note that we expect a basic round UP here
(expect "1,234.57" (format-num (float 1234.5678)))
(expect "1,234.57" (format-num (double 1234.5678)))
(expect "1,234.57" (format-num (bigdec 1234.5678)))
;;; ## tests for IS-URL? ;;; ## tests for IS-URL?
(expect true (is-url? "http://google.com")) (expect true (is-url? "http://google.com"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment