ts some minor update HOPEFULLY wont make it run 16k times worse HOPEFULLY
This commit is contained in:
+20
-37
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use anyhow::Result;
|
||||
use wakey_core::{
|
||||
Device, DeviceInventory, DhcpLease, DhcpLeaseWithState, InventoryQuery, NeighborEntry,
|
||||
@@ -59,48 +61,29 @@ pub fn merge_devices(
|
||||
.map(|(neighbors, leases)| Device::from_parts(neighbors, leases))
|
||||
.collect();
|
||||
|
||||
let mut texts: Vec<&str> = Vec::new();
|
||||
let mut devs: Vec<&str> = Vec::new();
|
||||
let mut ips = Vec::new();
|
||||
let mut macs = Vec::new();
|
||||
let mut nuds = Vec::new();
|
||||
let mut texts = HashSet::new();
|
||||
let mut devs = HashSet::new();
|
||||
let mut ips = HashSet::new();
|
||||
let mut macs = HashSet::new();
|
||||
let mut nuds = HashSet::new();
|
||||
|
||||
for term in query {
|
||||
match term {
|
||||
Query::Text(v) => texts.push(v.as_str()),
|
||||
Query::Interface(v) => devs.push(v.as_str()),
|
||||
Query::Ip(v) => ips.push(*v),
|
||||
Query::Mac(v) => macs.push(*v),
|
||||
Query::NeighborState(v) => nuds.push(*v),
|
||||
}
|
||||
}
|
||||
|
||||
if !texts.is_empty() {
|
||||
devices.retain(|device| device.names.iter().any(|n| texts.iter().any(|t| n == t)));
|
||||
}
|
||||
if !devs.is_empty() {
|
||||
devices.retain(|device| {
|
||||
device
|
||||
.interfaces
|
||||
.iter()
|
||||
.any(|iface| devs.iter().any(|d| iface == d))
|
||||
});
|
||||
}
|
||||
if !ips.is_empty() {
|
||||
devices.retain(|device| device.ips.iter().any(|ip| ips.contains(ip)));
|
||||
}
|
||||
if !macs.is_empty() {
|
||||
devices.retain(|device| device.macs.iter().any(|mac| macs.contains(mac)));
|
||||
}
|
||||
if !nuds.is_empty() {
|
||||
devices.retain(|device| {
|
||||
device
|
||||
.neighbors
|
||||
.iter()
|
||||
.any(|neighbor| nuds.contains(&neighbor.state))
|
||||
});
|
||||
Query::Text(v) => texts.insert(v.as_str()),
|
||||
Query::Interface(v) => devs.insert(v.as_str()),
|
||||
Query::Ip(v) => ips.insert(*v),
|
||||
Query::Mac(v) => macs.insert(*v),
|
||||
Query::NeighborState(v) => nuds.insert(*v),
|
||||
};
|
||||
}
|
||||
|
||||
devices.retain(|device| {
|
||||
(texts.is_empty() || device.names.iter().any(|n| texts.contains(n.as_str())))
|
||||
&& (devs.is_empty() || device.interfaces.iter().any(|i| devs.contains(i.as_str())))
|
||||
&& (ips.is_empty() || device.ips.iter().any(|ip| ips.contains(ip)))
|
||||
&& (macs.is_empty() || device.macs.iter().any(|mac| macs.contains(mac)))
|
||||
&& (nuds.is_empty() || device.neighbors.iter().any(|n| nuds.contains(&n.state)))
|
||||
});
|
||||
devices.sort_by(|a, b| {
|
||||
presence_rank(b.presence)
|
||||
.cmp(&presence_rank(a.presence))
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ pub mod wake;
|
||||
pub use interfaces::{get_interface_summaries, get_interface_summary, get_ips};
|
||||
pub use inventory::{inventory, merge_devices, resolve_devices};
|
||||
pub use leases::{get_leases, leases_without_state};
|
||||
pub use query::{query_to_inventory_query, resolve_query, resolve_selector};
|
||||
pub use query::{resolve_query, resolve_selector};
|
||||
pub use wake::{
|
||||
broadcast_wake_targets, resolve_wake_targets, wake_explicit, wake_from_query, wake_targets,
|
||||
};
|
||||
|
||||
+1
-12
@@ -2,11 +2,8 @@ use anyhow::Result;
|
||||
use wakey_core::{InventoryQuery, Query, QueryInput};
|
||||
|
||||
/// Resolve free-form user input into an `InventoryQuery` filter shape.
|
||||
///
|
||||
/// This is the compatibility entrypoint used by CLI and HTTP paths that still
|
||||
/// speak in terms of query/filter payloads.
|
||||
pub async fn resolve_query(input: impl Into<String>) -> Result<InventoryQuery> {
|
||||
query_to_inventory_query(resolve_selector(input).await?)
|
||||
Ok(vec![resolve_selector(input).await?])
|
||||
}
|
||||
|
||||
/// Classify one piece of free-form user input into a typed selector.
|
||||
@@ -24,11 +21,3 @@ pub async fn resolve_selector(input: impl Into<String>) -> Result<Query> {
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Convert the newer selector-oriented `Query` model into an `InventoryQuery`.
|
||||
///
|
||||
/// This keeps the old filter-based service and HTTP surfaces working while the
|
||||
/// internals migrate toward selector- and device-oriented APIs.
|
||||
pub fn query_to_inventory_query(query: Query) -> Result<InventoryQuery> {
|
||||
Ok(vec![query])
|
||||
}
|
||||
|
||||
+9
-7
@@ -6,6 +6,7 @@ use wakey_core::{InterfaceSummary, WakeResult, WakeTarget};
|
||||
|
||||
use crate::service::interfaces::get_interface_summaries;
|
||||
use crate::service::inventory::resolve_devices;
|
||||
use crate::utils::product;
|
||||
|
||||
/// Send Wake-on-LAN packets for already-concrete wake targets.
|
||||
#[instrument(skip_all, fields(targets = targets.len()))]
|
||||
@@ -50,14 +51,15 @@ pub async fn wake_explicit(mac: MacAddr, ip: Option<IpAddr>) -> Result<WakeResul
|
||||
#[instrument(skip_all)]
|
||||
pub async fn resolve_wake_targets(input: impl Into<String>) -> Result<Vec<WakeTarget>> {
|
||||
let devices = resolve_devices(input).await?;
|
||||
|
||||
let targets: Vec<WakeTarget> = devices
|
||||
.into_iter()
|
||||
.flat_map(|device| {
|
||||
let mac = device.macs.first().copied();
|
||||
device
|
||||
.ips
|
||||
.into_iter()
|
||||
.map(move |ip| WakeTarget { ip: Some(ip), mac })
|
||||
// TRY EVERYTHING some has to work
|
||||
product(device.ips, device.macs).map(|(i, m)| WakeTarget {
|
||||
ip: Some(i),
|
||||
mac: Some(m),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
debug!(targets = targets.len(), "resolved wake targets");
|
||||
@@ -79,8 +81,8 @@ fn broadcast_wake_targets_from_interfaces(
|
||||
.iter()
|
||||
.flat_map(|iface| iface.addrs.iter())
|
||||
.filter_map(|addr| addr.broadcast)
|
||||
.map(|ip| WakeTarget {
|
||||
ip: Some(IpAddr::V4(ip)),
|
||||
.map(|br| WakeTarget {
|
||||
ip: Some(IpAddr::V4(br)), // some will hit eventually AHHH logic
|
||||
mac: Some(mac),
|
||||
})
|
||||
.collect();
|
||||
|
||||
Reference in New Issue
Block a user