Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset committed Jun 6, 2019
1 parent 38f490a commit 37a18c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
37 changes: 22 additions & 15 deletions relay/sources/relayd/src/data/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ use nom::*;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

type AgentLogLevel = &'static str;

// A detail log entry
#[derive(Debug, PartialEq, Eq)]
struct LogEntry {
Expand All @@ -45,8 +47,6 @@ struct LogEntry {
datetime: DateTime<FixedOffset>,
}

type AgentLogLevel = &'static str;

named!(agent_log_level<&str, AgentLogLevel>,
complete!(alt!(
// CFEngine logs
Expand Down Expand Up @@ -103,7 +103,7 @@ named!(line_timestamp<&str, DateTime<FixedOffset>>,
)
);

named!(simpleline<&str, String>,
named!(simpleline<&str, &str>,
do_parse!(
opt!(
complete!(line_timestamp)
Expand All @@ -115,18 +115,17 @@ named!(simpleline<&str, String>,
complete!(
tag!("\n")
) >>
(res.to_string())
(res)
)
);

named!(multilines<&str, String>,
named!(multilines<&str, Vec<&str>>,
do_parse!(
// at least one
res: many1!(
complete!(simpleline)
) >>
// TODO perf: avoid reallocating everything twice and use the source slice
(res.join("\n"))
(res)
)
);

Expand All @@ -139,7 +138,7 @@ named!(log_entry<&str, LogEntry>,
>> (
LogEntry {
event_type,
msg,
msg: msg.join("\n"),
datetime,
}
)
Expand Down Expand Up @@ -196,13 +195,14 @@ named!(pub report<&str, RawReport>, do_parse!(
key_value: key_value.to_string(),
start_datetime,
event_type: event_type.to_string(),
msg: msg.to_string(),
msg: msg.join("\n"),
policy: policy.to_string(),
},
logs
})
));

// We could make RawReport insertable to avoid copying context to simple logs
#[derive(Debug, PartialEq, Eq)]
pub struct RawReport {
report: Report,
Expand Down Expand Up @@ -369,33 +369,40 @@ mod tests {

#[test]
fn it_parses_multilines() {
assert_eq!(multilines("Thething\n").unwrap().1, "Thething".to_string());
assert_eq!(
multilines("The thing\n").unwrap().1,
multilines("Thething\n").unwrap().1.join("\n"),
"Thething".to_string()
);
assert_eq!(
multilines("The thing\n").unwrap().1.join("\n"),
"The thing".to_string()
);
assert_eq!(
multilines("2019-05-09T13:36:46+00:00 The thing\n")
.unwrap()
.1,
.1
.join("\n"),
"The thing".to_string()
);
assert_eq!(
multilines(
"2019-05-09T13:36:46+00:00 The thing\n2019-05-09T13:36:46+00:00 The other thing\n"
)
.unwrap()
.1,
.1
.join("\n"),
"The thing\nThe other thing".to_string()
);
assert_eq!(
multilines("2019-05-09T13:36:46+00:00 The thing\n\n2019-05-09T13:36:46+00:00 The other thing\n").unwrap().1,
multilines("2019-05-09T13:36:46+00:00 The thing\n\n2019-05-09T13:36:46+00:00 The other thing\n")
.unwrap().1.join("\n"),
"The thing\n\nThe other thing".to_string()
);
assert_eq!(
multilines("Thething\n2019-05-09T13:36:46+00:00 Theotherthing\n")
.unwrap()
.1,
.1
.join("\n"),
"Thething\nTheotherthing".to_string()
);
}
Expand Down
16 changes: 4 additions & 12 deletions relay/sources/relayd/src/output/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub fn ping(pool: &PgPool) -> Result<(), Error> {
Ok(())
}

// TODO return if it inserted the runlog or not
pub fn insert_runlog(
pool: &PgPool,
runlog: &RunLog,
Expand All @@ -105,9 +104,6 @@ pub fn insert_runlog(
use self::schema::ruddersysevents::dsl::*;
let connection = &*pool.get()?;

// Non perfect as there could be race-conditions
// but should avoid most duplicates

let first_report = runlog
.reports
.first()
Expand All @@ -131,18 +127,14 @@ pub fn insert_runlog(
.and(directiveid.eq(&first_report.directive_id)),
)
.limit(1)
.load::<QueryableReport>(connection)
// FIXME: soft fail?
.expect("Error loading reports")
.load::<QueryableReport>(connection)?
.is_empty();

if behavior == InsertionBehavior::AllowDuplicate || new_runlog {
trace!("Inserting runlog {:#?}", runlog; "component" => LogComponent::Database, "node" => &first_report.node_id);
for report in &runlog.reports {
insert_into(ruddersysevents)
.values(report)
.execute(connection)?;
}
insert_into(ruddersysevents)
.values(&runlog.reports)
.execute(connection)?;
Ok(RunlogInsertion::Inserted)
} else {
error!("The {} runlog was already there, skipping insertion", runlog.info; "component" => LogComponent::Database, "node" => &first_report.node_id);
Expand Down

0 comments on commit 37a18c4

Please sign in to comment.