Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (60 sloc) 2.41 KB
(ns foreclojure.mongo
(:use somnium.congomongo
[ :only [load-problems]]
[foreclojure.config :only [config]]
[foreclojure.problems :only [number-from-mongo-key solved-stats get-problem-list]]
[foreclojure.users :only [get-users]]))
(defn connect-to-db []
(let [{:keys [db-user db-pwd db-host db-name]} config]
:host (or db-host "localhost")
:db (or db-name "mydb"))
(when (and db-user db-pwd)
(authenticate db-user db-pwd))))
(defn prepare-problems []
(when-not (fetch-one :problems)
(add-index! :problems [:solved]))
(defn prepare-seqs []
(update! :seqs
{:_id "problems"}
{:$set {:seq (->> (fetch :problems :only [:_id])
(map :_id)
(apply max)
;; make it easier to get off the ground by marking contributors automatically
;; useful since some "in-development" features aren't enabled for all users
(defn prepare-users []
(add-index! :users [:user] :unique true)
(add-index! :users [[:solved -1]])
(add-index! :users [:email])
(update! :users
{:user {:$in (:contributors config)}}
{:$set {:contributor true}}
:upsert false
:multiple true))
(defn prepare-solutions []
(add-index! :solutions [:user :problem]))
(defn reconcile-solved-count
"Overwrites the times-solved field in the problems collection based on data from the users collection. Should only be called on server startup since it isn't a safe operation. Also updates the total-solved agent."
(let [+ (fnil + 0)
users (fetch :users :only [:scores :solved])
scores (->> users
(mapcat :scores)
(reduce (fn [scores [[id score] times]]
(update-in scores
[(number-from-mongo-key id) score]
+ times))
solved-counts (frequencies (map int (mapcat :solved users)))
total (reduce + (vals solved-counts))]
(send solved-stats (constantly (assoc scores :total total :solved-counts solved-counts)))))
(defn prepare-mongo []
Something went wrong with that request. Please try again.