Skip to content
Snippets Groups Projects
Commit 762155a6 authored by Simon Belak's avatar Simon Belak
Browse files

Humanize datetime values

parent 2c8296bb
No related branches found
No related tags found
No related merge requests found
(ns metabase.automagic-dashboards.filters
(:require [clojure.string :as str]
[clj-time.format :as t.format]
[metabase.models
[field :refer [Field] :as field]
[table :refer [Table]]]
......@@ -66,12 +67,18 @@
(nil? (:card dashcard)) dashcard
mappings (update dashcard :parameter_mappings concat mappings))))
(defn- datetime?
[field]
(and (not (periodic-datetime? field))
(or (isa? (:base_type field) :type/DateTime)
(field/unix-timestamp? field))))
(defn- filter-type
"Return filter type for a given field."
[{:keys [base_type special_type] :as field}]
(cond
(isa? base_type :type/DateTime) "date/all-options"
(field/unix-timestamp? field) "date/all-options"
(datetime? field) "date/all-options"
(isa? special_type :type/State) "location/state"
(isa? special_type :type/Country) "location/country"
(isa? special_type :type/Category) "category"))
......@@ -128,20 +135,26 @@
dashboard)))
dashboard)))))
(def ^:private ^{:arglists '([field])} periodic-date-time?
(def ^:private ^{:arglists '([field])} periodic-datetime?
(comp #{:minute-of-hour :hour-of-day :day-of-week :day-of-month :day-of-year :week-of-year
:month-of-year :quarter-of-year}
:unit))
(defn- date-time?
[field]
(and (not (periodic-date-time? field))
(or (isa? (:base_type field) :type/DateTime)
(field/unix-timestamp? field))))
(def ^:private date-formatter (t.format/formatter "MMMM d, YYYY"))
(def ^:private datetime-formatter (t.format/formatter "EEEE, MMMM d, YYYY h:mm a"))
(defn- humanize-datetime
[dt]
(t.format/unparse (if (str/index-of dt "T")
datetime-formatter
date-formatter)
(t.format/parse dt)))
(defn- field-reference->field
[field-reference]
(-> field-reference collect-field-references first last Field))
(cond-> (-> field-reference collect-field-references first last Field)
(-> field-reference first qp.util/normalize-token (= :datetime-field))
(assoc :unit (-> field-reference last qp.util/normalize-token))))
(defmulti
^{:private true
......@@ -163,8 +176,8 @@
[[_ field-reference value & values]]
(let [field (field-reference->field field-reference)]
[{:field field-reference
:value (if (date-time? field)
(format "is on %s" value)
:value (if (datetime? field)
(format "is on %s" (humanize-datetime value))
(format "is %s" (apply either value values)))}]))
(defmethod humanize-filter-value :not=
......@@ -176,16 +189,16 @@
[[_ field-reference value]]
(let [field (field-reference->field field-reference)]
[{:field field-reference
:value (if (date-time? field)
(format "is after %s" value)
:value (if (datetime? field)
(format "is after %s" (humanize-datetime value))
(format "is greater than %s" value))}]))
(defmethod humanize-filter-value :<
[[_ field-reference value]]
(let [field (field-reference->field field-reference)]
[{:field field-reference
:value (if (date-time? field)
(format "is before %s" value)
:value (if (datetime? field)
(format "is before %s" (humanize-datetime value))
(format "is less than %s" value))}]))
(defmethod humanize-filter-value :>=
......@@ -259,6 +272,24 @@
[[_ & clauses]]
(mapcat humanize-filter-value clauses))
(def ^:private unit-name (comp {:minute-of-hour "minute of hour"
:hour-of-day "hour of day"
:day-of-week "day of week"
:day-of-month "day of month"
:week-of-year "week of year"
:month-of-year "month of year"
:quarter-of-year "quarter of year"}
qp.util/normalize-token))
(defn- field-name
[field field-reference]
(let [full-name (cond->> (:display_name field)
(periodic-datetime? field)
(format "%s of %s" (-> field :unit unit-name str/capitalize)))]
(if (-> field-reference first qp.util/normalize-token (= :fk->))
[(-> field :table_id Table :display_name) full-name]
[full-name])))
(defn applied-filters
"Extract fields and their values from MBQL filter clauses."
[filter-clause]
......@@ -266,13 +297,7 @@
not-empty
humanize-filter-value)]
(let [field (field-reference->field field-reference)]
{:field (if (-> field-reference
first
qp.util/normalize-token
(= :fk->))
[(-> field :table_id Table :display_name)
(:display_name field)]
[(:display_name field)])
{:field (field-name field field-reference)
:field_id (:id field)
:type (filter-type field)
:value value})))
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