Skip to content

Commit

Permalink
feat: added query function in aw-client (#477)
Browse files Browse the repository at this point in the history
* feat: added query function in aw-client

* fix: fixed client query

* fix: improved return type for client query
  • Loading branch information
ErikBjare committed Apr 5, 2024
1 parent 1886ebe commit c056e50
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions aw-client-rust/src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl AwClient {
stop: Option<DateTime<Utc>>,
limit: Option<u64>
);
proxy_method!(
query,
Vec<serde_json::Value>,
query: &str,
timeperiods: Vec<(DateTime<Utc>, DateTime<Utc>)>
);
proxy_method!(insert_event, (), bucketname: &str, event: &Event);
proxy_method!(insert_events, (), bucketname: &str, events: Vec<Event>);
proxy_method!(
Expand Down
29 changes: 28 additions & 1 deletion aw-client-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -98,6 +98,33 @@ impl AwClient {
Ok(())
}

pub async fn query(
&self,
query: &str,
timeperiods: Vec<(DateTime<Utc>, DateTime<Utc>)>,
) -> Result<Vec<serde_json::Value>, 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<String> = 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::<Vec<&str>>(),
"timeperiods": timeperiods_str,
}))
.send()
.await?
.json()
.await
}

pub async fn get_events(
&self,
bucketname: &str,
Expand Down
16 changes: 16 additions & 0 deletions aw-client-rust/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utc> = DateTime::parse_from_rfc3339("1996-12-19T00:00:00-08:00")
.unwrap()
.into();
let end: DateTime<Utc> = 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();
Expand Down

0 comments on commit c056e50

Please sign in to comment.