Skip to content

Commit

Permalink
Merge pull request #18 from tinythings/isbm-constraint-stdout-handler
Browse files Browse the repository at this point in the history
Add constraint stdout handler
  • Loading branch information
isbm authored Oct 22, 2024
2 parents fd1e5cd + b3953ae commit 983078b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
46 changes: 46 additions & 0 deletions docs/evthandlers/outcome_logger.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Outcome Logger
==============

.. note::

This document explains how to use **outcome logger** event handler.

Synopsis
--------

.. code-block:: text
outcome-logger: null
Outcome logger is one of the simplest event handlers, used to summarise the outcome
of all constraints.

Options
-------

``prefix``
^^^^^^^^^^

Prefix to the logging message text. Example:

.. code-block:: yaml
:caption: Prefix example
prefix: "Hello"
Example
-------

.. code-block:: yaml
:caption: Setup example
events:
# Capture all events
$/$/$/$:
handlers:
outcome-logger
outcome-logger:
prefix: "My constraints"
1 change: 1 addition & 0 deletions docs/evthandlers/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Event Handlers
:maxdepth: 1

console_logger
outcome_logger

Overview
--------
Expand Down
6 changes: 6 additions & 0 deletions examples/models/router/events/events-config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ events:
handler:
- console-logger

$/$/$/$:
handler:
- outcome-logger
outcome-logger:
prefix: Constraints

$/$/$/E:
handler:
- console-logger
Expand Down
19 changes: 17 additions & 2 deletions libsysinspect/src/intp/actproc/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl ConstraintFailure {

#[derive(Debug, Clone, Default)]
pub struct ConstraintResponse {
pub descr: String,
pub failures: Vec<ConstraintFailure>,
descr: String,
failures: Vec<ConstraintFailure>,
}

impl ConstraintResponse {
Expand All @@ -31,6 +31,21 @@ impl ConstraintResponse {
pub fn add_failure(&mut self, fl: ConstraintFailure) {
self.failures.push(fl);
}

/// Returns `true` if the response has failures
pub fn has_errors(&self) -> bool {
!self.failures.is_empty()
}

/// Get a description of the constraint
pub fn descr(&self) -> &str {
&self.descr
}

/// Get list of failures
pub fn failures(&self) -> &[ConstraintFailure] {
&self.failures
}
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
Expand Down
69 changes: 69 additions & 0 deletions libsysinspect/src/reactor/handlers/cstr_stdhdl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Constraint result handler for STDOUT
*/

use super::evthandler::EventHandler;
use crate::intp::{
actproc::response::ActionResponse,
conf::{EventConfig, EventConfigOption},
};
use colored::Colorize;

#[derive(Default, Debug)]
pub struct ConstraintHandler {
eid: String,
config: EventConfig,
}

impl ConstraintHandler {
/// Get prefix from the configuration
fn get_prefix(&self) -> String {
let mut prefix = "".to_string();
if let Some(config) = self.config() {
if let Some(p) = config.as_string("prefix") {
prefix = format!("{} - ", p.cyan());
}
}

prefix
}
}

/// STDOUT event handler. It just outputs the action response to a log.
impl EventHandler for ConstraintHandler {
fn new(eid: String, cfg: EventConfig) -> Self
where
Self: Sized,
{
ConstraintHandler { eid, config: cfg }
}

fn id() -> String
where
Self: Sized,
{
"outcome-logger".to_string()
}

fn handle(&self, evt: &ActionResponse) {
if !&evt.match_eid(&self.eid) {
return;
}

let prefix = self.get_prefix();

if !evt.errors.has_errors() {
log::info!("{}All constraints {}", prefix, "passed".bright_green().bold());
return;
}

log::info!("{}", evt.errors.descr());
for f in evt.errors.failures() {
log::error!("{}{}: {}", prefix, f.title.yellow(), f.msg);
}
}

fn config(&self) -> Option<EventConfigOption> {
self.config.cfg(&ConstraintHandler::id())
}
}
3 changes: 3 additions & 0 deletions libsysinspect/src/reactor/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cstr_stdhdl;
pub mod evthandler;
pub mod stdhdl;
use lazy_static::lazy_static;
Expand All @@ -13,6 +14,7 @@ pub mod registry {
use crate::intp::conf::EventConfig;

use super::*;
use cstr_stdhdl::ConstraintHandler;
use evthandler::EventHandler;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -44,6 +46,7 @@ pub mod registry {
// Handler registration
log::debug!("Intialising handlers");
registry.insert(StdoutEventHandler::id(), |eid, cfg| Box::new(StdoutEventHandler::new(eid, cfg)));
registry.insert(ConstraintHandler::id(), |eid, cfg| Box::new(ConstraintHandler::new(eid, cfg)));
}

/// Get all registered handlers.
Expand Down

0 comments on commit 983078b

Please sign in to comment.