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