changeset 39:0723411ca8a6

Tag geo points with ID.
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 03 Dec 2020 09:42:25 +0100
parents b66a3ef84f24
children 99f70bfe5606
files src/db.rs src/main.rs src/types.rs
diffstat 3 files changed, 15 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/db.rs	Thu Dec 03 09:37:27 2020 +0100
+++ b/src/db.rs	Thu Dec 03 09:42:25 2020 +0100
@@ -17,19 +17,20 @@
         to_ts: chrono::DateTime<chrono::Utc>,
         secret: &str,
         limit: i64,
+        last: Option<i32>,
     ) -> Result<types::GeoJSON, postgres::Error> {
         let mut returnable = types::GeoJSON::new();
         let stmt = self.0.prepare_cached(
-            r"SELECT t, lat, long, spd, ele FROM geohub.geodata
-        WHERE (client = $1) and (t between $2 and $3) AND (secret = public.digest($4, 'sha256') or secret is null)
+            r"SELECT id, t, lat, long, spd, ele FROM geohub.geodata
+        WHERE (client = $1) and (t between $2 and $3) AND (secret = public.digest($4, 'sha256') or secret is null) AND (id > $5)
         ORDER BY t ASC
-        LIMIT $5").unwrap(); // Must succeed.
-        let rows = stmt.query(&[&name, &from_ts, &to_ts, &secret, &limit])?;
+        LIMIT $6").unwrap(); // Must succeed.
+        let rows = stmt.query(&[&name, &from_ts, &to_ts, &secret, &last.unwrap_or(0), &limit])?;
         returnable.reserve_features(rows.len());
         for row in rows.iter() {
-            let (ts, lat, long, spd, ele) =
-                (row.get(0), row.get(1), row.get(2), row.get(3), row.get(4));
-            returnable.push_feature(types::geofeature_from_row(ts, lat, long, spd, ele));
+            let (id, ts, lat, long, spd, ele) =
+                (row.get(0), row.get(1), row.get(2), row.get(3), row.get(4), row.get(5));
+            returnable.push_feature(types::geofeature_from_row(id, ts, lat, long, spd, ele));
         }
         Ok(returnable)
     }
@@ -91,7 +92,7 @@
                         row.get(4),
                         row.get(5),
                     );
-                    returnable.push_feature(types::geofeature_from_row(ts, lat, long, spd, ele));
+                    returnable.push_feature(types::geofeature_from_row(Some(id), ts, lat, long, spd, ele));
                     if id > last {
                         last = id;
                     }
--- a/src/main.rs	Thu Dec 03 09:37:27 2020 +0100
+++ b/src/main.rs	Thu Dec 03 09:42:25 2020 +0100
@@ -57,7 +57,7 @@
 }
 
 /// Retrieve GeoJSON data.
-#[rocket::get("/geo/<name>/retrieve/json?<secret>&<from>&<to>&<limit>")]
+#[rocket::get("/geo/<name>/retrieve/json?<secret>&<from>&<to>&<limit>&<last>")]
 fn retrieve_json(
     db: db::DBConn,
     name: String,
@@ -65,6 +65,7 @@
     from: Option<String>,
     to: Option<String>,
     limit: Option<i64>,
+    last: Option<i32>,
 ) -> http::GeoHubResponse {
     if !ids::name_and_secret_acceptable(name.as_str(), secret.as_ref().map(|s| s.as_str())) {
         return http::bad_request(
@@ -85,7 +86,7 @@
     let limit = limit.unwrap_or(16384);
     let secret = secret.as_ref().map(|s| s.as_str()).unwrap_or("");
 
-    let result = db.retrieve_json(name.as_str(), from_ts, to_ts, secret, limit);
+    let result = db.retrieve_json(name.as_str(), from_ts, to_ts, secret, limit, last);
     match result {
         Ok(json) => http::return_json(&json),
         Err(e) => http::server_error(e.to_string()),
--- a/src/types.rs	Thu Dec 03 09:37:27 2020 +0100
+++ b/src/types.rs	Thu Dec 03 09:42:25 2020 +0100
@@ -35,6 +35,7 @@
     time: chrono::DateTime<chrono::Utc>,
     altitude: Option<f64>,
     speed: Option<f64>,
+    id: Option<i32>,
 }
 
 #[derive(serde::Serialize, Debug, Clone)]
@@ -75,6 +76,7 @@
 }
 
 pub fn geofeature_from_row(
+    id: Option<i32>,
     ts: chrono::DateTime<chrono::Utc>,
     lat: Option<f64>,
     long: Option<f64>,
@@ -84,6 +86,7 @@
     GeoFeature {
         typ: "Feature".into(),
         properties: GeoProperties {
+            id: id,
             time: ts,
             altitude: ele,
             speed: spd,