From 72c59dbce9aab10d54eb5b99e35115dc0e859c5d Mon Sep 17 00:00:00 2001 From: prplV Date: Thu, 23 Jan 2025 19:40:38 +0300 Subject: [PATCH] v2 completed, getting + preproc --- crates/api-grub/src/net.rs | 10 +++++++--- crates/integr-structs/Cargo.toml | 1 + crates/integr-structs/src/api.rs | 33 ++++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/crates/api-grub/src/net.rs b/crates/api-grub/src/net.rs index e6a59d6..ba587cb 100644 --- a/crates/api-grub/src/net.rs +++ b/crates/api-grub/src/net.rs @@ -1,6 +1,6 @@ // module to handle unix-socket connection + pulling info from api use anyhow::{Error, Result}; -use integr_structs::api::ApiConfigV2; +use integr_structs::api::{ApiConfigV2, ProcessedEndpoint}; use log::{error, info}; use tokio::sync::mpsc::Receiver; use tokio::time::{sleep, Duration}; @@ -9,7 +9,7 @@ use std::sync::Arc; use tokio::task::JoinHandle; use tokio::sync::Mutex; -type BufferType = Arc>>; +type BufferType = Arc>>; struct RestMethod; impl RestMethod { @@ -68,6 +68,10 @@ impl<'a> ApiPoll<'a> { return; } if let Ok(text) = resp.text().await { + // + let a = ProcessedEndpoint::from_target_response(&text, &point); + println!("{}", a.unwrap()); + // let mut buffer = buffer.lock().await; buffer.push(text); } else { @@ -149,7 +153,7 @@ mod net_unittests { #[test] async fn check_api_poll_is_default() { let mut conf1 = ApiConfigV2::default(); - let mut poll = ApiPoll::new(&mut conf1).await; + let poll = ApiPoll::new(&mut conf1).await; assert!(poll.is_default().await) } diff --git a/crates/integr-structs/Cargo.toml b/crates/integr-structs/Cargo.toml index fdf9743..9183deb 100644 --- a/crates/integr-structs/Cargo.toml +++ b/crates/integr-structs/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] +anyhow = "1.0.95" serde = { version = "1.0.217", features = ["derive"] } serde_json = "1.0.135" diff --git a/crates/integr-structs/src/api.rs b/crates/integr-structs/src/api.rs index 4365f3f..de77a5e 100644 --- a/crates/integr-structs/src/api.rs +++ b/crates/integr-structs/src/api.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; - use serde::{Serialize, Deserialize}; -use serde_json::Value; +use serde_json::{ to_string_pretty, Value }; +use anyhow::Result; #[derive(Serialize, Deserialize, Debug)] @@ -90,7 +90,7 @@ impl ApiConfigV2 { } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Template { pub id : String, pub name : String, @@ -117,6 +117,31 @@ pub struct ProcessedEndpoint { id : String, name : String, url : String, + method : String, #[serde(default)] - metrics : HashMap + metrics : HashMap, +} + +impl ProcessedEndpoint { + pub fn new(id: &str, name: &str, url: &str, method: &str, metrics: HashMap) -> Self { + ProcessedEndpoint { + id : id.to_owned(), + name : name.to_owned(), + url : url.to_owned(), + method : method.to_owned(), + metrics : metrics, + } + } + pub fn from_target_response(response: &str, keys: &Template) -> Result { + let mut hm: HashMap = HashMap::new(); + let mut response: Value = serde_json::from_str(response)?; + + let _ = keys.measure.iter() + .map(|key| (key, response[key].take())) + .for_each(|(key, value)| { + hm.insert(key.clone(), value); + }); + let val = ProcessedEndpoint::new(&keys.id, &keys.name, &keys.url, &keys.method,hm); + Ok(to_string_pretty(&val)?) + } } \ No newline at end of file