Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: total, impl caching, other stuff

Louis Beaumont20b5b936ef41
diff --git a/screenpipe-server/src/db.rs b/screenpipe-server/src/db.rs
index 6afbc27f2..5b0fb4730 100644
--- a/screenpipe-server/src/db.rs
+++ b/screenpipe-server/src/db.rs
@@ -159,6 +159,16 @@ impl DatabaseManager {
.execute(&pool)
.await?;
+ // Enable SQLite's query result caching
+ // PRAGMA cache_size = -2000; -- Set cache size to 2MB
+ // PRAGMA temp_store = MEMORY; -- Store temporary tables and indices in memory
+ sqlx::query("PRAGMA cache_size = -2000;")
+ .execute(&pool)
+ .await?;
+ sqlx::query("PRAGMA temp_store = MEMORY;")
+ .execute(&pool)
+ .await?;
+
let db_manager = DatabaseManager { pool };
// Run migrations after establishing the connection
@@ -986,20 +996,32 @@ impl DatabaseManager {
"#
.to_string();
+ let mut param_count = 3;
+
if app_name.is_some() {
- sql.push_str(" AND ocr_text.app_name = ?6 COLLATE NOCASE");
+ param_count += 1;
+ sql.push_str(&format!(
+ " AND ocr_text.app_name = ?{} COLLATE NOCASE",
+ param_count
+ ));
}
if window_name.is_some() {
- sql.push_str(" AND ocr_text.window_name = ?7 COLLATE NOCASE");
+ param_count += 1;
+ sql.push_str(&format!(
+ " AND ocr_text.window_name = ?{} COLLATE NOCASE",
+ param_count
+ ));
}
if min_length.is_some() {
- sql.push_str(" AND LENGTH(ocr_text.text) >= ?4");
+ param_count += 1;
+ sql.push_str(&format!(" AND LENGTH(ocr_text.text) >= ?{}", param_count));
}
if max_length.is_some() {
- sql.push_str(" AND LENGTH(ocr_text.text) <= ?5");
+ param_count += 1;
+ sql.push_str(&format!(" AND LENGTH(ocr_text.text) <= ?{}", param_count));
}
let mut query = sqlx::query_as::<_, (i64,)>(&sql)
@@ -1034,25 +1056,41 @@ impl DatabaseManager {
min_length: Option<usize>,
max_length: Option<usize>,
) -> Result<usize, sqlx::Error> {
- let (count,): (i64,) = sqlx::query_as(
- r#"
+ let mut sql = r#"
SELECT COUNT(*)
FROM audio_transcriptions
WHERE transcription LIKE '%' || ?1 || '%' COLLATE NOCASE
AND (?2 IS NULL OR timestamp >= ?2)
AND (?3 IS NULL OR timestamp <= ?3)
- AND (?4 IS NULL OR LENGTH(transcription) >= ?4)
- AND (?5 IS NULL OR LENGTH(transcription) <= ?5)
- "#,
- )
- .bind(query)
- .bind(start_time)
- .bind(end_time)
- .bind(min_length.map(|len| len as i64))
- .bind(max_length.map(|len| len as i64))
- .fetch_one(&self.pool)
- .await?;
+ "#
+ .to_string();
+
+ let mut param_count = 3;
+
+ if min_length.is_some() {
+ param_count += 1;
+ sql.push_str(&format!(" AND LENGTH(transcription) >= ?{}", param_count));
+ }
+ if max_length.is_some() {
+ param_count += 1;
+ sql.push_str(&format!(" AND LENGTH(transcription) <= ?{}", param_count));
+ }
+
+ let mut query = sqlx::query_as::<_, (i64,)>(&sql)
+ .bind(query)
+ .bind(start_time)
+ .bind(end_time);
+
+ if let Some(min) = min_length {
+ query = query.bind(min as i64);
+ }
+
+ if let Some(max) = max_length {
+ query = query.bind(max as i64);
+ }
+
+ let (count,) = query.fetch_one(&self.pool).await?;
Ok(count as usize)
}
pub async fn get_latest_timestamps(
diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rs
index ecb60f08b..4e04329b4 100644
--- a/screenpipe-server/src/server.rs
+++ b/screenpipe-server/src/server.rs
@@ -6,7 +6,7 @@ use axum::{
serve, Router,
};
use crossbeam::queue::SegQueue;
-use futures::future::try_join_all;
+use futures::future::{try_join, try_join_all};
use screenpipe_core::download_pipe;
use screenpipe_vision::monitor::list_monitors;
@@ -227,9 +227,8 @@ pub(crate) async fn search(
query.content_type
};
- let results = match state
- .db
- .search(
+ let (results, total) = try_join(
+ state.db.search(
query_str,
content_type,
query.pagination.limit,
@@ -240,22 +239,8 @@ pub(crate) async fn search(
query.window_name.as_deref(),
query.min_length,
query.max_length,
- )
- .await
- {
- Ok(results) => results,
- Err(e) => {
- error!("Failed to search for content: {}", e);
- return Err((
- StatusCode::INTERNAL_SERVER_ERROR,
- JsonResponse(json!({"error": format!("Failed to search for content: {}", e)})),
- ));
- }
- };
-
- let total = state
- .db
- .count_search_results(
+ ),
+ state.db.count_search_results(
query_str,
content_type,
query.start_time,
@@ -264,15 +249,16 @@ pub(crate) async fn search(
query.window_name.as_deref(),
query.min_length,
query.max_length,
+ ),
+ )
+ .await
+ .map_err(|e| {
+ error!("Failed to perform search operations: {}", e);
+ (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({"error": format!("Failed to perform search operations: {}", e)})),
)
- .await
- .map_err(|e| {
- error!("Failed to count search results: {}", e);
- (
- StatusCode::INTERNAL_SERVER_ERROR,
- JsonResponse(json!({"error": format!("Failed to count search results: {}", e)})),
- )
- })?;
+ })?;
let mut content_items: Vec<ContentItem> = results
.iter()
@@ -602,7 +588,6 @@ async fn run_pipe_handler(
) -> Result<JsonResponse<Value>, (StatusCode, JsonResponse<Value>)> {
debug!("Starting pipe: {}", payload.pipe_id);
-
match state
.pipe_manager
.update_config(
@@ -781,26 +766,27 @@ pub fn create_router() -> Router<Arc<AppState>> {
.route("/health", get(health_check))
}
-// Curl commands for reference:
-// # 1. Basic search query
-// # curl "http://localhost:3030/search?q=test&limit=5&offset=0"
+/*
+
+Curl commands for reference:
+# 1. Basic search query
+curl "http://localhost:3030/search?q=test&limit=5&offset=0" | jq
-// # 2. Search with content type filter (OCR)
-// # curl "http://localhost:3030/search?q=test&limit=5&offset=0&content_type=ocr"
+# 2. Search with content type filter (OCR)
+curl "http://localhost:3030/search?q=test&limit=5&offset=0&content_type=ocr" | jq
-// # 3. Search with content type filter (Audio)
-// # curl "http://localhost:3030/search?q=test&limit=5&offset=0&content_type=audio"
+# 3. Search with content type filter (Audio)
+curl "http://localhost:3030/search?q=test&limit=5&offset=0&content_type=audio" | jq
-// # 4. Search with pagination
-// # curl "http://localhost:3030/search?q=test&limit=10&offset=20"
+# 4. Search with pagination
+curl "http://localhost:3030/search?q=test&limit=10&offset=20" | jq
-// # 6. Search with no query (should return all results)
-// # curl "http://localhost:3030/search?limit=5&offset=0"
+# 6. Search with no query (should return all results)
+curl "http://localhost:3030/search?limit=5&offset=0"
// list devices
// # curl "http://localhost:3030/audio/list" | jq
-/*
echo "Listing audio devices:"
curl "http://localhost:3030/audio/list" | jq
@@ -932,10 +918,10 @@ curl "http://localhost:3030/search?q=meeting&content_type=audio&limit=5&offset=0
curl "http://localhost:3030/search?q=project&limit=10&offset=0&min_length=10&max_length=100&start_time=$(date -u -v-1H +%Y-%m-%dT%H:%M:%SZ)&end_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" | jq
# Search with app_name and length constraints
-curl "http://localhost:3030/search?app_name=vscode&limit=5&offset=0&min_length=15&max_length=150" | jq
+curl "http://localhost:3030/search?app_name=cursor&limit=5&offset=0&min_length=15&max_length=150" | jq
# Search with window_name and length constraints
-curl "http://localhost:3030/search?window_name=alacritty&limit=5&offset=0&min_length=5&max_length=50" | jq
+curl "http://localhost:3030/search?window_name=alacritty&min_length=5&max_length=50" | jq
# Search for very short content
curl "http://localhost:3030/search?q=&limit=10&offset=0&max_length=10" | jq