From c056e50646b45070c330de8a6dbd14042b3455e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Sat, 6 Apr 2024 00:26:41 +0200 Subject: [PATCH] feat: added query function in aw-client (#477) * feat: added query function in aw-client * fix: fixed client query * fix: improved return type for client query --- Cargo.lock | 6 +++--- aw-client-rust/src/blocking.rs | 6 ++++++ aw-client-rust/src/lib.rs | 29 ++++++++++++++++++++++++++++- aw-client-rust/tests/test.rs | 16 ++++++++++++++++ 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25dc2672..79203ca9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,9 +461,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.31" +version = "0.4.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e" dependencies = [ "android-tzdata", "iana-time-zone", @@ -471,7 +471,7 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-targets 0.48.5", + "windows-targets 0.52.0", ] [[package]] diff --git a/aw-client-rust/src/blocking.rs b/aw-client-rust/src/blocking.rs index 3acd8eb9..7d6b6a97 100644 --- a/aw-client-rust/src/blocking.rs +++ b/aw-client-rust/src/blocking.rs @@ -62,6 +62,12 @@ impl AwClient { stop: Option>, limit: Option ); + proxy_method!( + query, + Vec, + query: &str, + timeperiods: Vec<(DateTime, DateTime)> + ); proxy_method!(insert_event, (), bucketname: &str, event: &Event); proxy_method!(insert_events, (), bucketname: &str, events: Vec); proxy_method!( diff --git a/aw-client-rust/src/lib.rs b/aw-client-rust/src/lib.rs index 18fad49e..ed00606d 100644 --- a/aw-client-rust/src/lib.rs +++ b/aw-client-rust/src/lib.rs @@ -11,7 +11,7 @@ use std::vec::Vec; use std::{collections::HashMap, error::Error}; use chrono::{DateTime, Utc}; -use serde_json::Map; +use serde_json::{json, Map}; pub use aw_models::{Bucket, BucketMetadata, Event}; @@ -98,6 +98,33 @@ impl AwClient { Ok(()) } + pub async fn query( + &self, + query: &str, + timeperiods: Vec<(DateTime, DateTime)>, + ) -> Result, reqwest::Error> { + let url = reqwest::Url::parse(format!("{}/api/0/query", self.baseurl).as_str()).unwrap(); + + // Format timeperiods as ISO8601 strings, separated by / + let timeperiods_str: Vec = timeperiods + .iter() + .map(|(start, stop)| (start.to_rfc3339(), stop.to_rfc3339())) + .map(|(start, stop)| format!("{}/{}", start, stop)) + .collect(); + + // Result is a sequence, one element per timeperiod + self.client + .post(url) + .json(&json!({ + "query": query.split('\n').collect::>(), + "timeperiods": timeperiods_str, + })) + .send() + .await? + .json() + .await + } + pub async fn get_events( &self, bucketname: &str, diff --git a/aw-client-rust/tests/test.rs b/aw-client-rust/tests/test.rs index cce54a5f..d518aa0a 100644 --- a/aw-client-rust/tests/test.rs +++ b/aw-client-rust/tests/test.rs @@ -110,6 +110,22 @@ mod test { println!("Events: {events:?}"); assert!(events[0].duration == Duration::seconds(1)); + // Query + let query = format!( + "events = query_bucket(\"{}\"); +RETURN = events;", + bucket.id + ); + let start: DateTime = DateTime::parse_from_rfc3339("1996-12-19T00:00:00-08:00") + .unwrap() + .into(); + let end: DateTime = DateTime::parse_from_rfc3339("2020-12-19T00:00:00-08:00") + .unwrap() + .into(); + let timeperiods = (start, end); + let query_result = client.query(&query, vec![timeperiods]).unwrap(); + println!("Query result: {query_result:?}"); + client .delete_event(&bucketname, events[0].id.unwrap()) .unwrap();