timezones are now queried at launch rather than every time the timezone command is used

This commit is contained in:
jellywx 2021-01-19 12:19:20 +00:00
parent 9003beb1bb
commit 74874c6e99
4 changed files with 31 additions and 11 deletions

2
Cargo.lock generated
View File

@ -1314,7 +1314,7 @@ dependencies = [
[[package]] [[package]]
name = "reminder_rs" name = "reminder_rs"
version = "1.4.1" version = "1.4.2"
dependencies = [ dependencies = [
"Inflector", "Inflector",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder_rs" name = "reminder_rs"
version = "1.4.1" version = "1.4.2"
authors = ["jellywx <judesouthworth@pm.me>"] authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018" edition = "2018"

View File

@ -25,7 +25,7 @@ use crate::{
framework::SendIterator, framework::SendIterator,
get_ctx_data, get_ctx_data,
models::{ChannelData, GuildData, UserData}, models::{ChannelData, GuildData, UserData},
FrameworkCtx, FrameworkCtx, PopularTimezones,
}; };
use std::{collections::HashMap, iter, time::Duration}; use std::{collections::HashMap, iter, time::Duration};
@ -177,20 +177,21 @@ async fn timezone(ctx: &Context, msg: &Message, args: String) {
&GuildData::prefix_from_id(msg.guild_id, &pool).await, &GuildData::prefix_from_id(msg.guild_id, &pool).await,
); );
let popular_timezones = sqlx::query!( let popular_timezones = ctx
"SELECT timezone FROM users GROUP BY timezone ORDER BY COUNT(timezone) DESC LIMIT 20" .data
) .read()
.fetch_all(&pool) .await
.await .get::<PopularTimezones>()
.unwrap(); .cloned()
.unwrap();
let popular_timezones_iter = popular_timezones.iter().map(|t| { let popular_timezones_iter = popular_timezones.iter().map(|t| {
( (
t.timezone.clone(), t.to_string(),
format!( format!(
"🕗 `{}`", "🕗 `{}`",
Utc::now() Utc::now()
.with_timezone(&t.timezone.parse::<Tz>().unwrap()) .with_timezone(t)
.format(user_data.meridian().fmt_str_short()) .format(user_data.meridian().fmt_str_short())
.to_string() .to_string()
), ),

View File

@ -42,6 +42,8 @@ use serenity::futures::TryFutureExt;
use inflector::Inflector; use inflector::Inflector;
use log::info; use log::info;
use chrono_tz::Tz;
struct SQLPool; struct SQLPool;
impl TypeMapKey for SQLPool { impl TypeMapKey for SQLPool {
@ -60,6 +62,12 @@ impl TypeMapKey for FrameworkCtx {
type Value = Arc<Box<dyn Framework + Send + Sync>>; type Value = Arc<Box<dyn Framework + Send + Sync>>;
} }
struct PopularTimezones;
impl TypeMapKey for PopularTimezones {
type Value = Arc<Vec<Tz>>;
}
struct Handler; struct Handler;
#[async_trait] #[async_trait]
@ -249,9 +257,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
))) )))
.unwrap(); .unwrap();
let popular_timezones = sqlx::query!(
"SELECT timezone FROM users GROUP BY timezone ORDER BY COUNT(timezone) DESC LIMIT 21"
)
.fetch_all(&pool)
.await
.unwrap()
.iter()
.map(|t| t.timezone.parse::<Tz>().unwrap())
.collect::<Vec<Tz>>();
let mut data = client.data.write().await; let mut data = client.data.write().await;
data.insert::<SQLPool>(pool); data.insert::<SQLPool>(pool);
data.insert::<PopularTimezones>(Arc::new(popular_timezones));
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new())); data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
data.insert::<FrameworkCtx>(framework_arc); data.insert::<FrameworkCtx>(framework_arc);
data.insert::<LanguageManager>(Arc::new(language_manager)) data.insert::<LanguageManager>(Arc::new(language_manager))