Created
August 4, 2012 17:49
-
-
Save Frozenlock/3258950 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn with-random | |
"Add a random geospatial queries to a fetch. Should be used with a | |
limit, otherwise default limit to 5 items." [fetch-fn coll & {:as args}] | |
(let [where (:where args) | |
new-args (vec (merge {:limit 5} ;security limit if none is provided | |
args {:where (merge where {:random_point {:$near [(rand) 0]}})}))] | |
(apply fetch-fn (concat [coll] (apply concat new-args))))) | |
(defn get-object-historic-data | |
"Return an object historic-data in a trend-log frendly format; a | |
list of maps with :time and :value" | |
[project-id device-id object-type object-instance &[{:keys [before-date after-date] :as dates}]] | |
(let [mongo-keyword (keyword | |
(clojure.string/join | |
"." ["objects" object-type object-instance "Present-value"])) | |
basic-query-map {:device-id device-id} | |
date-query (make-date-query dates) | |
query-map (merge basic-query-map (when date-query {:update date-query})) | |
get-present-values (fn [arg] | |
(into [] | |
(filter #(:value %) | |
(map #(hash-map :value (-> % | |
(:objects) | |
((keyword object-type)) | |
((keyword object-instance)) | |
(:Present-value)) | |
:time | |
(:update %)) arg)))) | |
times-values-vector (get-present-values | |
(with-random fetch (historic-id project-id) | |
:where query-map | |
:limit 150 | |
:only [mongo-keyword :update]))] | |
(when-not (empty? times-values-vector) | |
times-values-vector))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment