Skip to content

Commit

Permalink
Major refactoring of std stuff; added errors, conditions; just a mock…
Browse files Browse the repository at this point in the history
…up of system state manager, it's needed for it to work correctly.
  • Loading branch information
Macronic committed Aug 31, 2024
1 parent 9f57643 commit 442b3c8
Show file tree
Hide file tree
Showing 20 changed files with 1,077 additions and 354 deletions.
2 changes: 1 addition & 1 deletion src/daemon_communication/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::{
debug_log::Logger,
events::{send_alert_event, send_log_event, send_update_tags_event},
process_watcher::ShortLivedProcessLog,
s3_upload::upload_from_file_path,
tracer_client::TracerClient,
upload::upload_from_file_path,
};

type ProcessOutput<'a> =
Expand Down
209 changes: 209 additions & 0 deletions src/errors/conditions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
use predicates::{prelude::predicate, str::RegexPredicate, Predicate};

use super::{Issue, SystemStateSnapshot};

pub trait ErrorBaseCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool;
}

pub struct FileExistsCondition {
pub file_path: RegexPredicate,
}

impl ErrorBaseCondition for FileExistsCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
for file in system_state.workspace_files.keys() {
if self.file_path.eval(file) {
return true;
}
}
false
}
}

impl FileExistsCondition {
pub fn new(file_path: &str) -> FileExistsCondition {
FileExistsCondition {
file_path: predicate::str::is_match(file_path).unwrap(),
}
}
}

pub struct ToolRunTimeGreaterThanCondition {
pub tool_name: String,
pub run_time: u64,
}

impl ErrorBaseCondition for ToolRunTimeGreaterThanCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
system_state
.tool_run_summaries
.iter()
.any(|t| t.tool_name == self.tool_name && t.run_duration > self.run_time)
}
}

pub struct ToolCPUUsageGreaterThanCondition {
pub tool_name: String,
pub threshold: f64,
}

impl ErrorBaseCondition for ToolCPUUsageGreaterThanCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
system_state
.tool_run_summaries
.iter()
.any(|t| t.tool_name == self.tool_name && t.max_cpu_usage > self.threshold)
}
}

pub struct ToolMemoryUsageGreaterThanCondition {
pub tool_name: String,
pub threshold: f64,
}

impl ErrorBaseCondition for ToolMemoryUsageGreaterThanCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
system_state
.tool_run_summaries
.iter()
.any(|t| t.tool_name == self.tool_name && t.max_memory_utilization > self.threshold)
}
}

pub struct LogContainsInner {
pub regex: RegexPredicate,
}

impl LogContainsInner {
pub fn new(regex: &str) -> LogContainsInner {
LogContainsInner {
regex: predicate::str::is_match(regex).unwrap(),
}
}

pub fn trigger(&self, logs: &[String]) -> bool {
logs.iter().any(|l| self.regex.eval(l))
}
}

pub enum LogContainsCondition {
Stdout(LogContainsInner),
Stderr(LogContainsInner),
Syslog(LogContainsInner),
}

impl ErrorBaseCondition for LogContainsCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
match self {
LogContainsCondition::Stdout(inner) => inner.trigger(system_state.stdout_lines),
LogContainsCondition::Stderr(inner) => inner.trigger(system_state.stderr_lines),
LogContainsCondition::Syslog(inner) => inner.trigger(system_state.syslog_lines),
}
}
}

pub struct SystemCPUCondition {
pub threshold: f64,
}

impl ErrorBaseCondition for SystemCPUCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
system_state.system_summary.cpu_utilization > self.threshold
}
}

pub struct SystemMemoryCondition {
pub threshold: f64,
}

impl ErrorBaseCondition for SystemMemoryCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
system_state.system_summary.memory_utilization > self.threshold
}
}

pub struct SystemDiskUtilizationCondition {
pub threshold: f64,
}

impl ErrorBaseCondition for SystemDiskUtilizationCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
system_state
.system_summary
.disk_utilizations
.iter()
.any(|d| *d > self.threshold)
}
}

pub struct IssueCondition {
pub issue: Issue,
}

impl ErrorBaseCondition for IssueCondition {
fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
system_state.found_issues.iter().any(|i| i == &self.issue)
}
}

pub enum ErrorCondition {
ExternalTrigger(Box<dyn ErrorBaseCondition + Sync>),
Or(Vec<ErrorCondition>),
And(Vec<ErrorCondition>),
Not(Box<ErrorCondition>),
}

impl ErrorCondition {
pub fn trigger(&self, system_state: &SystemStateSnapshot) -> bool {
match self {
ErrorCondition::ExternalTrigger(condition) => condition.trigger(system_state),
ErrorCondition::Or(conditions) => conditions.iter().any(|c| c.trigger(system_state)),
ErrorCondition::And(conditions) => conditions.iter().all(|c| c.trigger(system_state)),
ErrorCondition::Not(condition) => !condition.trigger(system_state),
}
}
}

#[macro_export]
macro_rules! condition {
($condition:expr) => {
ErrorCondition::ExternalTrigger(Box::new($condition) as Box<dyn ErrorBaseCondition + Sync>)
};
}

#[macro_export]
macro_rules! boxed_condition {
($condition:expr) => {
Box::new(ErrorCondition::ExternalTrigger(
Box::new($condition) as Box<dyn ErrorBaseCondition + Sync>
))
};
}

#[macro_export]
macro_rules! stdout_condition {
($stdout_text:literal) => {
ErrorCondition::ExternalTrigger(Box::new(LogContainsCondition::Stdout(
LogContainsInner::new($stdout_text),
)) as Box<dyn ErrorBaseCondition + Sync>)
};
}

#[macro_export]
macro_rules! stderr_condition {
($stderr_text:literal) => {
ErrorCondition::ExternalTrigger(Box::new(LogContainsCondition::Stderr(
LogContainsInner::new($stderr_text),
)) as Box<dyn ErrorBaseCondition + Sync>)
};
}

#[macro_export]
macro_rules! syslog_condition {
($syslog_test:literal) => {
ErrorCondition::ExternalTrigger(Box::new(LogContainsCondition::Syslog(
LogContainsInner::new($syslog_test),
)) as Box<dyn ErrorBaseCondition + Sync>)
};
}
Loading

0 comments on commit 442b3c8

Please sign in to comment.