Skip to content

Commit

Permalink
fixup! Move common stuff of c8y/tedge log manager to separate lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruadhri17 committed Aug 8, 2023
1 parent ef0527c commit 35b0494
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 63 deletions.
6 changes: 0 additions & 6 deletions crates/common/log_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ homepage = { workspace = true }
repository = { workspace = true }

[dependencies]
c8y_api = { path = "../../core/c8y_api" }
easy_reader = "0.5"
glob = "0.3"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tedge_actors = { path = "../../core/tedge_actors" }
tedge_http_ext = { path = "../../extensions/tedge_http_ext" }
tedge_mqtt_ext = { path = "../../extensions/tedge_mqtt_ext" }
tedge_utils = { path = "../../common/tedge_utils" }
thiserror = "1.0"
time = { version = "0.3", features = ["formatting"] }
toml = "0.5"
31 changes: 0 additions & 31 deletions crates/common/log_manager/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use c8y_api::smartrest::topic::C8yTopic;
use log::info;
use log::warn;
use serde::Deserialize;
use serde_json::json;
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use tedge_mqtt_ext::MqttError;
use tedge_mqtt_ext::MqttMessage;
use tedge_mqtt_ext::Topic;

#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Default)]
pub struct LogPluginConfig {
Expand Down Expand Up @@ -54,18 +49,6 @@ impl LogPluginConfig {
}
}

pub fn to_supported_config_types_message(
&self,
topic: &Topic,
) -> Result<MqttMessage, MqttError> {
Ok(MqttMessage::new(topic, self.to_payload()).with_retain())
}

pub fn to_supported_config_types_smartrest_message(&self) -> Result<MqttMessage, MqttError> {
let topic = C8yTopic::SmartRestResponse.to_topic()?;
Ok(MqttMessage::new(&topic, self.to_smartrest_payload()))
}

pub fn get_all_file_types(&self) -> Vec<String> {
self.files
.iter()
Expand All @@ -75,20 +58,6 @@ impl LogPluginConfig {
.map(|x| x.to_string())
.collect::<Vec<_>>()
}

fn to_payload(&self) -> String {
let mut config_types = self.get_all_file_types();
config_types.sort();
json!({ "types": config_types }).to_string()
}

// 118,typeA,typeB,...
fn to_smartrest_payload(&self) -> String {
let mut config_types = self.get_all_file_types();
config_types.sort();
let supported_config_types = config_types.join(",");
format!("118,{supported_config_types}")
}
}

#[test]
Expand Down
18 changes: 0 additions & 18 deletions crates/common/log_manager/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,4 @@ pub enum LogRetrievalError {

#[error("No such file or directory for log type: {log_type}")]
NoLogsAvailableForType { log_type: String },

#[error(transparent)]
FromHttpError(#[from] tedge_http_ext::HttpError),

#[error(transparent)]
FromChannelError(#[from] tedge_actors::ChannelError),

#[error(transparent)]
FromMqttError(#[from] tedge_mqtt_ext::MqttError),

#[error(transparent)]
FromPathsError(#[from] tedge_utils::paths::PathsError),
}

impl From<LogRetrievalError> for tedge_actors::RuntimeError {
fn from(error: LogRetrievalError) -> Self {
tedge_actors::RuntimeError::ActorError(Box::new(error))
}
}
8 changes: 4 additions & 4 deletions crates/common/log_manager/src/log_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn new_read_logs(
log_type: &str,
date_from: OffsetDateTime,
lines: usize,
needle: &Option<String>,
search_text: &Option<String>,
) -> Result<String, LogRetrievalError> {
let mut output = String::new();
// first filter logs on type
Expand All @@ -23,7 +23,7 @@ pub fn new_read_logs(

let mut line_counter = 0usize;
for logfile in logfiles_to_read {
match read_log_content(logfile.as_path(), line_counter, lines, needle) {
match read_log_content(logfile.as_path(), line_counter, lines, search_text) {
Ok((lines, file_content)) => {
line_counter = lines;
output.push_str(&file_content);
Expand All @@ -44,7 +44,7 @@ pub fn read_log_content(
logfile: &Path,
mut line_counter: usize,
max_lines: usize,
filter_text: &Option<String>,
search_text: &Option<String>,
) -> Result<(usize, String), LogRetrievalError> {
if line_counter >= max_lines {
Err(LogRetrievalError::MaxLines)
Expand All @@ -61,7 +61,7 @@ pub fn read_log_content(
reader.eof();
while line_counter < max_lines {
if let Some(haystack) = reader.prev_line()? {
if let Some(needle) = &filter_text {
if let Some(needle) = &search_text {
if haystack.contains(needle) {
file_content_as_vec.push_front(format!("{}\n", haystack));
line_counter += 1;
Expand Down
3 changes: 0 additions & 3 deletions crates/extensions/c8y_log_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ anyhow = "1.0"
async-trait = "0.1"
c8y_api = { path = "../../core/c8y_api" }
c8y_http_proxy = { path = "../../extensions/c8y_http_proxy" }
easy_reader = "0.5"
glob = "0.3"
log = "0.4"
log_manager = { path = "../../common/log_manager" }
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -27,7 +25,6 @@ tedge_mqtt_ext = { path = "../../extensions/tedge_mqtt_ext" }
tedge_utils = { path = "../../common/tedge_utils" }
thiserror = "1.0"
tokio = { version = "1.23", features = ["macros"] }
toml = "0.5"

[dev-dependencies]
filetime = "0.2"
Expand Down
7 changes: 6 additions & 1 deletion crates/extensions/c8y_log_manager/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ impl LogManagerActor {
&mut self,
plugin_config: &LogPluginConfig,
) -> Result<(), anyhow::Error> {
let msg = plugin_config.to_supported_config_types_smartrest_message()?;
let topic = C8yTopic::SmartRestResponse.to_topic()?;
let mut config_types = plugin_config.get_all_file_types();
config_types.sort();
let supported_config_types = config_types.join(",");
let payload = format!("118,{supported_config_types}");
let msg = MqttMessage::new(&topic, payload);
Ok(self.mqtt_publisher.send(msg).await?)
}

Expand Down

0 comments on commit 35b0494

Please sign in to comment.