   (:use korma.db)
   (:use korma.core)
   (:use [clojure.tools.logging :only (debug info warn error)])
+  (:require [ruuvi-server.util :as util])
   (:import org.joda.time.DateTime)
   )
 

  (defn- current-sql-timestamp []
    (to-sql-timestamp (org.joda.time.DateTime)))
  
  (defn- remove-nil-values [map-data]
    (flatten (filter (fn [x] (nth x 1) ) map-data) ))

  ;; public functions
  (defn get-trackers [ids]
    (select tracker

          max-results (:maxResults criteria max-result-count)
          result-limit (min max-result-count max-results)

          tracker-ids-crit (when tracker-ids {:tracker_id ['in tracker-ids]})
          event-start-crit (when event-start {:event_time ['>= event-start]})
          event-end-crit (when event-end {:event_time ['<= event-end]})
          store-start-crit (when store-start {:created_on ['>= store-start]})
          store-end-crit (when store-end {:created_on ['<= store-end]})
          conditions (filter identity (list event-start-crit event-end-crit tracker-ids-crit))
          ]
      (if (not (empty? conditions))
        (let [results (select event
                              (with event-location)
                              (with event-extension-value (fields :value)
                                    (with event-extension-type (fields :name)))              
                              (where (apply and conditions))
                              (limit result-limit)) ]
          results
          )
        '() )))
  
  (defn get-events [ids]
    (select event
