diff --git a/sr_download/src/db_part.rs b/sr_download/src/db_part.rs index f69949a..1c5b71e 100644 --- a/sr_download/src/db_part.rs +++ b/sr_download/src/db_part.rs @@ -1,9 +1,9 @@ use blake3::Hasher; use sea_orm::{ ActiveModelTrait, ColumnTrait, ConnectionTrait, DatabaseConnection, EntityTrait, - IntoActiveModel, ModelTrait, QueryFilter, QuerySelect, Statement, TransactionTrait, + IntoActiveModel, ModelTrait, QueryFilter, QueryResult, QuerySelect, Statement, + TransactionTrait, }; -use tracing::{event, Level}; // use tracing::{event, Level}; use crate::model; @@ -25,6 +25,7 @@ pub struct DbData { pub save_type: SaveType, pub len: i64, pub blake_hash: String, + pub xml_tested: bool, } impl From for DbData { @@ -35,6 +36,7 @@ impl From for DbData { save_type: data.save_type, len: data.len, blake_hash: data.blake_hash, + xml_tested: data.xml_tested.unwrap_or(false), } } } @@ -47,6 +49,7 @@ impl From<(model::main_data::Model, model::long_data::Model)> for DbData { save_type: data.0.save_type, len: data.0.len, blake_hash: data.0.blake_hash, + xml_tested: data.0.xml_tested.unwrap_or(false), } } } @@ -59,12 +62,14 @@ impl DbData { let mut hasher = Hasher::new(); hasher.update(data.as_bytes()); let hash = hasher.finalize().to_hex().to_string(); + let xml_tested = utils::verify_xml(&data).is_ok(); Self { text: Some(data), save_id, save_type, len, blake_hash: hash, + xml_tested, } } @@ -87,7 +92,20 @@ impl DbData { if self.text.is_none() { return false; } - utils::verify_xml(self.text.as_ref().unwrap()) + utils::verify_xml(self.text.as_ref().unwrap()).is_ok() + } + + pub fn xml_status(&self) -> String { + if self.xml_tested { + return "ok".to_string(); + } + if self.text.is_none() { + return "no data".to_string(); + } + if let Err(e) = utils::verify_xml(self.text.as_ref().unwrap()) { + return format!("error: {}", e); + } + "ok".to_string() } /// 直接从 full_data 里选即可 @@ -109,12 +127,14 @@ impl DbData { let save_type: SaveType = datas.try_get("", "save_type").ok()?; let len: i64 = datas.try_get("", "len").ok()?; let blake_hash: String = datas.try_get("", "blake_hash").ok()?; + let xml_tested: Option = datas.try_get("", "xml_tested").ok()?; Some(Self { text, save_id: save_id as SaveId, save_type, len, blake_hash, + xml_tested: xml_tested.unwrap_or(false), }) } } @@ -226,7 +246,7 @@ where exitst_data.delete(db).await?; } - let xml_tested = Some(utils::verify_xml(&data)); + let xml_tested = Some(utils::verify_xml(&data).is_ok()); if data_len > TEXT_DATA_MAX_LEN { // 过长, 需要把数据放到 long_data 里 diff --git a/sr_download/src/db_part/utils.rs b/sr_download/src/db_part/utils.rs index a8d3b0f..c171d1e 100644 --- a/sr_download/src/db_part/utils.rs +++ b/sr_download/src/db_part/utils.rs @@ -156,15 +156,15 @@ pub trait FromDb { } /// 校验一下是不是合法 xml -pub fn verify_xml(data: &str) -> bool { +pub fn verify_xml(data: &str) -> quick_xml::Result<()> { let mut reader = Reader::from_str(data); reader.config_mut().trim_text(true); loop { match reader.read_event() { Ok(Event::Eof) => break, Ok(_) => (), - Err(_) => return false, + Err(e) => return Err(e), } } - true + Ok(()) } diff --git a/sr_download/src/info.html b/sr_download/src/info.html index 5c18112..95218b0 100644 --- a/sr_download/src/info.html +++ b/sr_download/src/info.html @@ -30,21 +30,6 @@ font-size: medium; } - .box:nth-child(1) { - background-color: #FFE0B2; - /* 淡橙色 */ - } - - .box:nth-child(2) { - background-color: #C8E6C9; - /* 淡绿色 */ - } - - .box2:nth-child(1) { - background-color: #BBDEFB; - /* 淡蓝色 */ - } - .title { font-size: 30px; margin-bottom: 10px; @@ -113,7 +98,7 @@
-
+
最新数据
最大 id: |MAX_ID|
类型: |MAX_SAVE_TYPE|
@@ -124,14 +109,14 @@
-
+
最新飞船
最大飞船 id: |MAX_SHIP_ID|
长度: |MAX_SHIP_LEN|
xml校验: |MAX_SHIP_XML|
blake hash: |MAX_SHIP_HASH|
-
+
最新存档
最大存档 id: |MAX_SAVE_ID|
长度: |MAX_SAVE_LEN|
@@ -140,6 +125,14 @@
+
+
+
相关信息
+
请求用时: |COST_TIME|
+
sr-download 版本号: |VERSION|
+
+
+
diff --git a/sr_download/src/web_part.rs b/sr_download/src/web_part.rs index 0e5e760..3360360 100644 --- a/sr_download/src/web_part.rs +++ b/sr_download/src/web_part.rs @@ -200,20 +200,15 @@ async fn jump_to_dashboard_from_root() -> impl IntoResponse { /// 框上面分别是 "最新数据" "最新飞船" "最新存档" 的标题 const INFO_PAGE: &str = include_str!("info.html"); -fn xml_tested_to_str(tested: bool) -> &'static str { - if tested { - "通过了" - } else { - "未通过" - } -} - async fn dashboard_page(State(db): State) -> Html { + let start_time = std::time::Instant::now(); let max_id = db_part::search::max_id(&db).await; let max_id_data = DbData::from_db(max_id, &db).await; let max_ship = db_part::search::max_ship(&db).await; let max_save = db_part::search::max_save(&db).await; + let elapsed = start_time.elapsed(); + let mut page_content = INFO_PAGE.replace("|MAX_ID|", &max_id.to_string()); if let Some(max_id_data) = max_id_data { @@ -225,7 +220,7 @@ async fn dashboard_page(State(db): State) -> Html { ) .replace("|MAX_LEN|", &max_id_data.len.to_string()) .replace("|MAX_HASH|", &max_id_data.blake_hash) - .replace("|MAX_XML|", xml_tested_to_str(max_id_data.verify_xml())); + .replace("|MAX_XML|", &max_id_data.xml_status()); } else { page_content = page_content .replace("|MAX_ID|", "not found") @@ -239,7 +234,7 @@ async fn dashboard_page(State(db): State) -> Html { .replace("|MAX_SHIP_ID|", &max_ship.save_id.to_string()) .replace("|MAX_SHIP_LEN|", &max_ship.len.to_string()) .replace("|MAX_SHIP_HASH|", &max_ship.blake_hash) - .replace("|MAX_SHIP_XML|", xml_tested_to_str(max_ship.verify_xml())); + .replace("|MAX_SHIP_XML|", &max_ship.xml_status()); } else { page_content = page_content .replace("|MAX_SHIP_ID|", "not found") @@ -252,7 +247,7 @@ async fn dashboard_page(State(db): State) -> Html { .replace("|MAX_SAVE_ID|", &max_save.save_id.to_string()) .replace("|MAX_SAVE_LEN|", &max_save.len.to_string()) .replace("|MAX_SAVE_HASH|", &max_save.blake_hash) - .replace("|MAX_SAVE_XML|", xml_tested_to_str(max_save.verify_xml())); + .replace("|MAX_SAVE_XML|", &max_save.xml_status()); } else { page_content = page_content .replace("|MAX_SAVE_ID|", "not found") @@ -261,6 +256,10 @@ async fn dashboard_page(State(db): State) -> Html { .replace("|MAX_SAVE_XML|", "not found"); } + page_content = page_content + .replace("|COST_TIME|", &format!("{:?}", elapsed)) + .replace("|VERSION|", env!("CARGO_PKG_VERSION")); + Html(page_content) } diff --git a/update.md b/update.md index 9ced8b8..5851ccf 100644 --- a/update.md +++ b/update.md @@ -1,5 +1,17 @@ # v1.0 +## 1.2.6 + +1.3 会迁移到自己写的脚本的 +不过现在还是用 sea orm 的 +数据库表里添加了 time 字段 +用来记录时间 +添加了新的检查数据库内数据有效性的 api +现在会检测了 +去掉了 `get-save` +在网页 api 里添加了是否通过 xml 检测的选项 +也在 dashboard 上添加了用时显示 + ## 1.2.5 额, 准备迁移到自己写的迁移脚本