added cache to prevent the server from blocking itself if too many requests
This commit is contained in:
281
src/handlers.rs
281
src/handlers.rs
@@ -5,7 +5,7 @@ use axum::{
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc, time::Instant};
|
||||||
use tera::Context;
|
use tera::Context;
|
||||||
|
|
||||||
use crate::{AppState, TEMPLATES};
|
use crate::{AppState, TEMPLATES};
|
||||||
@@ -16,7 +16,7 @@ pub struct SortParams {
|
|||||||
pub order: Option<String>,
|
pub order: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Report {
|
pub struct Report {
|
||||||
pub id: u64,
|
pub id: u64,
|
||||||
pub mac_address: String,
|
pub mac_address: String,
|
||||||
@@ -44,14 +44,14 @@ pub struct ReportRow {
|
|||||||
pub is_new: bool,
|
pub is_new: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Benchmark {
|
pub struct Benchmark {
|
||||||
pub logical_cores: u32,
|
pub logical_cores: u32,
|
||||||
pub multi_thread: ScoreDetail,
|
pub multi_thread: ScoreDetail,
|
||||||
pub single_thread: ScoreDetail,
|
pub single_thread: ScoreDetail,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct ScoreDetail {
|
pub struct ScoreDetail {
|
||||||
pub score: u64,
|
pub score: u64,
|
||||||
}
|
}
|
||||||
@@ -100,134 +100,9 @@ fn parse_total_ram_raw(ram_str: &str) -> f32 {
|
|||||||
digits.parse::<f32>().unwrap_or(0.0) / 1024.0
|
digits.parse::<f32>().unwrap_or(0.0) / 1024.0
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn home_handler(
|
/// Map raw Report to ReportRow for UI
|
||||||
State(state): State<Arc<AppState>>,
|
fn process_reports(raw_reports: Vec<Report>) -> Vec<ReportRow> {
|
||||||
Query(params): Query<SortParams>,
|
raw_reports
|
||||||
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
|
||||||
let response = reqwest::get("https://alatreon.org/slimes?limit=1000")
|
|
||||||
.await
|
|
||||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
||||||
|
|
||||||
let raw_reports = response.json::<Vec<Report>>().await.map_err(|e| {
|
|
||||||
(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
format!("JSON Error: {}", e),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut reports: Vec<ReportRow> = raw_reports
|
|
||||||
.into_iter()
|
|
||||||
.map(|s| ReportRow {
|
|
||||||
id: s.id,
|
|
||||||
score: (s.benchmark.multi_thread.score + s.benchmark.single_thread.score) / 2,
|
|
||||||
single_score: s.benchmark.single_thread.score,
|
|
||||||
multi_score: s.benchmark.multi_thread.score,
|
|
||||||
cores: s.benchmark.logical_cores,
|
|
||||||
ram: s
|
|
||||||
.slimes
|
|
||||||
.get("RAM")
|
|
||||||
.and_then(|v| v.first())
|
|
||||||
.map(|r| parse_total_ram(r))
|
|
||||||
.unwrap_or_else(|| "-".into()),
|
|
||||||
ram_raw: s
|
|
||||||
.slimes
|
|
||||||
.get("RAM")
|
|
||||||
.and_then(|v| v.first())
|
|
||||||
.map(|r| parse_total_ram_raw(r))
|
|
||||||
.unwrap_or_else(|| 0.0),
|
|
||||||
os: s
|
|
||||||
.slimes
|
|
||||||
.get("OS")
|
|
||||||
.and_then(|v| v.first())
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_else(|| "Unknown".into()),
|
|
||||||
hostname: s
|
|
||||||
.slimes
|
|
||||||
.get("Hostname")
|
|
||||||
.and_then(|v| v.first())
|
|
||||||
.cloned()
|
|
||||||
.unwrap_or_else(|| "-".into()),
|
|
||||||
signature: s.signature,
|
|
||||||
timestamp: s.timestamp.clone(),
|
|
||||||
time_ago: format_time_ago(&s.timestamp),
|
|
||||||
is_new: is_recent(&s.timestamp),
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let sort_field = params.sort.unwrap_or_else(|| "score".to_string());
|
|
||||||
let order = params.order.unwrap_or_else(|| "desc".to_string());
|
|
||||||
reports.sort_by(|a, b| {
|
|
||||||
let cmp = match sort_field.as_str() {
|
|
||||||
"single" => a.single_score.cmp(&b.single_score),
|
|
||||||
"multi" => a.multi_score.cmp(&b.multi_score),
|
|
||||||
"ram" => a
|
|
||||||
.ram_raw
|
|
||||||
.partial_cmp(&b.ram_raw)
|
|
||||||
.unwrap_or(std::cmp::Ordering::Equal),
|
|
||||||
"threads" => a.cores.cmp(&b.cores),
|
|
||||||
"time" => a.timestamp.cmp(&b.timestamp),
|
|
||||||
_ => a.score.cmp(&b.score),
|
|
||||||
};
|
|
||||||
if order == "asc" { cmp } else { cmp.reverse() }
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut context = Context::new();
|
|
||||||
context.insert("reports", &reports);
|
|
||||||
context.insert("title", &state.app_name);
|
|
||||||
context.insert("current_sort", &sort_field);
|
|
||||||
context.insert("current_order", &order);
|
|
||||||
context.insert("current_page", "leaderboard");
|
|
||||||
context.insert("prefix", "");
|
|
||||||
|
|
||||||
match TEMPLATES.render("index.html", &context) {
|
|
||||||
Ok(html) => Ok(Html(html)),
|
|
||||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn report_details_handler(
|
|
||||||
State(state): State<Arc<AppState>>,
|
|
||||||
Path(id): Path<u64>,
|
|
||||||
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
|
||||||
let url = format!("https://alatreon.org/slimes/{}", id);
|
|
||||||
|
|
||||||
let report = reqwest::get(url)
|
|
||||||
.await
|
|
||||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
|
||||||
.json::<Report>()
|
|
||||||
.await
|
|
||||||
.map_err(|e| (StatusCode::NOT_FOUND, format!("Report not found: {}", e)))?;
|
|
||||||
|
|
||||||
let mut context = Context::new();
|
|
||||||
context.insert("report", &report);
|
|
||||||
context.insert("title", &format!("Report #{} | {}", id, state.app_name));
|
|
||||||
context.insert("time_ago", &format_time_ago(&report.timestamp));
|
|
||||||
context.insert(
|
|
||||||
"score",
|
|
||||||
&((report.benchmark.multi_thread.score + report.benchmark.single_thread.score) / 2),
|
|
||||||
);
|
|
||||||
context.insert("current_page", "details");
|
|
||||||
context.insert("prefix", "../");
|
|
||||||
|
|
||||||
match TEMPLATES.render("details.html", &context) {
|
|
||||||
Ok(html) => Ok(Html(html)),
|
|
||||||
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_processed_reports() -> Result<Vec<ReportRow>, (StatusCode, String)> {
|
|
||||||
let response = reqwest::get("https://alatreon.org/slimes?limit=10000")
|
|
||||||
.await
|
|
||||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
|
||||||
|
|
||||||
let raw_reports = response.json::<Vec<Report>>().await.map_err(|e| {
|
|
||||||
(
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
format!("JSON Error: {}", e),
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(raw_reports
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
let ram_raw = s
|
let ram_raw = s
|
||||||
@@ -267,7 +142,137 @@ async fn get_processed_reports() -> Result<Vec<ReportRow>, (StatusCode, String)>
|
|||||||
is_new: is_recent(&s.timestamp),
|
is_new: is_recent(&s.timestamp),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect())
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves reports from cache if fresh, otherwise fetches from API
|
||||||
|
async fn get_cached_reports(state: &AppState) -> Result<Vec<Report>, (StatusCode, String)> {
|
||||||
|
{
|
||||||
|
let cache_read = state.reports_cache.read().await;
|
||||||
|
if let Some(cache) = &*cache_read {
|
||||||
|
if cache.updated_at.elapsed() < state.cache_duration {
|
||||||
|
return Ok(cache.data.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut cache_write = state.reports_cache.write().await;
|
||||||
|
|
||||||
|
// Double-check?
|
||||||
|
if let Some(cache) = &*cache_write {
|
||||||
|
if cache.updated_at.elapsed() < state.cache_duration {
|
||||||
|
return Ok(cache.data.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tracing::debug!("Refreshing reports cache from API...");
|
||||||
|
let response = reqwest::get("https://alatreon.org/slimes?limit=10000")
|
||||||
|
.await
|
||||||
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
|
|
||||||
|
let fresh_reports = response.json::<Vec<Report>>().await.map_err(|e| {
|
||||||
|
(
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
format!("JSON Error: {}", e),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// NOTE: to be clear we ARE storing all the reports in RAM
|
||||||
|
*cache_write = Some(crate::Cache {
|
||||||
|
data: fresh_reports.clone(),
|
||||||
|
updated_at: Instant::now(),
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(fresh_reports)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn home_handler(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
Query(params): Query<SortParams>,
|
||||||
|
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
||||||
|
let raw_reports = get_cached_reports(&state).await?;
|
||||||
|
let mut reports = process_reports(raw_reports);
|
||||||
|
|
||||||
|
let sort_field = params.sort.unwrap_or_else(|| "score".to_string());
|
||||||
|
let order = params.order.unwrap_or_else(|| "desc".to_string());
|
||||||
|
|
||||||
|
reports.sort_by(|a, b| {
|
||||||
|
let cmp = match sort_field.as_str() {
|
||||||
|
"single" => a.single_score.cmp(&b.single_score),
|
||||||
|
"multi" => a.multi_score.cmp(&b.multi_score),
|
||||||
|
"ram" => a
|
||||||
|
.ram_raw
|
||||||
|
.partial_cmp(&b.ram_raw)
|
||||||
|
.unwrap_or(std::cmp::Ordering::Equal),
|
||||||
|
"threads" => a.cores.cmp(&b.cores),
|
||||||
|
"time" => a.timestamp.cmp(&b.timestamp),
|
||||||
|
_ => a.score.cmp(&b.score),
|
||||||
|
};
|
||||||
|
if order == "asc" { cmp } else { cmp.reverse() }
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut context = Context::new();
|
||||||
|
context.insert("reports", &reports);
|
||||||
|
context.insert("title", &state.app_name);
|
||||||
|
context.insert("current_sort", &sort_field);
|
||||||
|
context.insert("current_order", &order);
|
||||||
|
context.insert("current_page", "leaderboard");
|
||||||
|
context.insert("prefix", "");
|
||||||
|
|
||||||
|
match TEMPLATES.render("index.html", &context) {
|
||||||
|
Ok(html) => Ok(Html(html)),
|
||||||
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn report_details_handler(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
Path(id): Path<u64>,
|
||||||
|
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
||||||
|
// Check if the report exists in our current cache to avoid API call
|
||||||
|
{
|
||||||
|
let cache = state.reports_cache.read().await;
|
||||||
|
if let Some(c) = &*cache {
|
||||||
|
if let Some(report) = c.data.iter().find(|r| r.id == id) {
|
||||||
|
return render_details(report.clone(), &state).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: fetch specifically if not in list
|
||||||
|
let url = format!("https://alatreon.org/slimes/{}", id);
|
||||||
|
let report = reqwest::get(url)
|
||||||
|
.await
|
||||||
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||||
|
.json::<Report>()
|
||||||
|
.await
|
||||||
|
.map_err(|e| (StatusCode::NOT_FOUND, format!("Report not found: {}", e)))?;
|
||||||
|
|
||||||
|
render_details(report, &state).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn render_details(
|
||||||
|
report: Report,
|
||||||
|
state: &AppState,
|
||||||
|
) -> Result<impl IntoResponse + use<>, (StatusCode, String)> {
|
||||||
|
let mut context = Context::new();
|
||||||
|
context.insert("report", &report);
|
||||||
|
context.insert(
|
||||||
|
"title",
|
||||||
|
&format!("Report #{} | {}", report.id, state.app_name),
|
||||||
|
);
|
||||||
|
context.insert("time_ago", &format_time_ago(&report.timestamp));
|
||||||
|
context.insert(
|
||||||
|
"score",
|
||||||
|
&((report.benchmark.multi_thread.score + report.benchmark.single_thread.score) / 2),
|
||||||
|
);
|
||||||
|
context.insert("current_page", "details");
|
||||||
|
context.insert("prefix", "../");
|
||||||
|
|
||||||
|
match TEMPLATES.render("details.html", &context) {
|
||||||
|
Ok(html) => Ok(Html(html)),
|
||||||
|
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -290,7 +295,9 @@ pub struct AnalyticsData {
|
|||||||
pub async fn analytics_handler(
|
pub async fn analytics_handler(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
||||||
let reports = get_processed_reports().await?;
|
let raw_reports = get_cached_reports(&state).await?;
|
||||||
|
let reports = process_reports(raw_reports);
|
||||||
|
|
||||||
if reports.is_empty() {
|
if reports.is_empty() {
|
||||||
return Err((StatusCode::NOT_FOUND, "No data available".into()));
|
return Err((StatusCode::NOT_FOUND, "No data available".into()));
|
||||||
}
|
}
|
||||||
@@ -332,7 +339,6 @@ pub async fn analytics_handler(
|
|||||||
})
|
})
|
||||||
.or_insert(r.score);
|
.or_insert(r.score);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut os_min_stats: Vec<(String, u64)> = os_min_map.into_iter().collect();
|
let mut os_min_stats: Vec<(String, u64)> = os_min_map.into_iter().collect();
|
||||||
os_min_stats.sort_by(|a, b| b.1.cmp(&a.1));
|
os_min_stats.sort_by(|a, b| b.1.cmp(&a.1));
|
||||||
|
|
||||||
@@ -343,10 +349,9 @@ pub async fn analytics_handler(
|
|||||||
.collect();
|
.collect();
|
||||||
os_counts.sort_by(|a, b| b.1.cmp(&a.1));
|
os_counts.sort_by(|a, b| b.1.cmp(&a.1));
|
||||||
|
|
||||||
// Score distribution
|
// Score distribution
|
||||||
let min_score = reports.iter().map(|r| r.score).min().unwrap_or(0);
|
let min_score = reports.iter().map(|r| r.score).min().unwrap_or(0);
|
||||||
let max_score = reports.iter().map(|r| r.score).max().unwrap_or(0);
|
let max_score = reports.iter().map(|r| r.score).max().unwrap_or(0);
|
||||||
|
|
||||||
let range = (max_score - min_score).max(1);
|
let range = (max_score - min_score).max(1);
|
||||||
let bin_width = range as f32 / 10.0;
|
let bin_width = range as f32 / 10.0;
|
||||||
|
|
||||||
@@ -367,7 +372,7 @@ pub async fn analytics_handler(
|
|||||||
dist_counts[bin_idx] += 1;
|
dist_counts[bin_idx] += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ratios
|
// Efficiency Ratios
|
||||||
let total_reports = reports.len();
|
let total_reports = reports.len();
|
||||||
let avg_score_per_gb = reports
|
let avg_score_per_gb = reports
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
17
src/main.rs
17
src/main.rs
@@ -1,13 +1,17 @@
|
|||||||
use axum::{Router, routing::get};
|
use axum::{Router, routing::get};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use std::sync::Arc;
|
use std::{
|
||||||
|
sync::Arc,
|
||||||
|
time::{Duration, Instant},
|
||||||
|
};
|
||||||
use tera::Tera;
|
use tera::Tera;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
mod handlers;
|
mod handlers;
|
||||||
use handlers::home_handler;
|
use handlers::home_handler;
|
||||||
|
|
||||||
use crate::handlers::{analytics_handler, report_details_handler};
|
use crate::handlers::{Report, analytics_handler, report_details_handler};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref TEMPLATES: Tera = {
|
pub static ref TEMPLATES: Tera = {
|
||||||
@@ -43,8 +47,15 @@ pub struct Cli {
|
|||||||
verbose: bool,
|
verbose: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Cache<T> {
|
||||||
|
pub data: T,
|
||||||
|
pub updated_at: Instant,
|
||||||
|
}
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
app_name: String,
|
app_name: String,
|
||||||
|
pub reports_cache: RwLock<Option<Cache<Vec<Report>>>>,
|
||||||
|
pub cache_duration: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -57,6 +68,8 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
let shared_state = Arc::new(AppState {
|
let shared_state = Arc::new(AppState {
|
||||||
app_name: "Slimes Leaderboards".to_string(),
|
app_name: "Slimes Leaderboards".to_string(),
|
||||||
|
reports_cache: RwLock::new(None),
|
||||||
|
cache_duration: Duration::from_secs(30),
|
||||||
});
|
});
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
|
|||||||
@@ -232,7 +232,7 @@
|
|||||||
position: absolute;
|
position: absolute;
|
||||||
top: 1rem;
|
top: 1rem;
|
||||||
right: 1rem;
|
right: 1rem;
|
||||||
background: var(--primary-soft);
|
background: var(--bg-main);
|
||||||
border: 1px solid var(--primary-border);
|
border: 1px solid var(--primary-border);
|
||||||
color: var(--primary-color);
|
color: var(--primary-color);
|
||||||
padding: 0.4rem 0.7rem;
|
padding: 0.4rem 0.7rem;
|
||||||
@@ -245,8 +245,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.expand-btn:hover {
|
.expand-btn:hover {
|
||||||
background: var(--primary-color);
|
background: var(--primary-soft);
|
||||||
color: white;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.modal {
|
.modal {
|
||||||
@@ -282,7 +281,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.close-modal {
|
.close-modal {
|
||||||
background: var(--primary-soft);
|
background: var(--bg-main);
|
||||||
border: 1px solid var(--primary-border);
|
border: 1px solid var(--primary-border);
|
||||||
color: var(--primary-color);
|
color: var(--primary-color);
|
||||||
width: 32px;
|
width: 32px;
|
||||||
@@ -296,8 +295,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.close-modal:hover {
|
.close-modal:hover {
|
||||||
background: var(--primary-color);
|
background: var(--primary-soft);
|
||||||
color: white;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.modal-body {
|
.modal-body {
|
||||||
|
|||||||
Reference in New Issue
Block a user