   (c-route/resources "/"))
 
 
+(defn prep-rules-for-saving [rules-list]
+  (map
+   (fn [[k v]] {:name  k  :predicate (pr-str v)})
+   rules-list))
+
 (defn init [env]
   (db/init {:collection-name "rawdata"
             :db-name "spcr-db"
             :uri (if (= env :prod) heroku-mongo-connection-uri nil)})
+  (if (nil? (seq (get-rules)))
+    (db/save
+     (prep-rules-for-saving rule-predicate-forms)
+     "categoryrules"))
   (if (nil? (seq (get-data)))
     (import-file test-data)))
 


(defn abs [n] (max n (- n))) ;; move to Utils namespace

(def rule-predicates {:high-daily-diff (fn [{high :High low :Low}]
                                         (> (abs (- high low)) 3))
                      :daily-gain (fn [{open :Open close :Close}]
                                    (> close open))
                      :high-volume (fn [{volume :Volume}]
                                     (> volume 50000.0))
                      :default (fn [_] :true)})

(defn get-matching-rules [record rules]
  (remove nil?

  (->> (db/get-all "rawdata")
       (map #(dissoc % :_id))))

(defn get-rules []
  (->> (db/get-all "categoryrules")
       (map (fn [{name :name pred-fn :predicate}]
              [(keyword  name) (eval  (edn/read-string pred-fn))]))
       (into {})))

(defn get-labeled-data [] ;todo add a query mechanism to filter on labels
  (-> (get-data)
      (rule-engine (merge default-rules (get-rules)))))

(defn get-label-stats []
  (->> (get-labeled-data)
