gatherer: add hour_graphs.js
This commit is contained in:
parent
6b9ef41bde
commit
8a06aa10d5
|
@ -1,4 +1,4 @@
|
|||
use std::collections::BTreeSet;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use redis::{
|
||||
aio::ConnectionManager,
|
||||
RedisError,
|
||||
|
@ -114,6 +114,24 @@ impl TrendTag {
|
|||
after / before
|
||||
}
|
||||
|
||||
pub fn hour_scores(&self, period: u64, until: u64) -> Vec<usize> {
|
||||
let hours = self.by_hour.iter().cloned()
|
||||
.collect::<HashMap<_, _>>();
|
||||
|
||||
let from = until - period;
|
||||
let not_before = from - 3 * 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)
|
||||
.iter()
|
||||
.map(|count| format!("{} ", count))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn spellings(&self) -> impl Iterator<Item = (usize, &str)> {
|
||||
self.other.iter()
|
||||
.filter_map(|(key, value)| {
|
||||
|
|
28
gatherer/assets/hour_graphs.js
Normal file
28
gatherer/assets/hour_graphs.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
var W = 200;
|
||||
var H = 25;
|
||||
|
||||
var titleEls = document.getElementsByClassName("title");
|
||||
|
||||
for(var i = 0; i < titleEls.length; i++) {
|
||||
var titleEl = titleEls[i];
|
||||
var hours = titleEl.getAttribute("data-hours")
|
||||
.split(" ")
|
||||
.filter(function(s) { return s != ""; })
|
||||
.map(function(s) { return parseInt(s, 10); });
|
||||
let max = Math.max.apply(Math, hours);
|
||||
|
||||
var svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="';
|
||||
svg += [0, 0, W, H].join(" ");
|
||||
svg += '" preserveAspectRatio="none" width="100%" height="100%">';
|
||||
svg += '<rect x="0" y="0" width="' + W + '" height="' + H + '" fill="#484B90"/>';
|
||||
svg += '<path d="M 0 '+H+' L ';
|
||||
svg += hours.map(function(count, j) {
|
||||
var x = W * j / (hours.length - 1);
|
||||
var y = H * (1 - count / max);
|
||||
return [x, y].join(",");
|
||||
}).join(" L ");
|
||||
svg += ' L '+W+','+H+' z" stroke="#5582E0" stroke-width="1.5" stroke-linejoin="round" fill="#5582E0"/>';
|
||||
svg += '</svg>';
|
||||
console.log("svg", svg);
|
||||
titleEl.style.background = "url('data:image/svg+xml," + encodeURIComponent(svg) + "') no-repeat";
|
||||
}
|
|
@ -103,13 +103,11 @@ section .hosts {
|
|||
padding: 0;
|
||||
margin: 0;
|
||||
text-align: center;
|
||||
line-height: 1.8rem;
|
||||
height: 1.8rem;
|
||||
overflow: hidden;
|
||||
}
|
||||
section .hosts li {
|
||||
display: inline-block;
|
||||
margin: 0.2rem 0.3rem;
|
||||
margin: 0 0.2rem;
|
||||
font-size: 80%;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ use redis::{
|
|||
use cave::store::Store;
|
||||
use cave::trend_tag::TrendTag;
|
||||
|
||||
pub type TrendsResults = Vec<Vec<(f64, Arc<TrendTag>)>>;
|
||||
pub type TrendsResults = Vec<(u64, u64, Vec<(f64, Arc<TrendTag>)>)>;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, PartialOrd)]
|
||||
pub struct ScoreKey {
|
||||
|
@ -74,10 +74,11 @@ impl TrendAnalyzer {
|
|||
|
||||
let results = analyzers.into_iter()
|
||||
.map(|analyzer| {
|
||||
analyzer.result.iter()
|
||||
let result = analyzer.result.iter()
|
||||
.rev()
|
||||
.map(|(key, tag)| (key.score, tag.clone()))
|
||||
.collect()
|
||||
.collect();
|
||||
(analyzer.until, analyzer.period, result)
|
||||
})
|
||||
.collect();
|
||||
Ok(results)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Fedi.buzz: {% if language.is_some() %}{{ language.as_ref().unwrap() }}{% else %}Trends in the Fediverse{% endif %}</title>
|
||||
|
@ -23,14 +23,14 @@
|
|||
</nav>
|
||||
|
||||
<main>
|
||||
{% for (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 %}
|
||||
{% for (score, tag) in result.iter() %}
|
||||
<section>
|
||||
<div class="title">
|
||||
<div class="title" data-hours="{{ tag.hour_scores_data(24 * 7, until) }}">
|
||||
<h3>{{ tag.spelling() }}</h3>
|
||||
<p class="score">{{ format!("{}{:.0}%", if *score > 1. { "+" } else { "" }, 100. * score - 100.) }}</p>
|
||||
<p class="score">{{ format!("{}{:.0}%", if *score > 1. { "+" } else { "" }, 100. * *score - 100.) }}</p>
|
||||
</div>
|
||||
<ul class="hosts">
|
||||
{% for (_count, host) in tag.hosts_set().into_iter().rev().take(5) %}
|
||||
|
@ -52,5 +52,8 @@
|
|||
made by <a href="https://chaos.social/@astro">@astro@chaos.social</a>
|
||||
</p>
|
||||
</footer>
|
||||
|
||||
<script type="text/javascript" src="/assets/hour_graphs.js">
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue
Block a user