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
"Utility functions and macros to abstract away some common patterns and operations across the sync processes, such
as logging start/end messages."
(:require [buddy.core.hash :as buddy-hash]
[clojure.math.numeric-tower :as math]
[clojure.string :as str]
[clojure.tools.logging :as log]
[java-time :as t]
[medley.core :as m]
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
[metabase.events :as events]
[metabase.models.database :refer [Database]]
[metabase.models.field :refer [Field]]
[metabase.models.interface :as mi]
[metabase.models.table :refer [Table]]
[metabase.models.task-history :refer [TaskHistory]]
[metabase.query-processor.interface :as qp.i]
[metabase.sync.interface :as i]
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.i18n :refer [trs]]
[metabase.util.schema :as su]
[ring.util.codec :as codec]
[schema.core :as s]
[taoensso.nippy :as nippy]
[toucan.db :as db])
(:import java.time.temporal.Temporal))
(:require
[clojure.math.numeric-tower :as math]
[clojure.string :as str]
[clojure.tools.logging :as log]
[java-time :as t]
[medley.core :as m]
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
[metabase.events :as events]
[metabase.models.database :refer [Database]]
[metabase.models.field :refer [Field]]
[metabase.models.interface :as mi]
[metabase.models.table :refer [Table]]
[metabase.models.task-history :refer [TaskHistory]]
[metabase.query-processor.interface :as qp.i]
[metabase.sync.interface :as i]
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.i18n :refer [trs]]
[metabase.util.schema :as su]
[schema.core :as s]
[toucan.db :as db])
(:import
(java.time.temporal Temporal)))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | SYNC OPERATION "MIDDLEWARE" |
......@@ -314,16 +313,6 @@
(defmethod name-for-logging :default [{field-name :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
"Given two datetimes, caculate the time between them, return the result as a string"
[begin-time :- Temporal, end-time :- Temporal]
......
......@@ -382,6 +382,7 @@
:triggers
(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
"Return a set of PersistedInfo ids of all jobs scheduled for individual refreshes."
[]
......
(ns metabase.util
"Common utility functions useful throughout the codebase."
(:require [clojure.data :as data]
[clojure.java.classpath :as classpath]
[clojure.math.numeric-tower :as math]
[clojure.pprint :refer [pprint]]
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.logging :as log]
[clojure.tools.namespace.find :as ns.find]
[clojure.walk :as walk]
[colorize.core :as colorize]
[flatland.ordered.map :refer [ordered-map]]
[medley.core :as m]
[metabase.config :as config]
[metabase.shared.util :as shared.u]
[metabase.util.i18n :refer [trs tru]]
[nano-id.core :as nano-id]
[potemkin :as p]
[ring.util.codec :as codec]
[weavejester.dependency :as dep])
(:import [java.math MathContext RoundingMode]
[java.net InetAddress InetSocketAddress Socket]
[java.text Normalizer Normalizer$Form]
[java.util Base64 Base64$Decoder Base64$Encoder Locale PriorityQueue]
java.util.concurrent.TimeoutException
[org.apache.commons.validator.routines RegexValidator UrlValidator]))
(:require
[clojure.data :as data]
[clojure.java.classpath :as classpath]
[clojure.math.numeric-tower :as math]
[clojure.pprint :refer [pprint]]
[clojure.set :as set]
[clojure.string :as str]
[clojure.tools.logging :as log]
[clojure.tools.namespace.find :as ns.find]
[clojure.walk :as walk]
[colorize.core :as colorize]
[flatland.ordered.map :refer [ordered-map]]
[medley.core :as m]
[metabase.config :as config]
[metabase.shared.util :as shared.u]
[metabase.util.i18n :refer [trs tru]]
[nano-id.core :as nano-id]
[potemkin :as p]
[ring.util.codec :as codec]
[weavejester.dependency :as dep])
(:import
(java.net InetAddress InetSocketAddress Socket)
(java.text Normalizer Normalizer$Form)
(java.util Base64 Base64$Decoder Base64$Encoder Locale PriorityQueue)
(java.util.concurrent TimeoutException)
(org.apache.commons.validator.routines RegexValidator UrlValidator)))
(comment shared.u/keep-me)
......@@ -241,11 +242,6 @@
(fn [_ x]
(str x))))
(defn decolorize
"Remove ANSI escape sequences from a String `s`."
^String [s]
(some-> s (str/replace #"\[[;\d]*m" "")))
(defn format-color
"With one arg, converts something to a string and colorizes it. With two args, behaves like `format`, but colorizes
the output.
......@@ -346,19 +342,6 @@
{:pre [(integer? decimal-place) (number? number)]}
(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?
"Is `x` a real number (i.e. not a `NaN` or an `Infinity`)?"
[x]
......@@ -587,6 +570,7 @@
^bytes [^String string]
(.decode base64-decoder string))
;;; TODO -- this is only used [[metabase.analytics.snowplow-test]] these days
(defn decode-base64
"Decodes the Base64 string `input` to a UTF-8 string."
[input]
......
......@@ -12,13 +12,6 @@
[]
(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
"Return an appropriate URL for a `Dashboard` with ID.
......@@ -33,13 +26,6 @@
[^Integer 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
"URL prefix for a public Cards. Get the complete URL by adding the UUID to the end."
[]
......
(ns metabase.sync.analyze.fingerprint.insights-test
(:require [clojure.test :refer :all]
[metabase.sync.analyze.fingerprint.insights :as insights :refer [change insights]]
[metabase.util :as u]))
(:require
[clojure.test :refer :all]
[metabase.sync.analyze.fingerprint.insights
:as
insights])
(:import
(java.math MathContext RoundingMode)))
(def ^:private cols [{:base_type :type/DateTime} {:base_type :type/Number}])
......@@ -21,7 +25,7 @@
:expected nil}]]
(testing (format "rows = %s" rows)
(is (= expected
(-> (transduce identity (insights cols) rows)
(-> (transduce identity (insights/insights cols) rows)
first
:last-value))))))
......@@ -90,6 +94,29 @@
["2018-12-02",179,3311]
["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
(is (= [{:last-value 144,
:previous-value 179,
......@@ -108,12 +135,12 @@
:col nil,
:unit :day}]
(-> (transduce identity
(insights [{:base_type :type/DateTime}
(insights/insights [{:base_type :type/DateTime}
{:base_type :type/Number}
{:base_type :type/Number}])
ts)
; 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 %)))))
; This value varies between machines (M1 Macs? JVMs?) so round it to avoid test failures.
(update-in [0 :best-fit 1] #(round-to-precision 6 %)))))
(testing "We should robustly survive weird values such as NaN, Infinity, and nil"
(is (= [{:last-value 20.0
:previous-value 10.0
......@@ -124,7 +151,7 @@
:unit :day
:col nil}]
(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-02" 20.0]
["2018-11-03" nil]
......@@ -132,13 +159,13 @@
["2018-11-08" Double/POSITIVE_INFINITY]])))))
(deftest change-test
(is (= 0.0 (change 1 1)))
(is (= -0.5 (change 1 2)))
(is (= 1.0 (change 2 1)))
(is (= nil (change 1 0)))
(is (= -1.0 (change 0 1)))
(is (= 2.0 (change 1 -1)))
(is (= -2.0 (change -1 1)))
(is (= 1.0 (change -1 -2)))
(is (= nil (change -1 0)))
(is (= 1.0 (change 0 -1))))
(is (= 0.0 (insights/change 1 1)))
(is (= -0.5 (insights/change 1 2)))
(is (= 1.0 (insights/change 2 1)))
(is (= nil (insights/change 1 0)))
(is (= -1.0 (insights/change 0 1)))
(is (= 2.0 (insights/change 1 -1)))
(is (= -2.0 (insights/change -1 1)))
(is (= 1.0 (insights/change -1 -2)))
(is (= nil (insights/change -1 0)))
(is (= 1.0 (insights/change 0 -1))))
......@@ -18,16 +18,6 @@
(is (= " "
(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
(testing "host-up?"
(are [s expected] (= expected
......@@ -370,16 +360,6 @@
false "cam.saul+1@metabase.co.uk" "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
(prop/for-all [coll (gen/list gen/int)]
(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