          (sort-by first >)
          time))
 
-
-  )
+  (def all-delays
+    (let [users (map :_id (mc/find-maps @db "users" {:screen_name {$in news-accounts}}))]
+      (->> (mc/find-maps @db "publications" {:user {$in users}
+                                             :ts {$lt (t/date-time 2014 9 1)}})
+           (pmap (comp :delays analyze-delays full-reaction-tree :_id)))))
+
+  (defn dispatch-types [type]
+    (case type
+      "source" 0
+      "retweet" 1
+      "reply" 2
+      "share" 3
+      4))
+
+  (let [source-uids (map :_id (mc/find-maps @db "users" {:screen_name {$in news-accounts}}))
+        graph (->> (mc/find-maps @db "publications" {:user {$in source-uids}})
+             (pmap (comp summary reaction-tree :_id))
+             (sort-by :size >)
+             (take 3)
+             last
+             :source
+             full-reaction-tree
+             create-d3-graph)
+        types (:types graph)
+        cleaned-graph (update-in
+                       (dissoc graph :types)
+                       [:nodes]
+                       #(mapv
+                         (fn [k]
+                           {:name k
+                            :group (dispatch-types (get types k))})
+                         %))]
+    (with-open [w (clojure.java.io/writer "resources/graph-3b.edn")]
+      (binding [*print-length* false
+                *out* w]
+        (pr cleaned-graph))))
+
+
+  ;; find all related shares
+  (let [origins (mc/find-maps @db "origins" {:source nil
+                                             :ts {$gt (t/date-time 2014 8 1)
+                                                  $lt (t/date-time 2014 9 1)}})]
+    (->> origins
+         (map (comp :in_reply_to_status_id #(mc/find-map-by-id @db "tweets" (:tweet %))))
+         (take 10)
+         aprint))
+
+
+  (->> (mc/find-maps @db "publications" {:type :share})
+       (pmap (comp #(mc/find-one-as-map @db "reactions" {:publication %}) :_id))
+       (remove nil?)
+       count
+       aprint
+       time)
+
+  ((comp float /) (mc/count @db "reactions" {:source nil})
+     (mc/count @db "reactions"))
+
+
+  (mc/find-maps @db "origins" {:source nil
+                               :ts {$gt (t/date-time 2014 8 1)
+                                    $lt (t/date-time 2014 9 1)}})
+
+)

            [clj-time.periodic :as p]
            [clojure.pprint :refer [pprint]]
            [opennlp.nlp :refer [make-tokenizer make-detokenizer]]
            [incanter.stats :refer [mean variance quantile]]
            [loom.graph :as lg]
            [loom.io :as lio]
            [ceres.collector :refer [db custom-formatter news-accounts store]])

          (take 25)
          (map (fn [[k v]] [(:screen_name (mc/find-map-by-id @db "users" k))
                           v])))]))


(defn create-d3-graph
  "Converts zipper into d3 readable format"
  [tree]
  (loop [counter 0
         types {}
         nodes []
         links []
         loc tree]
    (if (zip/end? loc)
      {:nodes nodes
       :types types
       :links links}
      (recur
       (if (zip/node loc)
         (inc counter)
         counter)
       (if-let [node (zip/node loc)]
         (assoc types (-> node :source :_id str) (-> node :source :type))
         types)
       (if-let [node (zip/node loc)]
         (conj nodes (-> node :source :_id str))
         nodes)
       (if-let [node (zip/node loc)]
         (if-not (= node (zip/root tree))
           (conj links {:source counter :target (.indexOf nodes (-> loc zip/up zip/node :source :_id str))})
           links)
         links)
       (zip/next loc)))))


(comment
