Bring back stats collection

This commit is contained in:
DaXcess
2023-09-19 13:49:32 +02:00
parent 2e273cdcde
commit 1c38067785
7 changed files with 108 additions and 9 deletions
+30 -1
View File
@@ -18,6 +18,12 @@ mod player;
mod session;
mod utils;
#[cfg(feature = "stats")]
mod stats;
#[cfg(feature = "stats")]
use crate::stats::StatsManager;
#[tokio::main]
async fn main() {
if std::env::var("RUST_LOG").is_err() {
@@ -51,6 +57,11 @@ async fn main() {
let token = env::var("DISCORD_TOKEN").expect("a token in the environment");
let db_url = env::var("DATABASE_URL").expect("a database URL in the environment");
#[cfg(feature = "stats")]
let stats_manager =
StatsManager::new(env::var("KV_URL").expect("a redis URL in the environment"))
.expect("Failed to connect to redis");
let session_manager = SessionManager::new();
// Create client
@@ -73,7 +84,9 @@ async fn main() {
}
let shard_manager = client.shard_manager.clone();
let _cache = client.cache_and_http.cache.clone();
#[cfg(feature = "stats")]
let cache = client.cache_and_http.cache.clone();
#[cfg(unix)]
let mut term: Option<Box<dyn Any + Send>> = Some(Box::new(
@@ -88,6 +101,22 @@ async fn main() {
tokio::spawn(async move {
loop {
tokio::select! {
_ = tokio::time::sleep(std::time::Duration::from_secs(60)) => {
#[cfg(feature = "stats")]
{
let guild_count = cache.guilds().len();
let active_count = session_manager.get_active_session_count().await;
if let Err(why) = stats_manager.set_server_count(guild_count) {
error!("Failed to update server count: {why}");
}
if let Err(why) = stats_manager.set_active_count(active_count) {
error!("Failed to update active count: {why}");
}
}
}
_ = tokio::signal::ctrl_c() => {
info!("Received interrupt signal, shutting down...");
+26
View File
@@ -0,0 +1,26 @@
use redis::{Client, Commands, RedisResult as Result};
#[derive(Clone)]
pub struct StatsManager {
redis: Client,
}
impl StatsManager {
pub fn new(url: impl AsRef<str>) -> Result<Self> {
let redis = Client::open(url.as_ref())?;
Ok(StatsManager { redis })
}
pub fn set_server_count(&self, count: usize) -> Result<()> {
let mut con = self.redis.get_connection()?;
con.set("sc-bot-total-servers", count.to_string())
}
pub fn set_active_count(&self, count: usize) -> Result<()> {
let mut con = self.redis.get_connection()?;
con.set("sc-bot-active-servers", count.to_string())
}
}