1. Miki Tebeka
  2. clj2010


Miki Tebeka  committed 1824831

num users

  • Participants
  • Parent commits 515ecc8
  • Branches default

Comments (0)

Files changed (1)

File src/clj2010.clj

View file
   (:use [clj-time.core :only (date-time plus minutes year month day)]
         [clj-time.coerce :only (to-long)]
         [clj-time.format :only (formatter unparse)]
-        [incanter.core :only (view)]
+        [incanter.core :only (save)]
         [incanter.charts :only (bar-chart)]
         [clojure.contrib.string :only (trim lower-case split)])
   (:import java.io.File))
 (defn day-only [time]
   (date-time (year time) (month time) (day time)))
-(def numlogs {
-    :map (fn [record] [[(month-only (:time record)) 1]])
-    :reduce (fn [key values] (reduce + values))
-    :title "# of logs by month"
-    :x-label "Month"
-    :y-label "Number of logs"
-    :x-format (fn [dt] (unparse (formatter "MMM") dt)) })
-(defn run-job [job records]
+(defn gen-chart [job records]
   (let [result (map-reduce (:map job) (:reduce job) records)
         xs (sort (keys result))
         ys (map #(result %) xs)
     (bar-chart fxs ys :title (:title job) :x-label (:x-label job) 
                :y-label (:y-label job))))
+(defn run-job [job records]
+  (let [chart (gen-chart job records)]
+    (save chart (str (:filename job) ".png"))))
+(defn time-fmt [fmt]
+  (fn [dt]
+    (unparse (formatter fmt) dt)))
+(def numlines {
+    :map (fn [record] [[(month-only (:time record)) 1]])
+    :reduce (fn [key values] (reduce + values))
+    :title "Lines/Month"
+    :x-label "Month"
+    :y-label "Lines"
+    :filename "numlines"
+    :x-format (time-fmt "MMM")})
+(def numusers {
+    :map (fn [record] [[(month-only (:time record)) (:user record)]])
+    :reduce (fn [key values] (count (set values)))
+    :title "Users/Month"
+    :x-label "Month"
+    :y-label "Users"
+    :filename "users"
+    :x-format (time-fmt "MMM")})
 (defn -main []
   (let [records (load-data "logs")]
     (map-reduce numlogs records)))