Skip to content

Instantly share code, notes, and snippets.

@ygrenzinger
Created August 13, 2017 18:45
Show Gist options
  • Save ygrenzinger/d7be4a549e2e5412531701c13d74f4b8 to your computer and use it in GitHub Desktop.
Save ygrenzinger/d7be4a549e2e5412531701c13d74f4b8 to your computer and use it in GitHub Desktop.
playing-with-projections try with Clojure
(ns playing-with-projections.core
(:require [clojure.data.json :as json]))
;; time helpers with Java 8 API
(def zoneId (java.time.ZoneId/systemDefault))
(defn parse-timestamp [str] (java.time.Instant/parse str))
(defn convert-to-datetime [instant] (java.time.LocalDateTime/ofInstant instant zoneId))
(defn enrich-datetime-info [event]
(let [instant (parse-timestamp (get event "timestamp"))
datetime (convert-to-datetime instant)]
(assoc event
"instant" instant
"datetime" datetime)))
;;loading json content
(defn parse-events-file [path]
(->> (slurp path)
(json/read-str)
(map enrich-datetime-info)))
;; projections
(defn count-events [events] (count events))
(defn group-by-type [events] (group-by #(get % "type") events))
(defn extract-year-month [event]
(let [datetime (get event "datetime")]
(.format (java.time.format.DateTimeFormatter/ofPattern "yyyy-MM") datetime)))
(defn fmap [])
(defn count-registration-by-month [registrations]
(let [by-month (group-by #(extract-year-month %) registrations)]
(map (fn [[k v]] [k (count v)]) by-month)))
(defn group-events-by [events & args]
(group-by #(get-in % args) events))
(defn retrieve-title [quizes id]
(let [quiz (first (filter #(= (get % "quiz_id") id) quizes))]()
(get quiz "quiz_title")))
(defn most-popular-quizzes [grouped-by-type]
(let [quizes (map #(get % "payload") (get grouped-by-type "QuizWasCreated"))]
(->> (get grouped-by-type "GameWasOpened")
(map #(get % "payload"))
(group-by #(get % "quiz_id"))
(map (fn [[k v]] [k (count v)]))
(map #(conj % (retrieve-title quizes (first %))))
(sort-by #(nth % 1))
(reverse)
(map #(str "quiz_id:" (nth % 0) " , quiz_title:" (nth % 2) " , games_played:" (nth % 1))))))
(defn -main [& args]
(let [events (parse-events-file (first args))
grouped-by-type (group-by-type events)]
(println "Count player registration" (count (get grouped-by-type "PlayerHasRegistered"))
(println "Count player registration by month" (count-registration-by-month (get grouped-by-type "PlayerHasRegistered"))))
(println "5 Most popular quizzes" (map println (take 5 (most-popular-quizzes grouped-by-type))))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment