Files
tg-archive/src/archive_stats.rs
T
Elias Wendland 126b9e9e9f
CI and release / Detect release commit (push) Successful in 27s
CI and release / Run tests (push) Successful in 4m14s
CI and release / Build and publish container (push) Successful in 9m2s
CI and release / Create release (push) Skipped
Large 0.3 changes
- Split the gigantic mess that web.rs was into seperate files
- Split the app into two clear functions: web and bot
- Add privacy policy
- Add privacy
- Make the site respect GDPR and Discord ToS
- Add basic API
- Add timezone detection and automatic time changes
2026-08-31 17:10:02 +02:00

58 lines
1.6 KiB
Rust

use sqlx::PgPool;
pub struct ArchiveStats {
pub messages: i64,
pub users: i64,
pub servers: i64,
pub channels: i64,
pub total_storage: i64,
pub message_storage: i64,
pub attachment_storage: i64,
}
pub async fn load(pool: &PgPool) -> Result<ArchiveStats, sqlx::Error> {
let stats = sqlx::query_as::<_, (i64, i64, i64, i64, i64, i64, i64)>(
"SELECT
(SELECT COUNT(*) FROM messages),
(SELECT COUNT(DISTINCT author_id) FROM messages),
(SELECT COUNT(*) FROM guilds),
(SELECT COUNT(*) FROM channels),
(SELECT COALESCE(SUM(OCTET_LENGTH(content)), 0)
FROM message_versions)
+ (SELECT COALESCE(SUM(OCTET_LENGTH(data)), 0)
FROM attachments),
(SELECT COALESCE(SUM(OCTET_LENGTH(content)), 0)
FROM message_versions),
(SELECT COALESCE(SUM(OCTET_LENGTH(data)), 0)
FROM attachments);",
)
.fetch_one(pool)
.await?;
Ok(ArchiveStats {
messages: stats.0,
users: stats.1,
servers: stats.2,
channels: stats.3,
total_storage: stats.4,
message_storage: stats.5,
attachment_storage: stats.6,
})
}
pub fn format_bytes(bytes: i64) -> String {
const UNITS: [&str; 5] = ["bytes", "KB", "MB", "GB", "TB"];
let mut size = bytes.max(0) as f64;
let mut unit = 0;
while size >= 1024.0 && unit < UNITS.len() - 1 {
size /= 1024.0;
unit += 1;
}
if unit == 0 {
format!("{} {}", bytes.max(0), UNITS[unit])
} else {
format!("{:.1} {}", size, UNITS[unit])
}
}