Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
- Move validity calculation into database
- use tokio::test for tests
  • Loading branch information
koarlchen committed Dec 30, 2024
1 parent 52602ea commit f1338c0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 38 deletions.
10 changes: 3 additions & 7 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::FILE_CONFIG;
use tokio::runtime::Runtime;

#[test]
fn parse() {
Runtime::new().unwrap().block_on(async {
read_config(FILE_CONFIG).await.unwrap();
});
#[tokio::test]
async fn parse() {
read_config(crate::FILE_CONFIG).await.unwrap();
}
}
20 changes: 10 additions & 10 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ impl Database {
&self,
name: &str,
unit: &str,
validity: DateTime<Utc>,
validity: chrono::TimeDelta,
) -> Result<TimeValue, StatusError> {
let time = match validity.is_zero() {
true => "".into(),
false => format!(r#"AND time > now() - {}s"#, validity.num_seconds()),
};

let query = influxdb::ReadQuery::new(format!(
r#"SELECT time, value FROM "{}" WHERE (entity_id = '{}' AND time > '{}') ORDER BY time DESC LIMIT 1"#,
unit,
name,
validity.to_rfc3339()
r#"SELECT time, value FROM "{}" WHERE (entity_id = '{}' {}) ORDER BY time DESC LIMIT 1"#,
unit, name, time
));

let mut result = self
.client
self.client
.json_query(query)
.await
.map_err(|e| StatusError::Database(e.to_string()))?;

result
.map_err(|e| StatusError::Database(e.to_string()))?
.deserialize_next::<TimeValue>()
.map_err(|e| StatusError::Database(format!("unexpected response: {:?}", e)))?
.series
Expand Down
17 changes: 3 additions & 14 deletions src/server/routes/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum::{extract::State, Json};
use chrono::{DateTime, TimeZone, Utc};
use chrono::{DateTime, Utc};
use spaceapi::{sensors, Status as SpaceStatus};
use std::sync::Arc;
use std::vec::Vec;
Expand Down Expand Up @@ -113,14 +113,11 @@ async fn get_value(
unit: &str,
validity: chrono::TimeDelta,
) -> Option<database::TimeValue> {
match database
.query_and_extract(entity, unit, get_start_time(validity))
.await
{
match database.query_and_extract(entity, unit, validity).await {
Ok(temp) => Some(temp),
Err(err) => {
log::warn!(
"Failed to get measurement for entity='{}' unit='{}' validity='{}' ({})",
"Failed to get measurement for entity='{}' unit='{}' validity='{:?}' ({})",
entity,
unit,
validity,
Expand All @@ -131,14 +128,6 @@ async fn get_value(
}
}

fn get_start_time(validity: chrono::TimeDelta) -> DateTime<Utc> {
if validity.is_zero() {
Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap()
} else {
Utc::now() - validity
}
}

// There is no definition of a generic sensor in the spaceapi crate.
// The sensors we currently use share the same data fields but are implemented as seperate types.
// Therefore we define our own generic type and implement the From trait for the sensors we currently use.
Expand Down
10 changes: 3 additions & 7 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ pub async fn read_template(fname: &str) -> Result<spaceapi::Status, StatusError>
#[cfg(test)]
mod tests {
use super::*;
use crate::FILE_TEMPLATE;
use tokio::runtime::Runtime;

#[test]
fn parse() {
Runtime::new().unwrap().block_on(async {
read_template(FILE_TEMPLATE).await.unwrap();
});
#[tokio::test]
async fn parse() {
read_template(crate::FILE_TEMPLATE).await.unwrap();
}
}

0 comments on commit f1338c0

Please sign in to comment.