Clarify blocking terms

This commit is contained in:
gil 2024-06-03 01:24:43 -05:00
parent 4e22338181
commit 51db6ea06e
8 changed files with 41 additions and 41 deletions

View file

@ -7,7 +7,7 @@ use glob::glob;
use manip::{Limit, LimitList, MergedLimitList}; use manip::{Limit, LimitList, MergedLimitList};
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
env_logger::init(); // TODO add more logging env_logger::init(); // TODO add more logging & actual error handling
// TODO utilize CLI // TODO utilize CLI
// let _cli = cli::Cli::parse(); // let _cli = cli::Cli::parse();
@ -16,35 +16,35 @@ fn main() -> std::io::Result<()> {
let mut merged_list = MergedLimitList::new(); let mut merged_list = MergedLimitList::new();
// Crawl /blocks for block lists -> glob pattern: "blocks/**/*.txt" // Crawl /suspend for suspend lists -> glob pattern: "suspend/**/*.txt"
{ {
let mut blocklists = vec![]; let mut suspend_lists = vec![];
let block_paths = glob("blocks/**/*.txt").expect("Bad glob pattern"); let suspend_paths = glob("suspend/**/*.txt").expect("Bad glob pattern");
for path in block_paths.filter_map(|x| x.ok()) { for path in suspend_paths.filter_map(|x| x.ok()) {
blocklists.extend(LimitList::from_file(path)); suspend_lists.extend(LimitList::from_file(path));
// TODO Check if path contains trust value as part of filename when building limitlist // TODO Check if path contains trust value as part of filename when building limitlist
} }
for list in blocklists { for list in suspend_lists {
merged_list.add_limit_list(list, Limit::Block); merged_list.add_limit_list(list, Limit::Suspend);
} }
} }
// Crawl /silences for silence lists -> glob pattern: "mutes/**/*.txt" // Crawl /silence for silence lists -> glob pattern: "silence/**/*.txt"
{ {
let mut mutelists = vec![]; let mut silence_lists = vec![];
let mute_paths = glob("mutes/**/*.txt").expect("Bad glob pattern"); let silence_paths = glob("silence/**/*.txt").expect("Bad glob pattern");
for path in mute_paths.filter_map(|x| x.ok()) { for path in silence_paths.filter_map(|x| x.ok()) {
mutelists.extend(LimitList::from_file(path)); silence_lists.extend(LimitList::from_file(path));
// TODO Check if path contains trust value as part of filename when building limitlist // TODO Check if path contains trust value as part of filename when building limitlist
} }
for list in mutelists { for list in silence_lists {
merged_list.add_limit_list(list, Limit::Silence); merged_list.add_limit_list(list, Limit::Silence);
} }
} }
merged_list.export_file("block.txt", "mute.txt", (50, 33)) merged_list.export_file("suspend.txt", "silence.txt", (50, 33))
} }

View file

@ -9,7 +9,7 @@ use log::error;
/// Specifies a limit type, whether block or silence /// Specifies a limit type, whether block or silence
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Limit { pub enum Limit {
Block, Suspend,
Silence, Silence,
} }
@ -18,24 +18,24 @@ pub enum Limit {
/// Wraps a host's weighted limits /// Wraps a host's weighted limits
#[derive(Debug, Default, Eq, PartialEq)] #[derive(Debug, Default, Eq, PartialEq)]
pub struct LimitIndices { pub struct LimitIndices {
pub block: u16, pub suspend: u16,
pub silence: u16, pub silence: u16,
} }
impl LimitIndices { impl LimitIndices {
pub fn add_limit(&mut self, limit: Limit, weight: u16) -> &mut Self { pub fn add_limit(&mut self, limit: Limit, weight: u16) -> &mut Self {
match limit { match limit {
Limit::Block => self.block += weight, Limit::Suspend => self.suspend += weight,
Limit::Silence => self.silence += weight, Limit::Silence => self.silence += weight,
} }
self self
} }
pub fn normalize(&self, max: (u16, u16)) -> (u16, u16) { pub fn normalize(&self, max: (u16, u16)) -> (u16, u16) {
let block = (self.block as f32) / (max.0 as f32) * 100f32; let suspend = (self.suspend as f32) / (max.0 as f32) * 100f32;
let mute = (self.silence as f32) / (max.1 as f32) * 100f32; let silence = (self.silence as f32) / (max.1 as f32) * 100f32;
(block as u16, mute as u16) (suspend as u16, silence as u16)
} }
} }
@ -44,7 +44,7 @@ impl From<(u16, u16)> for LimitIndices {
/// testing. /// testing.
fn from(value: (u16, u16)) -> Self { fn from(value: (u16, u16)) -> Self {
Self { Self {
block: value.0, suspend: value.0,
silence: value.1, silence: value.1,
} }
} }
@ -77,26 +77,26 @@ impl LimitList {
/// A map of hosts (as strings) to their limit weights /// A map of hosts (as strings) to their limit weights
#[derive(Debug, Default, Eq, PartialEq)] #[derive(Debug, Default, Eq, PartialEq)]
pub struct MergedLimitList { pub struct MergedLimitList {
pub hostmap: HashMap<String, LimitIndices>, pub hosts: HashMap<String, LimitIndices>,
pub trusts: (u16, u16), pub trusts: (u16, u16),
} }
impl MergedLimitList { impl MergedLimitList {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
hostmap: HashMap::new(), hosts: HashMap::new(),
trusts: (0, 0), trusts: (0, 0),
} }
} }
pub fn add_limit_list(&mut self, src: LimitList, limit_type: Limit) -> &mut Self { pub fn add_limit_list(&mut self, src: LimitList, limit_type: Limit) -> &mut Self {
for host in src.hosts.into_iter() { for host in src.hosts.into_iter() {
let entry = self.hostmap.entry(host).or_default(); let entry = self.hosts.entry(host).or_default();
entry.add_limit(limit_type, src.trust); entry.add_limit(limit_type, src.trust);
} }
match limit_type { match limit_type {
Limit::Block => self.trusts.0 += src.trust, Limit::Suspend => self.trusts.0 += src.trust,
Limit::Silence => self.trusts.1 += src.trust, Limit::Silence => self.trusts.1 += src.trust,
} }
@ -105,35 +105,35 @@ impl MergedLimitList {
pub fn export_file( pub fn export_file(
self, self,
block_path: &str, suspend_path: &str,
mute_path: &str, silence_path: &str,
indices: (u16, u16), thresholds: (u16, u16),
) -> std::io::Result<()> { ) -> std::io::Result<()> {
if self.hostmap.is_empty() { if self.hosts.is_empty() {
error!("Nothing to export!"); error!("Nothing to export!");
return Ok(()); return Ok(());
} }
let (block_thresh, mute_thresh) = indices; let (suspend_thresh, silence_thresh) = thresholds;
let mut block_output: String = String::default(); let mut suspend_contents: String = String::default();
let mut mute_output: String = String::default(); let mut silence_contents: String = String::default();
for item in self.hostmap.into_iter() { for item in self.hosts.into_iter() {
let (block_trust, mute_trust) = item.1.normalize(self.trusts); let (suspend_trust, silence_trust) = item.1.normalize(self.trusts);
// TODO What should the behavior be when an instance is blocked or // TODO What should the behavior be when an instance is blocked or
// muted by most instances, but neither rises above the threshold? // muted by most instances, but neither rises above the threshold?
if block_trust >= block_thresh { if suspend_trust >= suspend_thresh {
block_output.push_str(&(item.0.clone() + "\n")); suspend_contents.push_str(&(item.0.clone() + "\n"));
} }
if mute_trust >= mute_thresh { if silence_trust >= silence_thresh {
mute_output.push_str(&(item.0.clone() + "\n")); silence_contents.push_str(&(item.0.clone() + "\n"));
} }
} }
fs::write(block_path, alphabetize_lines(block_output))?; fs::write(suspend_path, alphabetize_lines(suspend_contents))?;
fs::write(mute_path, alphabetize_lines(mute_output))?; fs::write(silence_path, alphabetize_lines(silence_contents))?;
Ok(()) Ok(())
} }