Ugh
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::meta_plugin::{MetaPlugin, MetaPluginType, BaseMetaPlugin};
|
||||
use crate::meta_plugin::{BaseMetaPlugin, MetaPlugin, MetaPluginType};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
/// Meta plugin that calculates the read rate (KB/s) of input data.
|
||||
///
|
||||
/// Tracks bytes read and elapsed time, then computes the rate in finalize().
|
||||
/// Tracks bytes read and elapsed time, then computes the rate in finalize().
|
||||
/// Outputs the rate via configured mappings. Supports options for customization
|
||||
/// (though defaults are used here).
|
||||
///
|
||||
@@ -48,11 +48,11 @@ impl ReadRateMetaPlugin {
|
||||
outputs: Option<std::collections::HashMap<String, serde_yaml::Value>>,
|
||||
) -> ReadRateMetaPlugin {
|
||||
let mut base = BaseMetaPlugin::new();
|
||||
|
||||
|
||||
// Set default outputs
|
||||
let default_outputs = &["read_rate"];
|
||||
base.initialize_plugin(default_outputs, &_options, &outputs);
|
||||
|
||||
|
||||
ReadRateMetaPlugin {
|
||||
start_time: None,
|
||||
bytes_read: 0,
|
||||
@@ -60,7 +60,6 @@ impl ReadRateMetaPlugin {
|
||||
base,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl MetaPlugin for ReadRateMetaPlugin {
|
||||
@@ -72,7 +71,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
fn is_finalized(&self) -> bool {
|
||||
self.is_finalized
|
||||
}
|
||||
|
||||
|
||||
/// Sets the finalized state of the plugin.
|
||||
///
|
||||
/// Marks the plugin as complete or resets it.
|
||||
@@ -83,7 +82,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
fn set_finalized(&mut self, finalized: bool) {
|
||||
self.is_finalized = finalized;
|
||||
}
|
||||
|
||||
|
||||
/// Finalizes the plugin, calculating the read rate.
|
||||
///
|
||||
/// Computes KB/s from bytes read and elapsed time. Outputs via mappings.
|
||||
@@ -104,27 +103,30 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
is_finalized: true,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
let mut metadata = Vec::new();
|
||||
|
||||
if let Some(start_time) = self.start_time {
|
||||
let duration = start_time.elapsed();
|
||||
let rate = if duration.as_secs_f64() > 0.0 {
|
||||
format!("{:.2} KB/s", (self.bytes_read as f64 / 1024.0) / duration.as_secs_f64())
|
||||
format!(
|
||||
"{:.2} KB/s",
|
||||
(self.bytes_read as f64 / 1024.0) / duration.as_secs_f64()
|
||||
)
|
||||
} else {
|
||||
"N/A".to_string()
|
||||
};
|
||||
|
||||
// Use process_metadata_outputs to handle output mapping
|
||||
if let Some(meta_data) = crate::meta_plugin::process_metadata_outputs(
|
||||
"read_rate",
|
||||
serde_yaml::Value::String(rate),
|
||||
self.base.outputs()
|
||||
"read_rate",
|
||||
serde_yaml::Value::String(rate),
|
||||
self.base.outputs(),
|
||||
) {
|
||||
metadata.push(meta_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Mark as finalized
|
||||
self.is_finalized = true;
|
||||
|
||||
@@ -154,7 +156,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
is_finalized: true,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if self.start_time.is_none() {
|
||||
self.start_time = Some(Instant::now());
|
||||
}
|
||||
@@ -173,7 +175,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
fn meta_type(&self) -> MetaPluginType {
|
||||
MetaPluginType::ReadRate
|
||||
}
|
||||
|
||||
|
||||
/// Returns a reference to the outputs mapping.
|
||||
///
|
||||
/// # Returns
|
||||
@@ -182,7 +184,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
fn outputs(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||
self.base.outputs()
|
||||
}
|
||||
|
||||
|
||||
/// Returns a mutable reference to the outputs mapping.
|
||||
///
|
||||
/// Allows modification of output configurations.
|
||||
@@ -193,7 +195,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
fn outputs_mut(&mut self) -> &mut std::collections::HashMap<String, serde_yaml::Value> {
|
||||
self.base.outputs_mut()
|
||||
}
|
||||
|
||||
|
||||
/// Returns the default output names for this plugin.
|
||||
///
|
||||
/// # Returns
|
||||
@@ -202,7 +204,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
fn default_outputs(&self) -> Vec<String> {
|
||||
vec!["read_rate".to_string()]
|
||||
}
|
||||
|
||||
|
||||
/// Returns a reference to the options mapping.
|
||||
///
|
||||
/// # Returns
|
||||
@@ -211,7 +213,7 @@ impl MetaPlugin for ReadRateMetaPlugin {
|
||||
fn options(&self) -> &std::collections::HashMap<String, serde_yaml::Value> {
|
||||
self.base.options()
|
||||
}
|
||||
|
||||
|
||||
/// Returns a mutable reference to the options mapping.
|
||||
///
|
||||
/// Allows modification of plugin options.
|
||||
|
||||
Reference in New Issue
Block a user