From 8662d369d39e3d2b1503874d758837c70d806a5a Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Mon, 18 Mar 2024 11:50:57 -0400 Subject: [PATCH] fix: various - adds sql exporting option for areas - fixes casing issues for controller area table - set default `last_seen` value in client to now - 1 month --- client/src/assets/constants.ts | 1 + client/src/hooks/usePersist.ts | 1 + server/api/src/utils/response.rs | 5 ++-- server/model/src/api/args.rs | 2 ++ server/model/src/api/collection.rs | 8 ++++++ server/model/src/api/mod.rs | 4 +++ server/model/src/db/area.rs | 43 +++++++++++------------------- 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/client/src/assets/constants.ts b/client/src/assets/constants.ts index 44a0dfca..ab3f7ca7 100644 --- a/client/src/assets/constants.ts +++ b/client/src/assets/constants.ts @@ -122,6 +122,7 @@ export const CONVERSION_TYPES = [ 'text', 'altText', 'poracle', + 'sql', ] as const export const GEOMETRY_CONVERSION_TYPES = [ diff --git a/client/src/hooks/usePersist.ts b/client/src/hooks/usePersist.ts index 3ab10a93..c57ebd4c 100644 --- a/client/src/hooks/usePersist.ts +++ b/client/src/hooks/usePersist.ts @@ -144,6 +144,7 @@ export const usePersist = create( const date = new Date() date.setMinutes(0) date.setSeconds(0) + date.setMonth(date.getMonth() - 1) return date })(), snappable: true, diff --git a/server/api/src/utils/response.rs b/server/api/src/utils/response.rs index cf6f28db..09f8b631 100644 --- a/server/api/src/utils/response.rs +++ b/server/api/src/utils/response.rs @@ -3,7 +3,7 @@ use super::*; use actix_web::HttpResponse; use algorithms::stats::Stats; use geojson::JsonValue; -use model::api::{Precision, ToGeometry}; +use model::api::{Precision, ToGeometry, ToSql}; use serde::Serialize; use serde_json::json; @@ -88,7 +88,8 @@ pub fn send( ReturnTypeArg::FeatureVec => GeoFormats::FeatureVec(value.features), ReturnTypeArg::FeatureCollection => GeoFormats::FeatureCollection(value), ReturnTypeArg::Poracle => GeoFormats::Poracle(value.to_poracle_vec()), - ReturnTypeArg::PoracleSingle => GeoFormats::PoracleSingle(value.to_poracle_vec().first().unwrap().clone()) + ReturnTypeArg::PoracleSingle => GeoFormats::PoracleSingle(value.to_poracle_vec().first().unwrap().clone()), + ReturnTypeArg::Sql => GeoFormats::Text(value.to_sql()), }))}, stats, }) diff --git a/server/model/src/api/args.rs b/server/model/src/api/args.rs index dfe31cb7..8546d777 100644 --- a/server/model/src/api/args.rs +++ b/server/model/src/api/args.rs @@ -155,6 +155,7 @@ pub enum ReturnTypeArg { FeatureCollection, PoracleSingle, Poracle, + Sql, } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -606,6 +607,7 @@ pub fn get_return_type(return_type: String, default_return_type: &ReturnTypeArg) "featurevec" | "feature_vec" => ReturnTypeArg::FeatureVec, "poracle" => ReturnTypeArg::Poracle, "featurecollection" | "feature_collection" => ReturnTypeArg::FeatureCollection, + "sql" => ReturnTypeArg::Sql, _ => default_return_type.clone(), } } diff --git a/server/model/src/api/collection.rs b/server/model/src/api/collection.rs index 7361cfcb..352dfcb3 100644 --- a/server/model/src/api/collection.rs +++ b/server/model/src/api/collection.rs @@ -1,3 +1,5 @@ +use self::utils::sql_raw; + use super::{args::UnknownId, multi_vec::MultiVec, *}; pub trait Default { @@ -247,3 +249,9 @@ impl ToPoracleVec for FeatureCollection { return_vec } } + +impl ToSql for FeatureCollection { + fn to_sql(self) -> String { + format!("SELECT * FROM {{database.table}} WHERE{}", sql_raw(&self)) + } +} diff --git a/server/model/src/api/mod.rs b/server/model/src/api/mod.rs index f488c0f4..c913ad46 100644 --- a/server/model/src/api/mod.rs +++ b/server/model/src/api/mod.rs @@ -112,6 +112,10 @@ pub trait ToGeometryVec { fn to_geometry_vec(self) -> Vec; } +pub trait ToSql { + fn to_sql(self) -> String; +} + #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(untagged)] pub enum GeoFormats { diff --git a/server/model/src/db/area.rs b/server/model/src/db/area.rs index 98de1847..aa44aa25 100644 --- a/server/model/src/db/area.rs +++ b/server/model/src/db/area.rs @@ -1,7 +1,5 @@ //! SeaORM Entity. Generated by sea-orm-codegen 0.10.1 -use std::collections::HashMap; - use super::{sea_orm_active_enums::Type, *}; use sea_orm::entity::prelude::*; @@ -143,7 +141,6 @@ impl Query { async fn upsert_feature( conn: &DatabaseConnection, feat: Feature, - existing: &HashMap, inserts_updates: &mut InsertsUpdates, ) -> Result<(), DbErr> { let name = if let Some(name) = feat.property("name") { @@ -179,12 +176,19 @@ impl Query { if let Some(column) = column { let name = name.to_string(); let area = feat.to_text(" ", ",", false); - let is_update = existing.get(&name); + let is_update = area::Entity::find() + .select_only() + .column(area::Column::Id) + .column(area::Column::Name) + .filter(area::Column::Name.eq(name.clone())) + .into_model::() + .one(conn) + .await?; - if let Some(id) = is_update { + if let Some(existing) = is_update { area::Entity::update_many() .col_expr(column, Expr::value(area)) - .filter(area::Column::Id.eq(id.to_owned())) + .filter(area::Column::Id.eq(existing.id.to_owned())) .exec(conn) .await?; log::info!("[DB] {}.{:?} Area Updated!", name, column); @@ -219,7 +223,8 @@ impl Query { Column::PokemonModeRoute => new_model.pokemon_mode_route = Set(Some(area)), _ => {} } - inserts_updates.to_insert.push(new_model); + area::Entity::insert(new_model).exec(conn).await?; + inserts_updates.inserts += 1; Ok(()) } } else { @@ -238,17 +243,6 @@ impl Query { conn: &DatabaseConnection, area: GeoFormats, ) -> Result<(usize, usize), DbErr> { - let existing: HashMap = area::Entity::find() - .select_only() - .column(area::Column::Id) - .column(area::Column::Name) - .into_model::() - .all(conn) - .await? - .into_iter() - .map(|model| (model.name, model.id)) - .collect(); - let mut insert_update = InsertsUpdates:: { to_insert: vec![], updates: 0, @@ -256,7 +250,7 @@ impl Query { }; match area { GeoFormats::Feature(feat) => { - Query::upsert_feature(conn, feat, &existing, &mut insert_update).await? + Query::upsert_feature(conn, feat, &mut insert_update).await? } feat => { let fc = match feat { @@ -264,17 +258,10 @@ impl Query { geometry => geometry.to_collection(None, None), }; for feat in fc.into_iter() { - Query::upsert_feature(conn, feat, &existing, &mut insert_update).await? + Query::upsert_feature(conn, feat, &mut insert_update).await? } } } - let insert_len = insert_update.to_insert.len(); - if !insert_update.to_insert.is_empty() { - area::Entity::insert_many(insert_update.to_insert) - .exec(conn) - .await?; - log::info!("Updated {} Areas", insert_len); - } - Ok((insert_len, insert_update.updates)) + Ok((insert_update.inserts, insert_update.updates)) } }