Skip to content

Commit 5d02a50

Browse files
committed
More general agent tidying
1 parent d4631bb commit 5d02a50

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

agent/src/config.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
use byte_unit::Byte;
22
use serde::Deserialize;
33
use serde_with::{serde_as, DurationSeconds};
4-
use std::{
5-
fs,
6-
path::{Path, PathBuf},
7-
time::Duration,
8-
};
4+
use std::{path::PathBuf, time::Duration};
95
use url::Url;
106

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

2218
impl Config {
2319
pub(crate) fn get_disk_usage(&self) -> std::io::Result<Byte> {
24-
get_size(&self.video_directory)
20+
crate::utils::get_size(&self.video_directory)
2521
}
2622
}
2723

28-
pub(crate) fn get_size<P>(path: P) -> std::io::Result<Byte>
29-
where
30-
P: AsRef<Path>,
31-
{
32-
let mut result: u128 = 0;
33-
34-
for entry in fs::read_dir(&path)? {
35-
let path = entry?.path();
36-
37-
if path.is_file() {
38-
result += path.metadata()?.len() as u128;
39-
} else {
40-
result += get_size(path)?.get_bytes();
41-
}
42-
}
43-
44-
Ok(Byte::from_bytes(result))
45-
}
46-
4724
#[derive(Clone, Deserialize)]
4825
pub(crate) struct StreamConfig {
4926
pub(crate) url: Url,

agent/src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod config;
22
mod ffmpeg;
33
mod server;
4+
mod utils;
45

56
use clap::Parser;
67
use kagiyama::prometheus::{metrics::gauge::Gauge, registry::Unit};
@@ -120,9 +121,10 @@ async fn main() {
120121
#[tracing::instrument(skip_all)]
121122
fn update_segment_count_metric(metric: &Gauge, config: &config::Config) {
122123
debug!("Updating segment count metric");
124+
123125
match std::fs::read_dir(&config.video_directory) {
124-
Ok(doot) => {
125-
let num = doot
126+
Ok(contents) => {
127+
let ts_file_count = contents
126128
.filter_map(|i| i.ok())
127129
.map(|i| i.path())
128130
.filter(|i| {
@@ -137,7 +139,8 @@ fn update_segment_count_metric(metric: &Gauge, config: &config::Config) {
137139
}
138140
})
139141
.count();
140-
metric.set(num as i64);
142+
143+
metric.set(ts_file_count as i64);
141144
}
142145
Err(e) => {
143146
warn!("Failed to read video directory, err={}", e);
@@ -148,6 +151,7 @@ fn update_segment_count_metric(metric: &Gauge, config: &config::Config) {
148151
#[tracing::instrument(skip_all)]
149152
fn update_disk_usage_metric(metric: &Gauge, config: &config::Config) {
150153
debug!("Updating disk usage metric");
154+
151155
match config.get_disk_usage() {
152156
Ok(disk_usage) => {
153157
metric.set(disk_usage.get_bytes() as i64);

agent/src/utils.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use byte_unit::Byte;
2+
use std::{fs, path::Path};
3+
4+
pub(crate) fn get_size<P>(path: P) -> std::io::Result<Byte>
5+
where
6+
P: AsRef<Path>,
7+
{
8+
let mut result: u128 = 0;
9+
10+
for entry in fs::read_dir(&path)? {
11+
let path = entry?.path();
12+
13+
if path.is_file() {
14+
result += path.metadata()?.len() as u128;
15+
} else {
16+
result += get_size(path)?.get_bytes();
17+
}
18+
}
19+
20+
Ok(Byte::from_bytes(result))
21+
}

0 commit comments

Comments
 (0)