refactoring, log->tracing, gpsd source
This commit is contained in:
102
src/lib.rs
102
src/lib.rs
@@ -1,19 +1,27 @@
|
||||
pub mod chrony;
|
||||
pub mod chrony_refclock;
|
||||
pub mod gpsd;
|
||||
pub mod hwmon;
|
||||
pub mod uccm;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use figment::{
|
||||
Figment,
|
||||
providers::{Format, Serialized, Toml},
|
||||
util::map,
|
||||
value::Map,
|
||||
Figment,
|
||||
};
|
||||
use gethostname::gethostname;
|
||||
use influxdb2::models::DataPoint;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
use tokio::sync::broadcast::*;
|
||||
|
||||
use std::{fmt::Debug, net::SocketAddr, path::Path, sync::Arc};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct InfluxConfig {
|
||||
pub enabled: bool,
|
||||
pub url: String,
|
||||
pub org: String,
|
||||
pub bucket: String,
|
||||
@@ -25,6 +33,7 @@ impl Default for InfluxConfig {
|
||||
fn default() -> Self {
|
||||
let host = gethostname().into_string().unwrap();
|
||||
InfluxConfig {
|
||||
enabled: false,
|
||||
url: "http://localhost:8086".into(),
|
||||
org: "default".into(),
|
||||
bucket: "default".into(),
|
||||
@@ -100,6 +109,24 @@ impl Default for HwmonConfig {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct GpsdConfig {
|
||||
pub enabled: bool,
|
||||
pub interval: u64,
|
||||
pub host: String,
|
||||
}
|
||||
|
||||
impl Default for GpsdConfig {
|
||||
fn default() -> Self {
|
||||
GpsdConfig {
|
||||
enabled: false,
|
||||
interval: 60,
|
||||
host: "localhost:2947".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TimeReport {
|
||||
pub system_time: DateTime<Utc>,
|
||||
@@ -109,6 +136,67 @@ pub struct TimeReport {
|
||||
pub valid: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum SourceStatus {
|
||||
Healthy,
|
||||
LossOfSignal(Option<String>),
|
||||
LossOfSync(Option<String>),
|
||||
Other(Option<String>),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum MetricValue {
|
||||
Int(i64),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SourceMetric {
|
||||
name: String,
|
||||
value: MetricValue,
|
||||
tags: Arc<Vec<(String, String)>>,
|
||||
}
|
||||
|
||||
impl SourceMetric {
|
||||
pub fn new_int(name: &str, value: i64, tags: Arc<Vec<(String, String)>>) -> Self {
|
||||
Self {
|
||||
name: name.to_owned(),
|
||||
value: MetricValue::Int(value),
|
||||
tags,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_float(name: &str, value: f64, tags: Arc<Vec<(String, String)>>) -> Self {
|
||||
Self {
|
||||
name: name.to_owned(),
|
||||
value: MetricValue::Float(value),
|
||||
tags,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_bool(name: &str, value: bool, tags: Arc<Vec<(String, String)>>) -> Self {
|
||||
Self {
|
||||
name: name.to_owned(),
|
||||
value: MetricValue::Bool(value),
|
||||
tags,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SourceReportDetails: Debug + Send + Sync {
|
||||
fn to_metrics(&self) -> Vec<SourceMetric>;
|
||||
fn is_healthy(&self) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SourceReport {
|
||||
pub name: String,
|
||||
pub status: SourceStatus,
|
||||
pub details: Arc<dyn SourceReportDetails>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct UCCMConfig {
|
||||
pub enabled: bool,
|
||||
@@ -137,6 +225,7 @@ pub struct SourcesConfig {
|
||||
pub chrony: ChronyConfig,
|
||||
pub hwmon: HwmonConfig,
|
||||
pub uccm: UCCMConfig,
|
||||
pub gpsd: GpsdConfig,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Default)]
|
||||
@@ -155,11 +244,12 @@ pub fn load_config(filename: &Path) -> Figment {
|
||||
Figment::from(Serialized::defaults(Config::default())).merge(Toml::file(filename))
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ChimemonMessage {
|
||||
DataPoint(DataPoint),
|
||||
DataPoints(Vec<DataPoint>),
|
||||
TimeReport(TimeReport),
|
||||
SourceReport(SourceReport),
|
||||
}
|
||||
|
||||
impl From<DataPoint> for ChimemonMessage {
|
||||
@@ -179,6 +269,12 @@ impl From<TimeReport> for ChimemonMessage {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SourceReport> for ChimemonMessage {
|
||||
fn from(sr: SourceReport) -> Self {
|
||||
ChimemonMessage::SourceReport(sr)
|
||||
}
|
||||
}
|
||||
|
||||
pub type ChimemonSourceChannel = Sender<ChimemonMessage>;
|
||||
pub type ChimemonTargetChannel = Receiver<ChimemonMessage>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user