summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsostler <sbostler@gmail.com>2010-02-16 01:36:32 -0500
committersostler <sbostler@gmail.com>2010-02-16 01:36:32 -0500
commiteb764883beb1ddf4d5a2f429f4f73c6ee93e4728 (patch)
tree12a87b2d45742fe0bcb6491ef7ce0145b18ea664 /src
parentd2ede89204ffe08b5a4927b6ebc7365abffdafd6 (diff)
Split site.clj
Diffstat (limited to 'src')
-rwxr-xr-xsrc/site.clj32
-rwxr-xr-xsrc/utils.clj75
2 files changed, 77 insertions, 30 deletions
diff --git a/src/site.clj b/src/site.clj
index 4ba5981..b1774fd 100755
--- a/src/site.clj
+++ b/src/site.clj
@@ -9,20 +9,10 @@
org.antlr.stringtemplate.StringTemplateGroup)
(:use clojure.contrib.str-utils
clojure.contrib.duck-streams
- clojure.contrib.json.write
clojure.contrib.sql
compojure
cookie-login
- ))
-
-(let [db-host "localhost"
- db-port 5432
- db-name "dumpfm"]
- (def db {:classname "org.postgresql.Driver"
- :subprotocol "postgresql"
- :subname (str "//" db-host ":" db-port "/" db-name)
- :user "postgres"
- :password "root"}))
+ utils))
(def template-group (new StringTemplateGroup "dumpfm" "template"))
(.setRefreshInterval template-group 3)
@@ -74,12 +64,6 @@
(def formatter (new SimpleDateFormat "h:mm EEE M/d"))
-(defn resp-error [message]
- {:status 400 :headers {} :body message})
-
-(defn resp-success [message]
- {:status 200 :headers {} :body (json-str message)})
-
(defn non-empty-string? [s]
(and s (> (count s) 0)))
@@ -98,19 +82,6 @@
(.setTimeZone df (TimeZone/getTimeZone "GMT"))
(.format df dt))))
-;; Database
-
-(defn do-select [query]
- (with-connection db
- (with-query-results rs query
- (doall rs))))
-
-(defn do-count [query]
- ((first (with-connection db
- (with-query-results rs query
- (doall rs))))
- :count))
-
;; User authentication
(def nick-regex #"^[A-Za-z0-9\-_∆˚†]*$")
@@ -586,6 +557,7 @@
(-> request :route-params :room)
(-> request :route-params :offset)
params))
+ (GET "/stats" (validated-stats session params))
;; TODO: validate POST Referrer headers for POSTs
(POST "/msg" (validated-msg session params))
(POST "/submit-registration" (register session params))
diff --git a/src/utils.clj b/src/utils.clj
new file mode 100755
index 0000000..3ffd54b
--- /dev/null
+++ b/src/utils.clj
@@ -0,0 +1,75 @@
+(ns utils
+ (:import java.text.SimpleDateFormat
+ java.util.Date)
+ (:use clojure.contrib.json.write
+ clojure.contrib.sql))
+
+(let [db-host "localhost"
+ db-port 5432
+ db-name "dumpfm"]
+ (def db {:classname "org.postgresql.Driver"
+ :subprotocol "postgresql"
+ :subname (str "//" db-host ":" db-port "/" db-name)
+ :user "postgres"
+ :password "root"}))
+
+;; JSON responses
+
+(def yyyy-mm-dd-formatter (new SimpleDateFormat "yyyy-MM-dd"))
+
+(defmethod print-json Date
+ [d]
+ (print-json (.format yyyy-mm-dd-formatter d)))
+
+(defn resp-error [message]
+ {:status 400 :headers {} :body message})
+
+(defn resp-success [message]
+ {:status 200 :headers {} :body (json-str message)})
+
+;; Database
+
+(defn do-select [query]
+ (with-connection db
+ (with-query-results rs query
+ (doall rs))))
+
+(defn do-count [query]
+ ((first (with-connection db
+ (with-query-results rs query
+ (doall rs))))
+ :count))
+
+
+;; Stats
+
+(defn msg-stats [ts]
+ (let [qry (str "SELECT created_on::date, count(*) FROM messages "
+ "GROUP BY created_on::date "
+ "ORDER BY created_on::date")]
+ (do-select [qry])))
+
+(defn new-user-stats [ts]
+ (let [qry (str "SELECT created_on::date, count(*) FROM users "
+ "GROUP BY created_on::date "
+ "ORDER BY created_on::date")]
+ (do-select [qry])))
+
+(defn msgs-per-user-stats [ts]
+ true)
+
+(def *stat-map* {"msgs" msg-stats
+ "new users" new-user-stats
+ "msgs per user" msgs-per-user-stats})
+
+(defn stats [session params]
+ (let [stat (params :stat)
+ ts (params :timescale)]
+ (if-let [f (*stat-map* stat)]
+ (resp-success (f ts))
+ (resp-error "UNKNOWN STAT"))))
+
+(defn validated-stats [session params]
+ (if (session :is_admin)
+ (stats session params)
+ (resp-error "BAD_REQUEST"))) \ No newline at end of file