feat: initial make-space full-stack release with weighted multi-provider LLM and Dockerfile
This commit is contained in:
commit
37b323c9ac
55 files changed
+17087
No files matched your search
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "storage"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[dependencies]
|
||||
domain = { workspace = true }
|
||||
mongodb = { workspace = true }
|
||||
object_store = { workspace = true }
|
||||
bytes = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true }
|
||||
@@ -0,0 +1,557 @@
|
||||
//! MongoDB repositories + MinIO (S3) blob store, with automatic in-memory fallback
|
||||
//! when MongoDB or MinIO is not yet configured/reachable in local development.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use bytes::Bytes;
|
||||
use domain::{JobEvent, JobStatus, Layout, Photo, PhotoTag, Place, RecentPlace};
|
||||
use futures::TryStreamExt;
|
||||
use mongodb::bson::{self, doc};
|
||||
use mongodb::options::ClientOptions;
|
||||
use mongodb::{Collection, Database};
|
||||
use object_store::aws::AmazonS3Builder;
|
||||
use object_store::memory::InMemory;
|
||||
use object_store::path::Path as ObjPath;
|
||||
use object_store::{ObjectStore, PutPayload};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StorageConfig {
|
||||
pub mongodb_uri: String,
|
||||
pub mongodb_db: String,
|
||||
pub minio_endpoint: String,
|
||||
pub minio_access_key: String,
|
||||
pub minio_secret_key: String,
|
||||
pub minio_bucket: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LayoutDoc {
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
pub place_id: String,
|
||||
pub version: i64,
|
||||
pub created_at: String,
|
||||
pub layout: Layout,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JobDoc {
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
pub place_id: String,
|
||||
pub status: JobStatus,
|
||||
pub events: Vec<JobEvent>,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
}
|
||||
|
||||
/// Text evidence persisted per place (blog bodies, place info).
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TextDoc {
|
||||
pub place_id: String,
|
||||
pub source: domain::PhotoSourceId,
|
||||
pub url: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
/// View history is separate from place metadata, which analysis may replace.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct PlaceViewDoc {
|
||||
#[serde(rename = "_id")]
|
||||
place_id: String,
|
||||
last_viewed_at: String,
|
||||
}
|
||||
|
||||
const RECENT_PLACES_LIMIT: usize = 50;
|
||||
|
||||
#[derive(Default)]
|
||||
struct MemDb {
|
||||
places: HashMap<String, Place>,
|
||||
place_views: HashMap<String, String>,
|
||||
photos: HashMap<String, Photo>,
|
||||
layouts: HashMap<String, Vec<LayoutDoc>>,
|
||||
jobs: HashMap<String, JobDoc>,
|
||||
texts: HashMap<String, Vec<TextDoc>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Store {
|
||||
db: Option<Database>,
|
||||
mem: Arc<RwLock<MemDb>>,
|
||||
blobs: Arc<dyn ObjectStore>,
|
||||
mem_blobs: Arc<InMemory>,
|
||||
}
|
||||
|
||||
fn now() -> String {
|
||||
chrono::Utc::now().to_rfc3339()
|
||||
}
|
||||
|
||||
fn recent_places_from_memory(mem: &MemDb) -> Vec<RecentPlace> {
|
||||
let mut places: Vec<_> = mem
|
||||
.layouts
|
||||
.iter()
|
||||
.filter_map(|(place_id, versions)| {
|
||||
let place = mem.places.get(place_id)?.clone();
|
||||
let latest = versions.iter().max_by(|a, b| {
|
||||
a.version.cmp(&b.version).then(a.created_at.cmp(&b.created_at))
|
||||
})?;
|
||||
Some(RecentPlace {
|
||||
place,
|
||||
analyzed_at: latest.created_at.clone(),
|
||||
last_viewed_at: mem.place_views.get(place_id).cloned(),
|
||||
floor_labels: latest.layout.floors.iter().map(|f| f.label.clone()).collect(),
|
||||
seat_estimate: latest.layout.summary.seat_estimate,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
// MongoDB dates have millisecond precision. Match that ordering in fallback mode.
|
||||
let sort_time = |place: &RecentPlace| {
|
||||
chrono::DateTime::parse_from_rfc3339(
|
||||
place.last_viewed_at.as_deref().unwrap_or(&place.analyzed_at),
|
||||
)
|
||||
.map(|time| time.timestamp_millis())
|
||||
.unwrap_or(0)
|
||||
};
|
||||
places.sort_by(|a, b| sort_time(b).cmp(&sort_time(a)).then(a.place.id.cmp(&b.place.id)));
|
||||
places.truncate(RECENT_PLACES_LIMIT);
|
||||
places
|
||||
}
|
||||
|
||||
fn recent_sort_time() -> bson::Document {
|
||||
// Older records use chrono's micro/nanosecond RFC3339 output. Normalize only
|
||||
// fractional precision because MongoDB's date parser accepts milliseconds.
|
||||
doc! { "$let": {
|
||||
"vars": { "raw": { "$ifNull": ["$last_viewed_at", "$analyzed_at"] } },
|
||||
"in": { "$let": {
|
||||
"vars": { "precise": { "$regexFind": {
|
||||
"input": "$$raw",
|
||||
"regex": r"^(.*\.\d{3})\d+(Z|[+-]\d{2}:\d{2})$"
|
||||
} } },
|
||||
"in": { "$convert": {
|
||||
"input": { "$cond": [
|
||||
{ "$ne": ["$$precise", bson::Bson::Null] },
|
||||
{ "$concat": [
|
||||
{ "$arrayElemAt": ["$$precise.captures", 0] },
|
||||
{ "$arrayElemAt": ["$$precise.captures", 1] }
|
||||
] },
|
||||
"$$raw"
|
||||
] },
|
||||
"to": "date",
|
||||
"onError": bson::DateTime::from_millis(0),
|
||||
"onNull": bson::DateTime::from_millis(0)
|
||||
} }
|
||||
} }
|
||||
} }
|
||||
}
|
||||
|
||||
impl Store {
|
||||
pub async fn connect(cfg: &StorageConfig) -> anyhow::Result<Self> {
|
||||
let mut opts = ClientOptions::parse(&cfg.mongodb_uri).await?;
|
||||
opts.server_selection_timeout = Some(Duration::from_secs(3));
|
||||
opts.connect_timeout = Some(Duration::from_secs(3));
|
||||
let client = mongodb::Client::with_options(opts)?;
|
||||
let candidate_db = client.database(&cfg.mongodb_db);
|
||||
let db = match candidate_db.run_command(doc! { "ping": 1 }).await {
|
||||
Ok(_) => {
|
||||
tracing::info!(uri = %cfg.mongodb_uri, db = %cfg.mongodb_db, "connected to MongoDB");
|
||||
Some(candidate_db)
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("MongoDB unreachable ({e}); using in-memory store until MongoDB is available");
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let mem_blobs = Arc::new(InMemory::new());
|
||||
let blobs: Arc<dyn ObjectStore> = if !cfg.minio_access_key.trim().is_empty()
|
||||
&& !cfg.minio_secret_key.trim().is_empty()
|
||||
{
|
||||
let s3 = AmazonS3Builder::new()
|
||||
.with_endpoint(&cfg.minio_endpoint)
|
||||
.with_bucket_name(&cfg.minio_bucket)
|
||||
.with_access_key_id(&cfg.minio_access_key)
|
||||
.with_secret_access_key(&cfg.minio_secret_key)
|
||||
.with_region("us-east-1")
|
||||
.with_allow_http(true)
|
||||
.with_virtual_hosted_style_request(false)
|
||||
.build()?;
|
||||
let probe = ObjPath::from("_health/probe.txt");
|
||||
match s3.put(&probe, PutPayload::from_static(b"ok")).await {
|
||||
Ok(_) => {
|
||||
tracing::info!(endpoint = %cfg.minio_endpoint, bucket = %cfg.minio_bucket, "connected to MinIO/S3");
|
||||
Arc::new(s3)
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("MinIO bucket '{}' not reachable ({e}); falling back to in-memory blob store", cfg.minio_bucket);
|
||||
mem_blobs.clone()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tracing::warn!("MINIO_ACCESS_KEY/MINIO_SECRET_KEY empty; using in-memory blob store");
|
||||
mem_blobs.clone()
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
db,
|
||||
mem: Arc::new(RwLock::new(MemDb::default())),
|
||||
blobs,
|
||||
mem_blobs,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn health_check(&self) -> anyhow::Result<()> {
|
||||
if let Some(db) = &self.db {
|
||||
db.run_command(doc! { "ping": 1 }).await?;
|
||||
}
|
||||
let probe = ObjPath::from("_health/probe.txt");
|
||||
self.blobs.put(&probe, PutPayload::from_static(b"ok")).await?;
|
||||
self.blobs.get(&probe).await?.bytes().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn ensure_indexes(&self) -> anyhow::Result<()> {
|
||||
let Some(db) = &self.db else { return Ok(()) };
|
||||
use mongodb::IndexModel;
|
||||
db.collection::<Photo>("photos")
|
||||
.create_index(IndexModel::builder().keys(doc! { "place_id": 1 }).build())
|
||||
.await?;
|
||||
db.collection::<LayoutDoc>("layouts")
|
||||
.create_index(IndexModel::builder().keys(doc! { "place_id": 1, "version": -1 }).build())
|
||||
.await?;
|
||||
db.collection::<JobDoc>("jobs")
|
||||
.create_index(IndexModel::builder().keys(doc! { "place_id": 1, "created_at": -1 }).build())
|
||||
.await?;
|
||||
db.collection::<TextDoc>("texts")
|
||||
.create_index(IndexModel::builder().keys(doc! { "place_id": 1 }).build())
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn places(&self) -> Option<Collection<Place>> {
|
||||
self.db.as_ref().map(|d| d.collection("places"))
|
||||
}
|
||||
fn photos(&self) -> Option<Collection<Photo>> {
|
||||
self.db.as_ref().map(|d| d.collection("photos"))
|
||||
}
|
||||
fn layouts(&self) -> Option<Collection<LayoutDoc>> {
|
||||
self.db.as_ref().map(|d| d.collection("layouts"))
|
||||
}
|
||||
fn jobs(&self) -> Option<Collection<JobDoc>> {
|
||||
self.db.as_ref().map(|d| d.collection("jobs"))
|
||||
}
|
||||
fn texts(&self) -> Option<Collection<TextDoc>> {
|
||||
self.db.as_ref().map(|d| d.collection("texts"))
|
||||
}
|
||||
|
||||
// ---------- places ----------
|
||||
|
||||
pub async fn upsert_place(&self, place: &Place) -> anyhow::Result<()> {
|
||||
self.mem.write().unwrap().places.insert(place.id.clone(), place.clone());
|
||||
if let Some(col) = self.places() {
|
||||
col.replace_one(doc! { "_id": &place.id }, place).upsert(true).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_place(&self, id: &str) -> anyhow::Result<Option<Place>> {
|
||||
if let Some(col) = self.places() {
|
||||
if let Ok(Some(p)) = col.find_one(doc! { "_id": id }).await {
|
||||
return Ok(Some(p));
|
||||
}
|
||||
}
|
||||
Ok(self.mem.read().unwrap().places.get(id).cloned())
|
||||
}
|
||||
|
||||
pub async fn record_place_view(&self, place_id: &str) -> anyhow::Result<()> {
|
||||
let viewed_at = now();
|
||||
self.mem
|
||||
.write()
|
||||
.unwrap()
|
||||
.place_views
|
||||
.insert(place_id.to_string(), viewed_at.clone());
|
||||
if let Some(db) = &self.db {
|
||||
db.collection::<PlaceViewDoc>("place_views")
|
||||
.update_one(
|
||||
doc! { "_id": place_id },
|
||||
doc! { "$set": { "last_viewed_at": viewed_at } },
|
||||
)
|
||||
.upsert(true)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Latest saved analyses, including analyses created before view tracking existed.
|
||||
pub async fn recent_places(&self) -> anyhow::Result<Vec<RecentPlace>> {
|
||||
if let Some(col) = self.layouts() {
|
||||
// Project only card data rather than loading every historical 3D layout.
|
||||
let pipeline = vec![
|
||||
doc! { "$sort": { "place_id": 1, "version": -1, "created_at": -1, "_id": 1 } },
|
||||
doc! { "$group": { "_id": "$place_id", "latest": { "$first": "$$ROOT" } } },
|
||||
doc! { "$lookup": {
|
||||
"from": "places", "localField": "_id", "foreignField": "_id", "as": "place"
|
||||
} },
|
||||
doc! { "$unwind": "$place" },
|
||||
doc! { "$lookup": {
|
||||
"from": "place_views", "localField": "_id", "foreignField": "_id", "as": "views"
|
||||
} },
|
||||
doc! { "$project": {
|
||||
"_id": 0,
|
||||
"place": 1,
|
||||
"analyzed_at": "$latest.created_at",
|
||||
"last_viewed_at": { "$ifNull": [
|
||||
{ "$arrayElemAt": ["$views.last_viewed_at", 0] }, bson::Bson::Null
|
||||
] },
|
||||
"floor_labels": "$latest.layout.floors.label",
|
||||
"seat_estimate": "$latest.layout.summary.seat_estimate"
|
||||
} },
|
||||
doc! { "$set": { "sort_time": recent_sort_time() } },
|
||||
doc! { "$sort": { "sort_time": -1, "place._id": 1 } },
|
||||
doc! { "$limit": RECENT_PLACES_LIMIT as i64 },
|
||||
doc! { "$unset": "sort_time" },
|
||||
];
|
||||
let result = async {
|
||||
let docs = col.aggregate(pipeline).await?.try_collect::<Vec<_>>().await?;
|
||||
docs.into_iter()
|
||||
.map(bson::from_document::<RecentPlace>)
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(anyhow::Error::from)
|
||||
}
|
||||
.await;
|
||||
match result {
|
||||
Ok(list) if !list.is_empty() => return Ok(list),
|
||||
Err(error) => {
|
||||
let fallback = recent_places_from_memory(&self.mem.read().unwrap());
|
||||
if fallback.is_empty() {
|
||||
return Err(error);
|
||||
}
|
||||
tracing::warn!(%error, "recent places query failed; using in-memory history");
|
||||
return Ok(fallback);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(recent_places_from_memory(&self.mem.read().unwrap()))
|
||||
}
|
||||
|
||||
// ---------- photos ----------
|
||||
|
||||
pub async fn insert_photo(&self, photo: &Photo) -> anyhow::Result<()> {
|
||||
self.mem.write().unwrap().photos.insert(photo.id.clone(), photo.clone());
|
||||
if let Some(col) = self.photos() {
|
||||
col.replace_one(doc! { "_id": &photo.id }, photo).upsert(true).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn photos_for_place(&self, place_id: &str) -> anyhow::Result<Vec<Photo>> {
|
||||
if let Some(col) = self.photos() {
|
||||
if let Ok(cur) = col.find(doc! { "place_id": place_id }).await {
|
||||
if let Ok(list) = cur.try_collect::<Vec<Photo>>().await {
|
||||
if !list.is_empty() {
|
||||
return Ok(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(self
|
||||
.mem
|
||||
.read()
|
||||
.unwrap()
|
||||
.photos
|
||||
.values()
|
||||
.filter(|p| p.place_id == place_id)
|
||||
.cloned()
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn get_photo(&self, id: &str) -> anyhow::Result<Option<Photo>> {
|
||||
if let Some(col) = self.photos() {
|
||||
if let Ok(Some(p)) = col.find_one(doc! { "_id": id }).await {
|
||||
return Ok(Some(p));
|
||||
}
|
||||
}
|
||||
Ok(self.mem.read().unwrap().photos.get(id).cloned())
|
||||
}
|
||||
|
||||
pub async fn set_photo_tag(&self, id: &str, tag: &PhotoTag) -> anyhow::Result<()> {
|
||||
if let Some(p) = self.mem.write().unwrap().photos.get_mut(id) {
|
||||
p.tag = Some(tag.clone());
|
||||
}
|
||||
if let Some(col) = self.photos() {
|
||||
col.update_one(doc! { "_id": id }, doc! { "$set": { "tag": bson::to_bson(tag)? } })
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_photos_for_place(&self, place_id: &str) -> anyhow::Result<()> {
|
||||
self.mem.write().unwrap().photos.retain(|_, p| p.place_id != place_id);
|
||||
if let Some(col) = self.photos() {
|
||||
col.delete_many(doc! { "place_id": place_id }).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------- texts ----------
|
||||
|
||||
pub async fn replace_texts(&self, place_id: &str, texts: &[TextDoc]) -> anyhow::Result<()> {
|
||||
self.mem
|
||||
.write()
|
||||
.unwrap()
|
||||
.texts
|
||||
.insert(place_id.to_string(), texts.to_vec());
|
||||
if let Some(col) = self.texts() {
|
||||
col.delete_many(doc! { "place_id": place_id }).await?;
|
||||
if !texts.is_empty() {
|
||||
col.insert_many(texts).await?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn texts_for_place(&self, place_id: &str) -> anyhow::Result<Vec<TextDoc>> {
|
||||
if let Some(col) = self.texts() {
|
||||
if let Ok(cur) = col.find(doc! { "place_id": place_id }).await {
|
||||
if let Ok(list) = cur.try_collect::<Vec<TextDoc>>().await {
|
||||
if !list.is_empty() {
|
||||
return Ok(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(self
|
||||
.mem
|
||||
.read()
|
||||
.unwrap()
|
||||
.texts
|
||||
.get(place_id)
|
||||
.cloned()
|
||||
.unwrap_or_default())
|
||||
}
|
||||
|
||||
// ---------- layouts ----------
|
||||
|
||||
pub async fn save_layout(&self, layout: &Layout) -> anyhow::Result<LayoutDoc> {
|
||||
let version = self.latest_layout(&layout.place_id).await?.map(|d| d.version + 1).unwrap_or(1);
|
||||
let d = LayoutDoc {
|
||||
id: format!("{}#v{version}", layout.place_id),
|
||||
place_id: layout.place_id.clone(),
|
||||
version,
|
||||
created_at: now(),
|
||||
layout: layout.clone(),
|
||||
};
|
||||
self.mem
|
||||
.write()
|
||||
.unwrap()
|
||||
.layouts
|
||||
.entry(layout.place_id.clone())
|
||||
.or_default()
|
||||
.push(d.clone());
|
||||
if let Some(col) = self.layouts() {
|
||||
col.insert_one(&d).await?;
|
||||
}
|
||||
Ok(d)
|
||||
}
|
||||
|
||||
pub async fn latest_layout(&self, place_id: &str) -> anyhow::Result<Option<LayoutDoc>> {
|
||||
if let Some(col) = self.layouts() {
|
||||
if let Ok(Some(d)) = col
|
||||
.find_one(doc! { "place_id": place_id })
|
||||
.sort(doc! { "version": -1 })
|
||||
.await
|
||||
{
|
||||
return Ok(Some(d));
|
||||
}
|
||||
}
|
||||
Ok(self
|
||||
.mem
|
||||
.read()
|
||||
.unwrap()
|
||||
.layouts
|
||||
.get(place_id)
|
||||
.and_then(|v| v.last().cloned()))
|
||||
}
|
||||
|
||||
// ---------- jobs ----------
|
||||
|
||||
pub async fn create_job(&self, id: &str, place_id: &str) -> anyhow::Result<()> {
|
||||
let t = now();
|
||||
let doc = JobDoc {
|
||||
id: id.to_string(),
|
||||
place_id: place_id.to_string(),
|
||||
status: JobStatus::Running,
|
||||
events: vec![],
|
||||
created_at: t.clone(),
|
||||
updated_at: t,
|
||||
};
|
||||
self.mem.write().unwrap().jobs.insert(id.to_string(), doc.clone());
|
||||
if let Some(col) = self.jobs() {
|
||||
col.insert_one(doc).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn push_job_event(&self, id: &str, ev: &JobEvent) -> anyhow::Result<()> {
|
||||
if let Some(j) = self.mem.write().unwrap().jobs.get_mut(id) {
|
||||
j.updated_at = now();
|
||||
if matches!(ev, JobEvent::Done { .. }) {
|
||||
j.status = JobStatus::Done;
|
||||
} else if matches!(ev, JobEvent::Error { .. }) {
|
||||
j.status = JobStatus::Failed;
|
||||
}
|
||||
j.events.push(ev.clone());
|
||||
}
|
||||
if let Some(col) = self.jobs() {
|
||||
let mut set = doc! { "updated_at": now() };
|
||||
match ev {
|
||||
JobEvent::Done { .. } => {
|
||||
set.insert("status", bson::to_bson(&JobStatus::Done)?);
|
||||
}
|
||||
JobEvent::Error { .. } => {
|
||||
set.insert("status", bson::to_bson(&JobStatus::Failed)?);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
col.update_one(
|
||||
doc! { "_id": id },
|
||||
doc! { "$push": { "events": bson::to_bson(ev)? }, "$set": set },
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_job(&self, id: &str) -> anyhow::Result<Option<JobDoc>> {
|
||||
if let Some(col) = self.jobs() {
|
||||
if let Ok(Some(j)) = col.find_one(doc! { "_id": id }).await {
|
||||
return Ok(Some(j));
|
||||
}
|
||||
}
|
||||
Ok(self.mem.read().unwrap().jobs.get(id).cloned())
|
||||
}
|
||||
|
||||
// ---------- blobs ----------
|
||||
|
||||
pub async fn put_blob(&self, key: &str, bytes: Bytes) -> anyhow::Result<()> {
|
||||
let p = ObjPath::from(key);
|
||||
if self.blobs.put(&p, PutPayload::from(bytes.clone())).await.is_err() {
|
||||
self.mem_blobs.put(&p, PutPayload::from(bytes)).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_blob(&self, key: &str) -> anyhow::Result<Bytes> {
|
||||
let p = ObjPath::from(key);
|
||||
match self.blobs.get(&p).await {
|
||||
Ok(r) => Ok(r.bytes().await?),
|
||||
Err(_) => Ok(self.mem_blobs.get(&p).await?.bytes().await?),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod recent_tests;
|
||||
@@ -0,0 +1,136 @@
|
||||
use super::*;
|
||||
use domain::{Accessibility, Floor, Summary, Tri};
|
||||
|
||||
fn store() -> Store {
|
||||
let blobs = Arc::new(InMemory::new());
|
||||
Store {
|
||||
db: None,
|
||||
mem: Arc::new(RwLock::new(MemDb::default())),
|
||||
blobs: blobs.clone(),
|
||||
mem_blobs: blobs,
|
||||
}
|
||||
}
|
||||
|
||||
fn place(id: &str) -> Place {
|
||||
Place {
|
||||
id: id.into(),
|
||||
kakao_id: None,
|
||||
naver_place_id: None,
|
||||
name: id.into(),
|
||||
category: "카페".into(),
|
||||
address: String::new(),
|
||||
road_address: String::new(),
|
||||
phone: String::new(),
|
||||
lat: 0.0,
|
||||
lng: 0.0,
|
||||
place_url: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn layout(id: &str, label: &str, seats: u32) -> Layout {
|
||||
Layout {
|
||||
place_id: id.into(),
|
||||
generated_at: "2024-01-01T00:00:00Z".into(),
|
||||
model: "test".into(),
|
||||
confidence: 0.5,
|
||||
floors: vec![Floor {
|
||||
level: 1,
|
||||
label: label.into(),
|
||||
confidence: 0.5,
|
||||
width_m: 10.0,
|
||||
depth_m: 10.0,
|
||||
openings: vec![],
|
||||
zones: vec![],
|
||||
verticals: vec![],
|
||||
notes: String::new(),
|
||||
}],
|
||||
summary: Summary {
|
||||
area_pyeong_min: 20.0,
|
||||
area_pyeong_max: 30.0,
|
||||
seat_estimate: seats,
|
||||
seat_types: vec![],
|
||||
highlights: vec![],
|
||||
accessibility: Accessibility {
|
||||
elevator: Tri::Unknown,
|
||||
step_free_entrance: Tri::Unknown,
|
||||
},
|
||||
caveats: vec![],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn saved_place(store: &Store, id: &str, created_at: &str) {
|
||||
store.upsert_place(&place(id)).await.unwrap();
|
||||
store.save_layout(&layout(id, "1F", 20)).await.unwrap();
|
||||
store.mem.write().unwrap().layouts.get_mut(id).unwrap()[0].created_at = created_at.into();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn recent_places_require_metadata_and_saved_layout() {
|
||||
let store = store();
|
||||
store.upsert_place(&place("unfinished")).await.unwrap();
|
||||
store.record_place_view("unfinished").await.unwrap();
|
||||
store.save_layout(&layout("orphaned-layout", "1F", 20)).await.unwrap();
|
||||
saved_place(&store, "completed", "2024-01-01T00:00:00Z").await;
|
||||
|
||||
let recent = store.recent_places().await.unwrap();
|
||||
assert_eq!(recent.len(), 1);
|
||||
assert_eq!(recent[0].place.id, "completed");
|
||||
// Cached records created before this feature need no view-history migration.
|
||||
assert_eq!(recent[0].last_viewed_at, None);
|
||||
assert_eq!(recent[0].analyzed_at, "2024-01-01T00:00:00Z");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn recent_places_use_latest_version_summary_once() {
|
||||
let store = store();
|
||||
saved_place(&store, "revised", "2024-01-01T00:00:00Z").await;
|
||||
let latest = store.save_layout(&layout("revised", "2F", 42)).await.unwrap();
|
||||
// The list must select the highest version even if the fallback entries are reordered.
|
||||
store.mem.write().unwrap().layouts.get_mut("revised").unwrap().reverse();
|
||||
|
||||
let recent = store.recent_places().await.unwrap();
|
||||
assert_eq!(recent.len(), 1);
|
||||
assert_eq!(recent[0].floor_labels, ["2F"]);
|
||||
assert_eq!(recent[0].seat_estimate, 42);
|
||||
assert_eq!(recent[0].analyzed_at, latest.created_at);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn revisiting_updates_order_and_survives_place_refresh() {
|
||||
let store = store();
|
||||
saved_place(&store, "older", "2024-01-01T00:00:00Z").await;
|
||||
saved_place(&store, "newer", "2025-01-01T00:00:00Z").await;
|
||||
assert_eq!(store.recent_places().await.unwrap()[0].place.id, "newer");
|
||||
|
||||
store.record_place_view("older").await.unwrap();
|
||||
let mut updated = place("older");
|
||||
updated.name = "Updated name".into();
|
||||
store.upsert_place(&updated).await.unwrap();
|
||||
|
||||
let recent = store.recent_places().await.unwrap();
|
||||
assert_eq!(recent[0].place.name, "Updated name");
|
||||
assert_eq!(recent[0].place.id, "older");
|
||||
assert!(recent[0].last_viewed_at.is_some());
|
||||
assert_eq!(recent[0].analyzed_at, "2024-01-01T00:00:00Z");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn recent_places_are_bounded_and_stable_on_timestamp_ties() {
|
||||
let store = store();
|
||||
for number in (0..55).rev() {
|
||||
// Equivalent instants expressed with different offsets still tie.
|
||||
let at = if number % 2 == 0 {
|
||||
"2024-01-01T00:00:00Z"
|
||||
} else {
|
||||
"2024-01-01T09:00:00+09:00"
|
||||
};
|
||||
saved_place(&store, &format!("place-{number:02}"), at).await;
|
||||
}
|
||||
|
||||
let recent = store.recent_places().await.unwrap();
|
||||
assert_eq!(recent.len(), 50);
|
||||
let ids: Vec<_> = recent.iter().map(|row| row.place.id.clone()).collect();
|
||||
let expected: Vec<_> = (0..50).map(|number| format!("place-{number:02}")).collect();
|
||||
assert_eq!(ids, expected);
|
||||
}
|
||||
Reference in new issue
Block a user