working avgppd calculated live and saved into ustats struct
authorKyle Spaans <kspaans@student.math.uwaterloo.ca>
Mon, 20 Apr 2009 17:43:34 +0000 (13:43 -0400)
committerKyle Spaans <kspaans@student.math.uwaterloo.ca>
Mon, 20 Apr 2009 17:43:34 +0000 (13:43 -0400)
userstats.ss

index 550e98c..047287a 100644 (file)
 (provide count-users)
 
 ;; User Statistics Struct
-;; String, int, date, date...
-(define-struct ustats (user nump firstp lastp))
+;; String - usename in the form of an email address, possibility of duplicates
+;; int    - total number of posts in newsgroup
+;; date   - SRFI-19 date struct, date of first post to group
+;; date   - "     "    ", date of last post to struct
+;; int    - number of days in range from first post day to past post day
+;; real   - number of posts / number of days
+(define-struct ustats (user nump firstp lastp days avgppd))
 
 ;; Collect stats in the form of '(USERNAME ustats)
 (define users (make-hash))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
-;; Need to collect dates into SRFI 19, then use "time-difference"
-;; should refactor out the date getter from ppd code
-;;
-;;
-
 ;; print-stats: void -> void
 ;; Pretty print the contents of the user statistics table to stdout
 (define (print-stats)
   (hash-for-each
     users
     (lambda (k v)
-      ;(printf "  time types: ~a  ~a~n" (time-type (ustats-firstp v)) (time-type (ustats-lastp v)))
       (printf "User: ~a~nCount: ~a~nFirst: ~a~nLast:  ~a~nAvgPPD: ~a~n~n"
               (ustats-user v)
               (ustats-nump v)
               (ustats-firstp v)
               (ustats-lastp v)
-              ;; Alternative method
-              ;(if (empty? (ustats-lastp v))
-              ;    1
-              ;    (quotient (time-second (time-difference (date->time-monotonic (ustats-lastp v))
-              ;                               (date->time-monotonic (ustats-firstp v))))
-              ;              86400))
-              (/ (ustats-nump v)
-                 (if (empty? (ustats-lastp v))
-                     1
-                     (+ 1 (- (date-year-day (ustats-lastp v))
-                             (date-year-day (ustats-firstp v))))))))))
+              (ustats-avgppd v)))))
 
 ;; count-users: int int newsgroup -> void
 ;; build a hash table with each user from the group collecting stats:
                  (cond
                    [(boolean? result)
                     ;(printf "First post ~a ::@:: ~a~n" mesg-from mesg-date)
-                    (hash-set! users mesg-from (make-ustats mesg-from 1 (get-date mesg-date) '()))]
+                    (hash-set! users mesg-from (make-ustats mesg-from 1 (get-date mesg-date) '() 1 1))]
                    [else
+                                     ;; Calculate the ordinal day of the year (1-365) of the given date
+                                     ;; then subtract. Alternatively date->time and time-difference
+                                     ;; functions can be used to calculate this
+                    (let [(pdays (if (empty? (ustats-lastp result))
+                                     1
+                                     (+ 1 (- (date-year-day (ustats-lastp result))
+                                             (date-year-day (ustats-firstp result))))))
+                          (new-nump (+ 1 (ustats-nump result)))]
                     (hash-set! users mesg-from (make-ustats
                                                  mesg-from
-                                                 (+ 1 (ustats-nump result))
+                                                 new-nump
                                                  (ustats-firstp result)
-                                                 (get-date mesg-date)))]))]))
+                                                 (get-date mesg-date)
+                                                 pdays
+                                                 (/ new-nump pdays))))]))]))
           (count-users (+ first 1) last newsd)]))