Skip to content
Snippets Groups Projects
Unverified Commit fdf9a467 authored by Tim Macdonald's avatar Tim Macdonald Committed by GitHub
Browse files

Support empty date field filters (dashboard subscriptions) (#15323)

* Support empty date field filters

[Fixes #15067]

* Add a test
parent 05835fab
No related branches found
No related tags found
No related merge requests found
......@@ -108,6 +108,10 @@
:target [:dimension [:template-tag (:name tag)]]
:value default}))
(s/defn ^:private defaulted-param
[{:keys [default value] :as param}]
(assoc param :value (or value default)))
(s/defn ^:private field-filter->field-id :- su/IntGreaterThanZero
[field-filter]
(second field-filter))
......@@ -120,22 +124,28 @@
(or (db/select-one [Field :name :parent_id :table_id :base_type :special_type] :id field-id)
(throw (ex-info (str (deferred-tru "Can''t find field with ID: {0}" field-id))
{:field-id field-id, :type qp.error-type/invalid-parameter}))))
:value (if-let [value-info-or-infos (or
;; look in the sequence of params we were passed to see if there's anything
;; that matches
(param-with-target params [:dimension [:template-tag (:name tag)]])
;; if not, check and see if we have a default param
(default-value-for-field-filter tag))]
;; `value-info` will look something like after we remove `:target` which is not needed after this point
;;
;; {:type :date/single
;; :value #t "2019-09-20T19:52:00.000-07:00"}
;;
;; (or it will be a vector of these maps for multiple values)
(cond
(map? value-info-or-infos) (dissoc value-info-or-infos :target)
(sequential? value-info-or-infos) (mapv #(dissoc % :target) value-info-or-infos))
i/no-value)}))
:value (or (when-let [value-info-or-infos (or
;; look in the sequence of params we were passed to see if there's anything
;; that matches
(param-with-target (map defaulted-param params) [:dimension [:template-tag (:name tag)]])
;; if not, check and see if we have a default param
(default-value-for-field-filter tag))]
;; `value-info` will look something like after we remove `:target` which is not needed after this point
;;
;; {:type :date/single
;; :value #t "2019-09-20T19:52:00.000-07:00"}
;;
;; (or it will be a vector of these maps for multiple values)
(let [has-value? (some-fn :value :default)
dissoc-target #(dissoc % :target)]
(cond
(map? value-info-or-infos)
(when (has-value? value-info-or-infos)
(dissoc-target value-info-or-infos))
(sequential? value-info-or-infos)
(when (every? has-value? value-info-or-infos)
(mapv dissoc-target value-info-or-infos)))))
i/no-value)}))
(s/defmethod parse-tag :card :- ReferencedCardQuery
[{:keys [card-id], :as tag} :- TagParam, params :- (s/maybe [i/ParamValue])]
......
......@@ -14,7 +14,7 @@
(let [{:keys [replacement-snippet prepared-statement-args]} (substitution/->replacement-snippet-info driver/*driver* v)]
[(str sql replacement-snippet) (concat args prepared-statement-args) missing])))
(defn- subsistute-card-query [[sql args missing] v]
(defn- substitute-card-query [[sql args missing] v]
(let [{:keys [replacement-snippet]} (substitution/->replacement-snippet-info driver/*driver* v)]
[(str sql replacement-snippet) args missing]))
......@@ -31,7 +31,7 @@
(substitute-field-filter [sql args missing] in-optional? k v)
(i/ReferencedCardQuery? v)
(subsistute-card-query [sql args missing] v)
(substitute-card-query [sql args missing] v)
(i/ReferencedQuerySnippet? v)
(substitute-native-query-snippet [sql args missing] v)
......
......@@ -82,7 +82,7 @@
(defn execute-dashboard
"Execute all the cards in a dashboard for a Pulse"
[{pulse-creator-id :creator_id, :as pulse} dashboard-or-id & {:as options}]
(let [dashboard-id (u/get-id dashboard-or-id)
(let [dashboard-id (u/the-id dashboard-or-id)
dashboard (Dashboard :id dashboard-id)]
(for [dashcard (db/select DashboardCard :dashboard_id dashboard-id, :card_id [:not= nil])]
(execute-dashboard-subscription-card pulse-creator-id dashboard dashcard (:card_id dashcard)))))
......
......@@ -156,6 +156,21 @@
:dimension [:field-id (mt/id :checkins :date)]
:default "past5days"
:widget-type :date/all-options}
nil)))))
(testing "Make sure nil values result in no value"
(is (= {:field (map->FieldInstance
{:name "DATE"
:parent_id nil
:table_id (mt/id :checkins)
:base_type :type/Date
:special_type nil})
:value i/no-value}
(into {} (#'values/parse-tag
{:name "checkin_date"
:display-name "Checkin Date"
:type :dimension
:dimension [:field-id (mt/id :checkins :date)]
:widget-type :date/all-options}
nil))))))
(deftest field-filter-errors-test
......
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