Miki Tebeka committed 01386d3


Comments (0)

Files changed (1)

 (ns clj2010
   (:require [apricot-soup :as soup])
-  (:use [clj-time.core :only (hours minutes date-time plus year month day)]
+  (:use [clj-time.core :only (minutes date-time plus year month day)]
         [clojure.contrib.string :only (trim lower-case split)])
   (map lower-case (re-seq #"[a-zA-Z0-9'_-]+" sentence)))
-(defn str->long [s]
+(defn str->long 
+  "Parse string to long"
+  [s]
   (Long/valueOf s))
 (def *date-re* #"(\d+)-(\d+)-(\d+)")
-(defn logfile-day [logfile]
+(defn logfile-day 
+  "\"2010-01-01\" -> #<DateTime 2010-01-01T00:00:00.000Z>"
+  [logfile]
   (let [[match year month day] (re-find *date-re* logfile)]
     (apply date-time (map str->long [year month day]))))
-(defn log-time [day hour minute]
-  (plus (plus day (hours hour)) (minutes minute)))
+(defn add-time 
+  "Add time to day of log"
+  [day hour minute]
+  (plus day (minutes (+ (* 60 hour) minute))))
-(defn parse-text [text]
+(defn parse-text
+  "\"21:38 chouser: great, thanks!\" -> 
+      [\"21:38\" \"chouser:\" \"great, thanks!\"]"
+  [text]
   (rest (re-find #"^(\d+):(\d+)(.+:)?(.*)" text)))
-(defn process-p [day previous-log p]
+(defn process-p
+  "Process a <p>...</p> log recrod, return {:time ... :tokens ... :user ..}"
+  [day previous-log p]
   (let [[hour minute user text] (parse-text (soup/text p))]
-    { :time (log-time day (str->long hour) (str->long minute))
+    { :time (add-time day (str->long hour) (str->long minute))
       :tokens (tokenize text)
       :user (if user (fix-user user) (:user previous-log))}))
-(defn process-logfile [logfile]
+(defn process-logfile 
+  [logfile]
   (let [day (logfile-day logfile)
         pp (partial process-p day)]
     (rest (reductions pp (cons nil (soup/$ (slurp logfile) p))))))
-(defn log-files [root]
+(defn log-files 
+  "Return list of log files under root"
+  [root]
   (let [dir (File. root)
         files (filter #(not (nil? (re-find *date-re* %))) (.list dir))]
     (map #(str root "/" %) files)))