Parallelize Api

This commit is contained in:
2026-05-16 10:02:22 +02:00
parent 17dfc144b2
commit 900260f67b
2 changed files with 50 additions and 80 deletions

View File

@@ -1,44 +1,38 @@
use std::{collections::HashMap, iter::repeat, sync::Arc}; use std::{
collections::HashMap,
iter::repeat,
sync::{Arc, Mutex},
};
use anyhow::{Context as _, anyhow}; use anyhow::{Context as _, anyhow};
use immich_sdk::{AssetId, AssetVisibility}; use immich_sdk::{AssetId, AssetVisibility};
use kameo::{
Actor,
prelude::{Context, Message},
};
use slint::{Rgba8Pixel, SharedPixelBuffer}; use slint::{Rgba8Pixel, SharedPixelBuffer};
use crate::thumbhash::thumbhashes_to_pixels; use crate::thumbhash::thumbhashes_to_pixels;
pub type TimeBucketKey = String; pub type TimeBucketKey = String;
#[derive(Actor)]
pub struct Api { pub struct Api {
client: immich_sdk::Client, client: immich_sdk::Client,
buckets: HashMap<TimeBucketKey, Arc<TimeBucket>>, buckets: Mutex<HashMap<TimeBucketKey, Arc<TimeBucket>>>,
thumbnails: HashMap<AssetId, Arc<AssetThumbnail>>, // TODO thumbnails: Mutex<HashMap<AssetId, Arc<AssetThumbnail>>>,
} }
impl Api { impl Api {
pub fn new(client: immich_sdk::Client) -> Self { pub fn new(client: immich_sdk::Client) -> Arc<Self> {
Self { Arc::new(Self {
client, client,
buckets: Default::default(), buckets: Default::default(),
thumbnails: Default::default(), thumbnails: Default::default(),
} })
} }
} }
pub struct GetTimeBuckets;
pub struct TimeBucketRef { pub struct TimeBucketRef {
pub key: TimeBucketKey, pub key: TimeBucketKey,
pub count: usize, pub count: usize,
} }
pub struct GetTimeBucket {
pub time_bucket: TimeBucketKey,
}
pub struct TimeBucket { pub struct TimeBucket {
pub key: TimeBucketKey, pub key: TimeBucketKey,
pub entries: Arc<[TimeBucketEntry]>, pub entries: Arc<[TimeBucketEntry]>,
@@ -54,23 +48,13 @@ pub struct TimeBucketEntry {
pub visibility: AssetVisibility, pub visibility: AssetVisibility,
} }
pub struct GetAssetThumbnail {
pub id: AssetId,
}
pub struct AssetThumbnail { pub struct AssetThumbnail {
pub id: AssetId, pub id: AssetId,
pub thumbnail: SharedPixelBuffer<Rgba8Pixel>, pub thumbnail: SharedPixelBuffer<Rgba8Pixel>,
} }
impl Message<GetTimeBuckets> for Api { impl Api {
type Reply = anyhow::Result<Arc<[TimeBucketRef]>>; pub async fn get_time_buckets(&self) -> anyhow::Result<Vec<TimeBucketRef>> {
async fn handle(
&mut self,
_msg: GetTimeBuckets,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
let buckets = self let buckets = self
.client .client
.timeline() .timeline()
@@ -90,30 +74,22 @@ impl Message<GetTimeBuckets> for Api {
}) })
.collect()) .collect())
} }
}
impl Message<GetTimeBucket> for Api { pub async fn get_time_bucket(
type Reply = anyhow::Result<Arc<TimeBucket>>; &self,
time_bucket: TimeBucketKey,
async fn handle( ) -> anyhow::Result<Arc<TimeBucket>> {
&mut self, if let Some(time_bucket) = self.buckets.lock().unwrap().get(&time_bucket).cloned() {
msg: GetTimeBucket,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
if let Some(time_bucket) = self.buckets.get(&msg.time_bucket).cloned() {
return Ok(time_bucket); return Ok(time_bucket);
} }
let bucket = self let bucket = self
.client .client
.timeline() .timeline()
.bucket(&msg.time_bucket) .bucket(&time_bucket)
.execute() .execute()
.await .await
.context(anyhow!( .context(anyhow!("Failed to fetch time bucket {:?}", &time_bucket))
"Failed to fetch time bucket {:?}",
&msg.time_bucket
))
.inspect_err(|e| { .inspect_err(|e| {
tracing::error!("{e:?}"); tracing::error!("{e:?}");
})?; })?;
@@ -144,40 +120,38 @@ impl Message<GetTimeBucket> for Api {
.collect(); .collect();
let bucket = Arc::new(TimeBucket { let bucket = Arc::new(TimeBucket {
key: msg.time_bucket, key: time_bucket,
entries, entries,
}); });
self.buckets.insert(bucket.key.clone(), bucket.clone()); self.buckets
.lock()
.unwrap()
.insert(bucket.key.clone(), bucket.clone());
Ok(bucket) Ok(bucket)
} }
}
impl Message<GetAssetThumbnail> for Api { pub async fn get_asset_thumbnail(
type Reply = anyhow::Result<Arc<AssetThumbnail>>; &self,
asset_id: AssetId,
async fn handle( ) -> anyhow::Result<Arc<AssetThumbnail>> {
&mut self, if let Some(thumbnail) = self.thumbnails.lock().unwrap().get(&asset_id).cloned() {
msg: GetAssetThumbnail,
_ctx: &mut Context<Self, Self::Reply>,
) -> Self::Reply {
if let Some(thumbnail) = self.thumbnails.get(&msg.id).cloned() {
return Ok(thumbnail); return Ok(thumbnail);
} }
let response = self let response = self
.client .client
.assets() .assets()
.thumbnail(msg.id) .thumbnail(asset_id)
.size(immich_sdk::AssetMediaSize::Thumbnail) .size(immich_sdk::AssetMediaSize::Thumbnail)
.execute() .execute()
.await .await
.context(anyhow!("Failed to get asset thumbnail for {}", msg.id))?; .context(anyhow!("Failed to get asset thumbnail for {asset_id}"))?;
let thumbnail = response let thumbnail = response
.decode() .decode()
.context(anyhow!("Failed to decode asset thumbnail for {}", msg.id))? .context(anyhow!("Failed to decode asset thumbnail for {asset_id}"))?
.into_rgba8(); .into_rgba8();
let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice( let pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
@@ -187,11 +161,14 @@ impl Message<GetAssetThumbnail> for Api {
); );
let thumbnail = Arc::new(AssetThumbnail { let thumbnail = Arc::new(AssetThumbnail {
id: msg.id, id: asset_id,
thumbnail: pixel_buffer, thumbnail: pixel_buffer,
}); });
self.thumbnails.insert(msg.id, Arc::clone(&thumbnail)); self.thumbnails
.lock()
.unwrap()
.insert(asset_id, Arc::clone(&thumbnail));
Ok(thumbnail) Ok(thumbnail)
} }

View File

@@ -1,11 +1,10 @@
// Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms. // Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::{mem, ops::Deref}; use std::{mem, ops::Deref, sync::Arc};
use clap::Parser; use clap::Parser;
use immich_sdk::AssetId; use immich_sdk::AssetId;
use kameo::actor::{ActorRef, Spawn};
use slint::{ use slint::{
ComponentHandle as _, Model, ModelRc, SharedPixelBuffer, SharedString, ToSharedString, ComponentHandle as _, Model, ModelRc, SharedPixelBuffer, SharedString, ToSharedString,
VecModel, Weak, VecModel, Weak,
@@ -13,7 +12,7 @@ use slint::{
use tracing::Level; use tracing::Level;
use crate::{ use crate::{
api::{Api, GetAssetThumbnail, GetTimeBucket, GetTimeBuckets, TimeBucketKey}, api::{Api, TimeBucketKey},
ui::{AppWindow, ImageBucket}, ui::{AppWindow, ImageBucket},
}; };
@@ -66,8 +65,7 @@ fn main() -> anyhow::Result<()> {
let immich_config = let immich_config =
immich_sdk::Config::new(opt.immich_base_url).with_api_key(opt.immich_api_key); immich_sdk::Config::new(opt.immich_base_url).with_api_key(opt.immich_api_key);
let api = Api::new(immich_sdk::Client::new(immich_config).unwrap()); let api_ = Api::new(immich_sdk::Client::new(immich_config).unwrap());
let api_ = Api::spawn(api);
let app = ui::AppWindow::new()?; let app = ui::AppWindow::new()?;
let global = app.global::<ui::Global>(); let global = app.global::<ui::Global>();
@@ -76,7 +74,7 @@ fn main() -> anyhow::Result<()> {
let app_weak = app.as_weak(); let app_weak = app.as_weak();
let api = api_.clone(); let api = api_.clone();
tokio::spawn(async move { tokio::spawn(async move {
let Ok(buckets) = api.ask(GetTimeBuckets).await else { let Ok(buckets) = api.get_time_buckets().await else {
return; return;
}; };
@@ -132,7 +130,7 @@ fn main() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
fn calculate_timeline_visibility(app: &AppWindow, api: ActorRef<Api>, scroll: f32) { fn calculate_timeline_visibility(app: &AppWindow, api: Arc<Api>, scroll: f32) {
let global = app.global::<ui::Global>(); let global = app.global::<ui::Global>();
global.set_timeline_scroll(scroll); global.set_timeline_scroll(scroll);
@@ -178,7 +176,7 @@ fn calculate_timeline_visibility(app: &AppWindow, api: ActorRef<Api>, scroll: f3
} }
} }
fn calculate_timeline_layout(app: &AppWindow, api: ActorRef<Api>, timeline_width: f32) { fn calculate_timeline_layout(app: &AppWindow, api: Arc<Api>, timeline_width: f32) {
let global = app.global::<ui::Global>(); let global = app.global::<ui::Global>();
let min_image_size = global.get_min_image_size(); let min_image_size = global.get_min_image_size();
let image_margin = global.get_image_margin(); let image_margin = global.get_image_margin();
@@ -267,14 +265,9 @@ fn unload_bucket(time_bucket: &TimeBucketKey, app: &AppWindow) {
// TODO: write `bucket` into `buckets?` // TODO: write `bucket` into `buckets?`
} }
fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: ActorRef<Api>) { fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: Arc<Api>) {
tokio::spawn(async move { tokio::spawn(async move {
let Ok(api_bucket) = api let Ok(api_bucket) = api.get_time_bucket(time_bucket.clone()).await else {
.ask(GetTimeBucket {
time_bucket: time_bucket.clone(),
})
.await
else {
return; return;
}; };
@@ -312,13 +305,13 @@ fn load_bucket(time_bucket: TimeBucketKey, app_weak: Weak<AppWindow>, api: Actor
fn load_thumbnail( fn load_thumbnail(
time_bucket: TimeBucketKey, time_bucket: TimeBucketKey,
id: AssetId, asset_id: AssetId,
app_weak: Weak<AppWindow>, app_weak: Weak<AppWindow>,
api: ActorRef<Api>, api: Arc<Api>,
) { ) {
tokio::spawn(async move { tokio::spawn(async move {
tracing::debug!("Fetching thumbnail for {id}"); tracing::debug!("Fetching thumbnail for {asset_id}");
let thumbnail = match api.ask(GetAssetThumbnail { id }).await { let thumbnail = match api.get_asset_thumbnail(asset_id).await {
Ok(thumbnail) => thumbnail, Ok(thumbnail) => thumbnail,
Err(e) => { Err(e) => {
tracing::error!("{e:?}"); tracing::error!("{e:?}");
@@ -333,7 +326,7 @@ fn load_thumbnail(
}; };
let bucket = buckets.row_data(i).expect("i is in the list"); let bucket = buckets.row_data(i).expect("i is in the list");
let id_str = id.to_string(); let id_str = asset_id.to_string();
let Some(i) = bucket.previews.iter().position(|p| &p.asset_id == &id_str) else { let Some(i) = bucket.previews.iter().position(|p| &p.asset_id == &id_str) else {
return; return;
}; };