summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db_populate.clj96
-rw-r--r--src/origin_check.clj9
2 files changed, 105 insertions, 0 deletions
diff --git a/src/db_populate.clj b/src/db_populate.clj
new file mode 100644
index 0000000..5d47d13
--- /dev/null
+++ b/src/db_populate.clj
@@ -0,0 +1,96 @@
+(ns db-populate
+ (:import java.lang.Math)
+ (:require [clojure.contrib.generic.collection :as gc])
+ (:use clojure.contrib.sql
+ clojure.contrib.probabilities.finite-distributions
+ clojure.contrib.probabilities.monte-carlo
+ clojure.contrib.probabilities.random-numbers
+ utils))
+
+(def *num-users* 500)
+(def *mean-posts-per-user* 200)
+(def *std-dev-posts-per-user* 1.5)
+(def *image-msg-frequency* 0.5)
+(def *nick-prefix* "user")
+(def *sample-message-sentences*
+ ["Aim at new love victories",
+ "Bomb her womb from your huge battleship",
+ "Face your new mate without fear",
+ "Get armed for a new love battle",
+ "Upgrade your weapon used to make love"])
+
+(defn sample-unit []
+ (first (gc/seq rand-stream)))
+
+(defn sample-lognormal [mean sigma]
+ (first (gc/seq (random-stream (lognormal (Math/log mean) sigma)
+ rand-stream))))
+
+(defn rand-elt [coll]
+ (nth coll (rand-int (count coll))))
+
+(defn nick-func [i]
+ (str *nick-prefix* "-" i "-" (System/currentTimeMillis)))
+
+(defn hash-func [i]
+ (str "HASH"))
+
+(defn email-func [i]
+ (str *nick-prefix* "-" i "@" i ".com"))
+
+(defn make-user [label]
+ (println "Creating user" label)
+ (let [qry (str "INSERT INTO users (nick, hash, email) "
+ "VALUES (?, ?, ?) RETURNING user_id")
+ nick (nick-func label)
+ hash (hash-func label)
+ email (email-func label)
+ res (do-select [qry nick hash email])]
+ ((first res) :user_id)))
+
+(defn sample-text-msg []
+ (rand-elt *sample-message-sentences*))
+
+(defn sample-image-msg []
+ "http://localhost:8080/images/20100310/1268271054246-abc-b7413897775a39087737d54768adca55d3c97cf4.jpeg")
+
+(defn sample-msg-contents []
+ (if (<= *image-msg-frequency* (sample-unit))
+ [(sample-image-msg) true]
+ [(sample-text-msg) false]))
+
+(defn sample-room-id []
+ 1)
+
+(defn make-messages [user-id]
+ (let [num-msgs (int (sample-lognormal *mean-posts-per-user*
+ *std-dev-posts-per-user*))]
+ (println "Creating" num-msgs "messages for userid" user-id)
+ (doseq [i (range num-msgs)]
+ (let [[content is-image] (sample-msg-contents)
+ room-id (sample-room-id)
+ qry (str "INSERT INTO messages "
+ "(user_id, room_id, content, is_image) "
+ "VALUES (?, ?, ?, ?) RETURNING message_id")]
+ (do-select [qry user-id room-id content is-image])))))
+
+(defn populate [n-str]
+ (let [n (Integer/parseInt n-str)]
+ (doall (map (comp make-messages make-user) (range n)))))
+
+(defn clear []
+ (println "Clearing all users starting with" *nick-prefix*)
+ (let [params ["nick LIKE ?" (str *nick-prefix* "%")]
+ cnt (first (do-delete "users" params))]
+ (println "Cleared" cnt "users")))
+
+(def *command-map* {"populate" populate "clear" clear})
+
+(defn parse-command-args
+ ([] (prn "Usage: db_populate.clj command num"))
+ ([cmd-str & more]
+ (if-let [cmd (*command-map* cmd-str)]
+ (apply cmd more)
+ (println "Unknown command '" cmd-str "'"))))
+
+(apply parse-command-args (rest *command-line-args*)) \ No newline at end of file
diff --git a/src/origin_check.clj b/src/origin_check.clj
new file mode 100644
index 0000000..3dada54
--- /dev/null
+++ b/src/origin_check.clj
@@ -0,0 +1,9 @@
+(ns origin-check)
+
+(defn with-origin-check
+ "Middleware to validate that state-changing URL access
+ originated from a page in the local domain."
+ [handler]
+ (fn [request]
+ (println (get-in request [:headers]))
+ (handler request))) \ No newline at end of file