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

feat: add disk usage in app (#1146)

tribhuwaneefcb754958b
diff --git a/screenpipe-app-tauri/components/settings.tsx b/screenpipe-app-tauri/components/settings.tsx
index efe773f32..b8dc3185e 100644
--- a/screenpipe-app-tauri/components/settings.tsx
+++ b/screenpipe-app-tauri/components/settings.tsx
@@ -11,12 +11,14 @@ import {
Plus,
Trash2,
Check,
+ HardDrive
} from "lucide-react";
import { DialogHeader, DialogTitle } from "./ui/dialog";
import { cn } from "@/lib/utils";
import { RecordingSettings } from "./recording-settings";
import { AccountSection } from "./settings/account-section";
import ShortcutSection from "./settings/shortcut-section";
+import DiskUsage from "./settings/disk-usage";
import AISection from "./settings/ai-section";
import {
DropdownMenu,
@@ -32,7 +34,7 @@ import { invoke } from "@tauri-apps/api/core";
import { useProfiles } from "@/lib/hooks/use-profiles";
import { toast } from "./ui/use-toast";
-type SettingsSection = "ai" | "shortcuts" | "recording" | "account";
+type SettingsSection = "ai" | "shortcuts" | "recording" | "account" | "diskUsage";
export function Settings() {
// const { settings, switchProfile, deleteProfile } = useSettings();
@@ -100,6 +102,8 @@ export function Settings() {
return <RecordingSettings />;
case "shortcuts":
return <ShortcutSection />;
+ case "diskUsage":
+ return <DiskUsage />;
}
};
@@ -212,6 +216,11 @@ export function Settings() {
label: "shortcuts",
icon: <Keyboard className="h-4 w-4" />,
},
+ {
+ id: "diskUsage",
+ label: "disk usage",
+ icon: <HardDrive className="h-4 w-4" />,
+ },
].map((section) => (
<button
key={section.id}
diff --git a/screenpipe-app-tauri/components/settings/disk-usage.tsx b/screenpipe-app-tauri/components/settings/disk-usage.tsx
new file mode 100644
index 000000000..fc8e7a164
--- /dev/null
+++ b/screenpipe-app-tauri/components/settings/disk-usage.tsx
@@ -0,0 +1,204 @@
+"use client";
+import React, { useEffect, useState } from "react";
+import { Badge } from "@/components/ui/badge";
+import { toast } from "@/components/ui/use-toast";
+import { invoke } from "@tauri-apps/api/core";
+import localforage from "localforage";
+import { Skeleton } from "@/components/ui/skeleton";
+import {
+ Accordion,
+ AccordionContent,
+ AccordionItem,
+ AccordionTrigger,
+} from "@/components/ui/accordion"
+
+interface DiskUsageData {
+ media: {
+ audios_size: string;
+ videos_size: string;
+ total_media_size: string;
+ };
+ pipes: {
+ pipes: [string, string][];
+ total_pipes_size: string;
+ };
+ total_data_size: string;
+ total_cache_size: string;
+ avaiable_space: string;
+}
+
+const BadgeItem = ({ label, value, description}: {label: string, value: string, description?: string}) => (
+ <div className="flex flex-row items-center justify-between">
+ <div className="flex flex-col !float-left items-start">
+ <span className="font-semibold">{label}</span>
+ <span className="text-[14px] !font-normal text-muted-foreground">{description}</span>
+ </div>
+ <Badge
+ variant={"outline"}
+ className="mr-4 font-semibold min-w-[5.5rem] flex flex-row justify-center">
+ {value}
+ </Badge>
+ </div>
+);
+
+const Divider = () => (
+ <div className="flex my-2 justify-center">
+ <div className="h-[1px] w-[250px] rounded-full bg-gradient-to-l from-slate-500/30 to-transparent"></div>
+ <div className="h-[1px] w-[250px] rounded-full bg-gradient-to-r from-slate-500/30 to-transparent"></div>
+ </div>
+);
+
+export default function DiskUsage() {
+ const [diskUsage, setDiskUsage] = useState<DiskUsageData | null>(null);
+ const [loading, setLoading] = useState<boolean>(false);
+
+ const getDisk = async () => {
+ setLoading(true);
+ try {
+ const cachedData = await localforage.getItem<{diskData: DiskUsageData, lastUpdated: number}>("diskUsage");
+ const now = Date.now();
+ const twoDaysInMillis = 2 * 24 * 60 * 60 * 1000;
+ if (cachedData && (now - cachedData.lastUpdated) < twoDaysInMillis) {
+ setDiskUsage(cachedData.diskData);
+ setLoading(false);
+ } else {
+ const result = await invoke<DiskUsageData>("get_disk_usage");
+ await new Promise<DiskUsageData>((resolve) => {
+ setTimeout(() => resolve(result), 3000);
+ });
+ await localforage.setItem("diskUsage", { diskData: result, lastUpdated: now });
+ setDiskUsage(result);
+ setLoading(false);
+ }
+ } catch (error) {
+ console.error("Failed to fetch disk usage:", error);
+ toast({
+ title: "error",
+ description: "failed to fetch disk usage, please try again!",
+ variant: "destructive"
+ });
+ }
+ };
+
+ useEffect(() => {
+ getDisk();
+ }, []);
+
+ return (
+ <div className="w-full space-y-6 py-4">
+ <h1 className="text-2xl font-bold">disk usage
+ {loading && !diskUsage ?
+ <span className="text-sm ml-2 !font-normal text-muted-foreground">loading...</span>
+ : ""}
+ </h1>
+ <div className="flex flex-col items-center justify-center space-y-4">
+ {loading && !diskUsage ?
+ <div className="w-full space-y-4">
+ <Skeleton className="h-[80px] w-[90%] mx-auto" />
+ <Skeleton className="h-[80px] w-[90%] mx-auto" />
+ <Skeleton className="h-[200px] w-[90%] mx-auto" />
+ </div>
+ : ""}
+ {diskUsage && diskUsage.pipes && (
+ <Accordion type="single" collapsible
+ className="w-[90%] border rounded-lg">
+ <AccordionItem value="total-pipes-size">
+ <AccordionTrigger className="mx-4 h-[80px] hover:no-underline">
+ <div className="w-full flex items-center justify-between">
+ <div className="flex flex-col !float-left items-start">
+ <span className="font-semibold">disk used by pipes</span>
+ <span className="text-[14px] !font-normal text-muted-foreground">total space used by installed pipes</span>
+ </div>
+ <Badge
+ variant={"outline"}
+ className="mr-4 font-semibold min-w-[5.5rem] flex flex-row justify-center">
+ {diskUsage.pipes.total_pipes_size}
+ </Badge>
+ </div>
+ </AccordionTrigger>
+ <AccordionContent className="w-full">
+ {diskUsage.pipes.pipes.map(([name, size], index) => (
+ <div key={index} className="flex items-center justify-between px-1 py-1">
+ <span className="text-base ml-8">{name}</span>
+ <Badge
+ variant={"outline"}
+ className="mr-10 min-w-[5.5rem] flex flex-row justify-center">
+ {size}
+ </Badge>
+ </div>
+ ))}
+ </AccordionContent>
+ </AccordionItem>
+ </Accordion>
+ )}
+ {diskUsage && diskUsage.media && (
+ <Accordion type="single" collapsible
+ className="w-[90%] border rounded-lg">
+ <AccordionItem value="total-pipes-size">
+ <AccordionTrigger className="mx-4 h-[80px] hover:no-underline">
+ <div className="w-full flex items-center justify-between">
+ <div className="flex flex-col !float-left items-start">
+ <span className="font-semibold">total data captured</span>
+ <span className="text-[14px] !font-normal text-muted-foreground">
+ amount of data captured by screenpipe over the time
+ </span>
+ </div>
+ <Badge
+ variant={"outline"}
+ className="mr-4 font-semibold min-w-[5.5rem] flex flex-row justify-center">
+ {diskUsage.media.total_media_size}
+ </Badge>
+ </div>
+ </AccordionTrigger>
+ <AccordionContent className="w-full">
+ <div key={"video"} className="flex items-center justify-between px-1 py-1">
+ <span className="text-base ml-8">video data</span>
+ <Badge
+ variant={"outline"}
+ className="mr-10 min-w-[5.5rem] flex flex-row justify-center">
+ {diskUsage.media.videos_size}
+ </Badge>
+ </div>
+ <div key={"audio"} className="flex items-center justify-between px-1 py-1">
+ <span className="text-base ml-8">audio data</span>
+ <Badge
+ variant={"outline"}
+ className="mr-10 min-w-[5.5rem] flex flex-row justify-center ">
+ {diskUsage.media.audios_size}
+ </Badge>
+ </div>
+ </AccordionContent>
+ </AccordionItem>
+ </Accordion>
+ )}
+ {diskUsage && diskUsage.total_data_size && diskUsage.avaiable_space && (
+ <Accordion type="single"
+ className="w-[90%] border rounded-lg">
+ <AccordionItem value="total-pipes-size">
+ <AccordionTrigger className="mx-4 h-[200px] flex hover:no-underline">
+ <div className="w-full">
+ <BadgeItem
+ label="screenpipe cache size"
+ description="disk space used for models, frames..."
+ value={diskUsage.total_cache_size} />
+ <Divider />
+ <BadgeItem
+ label="disk space used by screenpipe"
+ description="total disk space utilized by the screenpipe application"
+ value={diskUsage.total_data_size} />
+ <Divider />
+ <BadgeItem
+ label="available disk space"
+ description="remaining free disk space on your device"
+ value={diskUsage.avaiable_space}
+ />
+ </div>
+ </AccordionTrigger>
+ </AccordionItem>
+ </Accordion>
+ )}
+ </div>
+ </div>
+ );
+}
+
diff --git a/screenpipe-app-tauri/src-tauri/src/commands.rs b/screenpipe-app-tauri/src-tauri/src/commands.rs
index a6f0dd627..832e69cfd 100644
--- a/screenpipe-app-tauri/src-tauri/src/commands.rs
+++ b/screenpipe-app-tauri/src-tauri/src/commands.rs
@@ -296,3 +296,28 @@ pub async fn open_pipe_window(
Ok(())
}
+
+#[tauri::command]
+pub async fn get_disk_usage(
+ app_handle: tauri::AppHandle<tauri::Wry>,
+) -> Result<serde_json::Value, String> {
+ let screenpipe_dir_path = get_data_dir(&app_handle)
+ .unwrap_or_else(|_| dirs::home_dir().unwrap().join(".screenpipe"));
+ match crate::disk_usage::disk_usage(&screenpipe_dir_path).await {
+ Ok(Some(disk_usage)) => {
+ match serde_json::to_value(&disk_usage) {
+ Ok(json_value) => Ok(json_value),
+ Err(e) => {
+ error!("Failed to serialize disk usage: {}", e);
+ Err(format!("Failed to serialize disk usage: {}", e))
+ }
+ }
+ }
+ Ok(None) => Err("No disk usage data found".to_string()),
+ Err(e) => {
+ error!("Failed to get disk usage: {}", e);
+ Err(format!("Failed to get disk usage: {}", e))
+ }
+ }
+}
+
diff --git a/screenpipe-app-tauri/src-tauri/src/disk_usage.rs b/screenpipe-app-tauri/src-tauri/src/disk_usage.rs
new file mode 100644
index 000000000..a7c32a522
--- /dev/null
+++ b/screenpipe-app-tauri/src-tauri/src/disk_usage.rs
@@ -0,0 +1,132 @@
+use std::fs;
+use std::io;
+use tracing::info;
+use std::path::{Path, PathBuf};
+use serde::{Deserialize, Serialize};
+use sysinfo::{System, SystemExt, DiskExt};
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct DiskUsage {
+ pub pipes: DiskUsedByPipes,
+ pub media: DiskUsedByMedia,
+ pub total_data_size: String,
+ pub total_cache_size: String,
+ pub avaiable_space: String,
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct DiskUsedByPipes {
+ pub pipes: Vec<(String, String)>, // why not pipes' size??
+ pub total_pipes_size: String,
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct DiskUsedByMedia {
+ pub videos_size: String,
+ pub audios_size: String,
+ pub total_media_size: String,
+}
+
+pub fn get_cache_dir() -> Result<Option<PathBuf>, String> {
+ let proj_dirs = dirs::cache_dir().ok_or_else(|| "failed to get cache dir".to_string())?;
+ Ok(Some(proj_dirs.join("screenpipe")))
+}
+
+pub fn directory_size(path: &Path) -> io::Result<u64> {
+ let mut size = 0;
+ for entry in fs::read_dir(path)? {
+ let entry = entry?;
+ let metadata = entry.metadata()?;
+ if metadata.is_dir() {
+ size += directory_size(&entry.path())?;
+ } else {
+ size += metadata.len();
+ }
+ }
+ Ok(size)
+}
+
+pub fn readable(size: u64) -> String {
+ let units = ["KB", "MB", "GB"];
+ let mut size = size as f64 / 1024.0;
+ let mut unit = 0;
+ while size >= 1024.0 && unit < units.len() - 1 {
+ size /= 1024.0;
+ unit += 1;
+ }
+ if units[unit] == "GB" {
+ format!("{:.2} {}", size, units[unit])
+ } else {
+ format!("{:.1} {}", size, units[unit])
+ }
+}
+
+pub async fn disk_usage(screenpipe_dir: &PathBuf) -> Result<Option<DiskUsage>, String> {
+ info!("Getting total disk usage, path {:?}", screenpipe_dir);
+ let mut pipes = Vec::new();
+ let mut total_video_size = 0;
+ let mut total_audio_size = 0;
+
+ let pipes_dir = screenpipe_dir.join("pipes");
+ let data_dir = screenpipe_dir.join("data");
+ let cache_dir = match get_cache_dir()? {
+ Some(dir) => dir,
+ None => return Err("Cache directory not found".to_string()),
+ };
+
+ for entry in fs::read_dir(&pipes_dir).map_err(|e| e.to_string())? {
+ let entry = entry.map_err(|e| e.to_string())?;
+ let path = entry.path();
+ if path.is_dir() {
+ let size = directory_size(&path).map_err(|e| e.to_string())?;
+ pipes.push((path.file_name().unwrap().to_string_lossy().to_string(), readable(size)));
+ }
+ }
+
+ let total_data_size = directory_size(&screenpipe_dir).map_err(|e| e.to_string())?;
+ let total_media_size= directory_size(&data_dir).map_err(|e| e.to_string())?;
+ let total_pipes_size = directory_size(&pipes_dir).map_err(|e| e.to_string())?;
+ let total_cache_size = directory_size(&cache_dir).map_err(|e| e.to_string())?;
+
+ for entry in fs::read_dir(&data_dir).map_err(|e| e.to_string())? {
+ let entry = entry.map_err(|e| e.to_string())?;
+ let path = entry.path();
+ if path.is_file() {
+ let size = entry.metadata().map_err(|e| e.to_string())?.len();
+ let file_name = path.file_name().unwrap().to_string_lossy().to_string();
+ if file_name.contains("input") || file_name.contains("output") {
+ total_audio_size += size;
+ } else {
+ total_video_size += size;
+ }
+ }
+ }
+
+ let avaiable_space = {
+ let mut sys = System::new();
+ sys.refresh_disks_list();
+ let path_obj = Path::new(&screenpipe_dir);
+ sys.disks()
+ .iter()
+ .find(|disk| path_obj.starts_with(disk.mount_point()))
+ .map(|disk| disk.available_space())
+ .unwrap_or(0)
+ };
+
+ let disk_usage = DiskUsage {
+ pipes: DiskUsedByPipes {
+ pipes,
+ total_pipes_size: readable(total_pipes_size),
+ },
+ media: DiskUsedByMedia {
+ videos_size: readable(total_video_size),
+ audios_size: readable(total_audio_size),
+ total_media_size: readable(total_media_size),
+ },
+ total_data_size: readable(total_data_size + total_cache_size),
+ total_cache_size: readable(total_cache_size),
+ avaiable_space: readable(avaiable_space),
+ };
+
+ Ok(Some(disk_usage))
+}
diff --git a/screenpipe-app-tauri/src-tauri/src/main.rs b/screenpipe-app-tauri/src-tauri/src/main.rs
index afa1e38f3..ef49c3615 100755
--- a/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -48,6 +48,7 @@ mod sidecar;
mod store;
mod tray;
mod updates;
+mod disk_usage;
pub use commands::reset_all_pipes;
pub use commands::set_tray_health_icon;
pub use commands::set_tray_unhealth_icon;
@@ -481,7 +482,7 @@ fn get_data_dir(app: &tauri::AppHandle) -> anyhow::Result<PathBuf> {
.and_then(|v| v.as_str().map(String::from))
.unwrap_or(String::from("default"));
- if data_dir == "default" {
+ if data_dir == "default" || data_dir.is_empty() {
Ok(default_path)
} else {
get_base_dir(app, Some(data_dir))
@@ -668,6 +669,7 @@ async fn main() {
commands::update_show_screenpipe_shortcut,
commands::show_meetings,
commands::show_identify_speakers,
+ commands::get_disk_usage,
commands::open_pipe_window,
get_log_files,
update_global_shortcuts,