Skip to content

Commit

Permalink
feat(worker/monitor): Fixing CR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lemosep committed Apr 11, 2024
1 parent 668c82b commit 710e8fe
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 1,098 deletions.
983 changes: 9 additions & 974 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ reqwest = { version = "0.12.1" , features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sysinfo = "0.30.7"
tokio = { version = "1.36.0", features = ["full"] }
tokio = "1.36.0"
uuid = { version = "1", features = ["serde", "v4"] }

[workspace.lints.clippy]
Expand Down
10 changes: 5 additions & 5 deletions proto/src/common/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub enum NodeKind {

#[derive(Debug, Serialize, Deserialize)]
pub struct Metrics {
/// The CPU usage, in MiB.
/// The average CPU usage.
pub cpu_usage: f64,
/// The total memory, in MiB.
pub mem_total_mib: f64,
/// The used memory, in MiB.
pub mem_used_mib: f64,
/// The total memory, in bytes.
pub mem_total: u64,
/// The used memory, in bytes.
pub mem_used: u64,
}
5 changes: 1 addition & 4 deletions worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ workspace = true
[dependencies]
eyre.workspace = true
proto.workspace = true
reqwest.workspace = true
serde.workspace = true
serde_json.workspace = true
sysinfo.workspace = true
tokio.workspace = true
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "time"] }
17 changes: 7 additions & 10 deletions worker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
mod monitor;
use std::{thread::sleep, time::Duration};

use eyre::Result;
use monitor::{MetricsCollector, MetricsSender};
use tokio::time::{sleep, Duration};

use crate::monitor::collector::MetricsCollector;

pub mod monitor;

/// `clap` crate report interval placeholder
/// The value 500 is for quick visualization purposes.
const REPORT_INTERVAL_IN_MILLIS: u64 = 500;

/// `clap` crate url placeholder
const AGT_MGR_REQUEST_URL: &str = "http://localhost:8080/http/agt_mgr";

#[tokio::main]
async fn main() -> Result<()> {
let mut metrics_sender = MetricsSender::new(AGT_MGR_REQUEST_URL)?;
let mut metrics_report: MetricsCollector = MetricsCollector::new();

loop {
sleep(Duration::from_millis(REPORT_INTERVAL_IN_MILLIS)).await;
let metrics = metrics_report.get_metrics();
metrics_sender.send_request(&metrics).await?;
sleep(Duration::from_millis(REPORT_INTERVAL_IN_MILLIS));
println!("{metrics:#?}");
}
}
51 changes: 51 additions & 0 deletions worker/src/monitor/collector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use proto::common::node::Metrics;
use sysinfo::System;

/// Responsable for collecting metrics about the System, such as
/// CPU Percentage Usage, Free Memory Space.
#[derive(Default)]
pub struct MetricsCollector {
system: System,
}

impl MetricsCollector {
/// Instantiates a new [`MetricsCollector`].
#[must_use]
pub fn new() -> Self {
MetricsCollector::default()
}

/// Retruns the [`Metrics`] struct for the current system.
#[must_use]
pub fn get_metrics(&mut self) -> Metrics {
self.system.refresh_memory();
self.system.refresh_cpu();

Metrics {
cpu_usage: self.get_cpu_usage(),
mem_total: self.get_total_memory(),
mem_used: self.get_used_memory(),
}
}

fn get_cpu_usage(&mut self) -> f64 {
let amount_cpus = self.system.cpus().len();

let sum_cpus_usages: f64 = self
.system
.cpus()
.iter()
.map(|cpu| f64::from(cpu.cpu_usage()))
.sum();

sum_cpus_usages / amount_cpus as f64
}

fn get_total_memory(&mut self) -> u64 {
self.system.total_memory()
}

fn get_used_memory(&mut self) -> u64 {
self.system.used_memory()
}
}
67 changes: 0 additions & 67 deletions worker/src/monitor/metrics_collector.rs

This file was deleted.

32 changes: 0 additions & 32 deletions worker/src/monitor/metrics_sender.rs

This file was deleted.

6 changes: 1 addition & 5 deletions worker/src/monitor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
mod metrics_collector;
mod metrics_sender;

pub use metrics_collector::*;
pub use metrics_sender::*;
pub mod collector;

0 comments on commit 710e8fe

Please sign in to comment.