Skip to content

Commit 7ad9512

Browse files
committed
feat: added hostname filtering support to find_bucket
1 parent 4a1943a commit 7ad9512

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

aw-query/src/functions.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ mod qfunctions {
138138
) -> Result<DataType, QueryError> {
139139
// Typecheck
140140
validate::args_length(&args, 1)?;
141+
141142
let bucket_id: String = (&args[0]).try_into()?;
142143
let interval = validate::get_timeinterval(env)?;
143144

@@ -189,8 +190,14 @@ mod qfunctions {
189190
_env: &VarEnv,
190191
ds: &Datastore,
191192
) -> Result<DataType, QueryError> {
192-
validate::args_length(&args, 1)?;
193+
validate::args_length(&args, 1).or_else(|_| validate::args_length(&args, 2))?;
194+
193195
let bucket_filter: String = (&args[0]).try_into()?;
196+
let hostname_filter: Option<String> = match args.len() {
197+
2 => Some((&args[1]).try_into()?),
198+
_ => None,
199+
};
200+
194201
let buckets = match ds.get_buckets() {
195202
Ok(buckets) => buckets,
196203
Err(e) => {
@@ -200,15 +207,16 @@ mod qfunctions {
200207
)))
201208
}
202209
};
203-
let bucketname = match aw_transform::find_bucket(&bucket_filter, buckets.keys()) {
204-
Some(bucketname) => bucketname,
205-
None => {
206-
return Err(QueryError::BucketQueryError(format!(
207-
"Couldn't find any bucket which starts with {}",
208-
bucket_filter
209-
)));
210-
}
211-
};
210+
let bucketname =
211+
match aw_transform::find_bucket(&bucket_filter, &hostname_filter, buckets.values()) {
212+
Some(bucketname) => bucketname,
213+
None => {
214+
return Err(QueryError::BucketQueryError(format!(
215+
"Couldn't find any bucket which starts with {}",
216+
bucket_filter
217+
)));
218+
}
219+
};
212220
Ok(DataType::String(bucketname))
213221
}
214222

aw-query/tests/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ mod query_tests {
351351

352352
let code = format!(
353353
r#"
354-
events = query_bucket(find_bucket("{}"));
354+
events = query_bucket(find_bucket("{}", "testhost"));
355355
events = flood(events);
356356
events = sort_by_duration(events);
357357
events = limit_events(events, 10000);

aw-transform/src/find_bucket.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
/// Finds the first bucket which starts with the specified string
1+
use aw_models::Bucket;
2+
3+
/// Finds the first bucket which starts with the specified string, and optionally matches a
4+
/// hostname.
25
pub fn find_bucket<'a>(
36
bucket_filter: &str,
4-
bucketnames: impl IntoIterator<Item = &'a String>,
7+
hostname_filter: &Option<String>,
8+
buckets: impl IntoIterator<Item = &'a Bucket>,
59
) -> Option<String> {
6-
for bucketname in bucketnames {
7-
if bucketname.starts_with(bucket_filter) {
8-
return Some(bucketname.to_string());
10+
for bucket in buckets {
11+
if bucket.id.starts_with(bucket_filter) {
12+
if let Some(hostname) = hostname_filter {
13+
if hostname == &bucket.hostname {
14+
return Some(bucket.id.to_string());
15+
}
16+
} else {
17+
return Some(bucket.id.to_string());
18+
}
919
}
1020
}
1121
None
@@ -14,20 +24,46 @@ pub fn find_bucket<'a>(
1424
#[cfg(test)]
1525
mod tests {
1626
use super::find_bucket;
27+
use aw_models::Bucket;
28+
use aw_models::BucketMetadata;
1729

1830
#[test]
1931
fn test_find_bucket() {
2032
let expected_bucketname = "aw-datastore-test_test-host".to_string();
21-
let bucketnames = vec![
22-
"no match".to_string(),
23-
expected_bucketname.clone(),
24-
"no match 2".to_string(),
25-
];
26-
let res = find_bucket("aw-datastore-test", &bucketnames);
33+
let expected_hostname = "testhost".to_string();
34+
let b1 = Bucket {
35+
bid: None,
36+
id: "no match".to_string(),
37+
_type: "type".to_string(),
38+
hostname: expected_hostname.clone(),
39+
client: "testclient".to_string(),
40+
created: None,
41+
data: json_map! {},
42+
metadata: BucketMetadata::default(),
43+
events: None,
44+
last_updated: None,
45+
};
46+
let mut b2 = b1.clone();
47+
b2.id = expected_bucketname.clone();
48+
let b3 = b1.clone();
49+
50+
let buckets = vec![b1.clone(), b2.clone(), b3.clone()];
51+
52+
// Check that it correctly finds bucket
53+
let res = find_bucket("aw-datastore-test", &Some("testhost".to_string()), &buckets);
2754
assert_eq!(res, Some(expected_bucketname));
2855

29-
let bucketnames = vec!["no match".to_string(), "no match 2".to_string()];
30-
let res = find_bucket("aw-datastore-test", &bucketnames);
56+
// Check that it doesn't find a bucket for an unavailable hostname
57+
let res = find_bucket(
58+
"aw-datastore-test",
59+
&Some("unavailablehost".to_string()),
60+
&buckets,
61+
);
62+
assert_eq!(res, None);
63+
64+
// Check that it doesn't find a bucket for any hostname
65+
let buckets = vec![b1, b3];
66+
let res = find_bucket("aw-datastore-test", &None, &buckets);
3167
assert_eq!(res, None);
3268
}
3369
}

0 commit comments

Comments
 (0)