Skip to content
Snippets Groups Projects
Unverified Commit 87f6384d authored by Case Nelson's avatar Case Nelson Committed by GitHub
Browse files

[MLv2] Use date range formatting to display current date equal truncated date filters (#35075)

parent 1d4472e4
No related branches found
No related tags found
No related merge requests found
......@@ -325,19 +325,21 @@
"Given a `n` `unit` time interval and the current date, return a string representing the date-time range.
Provide an `offset-n` and `offset-unit` time interval to change the date used relative to the current date.
`options` is a map and supports `:include-current` to include the current given unit of time in the range."
[n unit offset-n offset-unit {:keys [include-current]}]
(let [offset-now (cond-> (now)
(neg? n) (apply-offset n unit)
(and (pos? n) (not include-current)) (apply-offset 1 unit)
(and offset-n offset-unit) (apply-offset offset-n offset-unit))
pos-n (cond-> (abs n)
include-current inc)
date-ranges (map (if (#{:hour :minute} unit)
#(t/format "yyyy-MM-dd'T'HH:mm" (t/local-date-time %))
#(str (t/local-date %)))
(common/to-range offset-now
{:unit unit
:n pos-n
:offset-n offset-n
:offset-unit offset-unit}))]
(apply format-diff date-ranges)))
([n unit offset-n offset-unit opts]
(format-relative-date-range (now) n unit offset-n offset-unit opts))
([t n unit offset-n offset-unit {:keys [include-current]}]
(let [offset-now (cond-> t
(neg? n) (apply-offset n unit)
(and (pos? n) (not include-current)) (apply-offset 1 unit)
(and offset-n offset-unit) (apply-offset offset-n offset-unit))
pos-n (cond-> (abs n)
include-current inc)
date-ranges (map (if (#{:hour :minute} unit)
#(t/format "yyyy-MM-dd'T'HH:mm" (t/local-date-time %))
#(str (t/local-date %)))
(common/to-range offset-now
{:unit unit
:n pos-n
:offset-n offset-n
:offset-unit offset-unit}))]
(apply format-diff date-ranges))))
......@@ -270,17 +270,19 @@
"Given a `n` `unit` time interval and the current date, return a string representing the date-time range.
Provide an `offset-n` and `offset-unit` time interval to change the date used relative to the current date.
`options` is a map and supports `:include-current` to include the current given unit of time in the range."
[n unit offset-n offset-unit {:keys [include-current]}]
(let [offset-now (cond-> (now)
(neg? n) (apply-offset n unit)
(and (pos? n) (not include-current)) (apply-offset 1 unit)
(and offset-n offset-unit) (apply-offset offset-n offset-unit))
pos-n (cond-> (abs n)
include-current inc)
date-ranges (map #(.format % (if (#{:hour :minute} unit) "YYYY-MM-DDTHH:mm" "YYYY-MM-DD"))
(common/to-range offset-now
{:unit unit
:n pos-n
:offset-n offset-n
:offset-unit offset-unit}))]
(apply format-diff date-ranges)))
([n unit offset-n offset-unit opts]
(format-relative-date-range (now) n unit offset-n offset-unit opts))
([t n unit offset-n offset-unit {:keys [include-current]}]
(let [offset-now (cond-> t
(neg? n) (apply-offset n unit)
(and (pos? n) (not include-current)) (apply-offset 1 unit)
(and offset-n offset-unit) (apply-offset offset-n offset-unit))
pos-n (cond-> (abs n)
include-current inc)
date-ranges (map #(.format % (if (#{:hour :minute} unit) "YYYY-MM-DDTHH:mm" "YYYY-MM-DD"))
(common/to-range offset-now
{:unit unit
:n pos-n
:offset-n offset-n
:offset-unit offset-unit}))]
(apply format-diff date-ranges))))
......@@ -74,4 +74,6 @@
([n unit offset-n offset-unit]
(format-relative-date-range n unit offset-n offset-unit nil))
([n unit offset-n offset-unit options]
(internal/format-relative-date-range n unit offset-n offset-unit options)))
(internal/format-relative-date-range n unit offset-n offset-unit options))
([t n unit offset-n offset-unit options]
(internal/format-relative-date-range (coerce-to-timestamp t) n unit offset-n offset-unit options)))
......@@ -239,6 +239,8 @@
(deftest format-relative-date-range
(with-redefs [internal/now (fn [] (from test-epoch))]
(are [exp n unit include-current] (= exp (shared.ut/format-relative-date-range n unit nil nil {:include-current include-current}))
"Jan 1 – Dec 31, 2022" 0 :year true
"Jan 1, 2022 – Dec 31, 2023" 1 :year true
"Jan 1 – Dec 31, 2023" 1 :year false
"Jan 1, 2022 – Dec 31, 2026" 4 :year true
......
......@@ -19,6 +19,7 @@
[metabase.lib.schema.expression :as lib.schema.expression]
[metabase.lib.schema.filter :as lib.schema.filter]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.lib.schema.temporal-bucketing :as lib.schema.temporal-bucketing]
[metabase.lib.temporal-bucket :as lib.temporal-bucket]
[metabase.lib.types.isa :as lib.types.isa]
[metabase.lib.util :as lib.util]
......@@ -67,17 +68,18 @@
(let [->display-name #(lib.metadata.calculation/display-name query stage-number % style)
->temporal-name lib.temporal-bucket/describe-temporal-pair
numeric? #(clojure.core/and (lib.util/original-isa? % :type/Number)
(lib.util/clause? %)
(-> (lib.metadata.calculation/metadata query stage-number %)
lib.types.isa/id?
clojure.core/not))
(lib.util/clause? %)
(-> (lib.metadata.calculation/metadata query stage-number %)
lib.types.isa/id?
clojure.core/not))
temporal? #(lib.util/original-isa? % :type/Temporal)
unit-is (fn [unit]
(fn [a]
(clojure.core/and
(temporal? a)
(lib.util/clause? a)
(clojure.core/= unit (:temporal-unit (second a))))))
unit-is (fn [unit-or-units]
(let [units (set (u/one-or-many unit-or-units))]
(fn [a]
(clojure.core/and
(temporal? a)
(lib.util/clause? a)
(clojure.core/contains? units (:temporal-unit (second a)))))))
->unbucketed-display-name #(-> %
(update 1 dissoc :temporal-unit)
->display-name)
......@@ -90,6 +92,9 @@
[:= _ (a :guard numeric?) b]
(i18n/tru "{0} is equal to {1}" (->display-name a) (->display-name b))
[:= _ (a :guard (unit-is lib.schema.temporal-bucketing/datetime-truncation-units)) (b :guard string?)]
(i18n/tru "{0} is {1}" (->unbucketed-display-name a) (shared.ut/format-relative-date-range b 0 (:temporal-unit (second a)) nil nil {:include-current true}))
[:= _ (a :guard temporal?) (b :guard (some-fn int? string?))]
(i18n/tru "{0} is on {1}" (->display-name a) (->temporal-name a b))
......
......@@ -460,6 +460,21 @@
query -1
(lib/expression-clause op args options))))))))
(deftest ^:parallel truncate-frontend-filter-display-names-test
(let [created-at (meta/field-metadata :products :created-at)
created-at-with #(lib/with-temporal-bucket created-at %1)]
(check-display-names
[{:clause [:= (created-at-with :year) "2023-10-02T00:00:00.000Z"]
:name "Created At is Jan 1 – Dec 31, 2023"}
{:clause [:= (created-at-with :month) "2023-10-02T00:00:00.000Z"]
:name "Created At is Oct 1–31, 2023"}
{:clause [:= (created-at-with :day) "2023-10-02T00:00:00.000Z"]
:name "Created At is Oct 2, 2023"}
{:clause [:= (created-at-with :hour) "2023-10-02T00:00:00.000Z"]
:name "Created At is Oct 2, 2023, 12:00 AM – 12:59 AM"}
{:clause [:= (created-at-with :minute) "2023-10-02T00:00:00.000Z"]
:name "Created At is Oct 2, 2023, 12:00 AM"}])))
(deftest ^:parallel exclude-date-frontend-filter-display-names-test
(let [created-at (meta/field-metadata :products :created-at)
created-at-with #(lib/with-temporal-bucket created-at %1)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment