guzzles data
templating
bendn 2024-03-02
parent 85f9687 · commit d59b74a
-rw-r--r--src/main.rs47
-rw-r--r--x.plot6
2 files changed, 43 insertions, 10 deletions
diff --git a/src/main.rs b/src/main.rs
index 4f4342d..e89fb21 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -103,10 +103,13 @@ pub async fn users(
data[d] += 1;
}
let min = min.max(*p.from());
- let mut f = std::fs::File::create("1.dat").unwrap();
- let mut sum = 0;
+ let i = c.id();
+
+ let mut f = std::fs::File::create(format!("{i}.dat")).unwrap();
+ let mut sum = data[..min].iter().map(|&x| x as u64).sum::<u64>();
+ let floor = sum;
for (i, &d) in data[min..max].iter().enumerate() {
- sum += d;
+ sum += d as u64;
writeln!(
&mut f,
r"{},{sum}",
@@ -118,16 +121,37 @@ pub async fn users(
)
.unwrap();
}
+ let plot = if cfg!(debug_assertions) {
+ std::fs::read_to_string("x.plot").unwrap()
+ } else {
+ include_str!("../x.plot").to_string()
+ };
+
+ let plot = plot
+ .replace(
+ "{floor}",
+ &match p {
+ Period::AllTime => "".to_string(),
+ _ => floor.to_string(),
+ },
+ )
+ .replace("{id}", &i.to_string());
+ let path = format!("{i}.plot");
+ std::fs::File::create_new(&path)
+ .unwrap()
+ .write_all(plot.as_bytes())
+ .unwrap();
+
assert!(std::process::Command::new("gnuplot")
- .arg("x.plot")
+ .arg(&path)
.spawn()
.unwrap()
.wait()
.unwrap()
.success());
assert!(std::process::Command::new("inkscape")
- .arg("--export-filename=data.png")
- .arg("data.svg")
+ .arg(format!("--export-filename={i}.png"))
+ .arg(format!("{i}.svg"))
.spawn()
.unwrap()
.wait()
@@ -135,9 +159,18 @@ pub async fn users(
.success());
poise::send_reply(
c,
- poise::CreateReply::default().attachment(CreateAttachment::path("data.png").await.unwrap()),
+ poise::CreateReply::default()
+ .attachment(CreateAttachment::path(format!("{i}.png")).await.unwrap()),
)
.await?;
+ tokio::task::spawn_blocking(move || {
+ std::fs::remove_file(format!("{i}.dat")).unwrap();
+ std::fs::remove_file(path).unwrap();
+ std::fs::remove_file(format!("{i}.svg")).unwrap();
+ std::fs::remove_file(format!("{i}.png")).unwrap();
+ })
+ .await
+ .unwrap();
Ok(())
}
diff --git a/x.plot b/x.plot
index 68551d8..92ef451 100644
--- a/x.plot
+++ b/x.plot
@@ -16,7 +16,7 @@ set key textcolor rgb '#E6EDF3' font "Verdana,14"
set xtics nomirror
set border lw 1
-set output "data.svg"
+set output "{id}.svg"
set title "users"
# set logscale y
set ytics 50
@@ -26,7 +26,7 @@ set style histogram cluster gap 1
set ylabel "change in user count in period"
set xlabel "days"
set auto x
-set yrange [0:*]
+set yrange [{floor}:*]
set style line 12 lc rgb '#1F2430' lt 1 lw 2 dt 22
unset xtics
set xtics format ""
@@ -36,4 +36,4 @@ set grid ytics ls 12
set datafile separator ","
set key top left
-plot '1.dat' u 2:xtic(1) smooth acsplines title "user count" \ No newline at end of file
+plot '{id}.dat' u 2:xtic(1) smooth acsplines title "user count" \ No newline at end of file