Skip to content

Commit

Permalink
Migration to rust 1.65
Browse files Browse the repository at this point in the history
  • Loading branch information
zdz committed Nov 4, 2022
1 parent 2472c95 commit 57e817a
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 59 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ edition = "2021"
name = "stat_client"
version = "1.5.6"

rust-version = "1.64"
rust-version = "1.65"

authors = ["doge <[email protected]>"]
categories = ["monitoring-tools"]
Expand All @@ -15,6 +15,9 @@ repository = "https://github.com/zdz/ServerStatus-Rust"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
chrono = "0.4"

[dependencies]
anyhow = "1"
bytes = {version = "1", features = ["serde"]}
Expand Down
29 changes: 17 additions & 12 deletions client/build.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
use chrono::Utc;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

fn commit_hash() -> Option<String> {
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|hash| hash.trim().into())
}

fn build_ts() -> Option<String> {
Command::new("date")
.args(&["+%Y-%m-%d %H:%M:%S %Z"])
fn rustc_version() -> Option<String> {
Command::new("rustc")
.args(["--version"])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|hash| hash.trim().into())
.map(|s| {
s.split_whitespace()
.into_iter()
.enumerate()
.filter(|&(idx, _)| idx < 2)
.map(|(_, s)| s)
.collect::<Vec<&str>>()
.join(" ")
})
}

fn main() {
let mut app_version = String::from(env!("CARGO_PKG_VERSION"));
if let Some(commit_hash) = commit_hash() {
app_version = format!(
"v{} GIT:{}, BUILD:{}",
"v{} ({}, {}, {})",
app_version,
commit_hash,
build_ts().unwrap_or_else(|| SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.to_string())
Utc::now().format("%Y-%m-%d %H:%M:%S %Z"),
rustc_version().unwrap()
);
}
println!("cargo:rustc-env=APP_VERSION={}", app_version);
Expand Down
4 changes: 2 additions & 2 deletions client/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn get_vnstat_traffic(args: &Args) -> (u64, u64, u64, u64) {
let local_now = Local::now();
let (mut network_in, mut network_out, mut m_network_in, mut m_network_out) = (0, 0, 0, 0);
let a = Command::new("/usr/bin/vnstat")
.args(&["--json", "m"])
.args(["--json", "m"])
.output()
.expect("failed to execute vnstat")
.stdout;
Expand Down Expand Up @@ -184,7 +184,7 @@ static DF_CMD:&str = "df -Tlm --total -t ext4 -t ext3 -t ext2 -t reiserfs -t jfs
pub fn get_hdd() -> (u64, u64) {
let (mut hdd_total, mut hdd_used) = (0, 0);
let a = &Command::new("/bin/sh")
.args(&["-c", DF_CMD])
.args(["-c", DF_CMD])
.output()
.expect("failed to execute df")
.stdout;
Expand Down
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ serde_json = {version = "1.0", default-features = false, features = ["alloc"]}
tonic = {version = "0.8", features = ["tokio-rustls"]}

[build-dependencies]
chrono = "0.4"
protobuf-src = "1"
tonic-build = "0.8"
21 changes: 4 additions & 17 deletions common/build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
use chrono::Utc;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

fn commit_hash() -> Option<String> {
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|hash| hash.trim().into())
}

fn build_ts() -> Option<String> {
Command::new("date")
.args(&["+%Y-%m-%d %H:%M:%S %Z"])
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
Expand All @@ -23,14 +14,10 @@ fn main() {
let mut app_version = String::from(env!("CARGO_PKG_VERSION"));
if let Some(commit_hash) = commit_hash() {
app_version = format!(
"v{} GIT:{}, BUILD:{}",
"v{} ({}, {})",
app_version,
commit_hash,
build_ts().unwrap_or_else(|| SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.to_string())
Utc::now().format("%Y-%m-%d %H:%M:%S %Z")
);
}
println!("cargo:rustc-env=APP_VERSION={}", app_version);
Expand Down
5 changes: 4 additions & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ edition = "2021"
name = "stat_server"
version = "1.5.6"

rust-version = "1.64"
rust-version = "1.65"

authors = ["doge <[email protected]>"]
categories = ["monitoring-tools"]
Expand All @@ -15,6 +15,9 @@ repository = "https://github.com/zdz/ServerStatus-Rust"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
chrono = "0.4"

[dependencies]
anyhow = "1"
bytes = {version = "1", features = ["serde"]}
Expand Down
29 changes: 17 additions & 12 deletions server/build.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
use chrono::Utc;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

fn commit_hash() -> Option<String> {
Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|hash| hash.trim().into())
}

fn build_ts() -> Option<String> {
Command::new("date")
.args(&["+%Y-%m-%d %H:%M:%S %Z"])
fn rustc_version() -> Option<String> {
Command::new("rustc")
.args(["--version"])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|hash| hash.trim().into())
.map(|s| {
s.split_whitespace()
.into_iter()
.enumerate()
.filter(|&(idx, _)| idx < 2)
.map(|(_, s)| s)
.collect::<Vec<&str>>()
.join(" ")
})
}

fn main() {
let mut app_version = String::from(env!("CARGO_PKG_VERSION"));
if let Some(commit_hash) = commit_hash() {
app_version = format!(
"v{} GIT:{}, BUILD:{}",
"v{} ({}, {}, {})",
app_version,
commit_hash,
build_ts().unwrap_or_else(|| SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.to_string())
Utc::now().format("%Y-%m-%d %H:%M:%S %Z"),
rustc_version().unwrap()
);
}
println!("cargo:rustc-env=APP_VERSION={}", app_version);
Expand Down
23 changes: 9 additions & 14 deletions server/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl StatsMgr {

// load last_network_in/out
if let Ok(mut hosts_map) = hosts_map_base.lock() {
self.load_last_network(&mut *hosts_map);
self.load_last_network(&mut hosts_map);
}

let (stat_tx, stat_rx) = sync_channel(512);
Expand Down Expand Up @@ -194,7 +194,7 @@ impl StatsMgr {

if stat_t.notify && (pre_stat.latest_ts + cfg.offline_threshold < stat_t.latest_ts) {
// node up notify
notifier_tx_1.send((Event::NodeUp, stat_c.to_owned()));
notifier_tx_1.send((Event::NodeUp, stat_c.clone()));
}
}
host_stat_map.insert(stat_c.name.to_string(), stat_c);
Expand Down Expand Up @@ -236,7 +236,7 @@ impl StatsMgr {
if let Ok(mut host_stat_map) = stat_map_2.lock() {
for (_, stat) in host_stat_map.iter_mut() {
if stat.disabled {
resp.servers.push(stat.to_owned().into_owned());
resp.servers.push(stat.as_ref().clone());
continue;
}
let stat_c = stat.borrow_mut();
Expand All @@ -252,16 +252,16 @@ impl StatsMgr {
// notify check /30 s
if latest_notify_ts + cfg.notify_interval < now {
if o.online4 || o.online6 {
notifier_tx_2.send((Event::Custom, stat_c.to_owned()));
notifier_tx_2.send((Event::Custom, stat_c.clone()));
} else {
o.disabled = true;
notifier_tx_2.send((Event::NodeDown, stat_c.to_owned()));
notifier_tx_2.send((Event::NodeDown, stat_c.clone()));
}
notified = true;
}
}

resp.servers.push(stat_c.to_owned().into_owned());
resp.servers.push(stat_c.as_ref().clone());
}
if notified {
latest_notify_ts = now;
Expand Down Expand Up @@ -348,14 +348,9 @@ impl StatsMgr {
// for skip_serializing
if let Some(srv_list) = resp_json["servers"].as_array_mut() {
for (idx, stat) in data.servers.iter().enumerate() {
match srv_list[idx].as_object_mut() {
Some(srv) => {
srv.insert("ip_info".into(), serde_json::to_value(stat.ip_info.as_ref())?);
srv.insert("sys_info".into(), serde_json::to_value(stat.sys_info.as_ref())?);
}
None => {
// todo!()
}
if let Some(srv) = srv_list[idx].as_object_mut() {
srv.insert("ip_info".into(), serde_json::to_value(stat.ip_info.as_ref())?);
srv.insert("sys_info".into(), serde_json::to_value(stat.sys_info.as_ref())?);
}
}
} else {
Expand Down

0 comments on commit 57e817a

Please sign in to comment.