begin recording system stats

This commit is contained in:
mbecker20
2023-01-02 10:44:48 +00:00
parent da83afc6f4
commit ef8f75b3e2
7 changed files with 138 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
use anyhow::Context;
use mungos::{Collection, Mungos};
use types::{Build, Deployment, Group, Procedure, Server, Update, User};
use types::{Build, Deployment, Group, Procedure, Server, SystemStatsRecord, Update, User};
pub async fn users_collection(mungos: &Mungos, db_name: &str) -> anyhow::Result<Collection<User>> {
let coll = mungos.collection(db_name, "users");
@@ -81,3 +81,17 @@ pub async fn groups_collection(
.context("failed at creating name index")?;
Ok(coll)
}
pub async fn server_stats_collection(
mungos: &Mungos,
db_name: &str,
) -> anyhow::Result<Collection<SystemStatsRecord>> {
let coll = mungos.collection(db_name, "stats");
coll.create_index("server_id")
.await
.context("failed at creating server_id index")?;
coll.create_index("ts")
.await
.context("failed at creating ts index")?;
Ok(coll)
}

View File

@@ -3,11 +3,12 @@ use std::time::Duration;
use anyhow::{anyhow, Context};
use collections::{
builds_collection, deployments_collection, groups_collection, procedures_collection,
servers_collection, updates_collection, users_collection,
server_stats_collection, servers_collection, updates_collection, users_collection,
};
use mungos::{Collection, Mungos};
use types::{
Build, Deployment, Group, MongoConfig, PermissionLevel, Procedure, Server, Update, User,
Build, Deployment, Group, MongoConfig, PermissionLevel, Procedure, Server, SystemStatsRecord,
Update, User,
};
mod collections;
@@ -20,6 +21,7 @@ pub struct DbClient {
pub procedures: Collection<Procedure>,
pub groups: Collection<Group>,
pub updates: Collection<Update>,
pub stats: Collection<SystemStatsRecord>,
}
impl DbClient {
@@ -50,6 +52,9 @@ impl DbClient {
groups: groups_collection(&mungos, db_name)
.await
.expect("failed to make groups collection"),
stats: server_stats_collection(&mungos, db_name)
.await
.expect("failed to make stats collection"),
}
}

View File

@@ -1,6 +1,9 @@
use diff::{Diff, HashMapDiff, OptionDiff, VecDiff};
use crate::{deployment::{DockerRunArgsDiff, RestartModeDiff}, TimelengthDiff};
use crate::{
deployment::{DockerRunArgsDiff, RestartModeDiff},
TimelengthDiff,
};
pub fn f64_diff_no_change(f64_diff: &f64) -> bool {
*f64_diff == 0.0

View File

@@ -174,7 +174,9 @@ pub enum PermissionsTarget {
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Display, EnumString, PartialEq, Hash, Eq, Clone, Copy, Diff)]
#[derive(
Serialize, Deserialize, Debug, Display, EnumString, PartialEq, Hash, Eq, Clone, Copy, Diff,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
#[diff(attr(#[derive(Debug, PartialEq, Serialize)]))]

View File

@@ -222,3 +222,33 @@ pub struct SystemProcess {
pub disk_read_kb: f64,
pub disk_write_kb: f64,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct SystemStatsRecord {
pub server_id: String,
pub ts: i64, // unix ts milliseconds
pub cpu_perc: f32, // in %
pub mem_used_gb: f64, // in GB
pub mem_total_gb: f64, // in GB
pub disk: DiskUsage,
pub networks: Vec<SystemNetwork>,
pub components: Vec<SystemComponent>,
pub polling_rate: Timelength,
}
impl SystemStatsRecord {
pub fn from_stats(server_id: String, ts: i64, stats: SystemStats) -> SystemStatsRecord {
SystemStatsRecord {
server_id,
ts,
cpu_perc: stats.cpu_perc,
mem_used_gb: stats.mem_used_gb,
mem_total_gb: stats.mem_total_gb,
disk: stats.disk,
networks: stats.networks,
components: stats.components,
polling_rate: stats.polling_rate,
}
}
}