From f5ebc54c691f370762e5fbc92d07f5e6c389c91a Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 26 Dec 2024 18:08:47 +0100 Subject: [PATCH] Housekeeping - Update dependencies - Remove explicit http status code from status endpoint - Remove unnecessary code --- Cargo.lock | 74 +++++++++++++++++++++++-------------- Cargo.toml | 14 +++---- src/database.rs | 70 +++++++---------------------------- src/main.rs | 8 +--- src/server/router.rs | 4 +- src/server/routes/status.rs | 14 +++---- 6 files changed, 75 insertions(+), 109 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9022e4b..11c96aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -109,9 +109,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "axum" -version = "0.7.7" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "504e3947307ac8326a5437504c517c4b56716c9d98fac0028c2acc7ca47d70ae" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" dependencies = [ "async-trait", "axum-core", @@ -215,9 +215,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.38" +version = "0.4.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" dependencies = [ "android-tzdata", "iana-time-zone", @@ -230,9 +230,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.20" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", @@ -240,9 +240,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.20" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", @@ -264,9 +264,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.2" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "colorchoice" @@ -634,7 +634,7 @@ dependencies = [ "reqwest", "serde", "serde_json", - "thiserror", + "thiserror 1.0.65", ] [[package]] @@ -677,7 +677,7 @@ dependencies = [ "serde_json", "simple_logger", "spaceapi", - "thiserror", + "thiserror 2.0.9", "tokio", ] @@ -823,9 +823,9 @@ checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -985,18 +985,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.214" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.214" +version = "1.0.216" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" dependencies = [ "proc-macro2", "quote", @@ -1005,9 +1005,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.132" +version = "1.0.134" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d" dependencies = [ "itoa", "memchr", @@ -1082,7 +1082,7 @@ dependencies = [ "log", "serde", "serde_json", - "thiserror", + "thiserror 1.0.65", ] [[package]] @@ -1099,9 +1099,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.85" +version = "2.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56" +checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035" dependencies = [ "proc-macro2", "quote", @@ -1147,7 +1147,16 @@ version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.65", +] + +[[package]] +name = "thiserror" +version = "2.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" +dependencies = [ + "thiserror-impl 2.0.9", ] [[package]] @@ -1161,6 +1170,17 @@ dependencies = [ "syn", ] +[[package]] +name = "thiserror-impl" +version = "2.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.36" @@ -1211,9 +1231,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.41.0" +version = "1.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "145f3413504347a2be84393cc8a7d2fb4d863b375909ea59f2158261aa258bbb" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" dependencies = [ "backtrace", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 860f911..feac9b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,12 +11,12 @@ keywords = ["spaceapi", "hackspace", "k4cg"] [dependencies] spaceapi = "0.9.0" influxdb = "0.7.2" -serde = "1.0.214" -serde_json = "1.0.132" -axum = "0.7.7" -tokio = { version = "1.41.0", features = ["rt-multi-thread", "fs", "sync"] } -chrono = "0.4.38" -thiserror = "1.0.65" +serde = "1.0.216" +serde_json = "1.0.134" +axum = "0.7.9" +tokio = { version = "1.42.0", features = ["rt-multi-thread", "fs", "sync"] } +chrono = "0.4.39" +thiserror = "2.0.9" log = "0.4.22" simple_logger = { version = "5.0.0", default-features = false, features = ["timestamps"] } -clap = { version = "4.5.20", features = ["derive", "env"] } +clap = { version = "4.5.23", features = ["derive", "env"] } diff --git a/src/database.rs b/src/database.rs index eca09a2..ca8a748 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,40 +1,18 @@ use crate::{configuration, StatusError}; use chrono::{DateTime, Utc}; use serde::Deserialize; -use std::vec::Vec; #[derive(Clone)] pub struct Database { client: influxdb::Client, } -#[derive(Debug)] +#[derive(Deserialize, Clone)] pub struct TimeValue { pub time: DateTime, pub value: f64, } -#[derive(Deserialize, Clone)] -struct QueryResults { - pub results: Vec, -} - -#[derive(Deserialize, Clone)] -struct QueryResult { - #[allow(dead_code)] - pub statement_id: u64, - pub series: Option>, -} - -#[derive(Deserialize, Clone)] -struct QuerySeries { - #[allow(dead_code)] - pub name: String, - #[allow(dead_code)] - pub columns: Vec, - pub values: Vec<(String, f64)>, -} - impl Database { pub fn new(config: &configuration::Database) -> Self { let client = influxdb::Client::new(&config.connection, &config.database) @@ -57,52 +35,32 @@ impl Database { unit: &str, validity: DateTime, ) -> Result { - let time = validity - .timestamp_nanos_opt() - .ok_or(StatusError::Database("DateTime out of range".into()))?; - let query = influxdb::ReadQuery::new(format!( - r#"SELECT time, value FROM "{}" WHERE (entity_id = '{}' AND time > {}) ORDER BY time DESC LIMIT 1"#, - unit, name, time + r#"SELECT time, value FROM "{}" WHERE (entity_id = '{}' AND time > '{}') ORDER BY time DESC LIMIT 1"#, + unit, + name, + validity.to_rfc3339() )); - let results_raw = self + let mut result = self .client - .query(query) + .json_query(query) .await .map_err(|e| StatusError::Database(e.to_string()))?; - let results: QueryResults = serde_json::from_str(&results_raw) - .map_err(|e| StatusError::Database(format!("serde '{}'", e)))?; - - let values = results - .results - .first() - .ok_or(StatusError::Database( - "unexpected response, no results".into(), - ))? + result + .deserialize_next::() + .map_err(|e| StatusError::Database(format!("unexpected response: {:?}", e)))? .series - .as_ref() - .ok_or(StatusError::Database( - "unexpected response, statements result does not contain any data".into(), - ))? .first() .ok_or(StatusError::Database( - "unexpected response, no series".into(), + "unexpected response: no series".into(), ))? .values .first() .ok_or(StatusError::Database( - "unexpected response, no values".into(), - ))?; - - let time: DateTime = DateTime::parse_from_rfc3339(&values.0) - .map(|e| e.into()) - .map_err(|e| StatusError::Database(e.to_string()))?; - - Ok(TimeValue { - time, - value: values.1, - }) + "unexpected response: no values".into(), + )) + .cloned() } } diff --git a/src/main.rs b/src/main.rs index d3d8d42..ce470dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,11 +59,5 @@ async fn main() -> Result<(), StatusError> { .init() .expect("Logger already initialized"); - match app().await { - Ok(_) => panic!(), - Err(err) => { - log::error!("{}", err); - Err(err) - } - } + app().await } diff --git a/src/server/router.rs b/src/server/router.rs index 055d28c..513adad 100644 --- a/src/server/router.rs +++ b/src/server/router.rs @@ -56,7 +56,5 @@ pub async fn run( axum::serve(listener, app) .await - .map_err(|e| StatusError::Server(format!("Failed to start server ({})", e)))?; - - Ok(()) + .map_err(|e| StatusError::Server(format!("Failed to start server ({})", e))) } diff --git a/src/server/routes/status.rs b/src/server/routes/status.rs index 4c81571..7b10fb3 100644 --- a/src/server/routes/status.rs +++ b/src/server/routes/status.rs @@ -1,4 +1,4 @@ -use axum::{extract::State, http, Json}; +use axum::{extract::State, Json}; use chrono::{DateTime, TimeZone, Utc}; use spaceapi::{sensors, Status as SpaceStatus}; use std::sync::Arc; @@ -23,17 +23,13 @@ impl StateStatus { } } -pub async fn get_status( - State(state): State>, -) -> (http::StatusCode, Json) { +pub async fn get_status(State(state): State>) -> Json { log::info!("GET /status.json"); - (http::StatusCode::OK, Json(update_template(&state).await)) + Json(update_template(&state).await) } -pub async fn get_status_cache( - State(state): State>, -) -> (http::StatusCode, Json) { +pub async fn get_status_cache(State(state): State>) -> Json { log::info!("GET /status.json"); let status = { @@ -46,7 +42,7 @@ pub async fn get_status_cache( current.status.clone() }; - (http::StatusCode::OK, Json(status)) + Json(status) } async fn update_template(appstate: &Arc) -> SpaceStatus {