diff options
-rw-r--r-- | stats/src/favicon.gif | bin | 0 -> 138 bytes | |||
-rw-r--r-- | stats/src/main.rs | 22 |
2 files changed, 21 insertions, 1 deletions
diff --git a/stats/src/favicon.gif b/stats/src/favicon.gif new file mode 100644 index 0000000..371343b --- /dev/null +++ b/stats/src/favicon.gif Binary files differdiff --git a/stats/src/main.rs b/stats/src/main.rs index 2e0c372..4b57834 100644 --- a/stats/src/main.rs +++ b/stats/src/main.rs @@ -1,5 +1,6 @@ +use std::{io::Write, time::Instant}; + use rusqlite::{Connection, params}; -use std::time::Instant; use time::{Duration, OffsetDateTime}; // Thank you, cat, for optimizing my query @@ -16,8 +17,23 @@ const TOP_TEN_ALL_TIME: &str = "\ "; const STYLE: &'static str = include_str!("style.css"); +const FAVICON: &'static [u8] = include_bytes!("favicon.gif"); fn main() { + let Some(path) = std::env::var("PATH_INFO").ok() else { + error_and_die(500, "no path provided"); + }; + + match path.as_ref() { + "/stats/favicon.gif" => { + println!("Content-Type: image/png\n"); + std::io::stdout().write_all(FAVICON).unwrap(); + std::process::exit(1); + } + "/stats" | "/stats/" => (), + _ => error_and_die(404, "not found"), + } + let db_path = std::env::var("CORGI_STATS_DB").ok(); let db = if let Some(path) = db_path { if let Ok(db) = Connection::open(path) { @@ -37,6 +53,7 @@ fn main() { WHERE requests.timestamp > ?1 \ GROUP BY requests.agent_id;"; + let start = Instant::now(); let mut prepared = db.prepare(query).unwrap(); let mut agents: Vec<(usize, String)> = prepared .query_map(params![fifteen_ago], |row| Ok((row.get(0)?, row.get(1)?))) @@ -53,6 +70,7 @@ fn main() { .map(|r| r.unwrap()) .collect(); let sum_highest_five = highest_five.iter().fold(0, |acc, (count, _)| acc + count); + let elapsed = start.elapsed(); println!("Content-Type: text/html\n"); println!("<html>"); @@ -60,10 +78,12 @@ fn main() { println!("<head>\n\ <title>corgi stats</title>\n\ <style>\n{STYLE}\n</style>\n\ + <link rel='icon' type='image/gif' href='/stats/favicon.gif' />\n\ </head>"); println!("<body>"); println!("<h1>Corgi Stats :)</h1>"); + println!("<p>generated in {}ms</p>", elapsed.as_millis()); #[rustfmt::skip] println!("<table>\n\ |