 (ns polismath.poller
   (:use clojure.pprint
+        clojure.core.matrix.impl.ndarray
         polismath.conversation
         polismath.named-matrix
         polismath.utils
         polismath.pca)
   (:require [korma.db :as kdb]
             [korma.core :as ko]
+            [cheshire.core :refer :all]
+            [cheshire.generate :refer [add-encoder encode-seq remove-encoder]]
             [clojure.java.jdbc :as sql]
             [monger.core :as mg]
-            [monger.collection :as mg.col]
+            [monger.collection :as mgcol]
             [environ.core :as env]))
 
+;(use 'alex-and-georges.debug-repl)
+
+(println "launching poller " (System/currentTimeMillis))
 
 (defn heroku-db-spec [db-uri]
   (let [[_ user password host port db] (re-matches #"postgres://(?:(.+):(.*)@)?([^:]+)(?::(\d+))?/(.+)" db-uri)

    (kdb/postgres settings)))


(defn mongo-db [mongo-url]
  (let [params  {}
        con     (mg/connect params)]
    (mg/get-db con "polismath")))


(defn split-by-conv [votes]
  (reduce

            new-votes (poll pg-spec @last-timestamp)
            split-votes (split-by-conv new-votes)]
        (doseq [[zid votes] split-votes]
          (let [lastVoteTimestamp (:created (last votes))]
            (swap! conversations
              (fn [convs]
                (assoc convs zid
                    (try
                      (small-conv-update {:conv (or (convs zid)
                                              {:rating-mat (named-matrix)})
                                      :votes votes
                                      :opts {}})
                        (catch Exception e (println "exception when processing zid: " zid))))))
                
            ;(upsert-results pg-spec 1001 1 "foo")
            (println "zid: " zid)
            (println "time: " (System/currentTimeMillis))
            (println "\n\n")
            (println (generate-string (prep-for-uploading (@conversations zid))))
            ; (debug-repl)
            (println 
              (mongo-upsert-results
                zid
                lastVoteTimestamp
                (generate-string
                  (prep-for-uploading
                    (@conversations zid)))))
        (swap! last-timestamp (fn [_] (:created (last new-votes))))
      ))))))
