Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unnecessary json serializing #156

Merged
merged 2 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "koji"
version = "1.1.3"
version = "1.1.4"
edition = "2021"

[workspace]
Expand Down
2 changes: 1 addition & 1 deletion server/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "api"
version = "1.1.1"
version = "1.1.2"
edition = "2021"
publish = false

Expand Down
2 changes: 1 addition & 1 deletion server/model/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "model"
version = "1.1.3"
version = "1.1.4"
edition = "2021"
publish = false

Expand Down
134 changes: 78 additions & 56 deletions server/model/src/db/geofence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.10.1

use std::{collections::HashMap, str::FromStr};
use std::{collections::HashMap, str::FromStr, time::Instant};

use crate::{
api::{
Expand All @@ -15,7 +15,11 @@ use crate::{
},
};

use super::{geofence_property::FullPropertyModel, sea_orm_active_enums::Type, *};
use super::{
geofence_property::{Basic, FullPropertyModel},
sea_orm_active_enums::Type,
*,
};

use geojson::{GeoJson, Geometry};
use sea_orm::{entity::prelude::*, UpdateResult};
Expand Down Expand Up @@ -108,17 +112,15 @@ impl Model {
async fn get_parent_name(&self, db: &DatabaseConnection) -> Result<Option<String>, ModelError> {
if let Some(parent_id) = self.parent {
if let Some(parent) = geofence::Entity::find_by_id(parent_id).one(db).await? {
let found_name = if let Some(name) = parent
let found_name = if let Some(full_property) = parent
.get_related_properties()
.filter(property::Column::Name.eq("name"))
.into_model::<FullPropertyModel>()
.all(db)
.one(db)
.await?
.iter()
.find(|parent_prop| parent_prop.name == "name")
{
let parsed = name.parse_db_value(&parent);
if let Some(parsed) = parsed.as_str() {
parsed.to_string()
if let Some(parsed) = full_property.value {
parsed
} else {
parent.name
}
Expand All @@ -137,8 +139,12 @@ impl Model {
args: &ApiQueryArgs,
) -> Result<Feature, ModelError> {
let mut has_manual_parent = String::from("");
let mut properties = self
.get_related_properties()
let mut properties = geofence_property::Entity::find()
.from_raw_sql(Statement::from_sql_and_values(
DbBackend::MySql,
r#"SELECT geofence_property.id, name, geofence_id, property_id, category, value FROM geofence_property JOIN property ON geofence_property.property_id = property.id WHERE geofence_id = ?"#,
vec![self.id.into()],
))
.into_model::<FullPropertyModel>()
.all(db)
.await?
Expand All @@ -149,49 +155,68 @@ impl Model {
}
prop.parse_db_value(&self)
})
.collect::<Vec<serde_json::Value>>();
.collect::<Vec<Basic>>();

let parent_name = if has_manual_parent.is_empty() {
self.get_parent_name(db).await?
} else {
Some(has_manual_parent)
};

if args.internal.is_some() {
properties.push(json!({ "name": "__id", "value": self.id }));
properties.push(json!({ "name": "__name", "value": self.name }));
properties.push(json!({ "name": "__mode", "value": self.mode.to_value() }));
if args.internal.unwrap_or(false) {
properties.push(Basic {
name: "__id".to_string(),
value: serde_json::Value::from(self.id),
});
properties.push(Basic {
name: "__name".to_string(),
value: serde_json::Value::from(self.name.to_string()),
});
properties.push(Basic {
name: "__mode".to_string(),
value: serde_json::Value::from(self.mode.to_value()),
});
}
if args.name.is_some() {
properties.push(json!({ "name": "name", "value": self.name }));
if args.name.unwrap_or(false) {
properties.push(Basic {
name: "name".to_string(),
value: serde_json::Value::from(self.name),
});
}
if args.id.is_some() {
properties.push(json!({ "name": "id", "value": self.id }));
if args.id.unwrap_or(false) {
properties.push(Basic {
name: "id".to_string(),
value: serde_json::Value::from(self.id),
});
}
if args.mode.is_some() {
properties.push(json!({ "name": "mode", "value": self.mode.to_value() }));
if args.mode.unwrap_or(false) {
properties.push(Basic {
name: "mode".to_string(),
value: serde_json::Value::from(self.mode.to_value()),
});
}
if args.parent.is_some() {
properties.push(json!({ "name": "parent", "value": parent_name }));
if args.group.unwrap_or(false) && parent_name.is_some() {
properties.push(Basic {
name: "group".to_string(),
value: serde_json::Value::from(parent_name.clone()),
});
}
if args.group.is_some() {
properties.push(json!({ "name": "group", "value": parent_name }));
if args.parent.unwrap_or(false) {
properties.push(Basic {
name: "parent".to_string(),
value: serde_json::Value::from(parent_name.clone()),
});
}

let mut feature = Feature {
geometry: Some(Geometry::from_json_value(self.geometry)?),
..Feature::default()
};
for prop in properties.iter() {
let key = prop.get("name");
let val = prop.get("value");
if let Some(key) = key {
if let Some(key) = key.as_str() {
if key.eq("parent") {
feature.set_property(key, parent_name.clone())
} else {
feature.set_property(key, val.unwrap().clone())
}
}
for prop in properties.into_iter() {
if prop.name.eq("parent") {
feature.set_property(prop.name, parent_name.clone())
} else {
feature.set_property(prop.name, prop.value)
}
}

Expand Down Expand Up @@ -315,7 +340,7 @@ impl Query {
.await?
.into_iter()
.map(|prop| prop.parse_db_value(&record))
.collect::<Vec<Json>>()),
.collect::<Vec<Basic>>()),
);
Ok(json!(json))
}
Expand Down Expand Up @@ -756,29 +781,26 @@ impl Query {
project_name: String,
args: &ApiQueryArgs,
) -> Result<Vec<Feature>, ModelError> {
let items = match project_name.parse::<u32>() {
Ok(id) => {
Entity::find()
.order_by(Column::Name, Order::Asc)
.left_join(project::Entity)
.filter(project::Column::Id.eq(id))
.all(db)
.await?
}
Err(_) => {
Entity::find()
.order_by(Column::Name, Order::Asc)
.left_join(project::Entity)
.filter(project::Column::Name.eq(project_name))
.all(db)
.await?
}
};
let time = Instant::now();

let items = Entity::find()
.order_by(Column::Name, Order::Asc)
.filter(match project_name.parse::<u32>() {
Ok(id) => project::Column::Id.eq(id),
Err(_) => project::Column::Name.eq(project_name),
})
.left_join(project::Entity)
.all(db)
.await?;

log::debug!("db query took {:?}", time.elapsed());

let time = Instant::now();
let items =
future::try_join_all(items.into_iter().map(|result| result.to_feature(db, args)))
.await?;

log::debug!("feature conversion took {:?}", time.elapsed());
Ok(items)
}

Expand Down
33 changes: 22 additions & 11 deletions server/model/src/db/geofence_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ impl Related<super::property::Entity> for Entity {

impl ActiveModelBehavior for ActiveModel {}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Basic {
pub name: String,
pub value: Json,
}

#[derive(Clone, Debug, FromQueryResult, Serialize, Deserialize)]
pub struct FullPropertyModel {
pub id: u32,
Expand All @@ -64,14 +70,18 @@ pub struct FullPropertyModel {
}

impl FullPropertyModel {
pub fn parse_db_value(&self, model: &geofence::Model) -> serde_json::Value {
let model_json = serde_json::to_value(model).unwrap();

let parsed_value = match self.category {
Category::Database => {
let value = model_json[&self.name].clone();
value
}
pub fn parse_db_value(self, model: &geofence::Model) -> Basic {
let parsed_value: Json = match self.category {
Category::Database => match self.name.as_str() {
"id" => serde_json::Value::from(model.id),
"name" => serde_json::Value::from(model.name.to_string()),
"mode" => serde_json::Value::from(model.mode.to_value()),
"geo_type" => serde_json::Value::from(model.geo_type.to_string()),
"parent" => serde_json::Value::from(model.parent),
"created_at" => serde_json::Value::from(model.created_at.to_string()),
"updated_at" => serde_json::Value::from(model.updated_at.to_string()),
_ => serde_json::Value::Null,
},
_ => {
if let Some(value) = &self.value {
parse_property_value(value, &self.category)
Expand All @@ -80,9 +90,10 @@ impl FullPropertyModel {
}
}
};
let mut new_json = serde_json::json!(self);
new_json["value"] = parsed_value;
new_json
Basic {
name: self.name,
value: parsed_value,
}
}
}

Expand Down