added analytics page

This commit is contained in:
2026-04-04 13:04:26 +02:00
parent 5f58664b6d
commit a82b6da9ca
6 changed files with 454 additions and 3 deletions

1
Cargo.lock generated
View File

@@ -1882,6 +1882,7 @@ dependencies = [
"lazy_static",
"reqwest",
"serde",
"serde_json",
"tera",
"tokio",
"tower-http",

View File

@@ -17,3 +17,4 @@ tower_governor = "0.8.0"
serde = { version = "1.0.228", features = ["derive"] }
chrono = { version = "0.4.44", features = ["serde"] }
reqwest = { version = "0.13.2", features = ["json"] }
serde_json = "1.0.149"

View File

@@ -156,7 +156,6 @@ pub async fn home_handler(
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),
@@ -169,7 +168,6 @@ pub async fn home_handler(
"time" => a.timestamp.cmp(&b.timestamp),
_ => a.score.cmp(&b.score),
};
if order == "asc" { cmp } else { cmp.reverse() }
});
@@ -178,6 +176,7 @@ pub async fn home_handler(
context.insert("title", &state.app_name);
context.insert("current_sort", &sort_field);
context.insert("current_order", &order);
context.insert("current_page", "leaderboard"); // NEW
match TEMPLATES.render("index.html", &context) {
Ok(html) => Ok(Html(html)),
@@ -212,3 +211,189 @@ pub async fn report_details_handler(
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=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),
)
})?;
Ok(raw_reports
.into_iter()
.map(|s| {
let ram_raw = s
.slimes
.get("RAM")
.and_then(|v| v.first())
.map(|r| parse_total_ram_raw(r))
.unwrap_or(0.0);
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,
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())
}
#[derive(Serialize)]
pub struct AnalyticsData {
pub os_labels: Vec<String>,
pub os_scores: Vec<u64>,
pub os_max_labels: Vec<String>,
pub os_max_scores: Vec<u64>,
pub os_min_labels: Vec<String>,
pub os_min_scores: Vec<u64>,
pub dist_labels: Vec<String>,
pub dist_counts: Vec<u32>,
pub avg_score_per_gb: f32,
pub avg_score_per_thread: f32,
pub total_reports: usize,
}
pub async fn analytics_handler(
State(state): State<Arc<AppState>>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
let reports = get_processed_reports().await?;
if reports.is_empty() {
return Err((StatusCode::NOT_FOUND, "No data available".into()));
}
// Average score per OS
let mut os_map: HashMap<String, (u64, u32)> = HashMap::new();
for r in &reports {
let entry = os_map.entry(r.os.clone()).or_insert((0, 0));
entry.0 += r.score;
entry.1 += 1;
}
let mut os_stats: Vec<(String, u64)> = os_map
.into_iter()
.map(|(os, (sum, count))| (os, sum / count as u64))
.collect();
os_stats.sort_by(|a, b| b.1.cmp(&a.1));
// Highest score per OS
let mut os_max_map: HashMap<String, u64> = HashMap::new();
for r in &reports {
let entry = os_max_map.entry(r.os.clone()).or_insert(0);
if r.score > *entry {
*entry = r.score;
}
}
let mut os_max_stats: Vec<(String, u64)> = os_max_map.into_iter().collect();
os_max_stats.sort_by(|a, b| b.1.cmp(&a.1));
// Lowest score per OS
let mut os_min_map: HashMap<String, u64> = HashMap::new();
for r in &reports {
os_min_map
.entry(r.os.clone())
.and_modify(|e| {
if r.score < *e {
*e = r.score;
}
})
.or_insert(r.score);
}
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));
// Score distribution
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 range = (max_score - min_score).max(1);
let bin_width = range as f32 / 10.0;
let mut dist_counts = vec![0u32; 10];
let mut dist_labels = vec![String::new(); 10];
for i in 0..10 {
let start = min_score + (i as f32 * bin_width) as u64;
let end = min_score + ((i + 1) as f32 * bin_width) as u64;
dist_labels[i] = format!("{}-{}", start, end);
}
for r in &reports {
let mut bin_idx = ((r.score - min_score) as f32 / bin_width).floor() as usize;
if bin_idx >= 10 {
bin_idx = 9;
}
dist_counts[bin_idx] += 1;
}
// Ratios
let total_reports = reports.len();
let avg_score_per_gb = reports
.iter()
.filter(|r| r.ram_raw > 0.0)
.map(|r| r.score as f32 / r.ram_raw)
.sum::<f32>()
/ total_reports as f32;
let avg_score_per_thread = reports
.iter()
.filter(|r| r.cores > 0)
.map(|r| r.score as f32 / r.cores as f32)
.sum::<f32>()
/ total_reports as f32;
let data = AnalyticsData {
os_labels: os_stats.iter().map(|x| x.0.clone()).collect(),
os_scores: os_stats.iter().map(|x| x.1).collect(),
os_max_labels: os_max_stats.iter().map(|x| x.0.clone()).collect(),
os_max_scores: os_max_stats.iter().map(|x| x.1).collect(),
os_min_labels: os_min_stats.iter().map(|x| x.0.clone()).collect(),
os_min_scores: os_min_stats.iter().map(|x| x.1).collect(),
dist_labels,
dist_counts,
avg_score_per_gb,
avg_score_per_thread,
total_reports,
};
let json_data = serde_json::to_string(&data)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
let mut context = Context::new();
context.insert("title", &format!("Analytics | {}", state.app_name));
context.insert("json_data", &json_data);
context.insert("data", &data);
context.insert("current_page", "analytics");
match TEMPLATES.render("analytics.html", &context) {
Ok(html) => Ok(Html(html)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}

View File

@@ -7,7 +7,7 @@ use tera::Tera;
mod handlers;
use handlers::home_handler;
use crate::handlers::report_details_handler;
use crate::handlers::{analytics_handler, report_details_handler};
lazy_static! {
pub static ref TEMPLATES: Tera = {
@@ -17,6 +17,10 @@ lazy_static! {
("_base.css", include_str!("../templates/_base.css")),
("index.html", include_str!("../templates/homepage.html")),
("details.html", include_str!("../templates/details.html")),
(
"analytics.html",
include_str!("../templates/analytics.html"),
),
])
.unwrap();
tera
@@ -58,6 +62,7 @@ async fn main() -> anyhow::Result<()> {
let app = Router::new()
.route("/", get(home_handler))
.route("/report/{id}", get(report_details_handler))
.route("/analytics", get(analytics_handler))
.with_state(shared_state);
let addr = if cli.host {

View File

@@ -9,9 +9,68 @@
</style>
</head>
<body>
<nav class="navbar">
<div class="nav-content">
<span class="nav-logo">Slimes 🧪</span>
<div class="nav-links">
<a href="/" class="{% if current_page == 'leaderboard' %}active{% endif %}">Leaderboard</a>
<a href="/analytics" class="{% if current_page == 'analytics' %}active{% endif %}">Analytics</a>
</div>
</div>
</nav>
<main>
{% block content %}{% endblock %}
</main>
<footer>{{ now() | date(format="%Y") }} - Slimes</footer>
</body>
<style>
.navbar {
background: var(--bg-card);
border-bottom: 1px solid var(--border-color);
padding: 0.75rem 1rem;
position: sticky;
top: 0;
z-index: 100;
}
.nav-content {
max-width: 1400px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-logo {
font-weight: 800;
color: var(--primary-color);
font-size: 1.2rem;
}
.nav-links {
display: flex;
gap: 1.5rem;
}
.nav-links a {
text-decoration: none;
color: var(--text-muted);
font-weight: 600;
font-size: 0.9rem;
transition: color 0.2s;
padding: 0.5rem 0;
border-bottom: 2px solid transparent;
}
.nav-links a:hover {
color: var(--primary-color);
}
.nav-links a.active {
color: var(--primary-color);
border-bottom-color: var(--primary-color);
}
</style>
</html>

200
templates/analytics.html Normal file
View File

@@ -0,0 +1,200 @@
{% extends "_layout.html" %}
{% block content %}
<div class="header-section">
<h2>Benchmark Analytics</h2>
<p style="text-align: center; color: var(--text-muted);">Insights from {{ data.total_reports }} benchmarks</p>
</div>
<div class="analytics-grid">
<div class="fact-card">
<span class="fact-title">RAM Efficiency</span>
<span class="fact-value">{{ data.avg_score_per_gb | round(precision=1) }}</span>
<span class="fact-label">Avg Score per GB RAM</span>
</div>
<div class="fact-card">
<span class="fact-title">CPU Thread Efficiency</span>
<span class="fact-value">{{ data.avg_score_per_thread | round(precision=1) }}</span>
<span class="fact-label">Avg Score per Thread</span>
</div>
<div class="chart-card">
<h3>Score Distribution</h3>
<canvas id="distChart"></canvas>
</div>
<div class="chart-card">
<h3>Avg Score by Operating System</h3>
<canvas id="osChart"></canvas>
</div>
<div class="chart-card">
<h3>Highest Score by Operating System</h3>
<canvas id="osMaxChart"></canvas>
</div>
<div class="chart-card">
<h3>Lowest Score by Operating System</h3>
<canvas id="osMinChart"></canvas>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const analyticsData = {{ json_data | safe }};
const primaryColor = '#d946ef';
const secondaryColor = '#a21caf';
const chartOptions = {
responsive: true,
maintainAspectRatio: true,
aspectRatio: 1.5,
plugins: {
legend: { display: false }
}
};
// Average per OS
new Chart(document.getElementById('osChart'), {
type: 'bar',
data: {
labels: analyticsData.os_labels,
datasets: [{
label: 'Average Score',
data: analyticsData.os_scores,
backgroundColor: primaryColor,
borderRadius: 6
}]
},
options: {
...chartOptions,
indexAxis: 'y',
}
});
// Highest per OS
new Chart(document.getElementById('osMaxChart'), {
type: 'bar',
data: {
labels: analyticsData.os_max_labels,
datasets: [{
label: 'Highest Score',
data: analyticsData.os_max_scores,
backgroundColor: secondaryColor,
borderRadius: 6
}]
},
options: {
...chartOptions,
indexAxis: 'y',
}
});
// Lowest per OS
new Chart(document.getElementById('osMinChart'), {
type: 'bar',
data: {
labels: analyticsData.os_min_labels,
datasets: [{
label: 'Lowest Score',
data: analyticsData.os_min_scores,
backgroundColor: secondaryColor,
borderRadius: 6
}]
},
options: {
...chartOptions,
indexAxis: 'y',
}
});
// Score distribution
new Chart(document.getElementById('distChart'), {
type: 'bar',
data: {
labels: analyticsData.dist_labels,
datasets: [{
label: 'Reports',
data: analyticsData.dist_counts,
backgroundColor: 'rgba(217, 70, 239, 0.6)',
borderColor: primaryColor,
borderWidth: 2,
borderRadius: 4,
barPercentage: 1.0,
categoryPercentage: 0.9
}]
},
options: {
...chartOptions,
scales: {
x: { grid: { display: false } },
y: { beginAtZero: true, ticks: { stepSize: 1 } }
}
}
});
</script>
<style>
.header-section h2 { margin-bottom: 0.5rem; }
.chart-card h3 {
font-size: 0.9rem;
text-transform: uppercase;
color: var(--text-muted);
margin-bottom: 1.5rem;
}
.analytics-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 500px), 1fr));
gap: 1.5rem;
margin-top: 2rem;
}
.chart-card {
background: var(--bg-card);
padding: 1.5rem;
border-radius: 12px;
border: 1px solid var(--border-color);
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
min-width: 0;
overflow: hidden;
}
.fact-card {
background: var(--bg-card);
color: var(--primary-color);
padding: 2rem;
border-radius: 12px;
text-align: center;
}
.fact-value {
font-size: 2.5rem;
font-weight: 800;
display: block;
}
.fact-title {
font-size: 0.9rem;
opacity: 0.9;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1px;
}
.fact-label {
font-size: 0.9rem;
opacity: 0.9;
/* text-transform: uppercase; */
/* letter-spacing: 1px; */
}
@media (max-width: 900px) {
.analytics-grid {
grid-template-columns: 1fr;
}
}
</style>
{% endblock %}