changed score calculation and added sorting and 'new' tag

This commit is contained in:
2026-03-27 21:50:19 +01:00
parent 012e6d7b48
commit 409583da1e
5 changed files with 220 additions and 27 deletions

View File

@@ -1,5 +1,5 @@
use axum::{
extract::{Path, State},
extract::{Path, Query, State},
response::{Html, IntoResponse},
};
use chrono::{DateTime, Utc};
@@ -10,6 +10,12 @@ use tera::Context;
use crate::{AppState, TEMPLATES};
#[derive(Debug, Serialize, Deserialize)]
pub struct SortParams {
pub sort: Option<String>,
pub order: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Report {
pub id: u64,
@@ -25,12 +31,17 @@ pub struct Report {
pub struct ReportRow {
pub id: u64,
pub score: u64,
pub single_score: u64,
pub multi_score: u64,
pub cores: u32,
pub ram: String,
pub ram_raw: f32,
pub os: String,
pub hostname: String,
pub time_ago: String,
pub timestamp: String,
pub signature: String,
pub is_new: bool,
}
#[derive(Debug, Serialize, Deserialize)]
@@ -63,6 +74,16 @@ fn format_time_ago(timestamp: &str) -> String {
}
}
fn is_recent(timestamp: &str) -> bool {
DateTime::parse_from_rfc3339(timestamp)
.map(|ts| {
let now = Utc::now();
let diff = now.signed_duration_since(ts.with_timezone(&Utc));
diff.num_hours() < 24 && diff.num_seconds() >= 0
})
.unwrap_or(false)
}
fn parse_total_ram(ram_str: &str) -> String {
let total_part = ram_str.split('/').last().unwrap_or("").trim();
let digits: String = total_part.chars().filter(|c| c.is_ascii_digit()).collect();
@@ -73,8 +94,15 @@ fn parse_total_ram(ram_str: &str) -> String {
}
}
fn parse_total_ram_raw(ram_str: &str) -> f32 {
let total_part = ram_str.split('/').last().unwrap_or("").trim();
let digits: String = total_part.chars().filter(|c| c.is_ascii_digit()).collect();
digits.parse::<f32>().unwrap_or(0.0) / 1024.0
}
pub async fn home_handler(
State(state): State<Arc<AppState>>,
Query(params): Query<SortParams>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
let response = reqwest::get("https://alatreon.org/slimes?limit=1000")
.await
@@ -87,11 +115,13 @@ pub async fn home_handler(
)
})?;
let reports: Vec<ReportRow> = raw_reports
let mut reports: Vec<ReportRow> = raw_reports
.into_iter()
.map(|s| ReportRow {
id: s.id,
score: s.benchmark.multi_thread.score,
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
@@ -99,6 +129,12 @@ pub async fn home_handler(
.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")
@@ -111,14 +147,37 @@ pub async fn home_handler(
.and_then(|v| v.first())
.cloned()
.unwrap_or_else(|| "-".into()),
time_ago: format_time_ago(&s.timestamp),
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);
match TEMPLATES.render("index.html", &context) {
Ok(html) => Ok(Html(html)),
@@ -143,6 +202,10 @@ pub async fn report_details_handler(
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),
);
match TEMPLATES.render("details.html", &context) {
Ok(html) => Ok(Html(html)),

View File

@@ -52,7 +52,7 @@ async fn main() -> anyhow::Result<()> {
.init();
let shared_state = Arc::new(AppState {
app_name: "Quick rust website template".to_string(),
app_name: "Slimes Leaderboards".to_string(),
});
let app = Router::new()

View File

@@ -37,7 +37,7 @@ body {
main {
width: 100%;
max-width: 1200px;
max-width: 1400px;
margin: 0 auto;
padding: 1rem;
flex: 1;
@@ -83,21 +83,6 @@ tr:hover {
background-color: var(--hover-color);
}
.score-cell {
color: var(--primary-color);
font-weight: 700;
}
.signature-cell {
color: var(--text-muted);
font-style: italic;
font-size: 0.8rem;
max-width: 200px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
footer {
padding: 2rem;
text-align: center;

View File

@@ -44,8 +44,9 @@
</div>
<div class="score-container">
<div class="score-value">{{ report.benchmark.multi_thread.score }}</div>
<div class="score-label">Multi-Thread Score</div>
<div class="score-value">{{ score }}</div>
<div class="score-label">CPU Score</div>
<div class="score-note">(mt+st)/2</div>
</div>
</div>
@@ -91,7 +92,7 @@
background: var(--bg-card);
border-radius: 12px;
padding: 2.5rem;
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
/* box-shadow: 0 4px 6px rgba(0,0,0,0.05); */
border: 1px solid var(--border-color);
}
@@ -165,6 +166,12 @@
line-height: 1;
}
.score-label {
font-size: 1rem;
text-transform: uppercase;
color: var(--text-muted);
letter-spacing: 0.1em;
}
.score-note {
font-size: 0.7rem;
text-transform: uppercase;
color: var(--text-muted);

View File

@@ -1,11 +1,41 @@
{% extends "_layout.html" %}
{% block content %}
<h2>Benchmark Reports</h2>
<div class="header-section">
<h2>Benchmark Reports</h2>
<div class="table-controls">
<form id="sortForm" method="get" action="/">
<div class="control-group">
<label for="sort">Sort by:</label>
<select name="sort" onchange="this.form.submit()">
<option value="score" {% if current_sort == "score" %}selected{% endif %}>Score</option>
<option value="single" {% if current_sort == "single" %}selected{% endif %}>Single Thread</option>
<option value="multi" {% if current_sort == "multi" %}selected{% endif %}>Multi Thread</option>
<option value="ram" {% if current_sort == "ram" %}selected{% endif %}>RAM Amount</option>
<option value="threads" {% if current_sort == "threads" %}selected{% endif %}>Thread Count</option>
<option value="time" {% if current_sort == "time" %}selected{% endif %}>Time Ago</option>
</select>
</div>
<div class="control-group">
<input type="hidden" name="order" value="{{ current_order }}">
<button type="button" class="order-toggle" onclick="toggleOrder()">
{% if current_order == "desc" %}
Descending ↓
{% else %}
Ascending ↑
{% endif %}
</button>
</div>
</form>
</div>
</div>
<table>
<thead>
<tr>
<th>MT Score</th>
<th>CPU Score</th>
<th>Threads</th>
<th>RAM</th>
<th>OS</th>
@@ -24,13 +54,26 @@
<td data-label="RAM">{{ report.ram }}</td>
<td data-label="OS">{{ report.os | truncate(length=30) }}</td>
<td data-label="Hostname">{{ report.hostname }}</td>
<td data-label="Time">{{ report.time_ago }}</td>
<td data-label="Time">{{ report.time_ago }}
{% if report.is_new %}
<span class="new-badge">NEW</span>
{% endif %}
</td>
<td data-label="Note" class="signature-cell">{{ report.signature }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
function toggleOrder() {
const form = document.getElementById('sortForm');
const orderInput = form.querySelector('input[name="order"]');
orderInput.value = orderInput.value === 'desc' ? 'asc' : 'desc';
form.submit();
}
</script>
<style>
.clickable-row {
cursor: pointer;
@@ -45,5 +88,100 @@
text-decoration: none;
color: inherit;
}
.score-cell {
color: var(--primary-color);
font-weight: 700;
}
.signature-cell {
color: var(--text-muted);
font-style: italic;
font-size: 0.8rem;
max-width: 400px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.new-badge {
font-size: 0.6rem;
padding: 3px;
color: var(--primary-text);
border: 1px solid var(--primary-color);
background: var(--primary-soft);
border-radius: 8px;
}
.header-section {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 25px;
margin-bottom: 2rem;
flex-wrap: wrap;
gap: 1rem;
}
.header-section h2 { margin: 0; }
.table-controls form {
display: flex;
gap: 1rem;
align-items: center;
background: var(--bg-card);
padding: 0.5rem 1rem;
border-radius: 8px;
border: 1px solid var(--border-color);
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
.control-group {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.9rem;
color: var(--text-muted);
}
select {
padding: 0.4rem 0.8rem;
border-radius: 6px;
border: 1px solid var(--border-color);
background: white;
color: var(--text-dark);
font-weight: 500;
cursor: pointer;
}
.order-toggle {
background: var(--primary-soft);
border: 1px solid var(--primary-border);
color: var(--primary-text);
padding: 0.4rem 0.8rem;
border-radius: 6px;
font-weight: 600;
cursor: pointer;
white-space: nowrap;
transition: all 0.2s;
}
.order-toggle:hover {
background: var(--primary-border);
}
.sub-scores {
font-size: 0.65rem;
font-weight: normal;
opacity: 0.7;
margin-top: 2px;
}
@media screen and (max-width: 640px) {
.table-controls form {
/* flex-direction: column; */
gap: 0.6rem;
}
}
</style>
{% endblock %}