Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added implementation for url /trackers/1,2/events (fetch events for g…

…iven trackers). Added transactionality to create event. Extension values are now correctly displayed in json responses.
  • Loading branch information...
commit e61bf9c7de93d2d5cdad0b972854eee5f3150649 1 parent a48f75d
@jsyrjala jsyrjala authored
View
16 src/ruuvi_server/api.clj
@@ -29,16 +29,20 @@
(-> #'tracker-api/handle-create-event
(wrap-request-logger "create-event")
))
- (GET [(str url-prefix "/trackers/:id") :id #"([0-9+],?)+"] [id]
- (-> (fn [request] (client-api/fetch-tracker request id))
+ (GET [(str url-prefix "/trackers/:ids") :ids #"([0-9]+,?)+"] [ids]
+ (-> (fn [request] (client-api/fetch-tracker request ids))
(wrap-request-logger "fetch-trackers")
- ))
- (GET (str url-prefix "/v1-dev/trackers") []
+ ))
+ (GET [(str url-prefix "/trackers/:ids/events") :ids #"([0-9]+,?)+"] [ids]
+ (-> (fn [request] (client-api/fetch-events (merge request {:tracker_ids ids})))
+ (wrap-request-logger "fetch events for -trackers")
+ ))
+ (GET (str url-prefix "/trackers") []
(-> #'client-api/fetch-trackers
(wrap-request-logger "fetch-trackers")
))
- (GET [(str url-prefix "/events/:id") :id #"([0-9+],?)+"] [id]
- (-> (fn [request] (client-api/fetch-event request id))
+ (GET [(str url-prefix "/events/:ids") :ids #"([0-9+],?)+"] [ids]
+ (-> (fn [request] (client-api/fetch-event request ids))
(wrap-request-logger "fetch-trackers")
))
(GET (str url-prefix "/events") []
View
33 src/ruuvi_server/client_api.clj
@@ -19,11 +19,10 @@
) data-map))
(defn- select-extension-data [extension-data]
- (when extension-data
- (let [selected-data (select-keys extension-data [:type :value])]
- (util/remove-nil-values selected-data)
- )))
-
+ (map (fn [data]
+ {(data :name) (data :value)})
+ extension-data))
+
(defn- select-location-data [location-data]
(when location-data
(let [selected-data (select-keys location-data
@@ -42,8 +41,7 @@
(get (event-data :event_locations)
0))
extension-data (select-extension-data
- (get (event-data :event_extension_values)
- 0))]
+ (event-data :event_extension_values))]
(util/remove-nil-values (merge renamed-data
{:location location-data
:extension_values extension-data}))
@@ -73,11 +71,11 @@
"time" (util/timestamp)}))
(defn- string-to-ids [value]
- (let [strings (.split value ",")
- ids (map #(Integer/parseInt %) strings)]
- ids
- )
- )
+ (when value
+ (let [strings (.split value ",")
+ ids (map #(Integer/parseInt %) strings)]
+ ids
+ )))
(defn fetch-trackers [request]
(json-response request {:trackers (db/get-all-trackers)} ))
@@ -93,18 +91,19 @@
;; Timezone may contain a + char. Browser converts + to space in url encode. This reverts the change.
(let [date-tmp (.replaceAll date-str " " "+")
date (util/parse-date-time date-tmp)]
- (if date
- {key date}
- nil))))
+ (if date
+ {key date}
+ nil))))
(let [params (request :params)
;; TODO this simply ignores invalid values => not good, should throw exception instead
eventTimeStart (parse-date :eventTimeStart (params :eventTimeStart))
createTimeStart (parse-date :createTimeStart (params :createTimeStart))
+ trackerIds {:trackerIds (string-to-ids (request :tracker_ids))}
]
- (merge {} eventTimeStart createTimeStart)
+ (merge {} trackerIds eventTimeStart createTimeStart)
))
-(defn fetch-events [request]
+(defn fetch-events [request ]
(let [query-params (parse-event-search-criterias request)
found-events (db/search-events query-params)]
(json-response request
View
158 src/ruuvi_server/database/entities.clj
@@ -57,8 +57,41 @@
(defn- remove-nil-values [map-data]
(flatten (filter (fn [x] (nth x 1) ) map-data) ))
-
+
;; public functions
+ (defn get-trackers [ids]
+ (select tracker
+ (where (in :id ids))))
+
+ (defn get-tracker-by-code [tracker-code]
+ (first (select tracker
+ (where {:tracker_code tracker-code}))))
+
+ (defn get-tracker-by-code! [tracker-code & tracker-name]
+ (let [existing-tracker (get-tracker-by-code tracker-code)]
+ (if existing-tracker
+ existing-tracker
+ (insert tracker (values {:tracker_code tracker-code
+ :name (or tracker-name tracker-code)}))
+ )))
+
+ (defn get-extension-type-by-id [id]
+ (first (select event-extension-type
+ (where {:name id}))))
+
+
+ (defn get-extension-type-by-name [type-name]
+ (first (select event-extension-type
+ (where {:name (str (name type-name))}))))
+
+ (defn get-extension-type-by-name! [type-name]
+ (let [existing-extension-type (get-extension-type-by-name type-name)]
+ (if existing-extension-type
+ existing-extension-type
+ (insert event-extension-type (values {:name (str (name type-name))
+ :description "Autogenerated"}))
+ )))
+
(defn get-event [event_id]
(first (select event
(with tracker)
@@ -66,7 +99,7 @@
(with event-extension-value)
(where {:id event_id})))
)
-
+
(defn search-events
"Search events: criteria is a map that can contain following keys.
- :createTimeStart <DateTime>, find events that are created (stored) to database later than given time (inclusive).
@@ -75,22 +108,23 @@ TODO supports only the eventTimeStart
TODO calculates milliseconds wrong (12:30:01.000 is rounded to 12:30:01 but 12:30:01.001 is rounded to 12:30:02)
"
[criteria]
-
+
(let [event-start (:eventTimeStart criteria)
create-start (:createTimeStart criteria)
+ tracker-ids (:trackerIds criteria)
conditions (merge (when event-start {:event_time ['>= (to-sql-timestamp event-start)]})
(when create-start {:created_on ['>= (to-sql-timestamp create-start)]})
+ (when tracker-ids {:tracker_id ['in tracker-ids]})
)]
(if (not (empty? conditions))
-;; (transaction
- (let [result (select event
- (with event-location)
- (with event-extension-value)
- (where (and conditions))
- )]
-;; (rollback)
- result
- );)
+ (let [results (select event
+ (with event-location)
+ (with event-extension-value (fields :value)
+ (with event-extension-type (fields :name)))
+ (where (and conditions))
+ )]
+ results
+ )
'())))
(defn get-events [ids]
@@ -112,35 +146,7 @@ TODO calculates milliseconds wrong (12:30:01.000 is rounded to 12:30:01 but 12:3
(defn get-tracker [id]
;; TODO support also fetching with tracker_indentifier
(first (select tracker
- (where {:id id}))))
-
- (defn get-trackers [ids]
- (select tracker
- (where (in :id ids))))
-
- (defn get-tracker-by-code [tracker-code]
- (first (select tracker
- (where {:tracker_code tracker-code}))))
-
- (defn get-tracker-by-code! [tracker-code & tracker-name]
- (let [existing-tracker (get-tracker-by-code tracker-code)]
- (if existing-tracker
- existing-tracker
- (insert tracker (values {:tracker_code tracker-code
- :name (or tracker-name tracker-code)}))
- )))
-
- (defn get-extension-type-by-name [type-name]
- (first (select event-extension-type
- (where {:name (str (name type-name))}))))
-
- (defn get-extension-type-by-name! [type-name]
- (let [existing-extension-type (get-extension-type-by-name type-name)]
- (if existing-extension-type
- existing-extension-type
- (insert event-extension-type (values {:name (str (name type-name))
- :description "Autogenerated"}))
- )))
+ (where {:id id}))))
(defn- update-tracker-latest-activity [id]
(update tracker
@@ -155,39 +161,39 @@ TODO calculates milliseconds wrong (12:30:01.000 is rounded to 12:30:01 but 12:3
:name name})))
(defn create-event [data]
- (let [extension-keys (filter (fn [key]
- (.startsWith (str (name key)) "X-"))
- (keys data))
- tracker (get-tracker-by-code! (:tracker_code data))
- latitude (:latitude data)
- longitude (:longitude data)
-
- event-entity (insert event (values
- {:tracker_id (tracker :id)
- :event_time (or (to-sql-timestamp (:event_time data))
- (current-sql-timestamp) )
- }))]
-
- (if (and latitude longitude)
- (insert event-location (values
- {:event_id (:id event-entity)
- :latitude latitude
- :longitude longitude
- :accuracy (:accuracy data)
- :satellite_count (:satellite_count data)
- :altitude (:altitude data)}))
- )
+ (transaction
+ (let [extension-keys (filter (fn [key]
+ (.startsWith (str (name key)) "X-"))
+ (keys data))
+ tracker (get-tracker-by-code! (:tracker_code data))
+ latitude (:latitude data)
+ longitude (:longitude data)
+
+ event-entity (insert event (values
+ {:tracker_id (tracker :id)
+ :event_time (or (to-sql-timestamp (:event_time data))
+ (current-sql-timestamp) )
+ }))]
+
+ (if (and latitude longitude)
+ (insert event-location (values
+ {:event_id (:id event-entity)
+ :latitude latitude
+ :longitude longitude
+ :accuracy (:accuracy data)
+ :satellite_count (:satellite_count data)
+ :altitude (:altitude data)}))
+ )
- (doseq [key extension-keys]
- (insert event-extension-value
- (values
- {:event_id (:id event-entity)
- :value (data key)
- :event_extension_type_id (:id (get-extension-type-by-name! key))
- }
- )))
- (update-tracker-latest-activity (tracker :id))
- event-entity
- ))
-
- )
+ (doseq [key extension-keys]
+ (insert event-extension-value
+ (values
+ {:event_id (:id event-entity)
+ :value (data key)
+ :event_extension_type_id (:id (get-extension-type-by-name! key))
+ }
+ )))
+ (update-tracker-latest-activity (tracker :id))
+ event-entity
+ )))
+ )
Please sign in to comment.
Something went wrong with that request. Please try again.