Skip to content
Snippets Groups Projects
Unverified Commit 4606fb15 authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Remove misc unused backend code 4 (#27134)

* Remove more misc unused backend stuff

* Remove unused requires
parent 610f87b4
No related branches found
No related tags found
No related merge requests found
(ns metabase.sync.util (ns metabase.sync.util
"Utility functions and macros to abstract away some common patterns and operations across the sync processes, such "Utility functions and macros to abstract away some common patterns and operations across the sync processes, such
as logging start/end messages." as logging start/end messages."
(:require [buddy.core.hash :as buddy-hash] (:require
[clojure.math.numeric-tower :as math] [clojure.math.numeric-tower :as math]
[clojure.string :as str] [clojure.string :as str]
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[java-time :as t] [java-time :as t]
[medley.core :as m] [medley.core :as m]
[metabase.driver :as driver] [metabase.driver :as driver]
[metabase.driver.util :as driver.u] [metabase.driver.util :as driver.u]
[metabase.events :as events] [metabase.events :as events]
[metabase.models.database :refer [Database]] [metabase.models.database :refer [Database]]
[metabase.models.field :refer [Field]] [metabase.models.field :refer [Field]]
[metabase.models.interface :as mi] [metabase.models.interface :as mi]
[metabase.models.table :refer [Table]] [metabase.models.table :refer [Table]]
[metabase.models.task-history :refer [TaskHistory]] [metabase.models.task-history :refer [TaskHistory]]
[metabase.query-processor.interface :as qp.i] [metabase.query-processor.interface :as qp.i]
[metabase.sync.interface :as i] [metabase.sync.interface :as i]
[metabase.util :as u] [metabase.util :as u]
[metabase.util.date-2 :as u.date] [metabase.util.date-2 :as u.date]
[metabase.util.i18n :refer [trs]] [metabase.util.i18n :refer [trs]]
[metabase.util.schema :as su] [metabase.util.schema :as su]
[ring.util.codec :as codec] [schema.core :as s]
[schema.core :as s] [toucan.db :as db])
[taoensso.nippy :as nippy] (:import
[toucan.db :as db]) (java.time.temporal Temporal)))
(:import java.time.temporal.Temporal))
;;; +----------------------------------------------------------------------------------------------------------------+ ;;; +----------------------------------------------------------------------------------------------------------------+
;;; | SYNC OPERATION "MIDDLEWARE" | ;;; | SYNC OPERATION "MIDDLEWARE" |
...@@ -314,16 +313,6 @@ ...@@ -314,16 +313,6 @@
(defmethod name-for-logging :default [{field-name :name}] (defmethod name-for-logging :default [{field-name :name}]
(trs "Field ''{0}''" field-name)) (trs "Field ''{0}''" field-name))
(defn calculate-hash
"Calculate a cryptographic hash on `clj-data` and return that hash as a string"
[clj-data]
(->> clj-data
;; Serialize the sorted list to bytes that can be hashed
nippy/fast-freeze
buddy-hash/md5
;; Convert the hash bytes to a string for storage/comparison with the hash in the database
codec/base64-encode))
(s/defn calculate-duration-str :- s/Str (s/defn calculate-duration-str :- s/Str
"Given two datetimes, caculate the time between them, return the result as a string" "Given two datetimes, caculate the time between them, return the result as a string"
[begin-time :- Temporal, end-time :- Temporal] [begin-time :- Temporal, end-time :- Temporal]
......
...@@ -382,6 +382,7 @@ ...@@ -382,6 +382,7 @@
:triggers :triggers
(m/index-by (comp #(get % "db-id") qc/from-job-data :data)))) (m/index-by (comp #(get % "db-id") qc/from-job-data :data))))
;;; TODO -- this is only used in [[metabase.api.card-test]] now
(defn job-info-for-individual-refresh (defn job-info-for-individual-refresh
"Return a set of PersistedInfo ids of all jobs scheduled for individual refreshes." "Return a set of PersistedInfo ids of all jobs scheduled for individual refreshes."
[] []
......
(ns metabase.util (ns metabase.util
"Common utility functions useful throughout the codebase." "Common utility functions useful throughout the codebase."
(:require [clojure.data :as data] (:require
[clojure.java.classpath :as classpath] [clojure.data :as data]
[clojure.math.numeric-tower :as math] [clojure.java.classpath :as classpath]
[clojure.pprint :refer [pprint]] [clojure.math.numeric-tower :as math]
[clojure.set :as set] [clojure.pprint :refer [pprint]]
[clojure.string :as str] [clojure.set :as set]
[clojure.tools.logging :as log] [clojure.string :as str]
[clojure.tools.namespace.find :as ns.find] [clojure.tools.logging :as log]
[clojure.walk :as walk] [clojure.tools.namespace.find :as ns.find]
[colorize.core :as colorize] [clojure.walk :as walk]
[flatland.ordered.map :refer [ordered-map]] [colorize.core :as colorize]
[medley.core :as m] [flatland.ordered.map :refer [ordered-map]]
[metabase.config :as config] [medley.core :as m]
[metabase.shared.util :as shared.u] [metabase.config :as config]
[metabase.util.i18n :refer [trs tru]] [metabase.shared.util :as shared.u]
[nano-id.core :as nano-id] [metabase.util.i18n :refer [trs tru]]
[potemkin :as p] [nano-id.core :as nano-id]
[ring.util.codec :as codec] [potemkin :as p]
[weavejester.dependency :as dep]) [ring.util.codec :as codec]
(:import [java.math MathContext RoundingMode] [weavejester.dependency :as dep])
[java.net InetAddress InetSocketAddress Socket] (:import
[java.text Normalizer Normalizer$Form] (java.net InetAddress InetSocketAddress Socket)
[java.util Base64 Base64$Decoder Base64$Encoder Locale PriorityQueue] (java.text Normalizer Normalizer$Form)
java.util.concurrent.TimeoutException (java.util Base64 Base64$Decoder Base64$Encoder Locale PriorityQueue)
[org.apache.commons.validator.routines RegexValidator UrlValidator])) (java.util.concurrent TimeoutException)
(org.apache.commons.validator.routines RegexValidator UrlValidator)))
(comment shared.u/keep-me) (comment shared.u/keep-me)
...@@ -241,11 +242,6 @@ ...@@ -241,11 +242,6 @@
(fn [_ x] (fn [_ x]
(str x)))) (str x))))
(defn decolorize
"Remove ANSI escape sequences from a String `s`."
^String [s]
(some-> s (str/replace #"\[[;\d]*m" "")))
(defn format-color (defn format-color
"With one arg, converts something to a string and colorizes it. With two args, behaves like `format`, but colorizes "With one arg, converts something to a string and colorizes it. With two args, behaves like `format`, but colorizes
the output. the output.
...@@ -346,19 +342,6 @@ ...@@ -346,19 +342,6 @@
{:pre [(integer? decimal-place) (number? number)]} {:pre [(integer? decimal-place) (number? number)]}
(double (.setScale (bigdec number) decimal-place BigDecimal/ROUND_HALF_UP))) (double (.setScale (bigdec number) decimal-place BigDecimal/ROUND_HALF_UP)))
(defn round-to-precision
"Round (presumably floating-point) `number` to a precision of `sig-figures`. Returns a `Double`.
This rounds by significant figures, not decimal places. See [[round-to-decimals]] for that.
(round-to-precision 4 1234567.89) -> 123500.0"
^Double [^Integer sig-figures ^Number number]
{:pre [(integer? sig-figures) (number? number)]}
(-> number
bigdec
(.round (MathContext. sig-figures RoundingMode/HALF_EVEN))
double))
(defn real-number? (defn real-number?
"Is `x` a real number (i.e. not a `NaN` or an `Infinity`)?" "Is `x` a real number (i.e. not a `NaN` or an `Infinity`)?"
[x] [x]
...@@ -587,6 +570,7 @@ ...@@ -587,6 +570,7 @@
^bytes [^String string] ^bytes [^String string]
(.decode base64-decoder string)) (.decode base64-decoder string))
;;; TODO -- this is only used [[metabase.analytics.snowplow-test]] these days
(defn decode-base64 (defn decode-base64
"Decodes the Base64 string `input` to a UTF-8 string." "Decodes the Base64 string `input` to a UTF-8 string."
[input] [input]
......
...@@ -12,13 +12,6 @@ ...@@ -12,13 +12,6 @@
[] []
(or (public-settings/notification-link-base-url) (public-settings/site-url))) (or (public-settings/notification-link-base-url) (public-settings/site-url)))
(defn pulse-url
"Return an appropriate URL for a `Pulse` with ID.
(pulse-url 10) -> \"http://localhost:3000/pulse#10\""
[^Integer id]
(format "%s/pulse#%d" (site-url) id))
(defn dashboard-url (defn dashboard-url
"Return an appropriate URL for a `Dashboard` with ID. "Return an appropriate URL for a `Dashboard` with ID.
...@@ -33,13 +26,6 @@ ...@@ -33,13 +26,6 @@
[^Integer id] [^Integer id]
(format "%s/question/%d" (site-url) id)) (format "%s/question/%d" (site-url) id))
(defn segment-url
"Return an appropriate URL for a `Segment` with ID.
(segment-url 10) -> \"http://localhost:3000/admin/datamodel/segment/10\""
[^Integer id]
(format "%s/admin/datamodel/segment/%d" (site-url) id))
(defn public-card-prefix (defn public-card-prefix
"URL prefix for a public Cards. Get the complete URL by adding the UUID to the end." "URL prefix for a public Cards. Get the complete URL by adding the UUID to the end."
[] []
......
(ns metabase.sync.analyze.fingerprint.insights-test (ns metabase.sync.analyze.fingerprint.insights-test
(:require [clojure.test :refer :all] (:require
[metabase.sync.analyze.fingerprint.insights :as insights :refer [change insights]] [clojure.test :refer :all]
[metabase.util :as u])) [metabase.sync.analyze.fingerprint.insights
:as
insights])
(:import
(java.math MathContext RoundingMode)))
(def ^:private cols [{:base_type :type/DateTime} {:base_type :type/Number}]) (def ^:private cols [{:base_type :type/DateTime} {:base_type :type/Number}])
...@@ -21,7 +25,7 @@ ...@@ -21,7 +25,7 @@
:expected nil}]] :expected nil}]]
(testing (format "rows = %s" rows) (testing (format "rows = %s" rows)
(is (= expected (is (= expected
(-> (transduce identity (insights cols) rows) (-> (transduce identity (insights/insights cols) rows)
first first
:last-value)))))) :last-value))))))
...@@ -90,6 +94,29 @@ ...@@ -90,6 +94,29 @@
["2018-12-02",179,3311] ["2018-12-02",179,3311]
["2018-12-03",144,2525]]) ["2018-12-03",144,2525]])
(defn- round-to-precision
"Round (presumably floating-point) `number` to a precision of `sig-figures`. Returns a `Double`.
This rounds by significant figures, not decimal places. See [[round-to-decimals]] for that.
(round-to-precision 4 1234567.89) -> 123500.0"
^Double [^Integer sig-figures ^Number number]
{:pre [(integer? sig-figures) (number? number)]}
(-> number
bigdec
(.round (MathContext. sig-figures RoundingMode/HALF_EVEN))
double))
(deftest ^:parallel round-to-precision-test
(are [exp figs n] (= exp
(round-to-precision figs n))
1.0 1 1.234
1.2 2 1.234
1.3 2 1.278
1.3 2 1.251
12300.0 3 12345.67
0.00321 3 0.003209817))
(deftest timeseries-insight-test (deftest timeseries-insight-test
(is (= [{:last-value 144, (is (= [{:last-value 144,
:previous-value 179, :previous-value 179,
...@@ -108,12 +135,12 @@ ...@@ -108,12 +135,12 @@
:col nil, :col nil,
:unit :day}] :unit :day}]
(-> (transduce identity (-> (transduce identity
(insights [{:base_type :type/DateTime} (insights/insights [{:base_type :type/DateTime}
{:base_type :type/Number} {:base_type :type/Number}
{:base_type :type/Number}]) {:base_type :type/Number}])
ts) ts)
; This value varies between machines (M1 Macs? JVMs?) so round it to avoid test failures. ; This value varies between machines (M1 Macs? JVMs?) so round it to avoid test failures.
(update-in [0 :best-fit 1] #(u/round-to-precision 6 %))))) (update-in [0 :best-fit 1] #(round-to-precision 6 %)))))
(testing "We should robustly survive weird values such as NaN, Infinity, and nil" (testing "We should robustly survive weird values such as NaN, Infinity, and nil"
(is (= [{:last-value 20.0 (is (= [{:last-value 20.0
:previous-value 10.0 :previous-value 10.0
...@@ -124,7 +151,7 @@ ...@@ -124,7 +151,7 @@
:unit :day :unit :day
:col nil}] :col nil}]
(transduce identity (transduce identity
(insights [{:base_type :type/DateTime} {:base_type :type/Number}]) (insights/insights [{:base_type :type/DateTime} {:base_type :type/Number}])
[["2018-11-01" 10.0] [["2018-11-01" 10.0]
["2018-11-02" 20.0] ["2018-11-02" 20.0]
["2018-11-03" nil] ["2018-11-03" nil]
...@@ -132,13 +159,13 @@ ...@@ -132,13 +159,13 @@
["2018-11-08" Double/POSITIVE_INFINITY]]))))) ["2018-11-08" Double/POSITIVE_INFINITY]])))))
(deftest change-test (deftest change-test
(is (= 0.0 (change 1 1))) (is (= 0.0 (insights/change 1 1)))
(is (= -0.5 (change 1 2))) (is (= -0.5 (insights/change 1 2)))
(is (= 1.0 (change 2 1))) (is (= 1.0 (insights/change 2 1)))
(is (= nil (change 1 0))) (is (= nil (insights/change 1 0)))
(is (= -1.0 (change 0 1))) (is (= -1.0 (insights/change 0 1)))
(is (= 2.0 (change 1 -1))) (is (= 2.0 (insights/change 1 -1)))
(is (= -2.0 (change -1 1))) (is (= -2.0 (insights/change -1 1)))
(is (= 1.0 (change -1 -2))) (is (= 1.0 (insights/change -1 -2)))
(is (= nil (change -1 0))) (is (= nil (insights/change -1 0)))
(is (= 1.0 (change 0 -1)))) (is (= 1.0 (insights/change 0 -1))))
...@@ -18,16 +18,6 @@ ...@@ -18,16 +18,6 @@
(is (= " " (is (= " "
(u/add-period " ")))) (u/add-period " "))))
(deftest ^:parallel decolorize-test
(is (= "message"
(u/colorize 'red "message")))
(is (= "message"
(u/decolorize "message")))
(is (= "message"
(u/decolorize (u/colorize 'red "message"))))
(is (= nil
(u/decolorize nil))))
(deftest ^:parallel host-up?-test (deftest ^:parallel host-up?-test
(testing "host-up?" (testing "host-up?"
(are [s expected] (= expected (are [s expected] (= expected
...@@ -370,16 +360,6 @@ ...@@ -370,16 +360,6 @@
false "cam.saul+1@metabase.co.uk" "metabase.com" false "cam.saul+1@metabase.co.uk" "metabase.com"
true "cam.saul+1@metabase.com" "metabase.com")) true "cam.saul+1@metabase.com" "metabase.com"))
(deftest ^:parallel round-to-precision-test
(are [exp figs n] (= exp
(u/round-to-precision figs n))
1.0 1 1.234
1.2 2 1.234
1.3 2 1.278
1.3 2 1.251
12300.0 3 12345.67
0.00321 3 0.003209817))
(defspec pick-first-test 100 (defspec pick-first-test 100
(prop/for-all [coll (gen/list gen/int)] (prop/for-all [coll (gen/list gen/int)]
(let [result (u/pick-first pos? coll)] (let [result (u/pick-first pos? coll)]
......
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