import { exec } from 'child_process' import { promisify } from 'util' import { Env_get_required } from './env' const execAsync = promisify(exec) // Cache for 10 seconds let cacheTimestamp = 0 let cachedData: StorageData | null = null export interface StorageData { activeDriveUsage: number totalDiskCapacity: number totalDiskUsed: number availableDisk: number } export async function fetchStorageData(): Promise { const now = Date.now() // Return cached data if less than 10 seconds old if (cachedData && now - cacheTimestamp < 10000) { return cachedData } try { // Get active drive usage in bytes const { stdout: driveSize } = await execAsync('fctdrive drive-size') const activeDriveUsage = parseInt(driveSize.trim()) || 0 // Get disk usage in bytes const fctdrivePath = Env_get_required('FCTDRIVE_PATH') const { stdout: dfOutput } = await execAsync(`df -B1 "${fctdrivePath}/blobs"`) // Parse df output - second line contains the data const lines = dfOutput.trim().split('\n') const dataLine = lines[1] // Skip header line const columns = dataLine.split(/\s+/) const totalDiskCapacity = parseInt(columns[1]) || 0 const totalDiskUsed = parseInt(columns[2]) || 0 const availableDisk = parseInt(columns[3]) || 0 const data: StorageData = { activeDriveUsage, totalDiskCapacity, totalDiskUsed, availableDisk } // Update cache cachedData = data cacheTimestamp = now return data } catch (error) { console.error('Failed to fetch storage data:', error) // Return zeros on error as requested return { activeDriveUsage: 0, totalDiskCapacity: 0, totalDiskUsed: 0, availableDisk: 0 } } }