Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
762155a6
Commit
762155a6
authored
6 years ago
by
Simon Belak
Browse files
Options
Downloads
Patches
Plain Diff
Humanize datetime values
parent
2c8296bb
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/automagic_dashboards/filters.clj
+47
-22
47 additions, 22 deletions
src/metabase/automagic_dashboards/filters.clj
with
47 additions
and
22 deletions
src/metabase/automagic_dashboards/filters.clj
+
47
−
22
View file @
762155a6
(
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
})))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment