-
-
Save currentoor/f9fbc881b489718630f8ee36d6aaad75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(let [conn (-> system :database :connection) | |
blob-store (-> system :blob-store) | |
db (d/db conn) | |
env {:db db :blob-store blob-store}] | |
(defmulti -pluck | |
"Multimethod for extending datomic.api/pull, while still maintaining the ability | |
to let the query decied what is actually fetched." | |
(fn [k env pull-result] k)) | |
;; Default case just delivers what datomic.api/pull gives you. | |
(defmethod -pluck :default [k env pull-result] | |
(get pull-result k)) | |
(defn pick | |
"Recursively annotates the pull-result with values supplied from the `-pluck` | |
multimethod." | |
[env pattern pull-result] | |
(->> pattern | |
(map (fn [k] | |
[(cond | |
(keyword? k) k | |
(map? k) (-> k keys first)) | |
(cond | |
(keyword? k) (-pluck k env pull-result) | |
(and (map? k) | |
(vector? (get pull-result (-> k keys first)))) | |
(mapv #(pick env (-> k vals first) %) (get pull-result (-> k keys first))) | |
(and (map? k)) | |
(pick env (-> k vals first) (get pull-result (-> k keys first))))])) | |
(into {}))) | |
(defn pluck [env pattern eid] | |
(let [pull-result (d/pull (:db env) pattern eid)] | |
(pick env pattern pull-result))) | |
;;; Extend the pluck API by implementing -pluck multimethod. | |
;; dashboard/created-at is a derived attribute from Datomic's time model. | |
(defmethod -pluck :dashboard/created-at [k {db :db} {eid :db/id}] | |
(d/q '[:find | |
?created-at . | |
:in $ ?d | |
:where | |
[?d :dashboard/organization _ ?org-tx _] | |
[?org-tx :db/txInstant ?created-at]] | |
db | |
eid)) | |
;; data-source/current-stream is stored in both Datomic and a separate blob-store. | |
(defmethod -pluck :data-source/current-stream | |
[k {:keys [db blob-store]} {ds-eid :db/id}] | |
(let [data-stream (-> (d/entity db ds-eid) :data-source/current-stream) | |
data-stream-eid (:db/id data-stream) | |
uuid (:data-stream/id data-stream)] | |
(merge (d/pull db [{:data-stream/status [:db/ident]} :data-stream/id] data-stream-eid) | |
(data-stream/find-by-id blob-store uuid)))) | |
;; data-source/fields need to be post-processed. | |
(defmethod -pluck :data-source/fields [k {db :db} pull-result] | |
(read-string (get pull-result :data-source/fields-str))) | |
(pluck env | |
[:db/id | |
:dashboard/created-at | |
{:dashboard/author [:db/id :user/first-name :user/last-name]} | |
:dashboard/title | |
{:dashboard/widgets [:db/id | |
:widget/title | |
{:widget/data-source [:db/id | |
:data-source/id | |
:data-source/fields | |
:data-source/current-stream | |
:data-source/fields-str]}]}] | |
17592186056144)) | |
;;; Returns something like | |
{:db/id 17592186056144, | |
:dashboard/created-at #inst "2016-06-25T00:06:14.571-00:00", | |
:dashboard/author | |
{:db/id 17592186045435, :user/first-name "Clark", :user/last-name "Gates-George"}, | |
:dashboard/title "Summary Statistics", | |
:dashboard/widgets | |
[{:db/id 17592186056448, | |
:widget/title "bar copyxx", | |
:widget/data-source | |
{:db/id 17592186056450, | |
:data-source/id #uuid "57719b0b-e736-49c2-8410-f7d572c058ce", | |
:data-source/fields ["clicks" "spend" "impressions" "ctr"], | |
:data-source/current-stream | |
{:data-stream/status {:db/ident :data-stream.status/ready}, | |
:data-stream/id #uuid "577eedab-386f-4de8-932f-93cf52a547cc", | |
:data-stream/start-date "2016-06-01", | |
:data-stream/end-date "2016-06-30", | |
:data-stream/filtered-totals | |
{:row/metrics | |
{"impressions" "900,019", | |
"ctr" "0.51%", | |
"clicks" "4,630", | |
"spend" "$13,263.14 USD"}, | |
:row/meta {"currency_code" "USD", "currency_symbol" "$"}, | |
:row/row-id "totals"}}, | |
:data-source/fields-str "[\"clicks\" \"spend\" \"impressions\" \"ctr\"]"}} | |
{:db/id 17592186056490, | |
:widget/title "bar copy copy", | |
:widget/data-source | |
{:db/id 17592186056492, | |
:data-source/id #uuid "57719c44-ef52-4ad6-abd6-534aedf63cb9", | |
:data-source/fields ["clicks" "spend" "impressions" "ctr"], | |
:data-source/current-stream | |
{:data-stream/status {:db/ident :data-stream.status/ready}, | |
:data-stream/id #uuid "57729c65-23bf-4dfc-bdd3-ca59a3e1c232", | |
:data-stream/start-date "2016-05-01", | |
:data-stream/end-date "2016-05-31", | |
:data-stream/filtered-totals | |
{:row/metrics | |
{"impressions" "983,378", | |
"ctr" "0.37%", | |
"clicks" "3,609", | |
"spend" "$11,760.04 USD"}, | |
:row/meta {"currency_code" "USD", "currency_symbol" "$"}, | |
:row/row-id "totals"}}, | |
:data-source/fields-str "[\"clicks\" \"spend\" \"impressions\" \"ctr\"]"}} | |
{:db/id 17592186056499, | |
:widget/title "", | |
:widget/data-source | |
{:db/id 17592186056501, | |
:data-source/id #uuid "57719c60-1891-44d0-95b2-52af308daa65", | |
:data-source/fields ["spend" "clicks" "impressions" "ctr"], | |
:data-source/current-stream | |
{:data-stream/status {:db/ident :data-stream.status/ready}, | |
:data-stream/id #uuid "57729c65-5541-4b80-acac-0af98bf02504", | |
:data-stream/start-date "2016-05-01", | |
:data-stream/end-date "2016-05-31", | |
:data-stream/filtered-totals | |
{:row/metrics | |
{"impressions" "983,378", | |
"ctr" "0.37%", | |
"clicks" "3,609", | |
"spend" "$11,760.04 USD"}, | |
:row/meta {"currency_code" "USD", "currency_symbol" "$"}, | |
:row/row-id "totals"}}, | |
:data-source/fields-str "[\"spend\" \"clicks\" \"impressions\" \"ctr\"]"}} | |
{:db/id 17592186056504, | |
:widget/title "bar copyxx copy", | |
:widget/data-source | |
{:db/id 17592186056506, | |
:data-source/id #uuid "57719c76-9a4e-4e3f-a57d-7760cfbef9dc", | |
:data-source/fields ["clicks" "spend" "impressions" "ctr"], | |
:data-source/current-stream | |
{:data-stream/status {:db/ident :data-stream.status/ready}, | |
:data-stream/id #uuid "57729c65-fc6b-4e2c-ba43-72902145d808", | |
:data-stream/start-date "2016-05-01", | |
:data-stream/end-date "2016-05-31", | |
:data-stream/filtered-totals | |
{:row/metrics | |
{"impressions" "983,378", | |
"ctr" "0.37%", | |
"clicks" "3,609", | |
"spend" "$11,760.04 USD"}, | |
:row/meta {"currency_code" "USD", "currency_symbol" "$"}, | |
:row/row-id "totals"}}, | |
:data-source/fields-str "[\"clicks\" \"spend\" \"impressions\" \"ctr\"]"}} | |
{:db/id 17592186056548, | |
:widget/title "bar copyxx copy", | |
:widget/data-source | |
{:db/id 17592186056550, | |
:data-source/id #uuid "5771bfac-aaf9-4273-a8b4-fb82447c8cd1", | |
:data-source/fields ["clicks" "spend" "impressions" "ctr"], | |
:data-source/current-stream | |
{:data-stream/status {:db/ident :data-stream.status/ready}, | |
:data-stream/id #uuid "57729c65-8333-4010-87da-522619ed4305", | |
:data-stream/start-date "2016-05-01", | |
:data-stream/end-date "2016-05-31", | |
:data-stream/filtered-totals | |
{:row/metrics | |
{"impressions" "983,378", | |
"ctr" "0.37%", | |
"clicks" "3,609", | |
"spend" "$11,760.04 USD"}, | |
:row/meta {"currency_code" "USD", "currency_symbol" "$"}, | |
:row/row-id "totals"}}, | |
:data-source/fields-str "[\"clicks\" \"spend\" \"impressions\" \"ctr\"]"}}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment