gatherer/templates/trends.html: fix hour_scores_data() to clip at 48h

This commit is contained in:
Astro 2022-11-14 22:44:16 +01:00
parent 55a903322e
commit d5b5516d03
3 changed files with 13 additions and 18 deletions

View File

@ -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<usize> {
let hours = self.hour_users.iter().cloned()
.collect::<HashMap<_, _>>();
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()
}

View File

@ -1,5 +1,4 @@
use std::net::SocketAddr;
use std::ops::Deref;
use askama::Template;
use axum::{
async_trait,

View File

@ -43,12 +43,12 @@
</nav>
<main>
{% 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"]) %}
<article>
<h2>{{ title }}</h2>
{% for (score, tag) in result %}
<section>
<div class="title" data-hours="{{ tag.hour_scores_data(period.deref() * 4, until.deref() * 1) }}">
<div class="title" data-hours="{{ tag.hour_scores_data(48) }}">
<h3>{{ tag.spelling() }}</h3>
<p class="score">{{ format!("{}{:.0}%", if *score > 1. { "+" } else { "" }, 100. * *score - 100.) }}</p>
</div>