diff options
| -rw-r--r-- | src/db_populate.clj | 96 | ||||
| -rw-r--r-- | src/origin_check.clj | 9 | ||||
| -rw-r--r-- | template/topic_list.st | 57 |
3 files changed, 162 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 diff --git a/template/topic_list.st b/template/topic_list.st new file mode 100644 index 0000000..dc7199e --- /dev/null +++ b/template/topic_list.st @@ -0,0 +1,57 @@ +<html> + <head> + <title>Topic List</title> + $head()$ + <script src="/static/js/topiclist.js"></script> + <style> + #main { + margin: 75px 2em 0 2em; + } + #main hr { margin-bottom: 0.5em; } + #main label { + display: inline-block; + width: 150px; + } + #main .deadline { + width: 100px; + } + </style> + </head> + <body> + $banner()$ + <div id="main"> + $rooms:{ r | + <div id="room-$r.key$" class="room-section"> + <h1>$r.key$</h1> + <hr> + + $if(r.topic)$ + <b>$r.topic$</b> by <b>$r.maker$</b> expires <b>$r.deadline$ </b> + $else$ + <span>No current topic!</span> + $endif$ + <br><br> + <div><label>New Topic</label><input type="text" name="topic"></div> + <div> + <label>Expires in</label> + <input type="text" class="deadline" name="hours"></input> hours, + <input type="text" class="deadline" name="minutes"></input> + <span>minutes</span> + <span class="deadline-update"> + </div> + <div><label>Maker</label><input type="text" name="maker" value="$user_nick$"></div> + <br> + <div> + <input class="set-topic" type="submit" value="Set new topic!"> + $if(r.topic)$ + <input class="end-topic" type="submit" value="End topic"> + $endif$ + <img class="spinner" src="/static/spinner.gif" style="display: none"/> + </div> + <br><br><br><br> + </div> + }$ + </div> + + </body> +</html> |
