Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/development/PERFORMANCE_BASELINE.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ transaction. Master ID matches take priority over product descriptions. Concurre
clients receive the same item ID. Older Hosts require an update for this Dashboard
shortcut. Manual purchase entry uses the ordinary create operation. Overlapping clicks share a request only for the same
product and target. A completed purchase opens Purchases directly for that library.
The Stock tab reads only the total purchase count locally. A paired Client sends
its saved Stock page revision to the Host. On the first read or a revision
change, the Host includes up to 50 recent orders in the same snapshot so the
Client can renew its offline copy. Only the count crosses the desktop bridge
on a successful compact read. Older Hosts use the existing purchase reader.

Opening and refreshing Purchases reads the queue independently of stock.
The current Host and local database count the full purchase queue and return
50 matching rows per page. Filtering and literal case-insensitive search run
Expand Down
10 changes: 10 additions & 0 deletions scripts/run-loan-dialog-regressions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ async function checkStockCards({page, state, fixture, calls, fallback, openWorks
const reads = [];
let mode = "live";
let revision = 1;
let purchaseCount = 1;
state.invoke = (command,payload) => {
switch (command) {
case "get_library_sync_page_status": return status;
Expand All @@ -196,6 +197,9 @@ async function checkStockCards({page, state, fixture, calls, fallback, openWorks
case "fetch_cached_library_sync_spools":
assert.deepEqual(payload.input,target);
return {rows:[hidden],captured_at:"2026-01-01"};
case "get_stock_purchase_count":
assert.deepEqual(payload.target,target);
return purchaseCount;
case "get_stock_card_page": {
assert.deepEqual(payload.target,target);
if (mode === "legacy") return null;
Expand All @@ -217,6 +221,7 @@ async function checkStockCards({page, state, fixture, calls, fallback, openWorks
await page.locator("#inventory-stock-tab").click();
const panel=page.locator("#inventory-stock-panel");
await panel.getByText(/8 spools/).waitFor();
await expectBadges(page, {purchases: 1});
assert.equal(await panel.locator(".inventory-roll-row").count(),3);
await panel.getByRole("button",{name:/Show all/}).click();
await panel.locator(".inventory-roll-row").nth(7).waitFor();
Expand All @@ -239,6 +244,11 @@ async function checkStockCards({page, state, fixture, calls, fallback, openWorks
assert.equal(await panel.locator(".inventory-product-card").count(), mode === "legacy" ? 3 : 1);
}
assert.ok(calls.some(c=>c.command==="fetch_cached_library_sync_spools"));
mode = "live";
purchaseCount++;
await page.getByRole("button", {name: "Refresh", exact: true}).click();
await expectBadges(page, {purchases: 2});
assert.ok(!calls.some(c => c.command === "get_purchase_queue_page"));
}

async function checkLegacyLocation({ page, state, fixture, calls, fallback, openWorkspace }) {
Expand Down
1 change: 1 addition & 0 deletions scripts/ui-browser-performance-probe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ export function resolveUiBrowserPerformanceInvoke(
return [];
case "set_window_title":
case "set_dock_icon_theme":
case "get_stock_purchase_count":
case "get_purchase_queue_page": // This fixture models a Host with the legacy purchase queue.
case "set_native_window_theme":
case "set_desktop_tray_menu_labels":
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/companion_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ pub(crate) fn is_companion_data_read<B>(request: &Request<B>) -> bool {
"/api/v1/library/printer-spool-picker"
| "/api/v1/library/loan-spool-picker"
| "/api/v1/library/purchase-queue"
| "/api/v1/library/stock-purchase-count"
| "/api/v1/library/location-spools"
)
}
Expand Down Expand Up @@ -522,6 +523,7 @@ mod tests {
"/api/v1/library/printer-spool-picker",
"/api/v1/library/loan-spool-picker",
"/api/v1/library/purchase-queue",
"/api/v1/library/stock-purchase-count",
"/api/v1/library/location-spools",
] {
let query = Request::post(path).body(()).unwrap();
Expand Down
17 changes: 17 additions & 0 deletions src-tauri/src/companion_library_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub(super) async fn handle_health(
PRINTER_PICKER_CAPABILITY,
LOAN_PICKER_CAPABILITY,
crate::companion_models::STOCK_CARDS_CAPABILITY,
crate::companion_models::STOCK_PURCHASE_COUNT_CAPABILITY,
crate::companion_models::PURCHASE_QUEUE_CAPABILITY,
crate::companion_models::DASHBOARD_PURCHASE_CAPABILITY,
LOCATION_SPOOLS_CAPABILITY,
Expand Down Expand Up @@ -248,6 +249,22 @@ pub(super) async fn handle_loan_picker_page(
.await
}

pub(super) async fn handle_stock_purchase_count(
State(state): State<CompanionApiState>,
headers: HeaderMap,
Json(query): Json<crate::backend::database_wishlist_queue::StockPurchaseQuery>,
) -> Result<Json<crate::backend::database_wishlist_queue::StockPurchaseSnapshot>, CompanionApiError>
{
require_allowed_host(&headers, &state.runtime)?;
state
.run_blocking("stock purchase count", move |state| {
Ok(Json(
FilamentDatabase::open(&state.db_path)?.stock_purchase_snapshot(&query)?,
))
})
.await
}

pub(super) async fn handle_purchase_queue_page(
State(state): State<CompanionApiState>,
headers: HeaderMap,
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/companion_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ pub(crate) const WISHLIST_RECEIPT_LOCATION_CAPABILITY: &str = "wishlist-receipt-
pub(crate) const WISHLIST_RECEIPT_IDEMPOTENCY_CAPABILITY: &str = "wishlist-receipt-idempotency-v1";
pub(crate) const CATALOG_SPOOL_BATCH_CAPABILITY: &str = "catalog-spool-batch-v1";
pub(crate) const LOAN_WORKSPACE_COUNTS: &str = "loan-workspace-counts-v1";
pub(crate) const STOCK_PURCHASE_COUNT_CAPABILITY: &str = "stock-purchase-count-v1";
pub(crate) const PURCHASE_QUEUE_CAPABILITY: &str = "purchase-queue-page-v1";
pub(crate) const STOCK_CARDS_CAPABILITY: &str = "stock-cards-v1";
pub(crate) const DASHBOARD_PURCHASE_CAPABILITY: &str = "dashboard-purchase-v1";
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/companion_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn build_router_with_security_config(
.route("/library/snapshot", get(handle_library_snapshot))
.route("/library/printer-spool-picker", post(handle_printer_picker_page))
.route("/library/loan-workspace-counts", get(handle_loan_workspace_counts))
.route("/library/stock-purchase-count", post(handle_stock_purchase_count))
.route("/library/purchase-queue", post(handle_purchase_queue_page))
.route("/library/stock-cards", post(handle_stock_cards))
.route("/library/location-spools", post(handle_location_spools))
Expand Down
62 changes: 62 additions & 0 deletions src-tauri/src/library_sync_resilience_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,3 +1436,65 @@ async fn real_tcp_client_completes_the_five_fixed_workflows_on_the_host() {
assert_eq!(client_local_snapshot(&client_db_path), client_local_before);
host.stop().await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stock_purchase_count_refreshes_the_offline_copy_after_an_order() {
use crate::spool_picker_commands::{stock_purchase_count_blocking, PickerTarget};
let _guard = RESILIENCE_PROCESS_TEST_LOCK.lock().await;
let host_path = temp_db_path("purchase-host");
let client_path = temp_db_path("purchase-client");
let _files = TestDatabases::new(vec![host_path.clone(), client_path.clone()]);
seed_host(&host_path);
let host = HostProcess::start(&host_path, 0, 1).await;
let url = format!("http://127.0.0.1:{}", host.port());
configure_client(&client_path, &url);
let client = app_state(
&client_path,
TrustedLanCompanionRuntime::new(host.port()).with_qa_mode(true),
);
let generation = pair_client_with_host(&client, &client_path, &url).await;
let read = || {
let state = client.clone();
let target = PickerTarget {
base_url: Some(url.clone()),
expected_library_id: HOST_LIBRARY_ID.into(),
target_generation: generation,
};
run_blocking(move || stock_purchase_count_blocking(&state, target))
};
assert_eq!(read().await, Some(0));
let db = FilamentDatabase::open(&host_path).unwrap();
let host_inventory = InventoryEngine::new(db);
host_inventory
.create_wishlist_item(CreateWishlistItemInput {
id: "new-order".into(),
master_id: None,
material: "PLA".into(),
filament_name: "Silk".into(),
color_name: "Blue".into(),
vendor: None,
quantity: Some(1),
note: None,
})
.unwrap();
host_inventory
.update_wishlist_item_status(UpdateWishlistStatusInput {
item_id: "new-order".into(),
status: "ON_ORDER".into(),
})
.unwrap();
assert_eq!(read().await, Some(1));
assert_eq!(read().await, Some(1));
host.stop().await;
let saved = FilamentDatabase::open(&client_path)
.unwrap()
.get_library_sync_settings()
.unwrap()
.cached_wishlist
.unwrap();
assert_eq!(saved.rows.len(), 1);
assert!(saved
.rows
.iter()
.any(|row| row.id == "new-order" && row.status == "ON_ORDER"));
}
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ mod trusted_lan_network_watcher;
mod trusted_lan_pairing_commands;
mod trusted_lan_runtime_commands;
mod trusted_lan_status_commands;

pub(crate) use app_storage::{with_db, with_inventory, with_stats};
use filament_manager_core::backend;
#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -378,6 +377,7 @@ fn main() {
printer_read_commands::list_printer_overview,
spool_picker_commands::get_printer_picker_page,
spool_picker_commands::get_loan_picker_page,
spool_picker_commands::get_stock_purchase_count,
spool_picker_commands::get_purchase_queue_page,
spool_picker_commands::get_stock_card_page,
spool_picker_commands::get_location_spool_page,
Expand Down
41 changes: 40 additions & 1 deletion src-tauri/src/spool_picker_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
backend::database_printer_picker::{PrinterPickerPage, PrinterPickerQuery},
backend::database_result::InventoryResult,
backend::database_stock_cards::{StockCardPage, StockCardQuery},
backend::database_wishlist_queue::{PurchaseQueuePage, PurchaseQueueQuery},
backend::database_wishlist_queue::{PurchaseQueuePage, PurchaseQueueQuery, StockPurchaseQuery},
backend::filament_database::FilamentDatabase,
companion_models::{
LOAN_PICKER_CAPABILITY, LOCATION_SPOOLS_CAPABILITY, PRINTER_PICKER_CAPABILITY,
Expand Down Expand Up @@ -78,6 +78,45 @@ pub(crate) fn loan_picker_page_blocking(
)
}

#[tauri::command]
pub(crate) async fn get_stock_purchase_count(
state: tauri::State<'_, AppState>,
target: PickerTarget,
) -> Result<Option<i64>, String> {
let state = state.inner().clone();
run_library_sync_blocking(move || stock_purchase_count_blocking(&state, target)).await
}

pub(crate) fn stock_purchase_count_blocking(
state: &AppState,
target: PickerTarget,
) -> Result<Option<i64>, String> {
if target.base_url.is_none() {
return target
.with_database(state, FilamentDatabase::stock_purchase_count)
.map(Some);
}
let cached_revision =
target.with_database(state, FilamentDatabase::cached_stock_purchase_revision)?;
let Some(snapshot) = read_picker_page(
state,
&StockPurchaseQuery { cached_revision },
target.clone(),
crate::companion_models::STOCK_PURCHASE_COUNT_CAPABILITY,
"/api/v1/library/stock-purchase-count",
FilamentDatabase::stock_purchase_snapshot,
)?
else {
return Ok(None);
};
target.with_database(state, |db| {
if let Some(rows) = snapshot.rows {
let _ = db.cache_purchase_rows(&rows, snapshot.total, snapshot.revision, true);
}
Ok(Some(snapshot.total))
})
}

#[tauri::command]
pub(crate) async fn get_purchase_queue_page(
state: tauri::State<'_, AppState>,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/database_wishlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub(crate) fn list_wishlist_items(
"SELECT id, master_id, material, filament_name, color_name, vendor, status, quantity,
note, created_at, updated_at
FROM wishlist_items
ORDER BY updated_at DESC, created_at DESC
ORDER BY updated_at DESC, created_at DESC, id
LIMIT ?1",
)?;
let rows = stmt.query_map(params![limit], map_wishlist_item)?;
Expand Down
Loading
Loading