Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fix

  • Loading branch information...
commit 44ed3790c26803b520fa0370dec0c3290626233b 1 parent 1faba59
Mattias Dalkvist authored March 27, 2013

Showing 1 changed file with 0 additions and 122 deletions. Show diff stats Hide diff stats

  1. 122  src/bf3/stats.clj
122  src/bf3/stats.clj
... ...
@@ -1,122 +0,0 @@
1  
-(ns bf3.stats
2  
-  (:require [clj-http.client :as client]
3  
-            [clojure.string :as s]
4  
-            [clojure.core.cache :as cache]
5  
-            [clojure.core.memoize :as mem]
6  
-            [clj-time.core :as time]
7  
-            [clj-time.format :as time-format]
8  
-            [bf3.bl :as bl])
9  
-  (:use [cheshire.core]
10  
-        [net.cgrand.enlive-html]
11  
-        [bf3.db])
12  
-  (:import java.net.URL))
13  
-
14  
-(def ^{:dynamic true} *cache-time* (* 2 60 1000))
15  
-
16  
-(def ^{:dynamic true} *debug* false)
17  
-
18  
-(defn debug [& messages]
19  
-  (when *debug*
20  
-    (println messages)))
21  
-
22  
-(defn- inc-date [dt]
23  
-  (time/plus dt (time/days 1)))
24  
-
25  
-(defn- dec-date [dt]
26  
-  (time/minus dt (time/days 1)))
27  
-
28  
-(defn- future-dates [& {:keys [dt]}] (iterate inc-date (if-not dt (time/now) dt)))
29  
-
30  
-(defn- prev-dates   [& {:keys [dt]}] (iterate dec-date (if-not dt (time/now) dt)))
31  
-
32  
-(defn- battleday? [dt] (= 6 (time/day-of-week dt)))
33  
-
34  
-(defn get-battleday
35  
-  "Return the a battledat interval, :prev for the latest one, and :next for the next one"
36  
-  ([& {:keys [next weeks] :or {next false weeks 1}}]
37  
-     (->> (time/now) (time/day-of-week)
38  
-          ((fn [weekday] (if next
39  
-                          (first (drop (dec weeks) (filter battleday? (future-dates ))))
40  
-                          (first (drop (dec weeks) (filter battleday? (prev-dates )))))))
41  
-          (#(time/date-time (time/year %) (time/month %) (time/day %) 18 0 0 0))
42  
-          (#(time/interval % (time/plus % (time/hours 6) ))))))
43  
-
44  
-
45  
-(defn- parse-stats [rows]
46  
-  (let [users (atom {})
47  
-        stats (atom {})
48  
-        current-time (:time (last rows))]
49  
-
50  
-    (doseq [server-rows (->> rows (sort-by :server) (partition-by :server))]
51  
-      (doseq [row (sort-by :time server-rows)]
52  
-        ;;logg all online users
53  
-        (doseq [user (:users row)]
54  
-          (if (contains? @users user)
55  
-            (swap! users assoc-in [user :last-seen]  (:time row))
56  
-            (swap! users assoc user {:first-seen (:time row) :server (:server row)})
57  
-            ))
58  
-
59  
-        ;;update offline users
60  
-        (doseq [[user stat] @users]
61  
-          (when (not-any? #(= % user) (:users row))
62  
-            (swap! stats assoc user (conj (get @stats user []) stat))
63  
-            (swap! users dissoc user)))
64  
-        )
65  
-
66  
-      ;;update users currently online
67  
-        (doseq [[user stat] @users]
68  
-          (swap! stats assoc user (conj (get @stats user [])
69  
-                                        (if (nil? (:last-seen stat))
70  
-                                          (assoc stat :last-seen current-time)
71  
-                                          stat)))
72  
-          (swap! users dissoc user))
73  
-
74  
-      ;; (doseq [user @users]
75  
-      ;;   ;; (println "u:" user "\nid:" (key user) "\nstats:" (val user))
76  
-      ;;   (swap! intervals merge (get @intervals (key user) {})
77  
-      ;;          {(key user) (first (for [server (val user)]
78  
-      ;;                               (do ;; (println "s:" server "\nsid:" (key server) "\nss:" (val server))
79  
-      ;;                                 (if-not (:last-seen (val server))
80  
-      ;;                                   {(key server) (assoc (val server) :last-seen current-time)}
81  
-      ;;                                   (apply hash-map server)))))})
82  
-      ;;   (swap! users dissoc (key user)))
83  
-      )
84  
-    @stats))
85  
-
86  
-(defn- get-live-stats
87  
-  "get user stats from the coll"
88  
-  ([coll]
89  
-     (sort-by first (parse-stats coll))))
90  
-
91  
-(def get-stats (mem/memo-ttl get-live-stats *cache-time*))
92  
-
93  
-(defn- stat-interval [stat]
94  
-  (time/interval (time-format/parse (:first-seen stat))
95  
-                 (time-format/parse (:last-seen stat))))
96  
-
97  
-(defn- attended-battleday [& {:keys [stats weeks]}]
98  
-  (filter #(time/overlaps? (get-battleday :weeks weeks)
99  
-                           (stat-interval %)) stats))
100  
-
101  
-(defn- get-active-users [stats]
102  
-  (->> stats (map :user) distinct (pmap #(->> % bl/get-username ))
103  
-       (filter #(not-empty %))))
104  
-
105  
-;;TODO take used servers as list, alt check if bl can give private flag
106  
-(defn- roster-last-battleday ([] (roster-last-battleday (test-stats)))
107  
-  ([stats & {:keys [weeks] :or {weeks 1}}]
108  
-     (->> stats
109  
-          (mapcat #(for [stat (last %)]
110  
-                    (assoc stat :user (first %))))
111  
-          (attended-battleday :weeks weeks :stats)
112  
-          (filter (fn [s] (some #(= % (:server s)) (vals bl/server-ids))))
113  
-          (sort-by :server)
114  
-          (partition-by :server)
115  
-          (map #(hash-map :server (:server (first %))
116  
-                          :users (->> %
117  
-                                      get-active-users
118  
-                                      (map s/lower-case)
119  
-                                      distinct
120  
-                                      sort))))))
121  
-
122  
-(def battleday-roster (mem/memo-ttl roster-last-battleday *cache-time*))

0 notes on commit 44ed379

Please sign in to comment.
Something went wrong with that request. Please try again.