Fork me on GitHub
#clojure-uk
<
2019-04-20
>
dominicm08:04:03

Data-y people in this room, good place to ask about making this less hairy:

(defn- twitter-follower-history
  [db id]
  (let [historys
        (crux.api/history-descending db (crux.api/new-snapshot db) id)]
    (into {}
          (map (fn [[k v]]
                 ;; Find last instant of the month, singular-group-by
                 [k (:count (apply max-key (comp t/long t/instant :at) v))])
               ;; Aggregate by year-month
               (group-by (comp t/year-month t/instant :at)
                         ;; Get data from history
                         (map (fn [history]
                                {:at (:crux.db/valid-time history)
                                 :count (get-in history
                                                [:crux.db/doc
                                                 :proj/followers_count])})
                              historys))))))

dominicm08:04:20

I feel like there's an aggregation utility library poking it's head out

dominicm08:04:38

I'm getting the year-month & :count of the last record within each year-month, that's the best english explantion I can give

dominicm08:04:56

Looks like xforms provides a fairly nifty toolkit for this 😄

dominicm08:04:38

(defn- twitter-follower-history2
  [db id]
  (into {}
        (comp
          (map (fn [history]
                 {:at (:crux.db/valid-time history)
                  :count (get-in history [:crux.db/doc ::twitter/followers_count])}))
          (x/by-key (comp t/year-month t/instant :at) (x/into []))
          (x/by-key (map #(:count (apply max-key (comp t/long t/instant :at) %)))))
        (crux.api/history-descending
          db
          (crux.api/new-snapshot db) id)))
by-key is pretty neat 🙂

otfrom09:04:01

xforms is good. Nice transducer there

dominicm09:04:15

> Theclass of queries they express is often referred to as thefirst-order queriesbecause relationalcalculus is essentially first-order predicate calculus without function symbols Looks like I'm in for a wild saturday