optimize puzzle gallery access
This commit is contained in:
@@ -5,6 +5,45 @@ use crate::module_bindings::delete_puzzle_work_procedure::delete_puzzle_work;
|
||||
use crate::module_bindings::record_puzzle_work_like_procedure::record_puzzle_work_like;
|
||||
use crate::module_bindings::remix_puzzle_work_procedure::remix_puzzle_work;
|
||||
use crate::module_bindings::save_puzzle_ui_background_procedure::save_puzzle_ui_background;
|
||||
use std::collections::HashMap;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
const PUBLIC_WORK_PLAY_DAY_MICROS: i64 = 86_400_000_000;
|
||||
const PUBLIC_WORK_RECENT_PLAY_WINDOW_DAYS: i64 = 7;
|
||||
|
||||
fn current_unix_micros() -> i64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_micros() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn current_public_work_day() -> i64 {
|
||||
current_unix_micros().div_euclid(PUBLIC_WORK_PLAY_DAY_MICROS)
|
||||
}
|
||||
|
||||
fn puzzle_gallery_recent_play_counts(connection: &DbConnection) -> HashMap<String, u32> {
|
||||
let current_day = current_public_work_day();
|
||||
let first_day = current_day - (PUBLIC_WORK_RECENT_PLAY_WINDOW_DAYS - 1);
|
||||
let mut counts = HashMap::new();
|
||||
|
||||
for row in connection
|
||||
.db()
|
||||
.public_work_play_daily_stat()
|
||||
.iter()
|
||||
{
|
||||
if row.source_type != "puzzle"
|
||||
|| row.played_day < first_day
|
||||
|| row.played_day > current_day
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let entry: &mut u32 = counts.entry(row.profile_id).or_insert(0);
|
||||
*entry = (*entry).saturating_add(row.play_count);
|
||||
}
|
||||
|
||||
counts
|
||||
}
|
||||
|
||||
impl SpacetimeClient {
|
||||
pub async fn create_puzzle_agent_session(
|
||||
@@ -397,15 +436,21 @@ impl SpacetimeClient {
|
||||
pub async fn list_puzzle_gallery(
|
||||
&self,
|
||||
) -> Result<Vec<PuzzleWorkProfileRecord>, SpacetimeClientError> {
|
||||
self.call_after_connect(move |connection, sender| {
|
||||
connection
|
||||
.procedures()
|
||||
.list_puzzle_gallery_then(move |_, result| {
|
||||
let mapped = result
|
||||
.map_err(SpacetimeClientError::from_sdk_error)
|
||||
.and_then(map_puzzle_works_procedure_result);
|
||||
send_once(&sender, mapped);
|
||||
});
|
||||
self.read_after_connect(move |connection| {
|
||||
let mut items = connection.db().puzzle_gallery_view().iter().collect::<Vec<_>>();
|
||||
items.sort_by(|left, right| right.updated_at_micros.cmp(&left.updated_at_micros));
|
||||
let recent_play_counts = puzzle_gallery_recent_play_counts(connection);
|
||||
Ok(items
|
||||
.into_iter()
|
||||
.map(|item| {
|
||||
let mut record = map_puzzle_work_profile(item);
|
||||
record.recent_play_count_7d = recent_play_counts
|
||||
.get(&record.profile_id)
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
record
|
||||
})
|
||||
.collect())
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user