From d4380260c15872caef592985e64576315910b8d0 Mon Sep 17 00:00:00 2001 From: Guillermo Roche Date: Mon, 2 Dec 2024 21:57:14 +0100 Subject: [PATCH] improve error tolerance and logs --- src/elastic_conection.rs | 41 +++++++++++++++++++++++++++++++++++----- src/main.rs | 24 ++++++++++++++++++++++- src/nut/nut_client.rs | 19 ------------------- src/nut/nut_data.rs | 18 ------------------ 4 files changed, 59 insertions(+), 43 deletions(-) diff --git a/src/elastic_conection.rs b/src/elastic_conection.rs index 0c62c98..b8fd607 100644 --- a/src/elastic_conection.rs +++ b/src/elastic_conection.rs @@ -5,11 +5,16 @@ use elasticsearch::http::transport::SingleNodeConnectionPool; use elasticsearch::IndexParts; use serde_json::Value; use elasticsearch::auth::Credentials; +use std::fmt; pub struct ElasticConection { conection: Elasticsearch } +pub struct ElasticConErr { + content: String, +} + impl ElasticConection { pub fn new() -> Result> { let url = Url::parse("http://127.0.0.1:9200")?; @@ -23,14 +28,40 @@ impl ElasticConection { }) } - pub async fn send(&self, data: Value) -> bool { - let response = self.conection + pub async fn send(&self, data: Value) -> Result<(), ElasticConErr> { + let raw_response = self.conection .index(IndexParts::Index("nutbeat-0.1")) .body(data) .send() - .await.unwrap(); - response.status_code().is_success() + .await; + let response = match raw_response { + Ok(r) => r, + Err(e) => return Err(ElasticConErr { content: e.to_string()}), + }; + let status_code = response.status_code(); + match status_code.clone().is_success() { + true => Ok(()), + false => { + let err_ret = match response.text().await { + Ok(ret) => ret, + Err(_e) => status_code.as_str().to_string(), + }; + Err(ElasticConErr { content: err_ret }) + }, + } } } - +impl fmt::Display for ElasticConErr { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.content) + } +} + +impl fmt::Debug for ElasticConErr { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, "{}", self.content + ) + } +} diff --git a/src/main.rs b/src/main.rs index 586e5ac..d4a80df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use nut::nut_client::NutClient; use elastic_conection::ElasticConection; use chrono; use tokio::time::{sleep, Duration}; +use std::fmt; #[tokio::main] async fn main() -> Result<(), Box> { @@ -17,8 +18,29 @@ async fn main() -> Result<(), Box> { } async fn start_loop(e_con: ElasticConection, mut n_con: NutClient) { + let mut has_failed = false; loop { - e_con.send(n_con.get_data(chrono::offset::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)).unwrap()).await; sleep(Duration::from_secs(10)).await; + let payload = match n_con.get_data(chrono::offset::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)) { + Ok(p) => p, + Err(e) => { + process_error(Box::new(e), "nut connection", &mut has_failed); + continue; + }, + }; + match e_con.send(payload).await { + Ok(()) => has_failed = false, + Err(e) => { + process_error(Box::new(e), "elasticsearch", &mut has_failed); + continue; + }, + } + } +} + +fn process_error(e: Box, system: &str, has_failed: &mut bool) { + if !*has_failed { + *has_failed = true; + log::error!("error in {}: {}", system, e); } } diff --git a/src/nut/nut_client.rs b/src/nut/nut_client.rs index 0959ab6..995a31b 100644 --- a/src/nut/nut_client.rs +++ b/src/nut/nut_client.rs @@ -3,7 +3,6 @@ use nut_client::ConfigBuilder; use serde_json::{json, Value}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use crate::nut::nut_data::Data; pub struct NutClient { conection: Connection, @@ -63,21 +62,3 @@ impl NutClient { ) } } - -fn insert(mut header: String, value: String, data: &mut Data) { - for key in header.split(".") { - - } - process(header.split_off(header.find(".").unwrap()+1), value, None, data); -} - -fn process(mut header: String, value: String, pre: Option, data: &mut Data) { - match header.find(".") { - Some(p) => { - process(header.split_off(p+1), value, Some(header), data); - }, - None => { - - } - } -} diff --git a/src/nut/nut_data.rs b/src/nut/nut_data.rs index 1772fd3..2ce754f 100644 --- a/src/nut/nut_data.rs +++ b/src/nut/nut_data.rs @@ -18,24 +18,6 @@ pub struct Batery { voltage: Option, } -impl Batery { - pub fn new() -> Self { - Self { - charge: None, - mrf_date: None, - runtime: None, - btype: None, - voltage: None - } - } - - /*pub fn add(&self, data: Vec) { - match data.remove(0) { - "charge": - } - }*/ -} - #[derive(Serialize, Deserialize)] pub struct Charge { value: Option,