diff options
| -rwxr-xr-x | src/cookie_login.clj | 2 | ||||
| -rwxr-xr-x | src/site.clj | 32 | ||||
| -rwxr-xr-x | src/utils.clj | 75 |
3 files changed, 78 insertions, 31 deletions
diff --git a/src/cookie_login.clj b/src/cookie_login.clj index ce41c66..e507876 100755 --- a/src/cookie_login.clj +++ b/src/cookie_login.clj @@ -38,7 +38,7 @@ "Middleware to support automatic cookie login. Must be placed after the with-session middleware. - Must be given three arguments: +Must be given three arguments: - process-login-token? Function to apply to request map to determine whether to process login token or not. If a false value is returned, diff --git a/src/site.clj b/src/site.clj index c36f1ae..e0a40b9 100755 --- a/src/site.clj +++ b/src/site.clj @@ -10,20 +10,10 @@ (:use clojure.xml 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 *run-flusher* true) (def *flusher-sleep-ms* 4000) @@ -77,12 +67,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))) @@ -101,19 +85,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\-_∆˚†]*$") @@ -598,6 +569,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 |
