monitor-common 0.2.8

Basic data structure and algorithm of linux-monitor tool.
Documentation
use serde::Deserialize;
use serde::Serialize;
use std::net::UdpSocket;
use std::time::{SystemTime, UNIX_EPOCH};

/// 网络传输结构体
#[derive(Serialize, Deserialize, Debug)]
pub struct WebResult<T> {
    pub data: Option<T>,
    pub code: usize,
    pub msg: Option<String>,
}

/// WebResult functions
impl<T> WebResult<T> {
    pub fn success(data: T) -> WebResult<T> {
        let result: WebResult<T> = WebResult {
            data: Some(data),
            code: 200,
            msg: Some("success".to_string()),
        };
        result
    }
    pub fn error(msg: String) -> WebResult<String> {
        let result: WebResult<String> = WebResult {
            data: None,
            code: 503,
            msg: Some(msg),
        };
        result
    }
}

/// 获取本机IP
pub fn get_local_ip() -> String {
    let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
    socket.connect("8.8.8.8:80").unwrap();
    socket.local_addr().unwrap().ip().to_string()
}

/// 获取时间戳
/// > 精确到秒,非毫秒
pub async fn get_timestamp() -> u64 {
    let start = SystemTime::now();
    let since_the_epoch = start
        .duration_since(UNIX_EPOCH)
        .expect("Time went backwards");
    let timestamp = since_the_epoch.as_secs();
    timestamp
}

/// Get deploy time line
pub async fn get_time_line() -> u64 {
    let timestamp = get_timestamp().await;
    let time_line = timestamp - 2 * 3600;
    time_line
}