Commits

Miki Tebeka committed 829d80f

Filter results

Comments (0)

Files changed (1)

 
 (defn gen-chart [records job]
   (let [result (map-reduce (:map job) (:reduce job) records)
-        xs (sort (keys result))
-        ys (map #(result %) xs)
+        filtered (if-let [f (:filter job)] (f result) result)
+        xs (sort (keys filtered))
+        ys (map #(filtered %) xs)
         fxs (map (:x-format job identity) xs)]
     (bar-chart fxs ys :title (:title job) :x-label (:x-label job) 
                :y-label (:y-label job))))
 (defn sum [values]
   (reduce + values))
 
+(defn max-n-filter [n results]
+  (let [xs (take n (reverse (sort-by results (keys results))))]
+    (zipmap xs (map results xs))))
+
 (def numlines {
     :map (fn [record] [[(month-only (:time record)) 1]])
     :reduce (fn [key values] (sum values))
 (def active {
     :map (fn [record] (if-let [u (:user record)] [[u 1]] []))
     :reduce (fn [key values] (sum values))
-    :title "Users/Month"
+    :title "Lines/User"
     :x-label "User"
     :y-label "Lines"
+    :filter (partial max-n-filter 10)
     :filename "active"})
 
 (def words {
     :y-label "Count"
     :filename "words"})
 
+(use '[incanter.core :only (view)])
 (defn -main []
   (let [records (load-data "logs")]
-    (run-job records active)))
+    (let [chart (gen-chart records active)]
+      (view chart))))