From 2fbdeeef067a63541b8f9c80d4b904b86fbaae3f Mon Sep 17 00:00:00 2001 From: Oliver Atkinson Date: Thu, 25 Jul 2024 13:57:49 -0600 Subject: [PATCH] Add better logging + Clippy warnings fix --- src/command.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/command.rs b/src/command.rs index 067f6b6..7f43ec1 100644 --- a/src/command.rs +++ b/src/command.rs @@ -3,6 +3,7 @@ use std::{collections::HashMap, fmt::Display, sync::Arc}; use crate::Context; use anyhow::Error; use poise::{serenity_prelude::{Cache, CacheHttp, ChannelId, ChannelType, GetMessages, GuildChannel, Http, Message}, CreateReply}; +use tokio::time::Instant; use tracing::{debug, error, trace}; struct Server { @@ -86,11 +87,10 @@ impl Server { if child.this.id == *find { return Some(child); } - match Self::search_by_id(&mut child.children, find) { - Some(x) => return Some(x), - None => {}, + if let Some(x) = Self::search_by_id(&mut child.children, find) { + return Some(x); } - } + } None } @@ -99,7 +99,7 @@ impl Server { if let Some(parent_id) = &insert.parent_id { // find the parent (needs to go thru all nodes) - match Self::search_by_id(&mut self.channels, &parent_id) { + match Self::search_by_id(&mut self.channels, parent_id) { Some(parent_node) => { parent_node.children.push(Channel::new(insert)); }, @@ -141,7 +141,10 @@ impl Server { /// Recursive walk thru the channels async fn walk_channels(all: &mut Vec, cache: impl CacheHttp + Clone) { - let settings = GetMessages::default().limit(5); + // Qty of messages to take at a time, max=100 + let batch_size = 100; + + let settings = GetMessages::default().limit(batch_size); for channel in all { // Clone *should* be cheap - it's Arc under the hood // get the messages @@ -169,7 +172,11 @@ impl Server { } } }, - Err(e) => error!("Error while trying to get messages - {e}"), + Err(e) => { + error!("While reading messages in \"{}\" before `{}` - {e}", channel.this.name, last); + // Stop reading this channel on an error. + last_id = None; + }, } } // Then recurse into children channels @@ -200,8 +207,14 @@ pub async fn scrape_all(ctx: Context<'_>) -> Result<(), Error> { let mut server = index(map).await; match ctx.reply("Starting scrape...").await { Ok(ok) => { + let start = Instant::now(); server.scrape_all().await; - let _ = ok.edit(ctx, CreateReply::default().content(&format!("Scraped {} messages", server.message_count()))).await; + let end = start.elapsed().as_millis(); + let msg_count = server.message_count(); + let _ = ok.edit(ctx, CreateReply::default().content( + &format!("Done. Stats: \n```toml\nMessages: {msg_count}\nElapsed time: {end}ms\n```") + )).await; + debug!("Scraped server in {}ms", end); }, Err(e) => error!("{e} - While trying to reply to scrape command"), }