          delays)
        (zip/next loc)))))
 
+(defn build-tree-from-source [id]
+  (let [origin (mc/find-map-by-id @db "origins" id)
+        tree (compute-impact-tree origin)]
+    {:title (:title (mc/find-map-by-id @db "articles" (:article origin)))
+     :tree tree
+     :analysis (analyze-tree tree)}))
 
 (defn random-tree []
   (let [origin (rand-nth (mc/find-maps @db "origins" {:source {$in news-accounts}}))

      (:_id x)
      (update-in x [:created_at] #(f/parse custom-formatter (:created_at %))) )))

  (def rand-tree (random-tree))

  (-> rand-tree :analysis :height)

  (def g
    (let [vertices (loop [nodes []
                          loc (:tree rand-tree)]
                     (if (zip/end? loc)
                       nodes
                       (recur
                        (if-let [node (zip/node loc)]
                          (if (= (zip/root loc) node)
                            (conj nodes (-> node :tweet :id_str))
                            (conj nodes [(-> node :tweet :id_str) (-> loc zip/up zip/node :tweet :id_str)]))
                          nodes)
                        (zip/next loc))))]
      (apply graph vertices)))

  (lio/view g)


    (view (line-chart time tweet-count))
    (view (line-chart (range 24) avg-tweets-per-hour)))


  (let [days-running (t/in-days (t/interval (t/date-time 2014 7 2) (t/date-time 2014 9 22)))
        dates (take days-running (p/periodic-seq (t/date-time 2014 7 2) (t/days 1)))
        tweet-count (map #(mc/count @db "tweets" {:created_at {$gte % $lt (t/plus % (t/days 1))}}) dates)
        days (range  2 (inc (inc days-running)))]
    (view (line-chart days tweet-count :title "tweet counts" :y-label "Tweet Count" :x-label "Day")))


  ;; user posts distribution
  (let [users (->> (mc/find-maps
                    @db "tweets"
                    {:created_at {$gt (t/date-time 2014 7 2)}}
                    ["user.screen_name"])
                   (pmap #(-> % :user :screen_name))
                   frequencies)
        unique-posters #(float (/ (count %) (count users)))]
    (view (histogram (filter #(< % 51) (vals users)) :nbins 50)))


  ;; news posts fraction
  (let [overall-count (mc/count @db "tweets" {:created_at {$gt (t/date-time 2014 7 1)}})
        news-count (mc/count
                    @db "tweets" {$and [{:created_at {$gt (t/date-time 2014 7 1)}}
                                        {:user.screen_name {$in news-accounts} }]})]
    (aprint (float (/ news-count overall-count))))




