From d5b5516d03cbb88fe1d31e921eda3222539ab4af Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 14 Nov 2022 22:44:16 +0100 Subject: [PATCH] gatherer/templates/trends.html: fix hour_scores_data() to clip at 48h --- cave/src/trend_tag.rs | 26 +++++++++++--------------- gatherer/src/http_server.rs | 1 - gatherer/templates/trends.html | 4 ++-- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/cave/src/trend_tag.rs b/cave/src/trend_tag.rs index 7bd4c8f..ec8da40 100644 --- a/cave/src/trend_tag.rs +++ b/cave/src/trend_tag.rs @@ -1,4 +1,4 @@ -use std::collections::{BTreeSet, HashMap}; +use std::collections::BTreeSet; use crate::PERIOD_COMPARE_WINDOW; const MIN_AFTER_MENTIONS: usize = 3; @@ -64,21 +64,17 @@ impl TrendTag { after / before } - pub fn hour_scores(&self, period: u64, until: u64) -> Vec { - let hours = self.hour_users.iter().cloned() - .collect::>(); - - let from = until - period; - let not_before = from - PERIOD_COMPARE_WINDOW * period; - (not_before + 1 ..= until).map(|hour| - *hours.get(&hour).unwrap_or(&0) - ).collect() - } - - pub fn hour_scores_data(&self, period: u64, until: u64) -> String { - self.hour_scores(period, until) + pub fn hour_scores_data(&self, period: u64) -> String { + let offset = self.hour_users.len().saturating_sub(period as usize); + self.hour_users[offset..] .iter() - .map(|count| format!("{} ", count)) + .map(|(_, count)| *count) + .enumerate() + .map(|(i, count)| if i == 0 { + format!("{}", count) + } else { + format!(" {}", count) + }) .collect() } diff --git a/gatherer/src/http_server.rs b/gatherer/src/http_server.rs index a858981..1558fb0 100644 --- a/gatherer/src/http_server.rs +++ b/gatherer/src/http_server.rs @@ -1,5 +1,4 @@ use std::net::SocketAddr; -use std::ops::Deref; use askama::Template; use axum::{ async_trait, diff --git a/gatherer/templates/trends.html b/gatherer/templates/trends.html index 02af56f..60b7434 100644 --- a/gatherer/templates/trends.html +++ b/gatherer/templates/trends.html @@ -43,12 +43,12 @@
- {% for ((until, period, result), title) in results.iter().zip(["Now", "Today", "This week"]) %} + {% for ((_until, _period, result), title) in results.iter().zip(["Now", "Today", "This week"]) %}

{{ title }}

{% for (score, tag) in result %}
-
+

{{ tag.spelling() }}

{{ format!("{}{:.0}%", if *score > 1. { "+" } else { "" }, 100. * *score - 100.) }}