Skip to content

Commit

Permalink
More general agent tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed Dec 1, 2023
1 parent f3aebc8 commit 5ad3a38
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
27 changes: 2 additions & 25 deletions agent/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use byte_unit::Byte;
use serde::Deserialize;
use serde_with::{serde_as, DurationSeconds};
use std::{
fs,
path::{Path, PathBuf},
time::Duration,
};
use std::{path::PathBuf, time::Duration};
use url::Url;

#[serde_as]
Expand All @@ -21,29 +17,10 @@ pub(crate) struct Config {

impl Config {
pub(crate) fn get_disk_usage(&self) -> std::io::Result<Byte> {
get_size(&self.video_directory)
crate::utils::get_size(&self.video_directory)
}
}

pub(crate) fn get_size<P>(path: P) -> std::io::Result<Byte>
where
P: AsRef<Path>,
{
let mut result: u128 = 0;

for entry in fs::read_dir(&path)? {
let path = entry?.path();

if path.is_file() {
result += path.metadata()?.len() as u128;
} else {
result += get_size(path)?.get_bytes();
}
}

Ok(Byte::from_bytes(result))
}

#[derive(Clone, Deserialize)]
pub(crate) struct StreamConfig {
pub(crate) url: Url,
Expand Down
10 changes: 7 additions & 3 deletions agent/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod config;
mod ffmpeg;
mod server;
mod utils;

use clap::Parser;
use kagiyama::prometheus::{metrics::gauge::Gauge, registry::Unit};
Expand Down Expand Up @@ -120,9 +121,10 @@ async fn main() {
#[tracing::instrument(skip_all)]
fn update_segment_count_metric(metric: &Gauge, config: &config::Config) {
debug!("Updating segment count metric");

match std::fs::read_dir(&config.video_directory) {
Ok(doot) => {
let num = doot
Ok(contents) => {
let ts_file_count = contents
.filter_map(|i| i.ok())
.map(|i| i.path())
.filter(|i| {
Expand All @@ -137,7 +139,8 @@ fn update_segment_count_metric(metric: &Gauge, config: &config::Config) {
}
})
.count();
metric.set(num as i64);

metric.set(ts_file_count as i64);
}
Err(e) => {
warn!("Failed to read video directory, err={}", e);
Expand All @@ -148,6 +151,7 @@ fn update_segment_count_metric(metric: &Gauge, config: &config::Config) {
#[tracing::instrument(skip_all)]
fn update_disk_usage_metric(metric: &Gauge, config: &config::Config) {
debug!("Updating disk usage metric");

match config.get_disk_usage() {
Ok(disk_usage) => {
metric.set(disk_usage.get_bytes() as i64);
Expand Down
21 changes: 21 additions & 0 deletions agent/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use byte_unit::Byte;
use std::{fs, path::Path};

pub(crate) fn get_size<P>(path: P) -> std::io::Result<Byte>
where
P: AsRef<Path>,
{
let mut result: u128 = 0;

for entry in fs::read_dir(&path)? {
let path = entry?.path();

if path.is_file() {
result += path.metadata()?.len() as u128;
} else {
result += get_size(path)?.get_bytes();
}
}

Ok(Byte::from_bytes(result))
}

0 comments on commit 5ad3a38

Please sign in to comment.