Skip to content

Commit

Permalink
Update with bandwidth metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
tcheronneau committed Nov 14, 2024
1 parent 031f510 commit e9f3c5f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 53 deletions.
5 changes: 4 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

version: '3'

env:
DOCKER_TAG: ""

vars:
DOCKER_TAG: latest
DOCKER_TAG: ${DOCKER_TAG:-latest}

tasks:
build:
Expand Down
17 changes: 17 additions & 0 deletions src/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ pub enum TaskResult {
Default,
}

#[derive(Clone, Hash, Eq, PartialEq, EncodeLabelSet, Debug)]
struct PlexSessionBandwidth {
pub user: String,
pub location: String,
}
#[derive(Clone, Hash, Eq, PartialEq, EncodeLabelSet, Debug)]
struct PlexSessionLabels {
pub name: String,
Expand Down Expand Up @@ -431,6 +436,7 @@ fn format_plex_session_metrics(
let plex_sessions = Family::<PlexSessionLabels, Gauge<f64, AtomicU64>>::default();
let plex_sessions_percentage =
Family::<PlexSessionPercentageLabels, Gauge<f64, AtomicU64>>::default();
let plex_session_bandwidth = Family::<PlexSessionBandwidth, Gauge<f64, AtomicU64>>::default();
registry.register(
"plex_sessions",
format!("Plex sessions status"),
Expand All @@ -441,6 +447,11 @@ fn format_plex_session_metrics(
format!("Plex sessions percentage status"),
plex_sessions_percentage.clone(),
);
registry.register(
"plex_session_bandwidth",
format!("Plex session bandwidth"),
plex_session_bandwidth.clone(),
);

sessions.into_iter().for_each(|(name, sessions)| {
sessions.into_iter().for_each(|session| {
Expand Down Expand Up @@ -488,6 +499,12 @@ fn format_plex_session_metrics(
latitude: session.location.latitude.clone(),
})
.set(1.0);
plex_session_bandwidth
.get_or_create(&PlexSessionBandwidth {
user: session.user.clone(),
location: session.bandwidth.location.to_string(),
})
.set(session.bandwidth.bandwidth as f64);
});
});
}
Expand Down
104 changes: 52 additions & 52 deletions src/providers/structs/plex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,58 +159,27 @@ impl SessionMetadata {
Some(parent) => parent.to_string(),
None => self.title.clone(),
};
match &media_type[..] {
"episode" => PlexSessions {
title,
user,
stream_decision,
media_type,
state,
progress,
quality,
season_number,
episode_number,
location,
address,
local,
secure,
relayed,
platform,
},
"movie" => PlexSessions {
title,
user,
stream_decision,
media_type,
state,
progress,
quality,
season_number: None,
episode_number: None,
location,
address,
local,
secure,
relayed,
platform,
},
_ => PlexSessions {
title,
user,
stream_decision,
media_type,
state,
progress,
quality,
season_number: None,
episode_number: None,
location,
address,
local,
secure,
relayed,
platform,
},
let bandwidth = Bandwidth {
bandwidth: self.session.bandwidth,
location: self.session.location.clone().into(),
};
PlexSessions {
title,
user,
stream_decision,
media_type,
state,
progress,
quality,
season_number,
episode_number,
location,
address,
local,
secure,
relayed,
platform,
bandwidth,
}
}
}
Expand Down Expand Up @@ -261,6 +230,7 @@ pub struct Player {
#[serde(rename_all = "camelCase")]
pub struct Session {
pub location: String,
pub bandwidth: i64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Location {
Expand Down Expand Up @@ -307,6 +277,36 @@ pub struct PlexSessions {
pub secure: bool,
pub relayed: bool,
pub platform: String,
pub bandwidth: Bandwidth,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Bandwidth {
pub bandwidth: i64,
pub location: BandwidthLocation,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BandwidthLocation {
Wan,
Lan,
}
impl From<String> for BandwidthLocation {
fn from(location: String) -> Self {
match location.as_str() {
"wan" => BandwidthLocation::Wan,
"lan" => BandwidthLocation::Lan,
_ => BandwidthLocation::Wan,
}
}
}
impl Display for BandwidthLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BandwidthLocation::Wan => write!(f, "WAN"),
BandwidthLocation::Lan => write!(f, "LAN"),
}
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down

0 comments on commit e9f3c5f

Please sign in to comment.