From 48cae5f7097f338afb81920fcce2d0a90c193d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Gr=C3=BCnewald?= Date: Tue, 10 Dec 2024 22:15:37 +0100 Subject: [PATCH] Optimize json loading, filter and reduce --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 41 ++++++++++++++--------------------------- 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83614db..77c0e3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1229,7 +1229,7 @@ dependencies = [ [[package]] name = "sticker-usage-analyzer" -version = "0.1.0" +version = "1.0.0" dependencies = [ "chrono", "maud", diff --git a/Cargo.toml b/Cargo.toml index 6d02a92..f8d32a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sticker-usage-analyzer" -version = "0.1.0" +version = "1.0.0" edition = "2021" [dependencies] diff --git a/src/main.rs b/src/main.rs index 9a1c3cb..fc59bd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use maud::{html, Markup, DOCTYPE}; use rocket::{Rocket,Build,launch,get,routes,fs::{FileServer}}; -use std::collections::HashMap; +use std::{fs::OpenOptions, io::BufReader, collections::HashMap}; use chrono::prelude::*; @@ -105,36 +105,23 @@ fn rocket() -> Rocket { #[get("/")] fn index() -> Markup { - println!("Parsing JSON... (this can take a moment)"); let tg_export_result: Chat = serde_json::from_reader( - std::fs::OpenOptions::new().read(true).open("./result.json").expect("Could not open ./result.json") + BufReader::new(OpenOptions::new().read(true).open("./result.json").expect("Could not open ./result.json")) ).expect("Could not parse result.json"); - println!("Done!"); - let mut messages: Vec = tg_export_result.messages.into_iter().filter(|m| { - return if let Some(media_type) = &m.media_type { - *media_type == MediaType::Sticker - } else { - false - } - }).fold(HashMap::new(), |mut acc: HashMap, message| { - let file = message.file.as_ref().expect("No file").to_owned(); - if acc.contains_key(&file) { - if let Some(scored_chat_message) = acc.get_mut(&file) { - let message_date = message.date_unixtime.parse().expect("Could not parse date_unixtime"); - if scored_chat_message.last_used < message_date { - scored_chat_message.last_used = message_date; - } - scored_chat_message.times = scored_chat_message.times + 1; + let mut messages: Vec = tg_export_result.messages.into_iter() + .filter(|m| matches!(m.media_type, Some(MediaType::Sticker))) + .fold(HashMap::new(), |mut acc: HashMap, message| { + if let Some(ref file) = message.file { + let message_date = (&message.date_unixtime).parse().expect("Could not parse date_unixtime"); + let entry = acc.entry(file.to_owned()).or_insert_with(|| ScoredChatMessage::from(message)); + entry.last_used = entry.last_used.max(message_date); + entry.times += 1; } - } else { - acc.insert(file, ScoredChatMessage::from(message)); - } - - acc - }) - .into_values() - .collect(); + acc + }) + .into_values() + .collect(); messages.sort_by(|a, b| b.times.cmp(&a.times));