Skip to content

Commit

Permalink
Use &str instead of &String
Browse files Browse the repository at this point in the history
  • Loading branch information
sophie-cluml committed Dec 2, 2024
1 parent 4cbc391 commit 90fa862
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 109 deletions.
35 changes: 12 additions & 23 deletions src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ pub fn get_timestamp_from_key(key: &[u8]) -> Result<DateTime<Utc>, anyhow::Error
fn get_peekable_iter<'c, T>(
store: &RawEventStore<'c, T>,
filter: &'c NetworkFilter,
after: Option<&String>,
before: Option<&String>,
after: Option<&str>,
before: Option<&str>,
first: Option<usize>,
last: Option<usize>,
) -> Result<(std::iter::Peekable<FilteredIter<'c, T>>, usize)>
Expand All @@ -472,8 +472,8 @@ where
fn get_filtered_iter<'c, T>(
store: &RawEventStore<'c, T>,
filter: &'c NetworkFilter,
after: Option<&String>,
before: Option<&String>,
after: Option<&str>,
before: Option<&str>,
first: Option<usize>,
last: Option<usize>,
) -> Result<(FilteredIter<'c, T>, Option<Vec<u8>>, usize)>
Expand Down Expand Up @@ -660,17 +660,17 @@ fn check_port(filter_port: Option<&PortRange>, target_port: Option<u16>) -> bool
true
}

fn check_contents(filter_str: Option<&String>, target_str: Option<String>) -> bool {
fn check_contents(filter_str: Option<&str>, target_str: Option<String>) -> bool {
filter_str.as_ref().map_or(true, |filter_str| {
target_str.map_or(false, |contents| contents.contains(*filter_str))

Check warning on line 665 in src/graphql.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql.rs#L665

Added line #L665 was not covered by tests
})
}

fn check_agent_id(filter_agent_id: Option<&String>, target_agent_id: Option<&String>) -> bool {
fn check_agent_id(filter_agent_id: Option<&str>, target_agent_id: Option<&str>) -> bool {
filter_by_str(filter_agent_id, target_agent_id)
}

fn filter_by_str(filter_str: Option<&String>, target_str: Option<&String>) -> bool {
fn filter_by_str(filter_str: Option<&str>, target_str: Option<&str>) -> bool {
filter_str.as_ref().map_or(true, |filter_id| {
target_str
.as_ref()
Expand Down Expand Up @@ -1165,7 +1165,7 @@ pub(crate) use paged_events_in_cluster;
fn combine_results<N>(
current_giganto_result: Connection<String, N>,
peer_results: Vec<Connection<String, N>>,
before: Option<&String>,
before: Option<&str>,

Check warning on line 1168 in src/graphql.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql.rs#L1168

Added line #L1168 was not covered by tests
first: Option<i32>,
last: Option<i32>,
) -> Connection<String, N>
Expand Down Expand Up @@ -1208,7 +1208,7 @@ enum TakeDirection {
#[allow(unused)]
fn sort_and_trunk_edges<N>(
mut edges: Vec<Edge<String, N, EmptyFields>>,
before: Option<&String>,
before: Option<&str>,
first: Option<i32>,
last: Option<i32>,
) -> Vec<Edge<String, N, EmptyFields>>
Expand Down Expand Up @@ -1785,30 +1785,19 @@ mod tests {
assert_eq!(result[0].cursor, "danger_001".to_string());
assert_eq!(result[result.len() - 1].cursor, "warn_001".to_string());

let result =
sort_and_trunk_edges(edges_fixture(), Some("zebra_001"), None, None);
let result = sort_and_trunk_edges(edges_fixture(), Some("zebra_001"), None, None);
assert_eq!(result.len(), 6);
assert!(result.windows(2).all(|w| w[0].cursor < w[1].cursor));
assert_eq!(result[0].cursor, "danger_001".to_string());
assert_eq!(result[result.len() - 1].cursor, "warn_001".to_string());

let result = sort_and_trunk_edges(
edges_fixture(),
Some("zebra_001".to_string()),
None,
Some(5),
);
let result = sort_and_trunk_edges(edges_fixture(), Some("zebra_001"), None, Some(5));
assert_eq!(result.len(), 5);
assert!(result.windows(2).all(|w| w[0].cursor < w[1].cursor));
assert_eq!(result[0].cursor, "danger_002".to_string());
assert_eq!(result[result.len() - 1].cursor, "warn_001".to_string());

let result = sort_and_trunk_edges(
edges_fixture(),
Some("zebra_001".to_string()),
None,
Some(10),
);
let result = sort_and_trunk_edges(edges_fixture(), Some("zebra_001"), None, Some(10));
assert_eq!(result.len(), 6);
assert!(result.windows(2).all(|w| w[0].cursor < w[1].cursor));
assert_eq!(result[0].cursor, "danger_001".to_string());
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,7 +1644,7 @@ impl RawEventFilter for ExportFilter {
&& check_address(self.resp_addr.as_ref(), resp_addr)?
&& check_port(self.orig_port.as_ref(), orig_port)
&& check_port(self.resp_port.as_ref(), resp_port)
&& check_agent_id(self.agent_id.as_ref(), agent_id.as_ref())
&& check_agent_id(self.agent_id.as_deref(), agent_id.as_deref())

Check warning on line 1647 in src/graphql/export.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/export.rs#L1643-L1647

Added lines #L1643 - L1647 were not covered by tests
{
return Ok(true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/netflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl RawEventFilter for NetflowFilter {
&& check_address(self.resp_addr.as_ref(), resp_addr)?
&& check_port(self.orig_port.as_ref(), orig_port)
&& check_port(self.resp_port.as_ref(), resp_port)
&& check_contents(self.contents.as_ref(), log_contents)
&& check_contents(self.contents.as_deref(), log_contents)

Check warning on line 86 in src/graphql/netflow.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/netflow.rs#L82-L86

Added lines #L82 - L86 were not covered by tests
{
return Ok(true);
}
Expand Down
207 changes: 155 additions & 52 deletions src/graphql/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl RawEventFilter for NetworkFilter {
&& check_address(self.resp_addr.as_ref(), resp_addr)?
&& check_port(self.orig_port.as_ref(), orig_port)
&& check_port(self.resp_port.as_ref(), resp_port)
&& check_agent_id(self.agent_id.as_ref(), agent_id.as_ref())
&& check_agent_id(self.agent_id.as_deref(), agent_id.as_deref())
{
return Ok(true);
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl RawEventFilter for SearchFilter {
&& check_address(self.resp_addr.as_ref(), resp_addr)?
&& check_port(self.orig_port.as_ref(), orig_port)
&& check_port(self.resp_port.as_ref(), resp_port)
&& check_agent_id(self.agent_id.as_ref(), agent_id.as_ref())
&& check_agent_id(self.agent_id.as_deref(), agent_id.as_deref())
{
return Ok(true);
}
Expand Down Expand Up @@ -1171,6 +1171,7 @@ async fn handle_paged_dhcp_raw_events<'ctx>(
handle_paged_events(store, filter, after, before, first, last).await
}

#[allow(clippy::too_many_lines)]
async fn handle_network_raw_events<'ctx>(
ctx: &Context<'ctx>,
filter: NetworkFilter,
Expand All @@ -1186,56 +1187,158 @@ async fn handle_network_raw_events<'ctx>(
first,
last,
|after, before, first, last| async move {
let (conn_iter, size) =
get_peekable_iter(&db.conn_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (dns_iter, _) =
get_peekable_iter(&db.dns_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (http_iter, _) =
get_peekable_iter(&db.http_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (rdp_iter, _) =
get_peekable_iter(&db.rdp_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (ntlm_iter, _) =
get_peekable_iter(&db.ntlm_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (kerberos_iter, _) =
get_peekable_iter(&db.kerberos_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (ssh_iter, _) =
get_peekable_iter(&db.ssh_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (dce_rpc_iter, _) =
get_peekable_iter(&db.dce_rpc_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (ftp_iter, _) =
get_peekable_iter(&db.ftp_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (mqtt_iter, _) =
get_peekable_iter(&db.mqtt_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (ldap_iter, _) =
get_peekable_iter(&db.ldap_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (tls_iter, _) =
get_peekable_iter(&db.tls_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (smb_iter, _) =
get_peekable_iter(&db.smb_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (nfs_iter, _) =
get_peekable_iter(&db.nfs_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (smtp_iter, _) =
get_peekable_iter(&db.smtp_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (bootp_iter, _) =
get_peekable_iter(&db.bootp_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;

let (dhcp_iter, _) =
get_peekable_iter(&db.dhcp_store()?, &filter, after.as_ref(), before.as_ref(), first, last)?;
let (conn_iter, size) = get_peekable_iter(
&db.conn_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1197 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1197

Added line #L1197 was not covered by tests

let (dns_iter, _) = get_peekable_iter(
&db.dns_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1206 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1206

Added line #L1206 was not covered by tests

let (http_iter, _) = get_peekable_iter(
&db.http_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1215 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1215

Added line #L1215 was not covered by tests

let (rdp_iter, _) = get_peekable_iter(
&db.rdp_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1224 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1224

Added line #L1224 was not covered by tests

let (ntlm_iter, _) = get_peekable_iter(
&db.ntlm_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1233 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1233

Added line #L1233 was not covered by tests

let (kerberos_iter, _) = get_peekable_iter(
&db.kerberos_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1242 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1242

Added line #L1242 was not covered by tests

let (ssh_iter, _) = get_peekable_iter(
&db.ssh_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1251 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1251

Added line #L1251 was not covered by tests

let (dce_rpc_iter, _) = get_peekable_iter(
&db.dce_rpc_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1260 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1260

Added line #L1260 was not covered by tests

let (ftp_iter, _) = get_peekable_iter(
&db.ftp_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1269 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1269

Added line #L1269 was not covered by tests

let (mqtt_iter, _) = get_peekable_iter(
&db.mqtt_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1278 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1278

Added line #L1278 was not covered by tests

let (ldap_iter, _) = get_peekable_iter(
&db.ldap_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1287 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1287

Added line #L1287 was not covered by tests

let (tls_iter, _) = get_peekable_iter(
&db.tls_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1296 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1296

Added line #L1296 was not covered by tests

let (smb_iter, _) = get_peekable_iter(
&db.smb_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1305 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1305

Added line #L1305 was not covered by tests

let (nfs_iter, _) = get_peekable_iter(
&db.nfs_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1314 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1314

Added line #L1314 was not covered by tests

let (smtp_iter, _) = get_peekable_iter(
&db.smtp_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1323 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1323

Added line #L1323 was not covered by tests

let (bootp_iter, _) = get_peekable_iter(
&db.bootp_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1332 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1332

Added line #L1332 was not covered by tests

let (dhcp_iter, _) = get_peekable_iter(
&db.dhcp_store()?,
&filter,
after.as_deref(),
before.as_deref(),
first,
last,
)?;

Check warning on line 1341 in src/graphql/network.rs

View check run for this annotation

Codecov / codecov/patch

src/graphql/network.rs#L1341

Added line #L1341 was not covered by tests

let mut is_forward: bool = true;
if before.is_some() || last.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl RawEventFilter for SecuLogFilter {
&& check_address(self.resp_addr.as_ref(), resp_addr)?
&& check_port(self.orig_port.as_ref(), orig_port)
&& check_port(self.resp_port.as_ref(), resp_port)
&& check_contents(self.log.as_ref(), log_contents)
&& check_contents(self.log.as_deref(), log_contents)
{
return Ok(true);
}
Expand Down
Loading

0 comments on commit 90fa862

Please sign in to comment.