icecast_stats/
icecast_stats.rs

1use crate::IcecastStatsSource;
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "chrono")]
5use crate::parser::parse_datetime;
6#[cfg(feature = "chrono")]
7use chrono::{DateTime, FixedOffset};
8
9/// Root element of the icecast information
10#[derive(Serialize, Deserialize, Debug)]
11pub struct IcecastStatsRoot {
12    pub icestats: IcecastStats,
13}
14
15/// Basic server information
16#[derive(Serialize, Deserialize, Debug, Clone)]
17pub struct IcecastStats {
18    admin: String,
19    #[serde(rename = "banned_IPs")]
20    banned_ips: Option<u32>,
21    client_connections: Option<u32>,
22    clients: Option<u32>,
23    connections: Option<u32>,
24    file_connections: Option<u32>,
25    host: String,
26    listener_connections: Option<u32>,
27    listeners: Option<u32>,
28    location: String,
29    server_id: String,
30    source_client_connections: Option<u32>,
31    source_relay_connections: Option<u32>,
32    source_total_connections: Option<u32>,
33    sources: Option<u32>,
34    stats: Option<u32>,
35    stats_connections: Option<u32>,
36    server_start: Option<String>,
37    server_start_iso8601: Option<String>,
38
39    /// List of active streaming sources
40    source: Option<IcecastStatsSourceEnum>,
41}
42
43impl IcecastStats {
44    /// As set in the server config, this should contain contact details for getting in touch
45    /// with the server administrator. Usually this will be an email address, but as
46    /// this can be an arbitrary string it could also be a phone number.
47    ///
48    /// Example: icemaster@localhost
49    pub fn admin(&self) -> &String {
50        &self.admin
51    }
52
53    /// Client connections are basically anything that is not a source connection. These include listeners (not concurrent, but cumulative), any admin function accesses, and any static content (file serving) accesses.
54    /// This is an accumulating counter.
55    pub fn client_connections(&self) -> &Option<u32> {
56        &self.client_connections
57    }
58
59    /// Number of currently active client connections.
60    pub fn clients(&self) -> &Option<u32> {
61        &self.clients
62    }
63
64    /// The total of all inbound TCP connections since start-up.
65    /// This is an accumulating counter.
66    pub fn connections(&self) -> &Option<u32> {
67        &self.connections
68    }
69
70    /// This is an accumulating counter.
71    pub fn file_connections(&self) -> &Option<u32> {
72        &self.file_connections
73    }
74
75    /// As set in the server config, this should be the full DNS resolveable name or FQDN for the host on which this Icecast instance is running.
76    ///
77    /// Example: localhost
78    pub fn host(&self) -> &String {
79        &self.host
80    }
81
82    /// Number of listener connections to mount points.
83    /// This is an accumulating counter.
84    pub fn listener_connections(&self) -> &Option<u32> {
85        &self.listener_connections
86    }
87
88    /// Number of currently active listener connections.
89    pub fn listeners(&self) -> &Option<u32> {
90        &self.listeners
91    }
92
93    /// As set in the server config, this is a free form field that should describe e.g. the physical location of this server.
94    ///
95    /// Example: Earth
96    pub fn location(&self) -> &String {
97        &self.location
98    }
99
100    /// Defaults to the version string of the currently running Icecast server. While not recommended it can be overriden in the server config.
101    ///
102    /// Example: Icecast 2.4.4
103    pub fn server_id(&self) -> &String {
104        &self.server_id
105    }
106
107    /// Source client connections are the number of times (cumulative since start-up, not just currently connected) a source client has connected to Icecast.
108    /// This is an accumulating counter.
109    pub fn source_client_connections(&self) -> &Option<u32> {
110        &self.source_client_connections
111    }
112
113    /// Number of outbound relay connections to (master) icecast servers.
114    /// This is an accumulating counter.
115    pub fn source_relay_connections(&self) -> &Option<u32> {
116        &self.source_relay_connections
117    }
118
119    /// Both clients and relays.
120    /// This is an accumulating counter.
121    pub fn source_total_connections(&self) -> &Option<u32> {
122        &self.source_total_connections
123    }
124
125    /// The total of currently connected sources.
126    pub fn sources(&self) -> &Option<u32> {
127        &self.sources
128    }
129
130    /// The total of currently connected STATS clients.
131    pub fn stats(&self) -> &Option<u32> {
132        &self.stats
133    }
134
135    /// Number of times a stats client has connected to Icecast.
136    /// This is an accumulating counter.
137    pub fn stats_connections(&self) -> &Option<u32> {
138        &self.stats_connections
139    }
140
141    /// Timestamp of server startup as unformated string from the server.
142    ///
143    /// Example: 2021-04-09T21:49:50+0200
144    pub fn server_start(&self) -> Option<&String> {
145        if self.server_start_iso8601.is_some() {
146            return self.server_start_iso8601.as_ref();
147        }
148        self.server_start.as_ref()
149    }
150
151    /// Timestamp of server startup as decoded Chrono object
152    ///
153    #[cfg(feature = "chrono")]
154    pub fn server_start_decoded(
155        &self,
156    ) -> Option<Result<DateTime<FixedOffset>, chrono::ParseError>> {
157        self.server_start().clone().map(|x| parse_datetime(&x))
158    }
159
160    /// Returns clones of all sources
161    pub fn sources_vec(&self) -> Vec<IcecastStatsSource> {
162        match &self.source {
163            Some(source_enum) => match source_enum {
164                IcecastStatsSourceEnum::Single(icecast_stats_source) => {
165                    vec![icecast_stats_source.clone()]
166                }
167                IcecastStatsSourceEnum::Multiple(list) => list.clone(),
168            },
169            None => vec![],
170        }
171    }
172}
173
174/// Multiple variants of sources
175#[derive(Serialize, Deserialize, Debug, Clone)]
176#[serde(untagged)]
177pub enum IcecastStatsSourceEnum {
178    Single(IcecastStatsSource),
179    Multiple(Vec<IcecastStatsSource>),
180}