perf: read gallery hot paths from spacetime cache
This commit is contained in:
@@ -5,45 +5,6 @@ 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(
|
||||
@@ -443,9 +404,13 @@ impl SpacetimeClient {
|
||||
&self,
|
||||
) -> Result<Vec<PuzzleWorkProfileRecord>, SpacetimeClientError> {
|
||||
self.read_after_connect(move |connection| {
|
||||
let mut items = connection.db().puzzle_gallery_view().iter().collect::<Vec<_>>();
|
||||
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);
|
||||
let recent_play_counts = public_work_recent_play_counts(connection, "puzzle");
|
||||
Ok(items
|
||||
.into_iter()
|
||||
.map(|item| {
|
||||
|
||||
Reference in New Issue
Block a user