system.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. """System information API routes."""
  2. import asyncio
  3. import os
  4. import platform
  5. import time
  6. from collections.abc import Callable
  7. from datetime import datetime
  8. from pathlib import Path
  9. import psutil
  10. from fastapi import APIRouter, Depends
  11. from sqlalchemy import func, select
  12. from sqlalchemy.ext.asyncio import AsyncSession
  13. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  14. from backend.app.core.config import APP_VERSION, settings
  15. from backend.app.core.database import get_db
  16. from backend.app.core.permissions import Permission
  17. from backend.app.models.archive import PrintArchive
  18. from backend.app.models.filament import Filament
  19. from backend.app.models.printer import Printer
  20. from backend.app.models.project import Project
  21. from backend.app.models.smart_plug import SmartPlug
  22. from backend.app.models.user import User
  23. from backend.app.services.printer_manager import printer_manager
  24. router = APIRouter(prefix="/system", tags=["system"])
  25. STORAGE_USAGE_CACHE_SECONDS = 300
  26. _storage_usage_cache: dict | None = None
  27. _storage_usage_cache_ts: float | None = None
  28. _storage_usage_lock = asyncio.Lock()
  29. def get_directory_size(path: Path) -> int:
  30. """Calculate total size of a directory in bytes."""
  31. total = 0
  32. try:
  33. for entry in path.rglob("*"):
  34. if entry.is_file():
  35. total += entry.stat().st_size
  36. except (PermissionError, OSError):
  37. pass # Return partial total if directory traversal is interrupted
  38. return total
  39. def format_bytes(bytes_value: int) -> str:
  40. """Format bytes to human-readable string."""
  41. for unit in ["B", "KB", "MB", "GB", "TB"]:
  42. if bytes_value < 1024:
  43. return f"{bytes_value:.1f} {unit}"
  44. bytes_value /= 1024
  45. return f"{bytes_value:.1f} PB"
  46. def format_uptime(seconds: float) -> str:
  47. """Format uptime in seconds to human-readable string."""
  48. days = int(seconds // 86400)
  49. hours = int((seconds % 86400) // 3600)
  50. minutes = int((seconds % 3600) // 60)
  51. parts = []
  52. if days > 0:
  53. parts.append(f"{days}d")
  54. if hours > 0:
  55. parts.append(f"{hours}h")
  56. if minutes > 0:
  57. parts.append(f"{minutes}m")
  58. return " ".join(parts) if parts else "< 1m"
  59. def _is_under(path: Path, root: Path) -> bool:
  60. try:
  61. path.resolve().relative_to(root.resolve())
  62. return True
  63. except ValueError:
  64. return False
  65. def _get_database_paths() -> list[Path]:
  66. from backend.app.core.db_dialect import is_sqlite
  67. if not is_sqlite():
  68. return [] # PostgreSQL — no local DB files
  69. candidates = [settings.base_dir / "bambuddy.db", settings.base_dir / "bambutrack.db"]
  70. return [path for path in candidates if path.exists()]
  71. def _get_database_items() -> list[dict]:
  72. items: list[dict] = []
  73. for path in _get_database_paths():
  74. try:
  75. size = path.stat().st_size
  76. except OSError:
  77. continue
  78. items.append(
  79. {
  80. "name": path.name,
  81. "path": str(path),
  82. "bytes": size,
  83. "formatted": format_bytes(size),
  84. }
  85. )
  86. items.sort(key=lambda item: item["bytes"], reverse=True)
  87. return items
  88. def _get_app_dir() -> Path:
  89. return settings.static_dir.parent
  90. def _get_data_dirs() -> list[Path]:
  91. return [
  92. settings.archive_dir,
  93. settings.log_dir,
  94. settings.plate_calibration_dir,
  95. settings.base_dir / "virtual_printer",
  96. settings.base_dir / "firmware",
  97. ]
  98. def _is_system_path(path: Path) -> bool:
  99. app_dir = _get_app_dir()
  100. if not _is_under(path, app_dir):
  101. return False
  102. return all(not _is_under(path, data_dir) for data_dir in _get_data_dirs())
  103. def _get_storage_rules() -> list[tuple[str, str, Callable]]:
  104. base_dir = settings.base_dir
  105. archive_dir = settings.archive_dir
  106. library_dir = archive_dir / "library"
  107. virtual_printer_dir = base_dir / "virtual_printer"
  108. upload_dir = virtual_printer_dir / "uploads"
  109. db_paths = set(_get_database_paths())
  110. return [
  111. (
  112. "database",
  113. "Database",
  114. lambda path: path in db_paths,
  115. ),
  116. (
  117. "library_thumbnails",
  118. "Library Thumbnails",
  119. lambda path: _is_under(path, library_dir / "thumbnails"),
  120. ),
  121. (
  122. "library_files",
  123. "Library Files",
  124. lambda path: _is_under(path, library_dir / "files"),
  125. ),
  126. (
  127. "library_other",
  128. "Library Other",
  129. lambda path: _is_under(path, library_dir),
  130. ),
  131. (
  132. "archive_timelapses",
  133. "Timelapses",
  134. lambda path: _is_under(path, archive_dir) and "timelapse" in path.name.lower(),
  135. ),
  136. (
  137. "archive_thumbnails",
  138. "Thumbnails",
  139. lambda path: _is_under(path, archive_dir) and path.name.lower().startswith("thumbnail"),
  140. ),
  141. (
  142. "archive_files",
  143. "Archives",
  144. lambda path: _is_under(path, archive_dir),
  145. ),
  146. (
  147. "virtual_printer_upload_cache",
  148. "Virtual Printer Upload Cache",
  149. lambda path: _is_under(path, upload_dir / "cache"),
  150. ),
  151. (
  152. "virtual_printer_uploads",
  153. "Virtual Printer Uploads",
  154. lambda path: _is_under(path, upload_dir),
  155. ),
  156. (
  157. "virtual_printer_certs",
  158. "Virtual Printer Certs",
  159. lambda path: _is_under(path, virtual_printer_dir / "certs"),
  160. ),
  161. (
  162. "virtual_printer_other",
  163. "Virtual Printer Other",
  164. lambda path: _is_under(path, virtual_printer_dir),
  165. ),
  166. (
  167. "downloads",
  168. "Downloads",
  169. lambda path: _is_under(path, base_dir / "firmware"),
  170. ),
  171. (
  172. "plate_calibration",
  173. "Plate Calibration",
  174. lambda path: _is_under(path, settings.plate_calibration_dir),
  175. ),
  176. (
  177. "logs",
  178. "Logs",
  179. lambda path: _is_under(path, settings.log_dir),
  180. ),
  181. ]
  182. def _classify_file(path: Path, rules: list[tuple[str, str, Callable]]) -> tuple[str, str]:
  183. for key, label, matcher in rules:
  184. try:
  185. if matcher(path):
  186. return key, label
  187. except OSError:
  188. continue
  189. return "other_data", "Other"
  190. def _format_percentage(part: int, total: int) -> float:
  191. if total <= 0:
  192. return 0.0
  193. return round((part / total) * 100, 2)
  194. def _get_other_bucket(path: Path, base_dir: Path) -> str:
  195. try:
  196. relative = path.resolve().relative_to(base_dir.resolve())
  197. except ValueError:
  198. return path.parent.name or path.name
  199. parts = relative.parts
  200. return parts[0] if parts else path.name
  201. def _walk_files(roots: list[Path]) -> list[Path]:
  202. files: list[Path] = []
  203. stack = [root for root in roots if root.exists()]
  204. while stack:
  205. current = stack.pop()
  206. try:
  207. with os.scandir(current) as entries:
  208. for entry in entries:
  209. try:
  210. if entry.is_symlink():
  211. continue
  212. if entry.is_dir(follow_symlinks=False):
  213. stack.append(Path(entry.path))
  214. elif entry.is_file(follow_symlinks=False):
  215. files.append(Path(entry.path))
  216. except OSError:
  217. continue
  218. except OSError:
  219. continue
  220. return files
  221. def _scan_storage_usage() -> dict:
  222. base_dir = settings.base_dir
  223. rules = _get_storage_rules()
  224. roots = _get_data_dirs()
  225. seen_roots = set()
  226. unique_roots = []
  227. for root in roots:
  228. resolved = root.resolve()
  229. if resolved not in seen_roots:
  230. seen_roots.add(resolved)
  231. unique_roots.append(root)
  232. total_bytes = 0
  233. error_count = 0
  234. category_sizes: dict[str, dict] = {}
  235. other_breakdown: dict[tuple[str, str], int] = {}
  236. database_items = _get_database_items()
  237. files = _walk_files(unique_roots)
  238. for file_path in files:
  239. try:
  240. size = file_path.stat().st_size
  241. except OSError:
  242. error_count += 1
  243. continue
  244. total_bytes += size
  245. key, label = _classify_file(file_path, rules)
  246. if key not in category_sizes:
  247. category_sizes[key] = {"key": key, "label": label, "bytes": 0}
  248. category_sizes[key]["bytes"] += size
  249. if key == "other_data":
  250. bucket = _get_other_bucket(file_path, base_dir)
  251. kind = "system" if _is_system_path(file_path) else "data"
  252. other_breakdown[(bucket, kind)] = other_breakdown.get((bucket, kind), 0) + size
  253. for item in database_items:
  254. total_bytes += item["bytes"]
  255. key = "database"
  256. label = "Database"
  257. if key not in category_sizes:
  258. category_sizes[key] = {"key": key, "label": label, "bytes": 0}
  259. category_sizes[key]["bytes"] += item["bytes"]
  260. categories = []
  261. for item in category_sizes.values():
  262. bytes_value = item["bytes"]
  263. categories.append(
  264. {
  265. "key": item["key"],
  266. "label": item["label"],
  267. "bytes": bytes_value,
  268. "formatted": format_bytes(bytes_value),
  269. "percent_of_total": _format_percentage(bytes_value, total_bytes),
  270. }
  271. )
  272. categories.sort(key=lambda entry: entry["bytes"], reverse=True)
  273. other_items = []
  274. for (bucket, kind), size in other_breakdown.items():
  275. other_items.append(
  276. {
  277. "bucket": bucket,
  278. "label": bucket,
  279. "kind": kind,
  280. "deletable": kind != "system",
  281. "bytes": size,
  282. "formatted": format_bytes(size),
  283. "percent_of_total": _format_percentage(size, total_bytes),
  284. }
  285. )
  286. other_items.sort(key=lambda entry: entry["bytes"], reverse=True)
  287. return {
  288. "roots": [str(root) for root in unique_roots],
  289. "total_bytes": total_bytes,
  290. "total_formatted": format_bytes(total_bytes),
  291. "categories": categories,
  292. "other_breakdown": other_items,
  293. "scan_errors": error_count,
  294. }
  295. async def _get_storage_usage_cached(refresh: bool, max_age_seconds: int) -> dict:
  296. global _storage_usage_cache
  297. global _storage_usage_cache_ts
  298. now = time.time()
  299. if not refresh and _storage_usage_cache and _storage_usage_cache_ts is not None:
  300. age = now - _storage_usage_cache_ts
  301. if age < max_age_seconds:
  302. return {
  303. **_storage_usage_cache,
  304. "cache": {
  305. "hit": True,
  306. "age_seconds": round(age, 2),
  307. "max_age_seconds": max_age_seconds,
  308. },
  309. }
  310. async with _storage_usage_lock:
  311. now = time.time()
  312. if not refresh and _storage_usage_cache and _storage_usage_cache_ts is not None:
  313. age = now - _storage_usage_cache_ts
  314. if age < max_age_seconds:
  315. return {
  316. **_storage_usage_cache,
  317. "cache": {
  318. "hit": True,
  319. "age_seconds": round(age, 2),
  320. "max_age_seconds": max_age_seconds,
  321. },
  322. }
  323. snapshot = await asyncio.to_thread(_scan_storage_usage)
  324. _storage_usage_cache = {
  325. **snapshot,
  326. "generated_at": datetime.now().isoformat(),
  327. }
  328. _storage_usage_cache_ts = time.time()
  329. return {
  330. **_storage_usage_cache,
  331. "cache": {
  332. "hit": False,
  333. "age_seconds": 0,
  334. "max_age_seconds": max_age_seconds,
  335. },
  336. }
  337. @router.get("/info")
  338. async def get_system_info(
  339. db: AsyncSession = Depends(get_db),
  340. _: User | None = RequirePermissionIfAuthEnabled(Permission.SYSTEM_READ),
  341. ):
  342. """Get comprehensive system information."""
  343. # Database stats
  344. archive_count = await db.scalar(select(func.count(PrintArchive.id)))
  345. printer_count = await db.scalar(select(func.count(Printer.id)))
  346. filament_count = await db.scalar(select(func.count(Filament.id)))
  347. project_count = await db.scalar(select(func.count(Project.id)))
  348. smart_plug_count = await db.scalar(select(func.count(SmartPlug.id)))
  349. # Archive stats by status
  350. completed_count = await db.scalar(select(func.count(PrintArchive.id)).where(PrintArchive.status == "completed"))
  351. failed_count = await db.scalar(select(func.count(PrintArchive.id)).where(PrintArchive.status == "failed"))
  352. printing_count = await db.scalar(select(func.count(PrintArchive.id)).where(PrintArchive.status == "printing"))
  353. # Total print time
  354. total_print_time = (
  355. await db.scalar(
  356. select(func.sum(PrintArchive.print_time_seconds)).where(PrintArchive.print_time_seconds.isnot(None))
  357. )
  358. or 0
  359. )
  360. # Total filament used
  361. total_filament = (
  362. await db.scalar(
  363. select(func.sum(PrintArchive.filament_used_grams)).where(PrintArchive.filament_used_grams.isnot(None))
  364. )
  365. or 0
  366. )
  367. # Connected printers
  368. connected_printers = []
  369. for printer_id, client in printer_manager._clients.items():
  370. state = client.state
  371. if state and state.connected:
  372. # Get printer name and model from database
  373. result = await db.execute(select(Printer.name, Printer.model).where(Printer.id == printer_id))
  374. row = result.first()
  375. name = row[0] if row else f"Printer {printer_id}"
  376. model = row[1] if row else "unknown"
  377. connected_printers.append(
  378. {
  379. "id": printer_id,
  380. "name": name,
  381. "state": state.state,
  382. "model": model,
  383. }
  384. )
  385. # Storage info
  386. archive_dir = settings.archive_dir
  387. archive_size = get_directory_size(archive_dir) if archive_dir.exists() else 0
  388. # Database file size
  389. db_path = settings.base_dir / "bambuddy.db"
  390. db_size = db_path.stat().st_size if db_path.exists() else 0
  391. # Disk usage
  392. disk = psutil.disk_usage(str(settings.base_dir))
  393. # System info
  394. memory = psutil.virtual_memory()
  395. boot_time = datetime.fromtimestamp(psutil.boot_time())
  396. uptime_seconds = (datetime.now() - boot_time).total_seconds()
  397. # Python and system info
  398. import sys
  399. return {
  400. "app": {
  401. "version": APP_VERSION,
  402. "base_dir": str(settings.base_dir),
  403. "archive_dir": str(archive_dir),
  404. },
  405. "database": {
  406. "archives": archive_count,
  407. "archives_completed": completed_count,
  408. "archives_failed": failed_count,
  409. "archives_printing": printing_count,
  410. "printers": printer_count,
  411. "filaments": filament_count,
  412. "projects": project_count,
  413. "smart_plugs": smart_plug_count,
  414. "total_print_time_seconds": total_print_time,
  415. "total_print_time_formatted": format_uptime(total_print_time),
  416. "total_filament_grams": round(total_filament, 1),
  417. "total_filament_kg": round(total_filament / 1000, 2),
  418. },
  419. "printers": {
  420. "total": printer_count,
  421. "connected": len(connected_printers),
  422. "connected_list": connected_printers,
  423. },
  424. "storage": {
  425. "archive_size_bytes": archive_size,
  426. "archive_size_formatted": format_bytes(archive_size),
  427. "database_size_bytes": db_size,
  428. "database_size_formatted": format_bytes(db_size),
  429. "disk_total_bytes": disk.total,
  430. "disk_total_formatted": format_bytes(disk.total),
  431. "disk_used_bytes": disk.used,
  432. "disk_used_formatted": format_bytes(disk.used),
  433. "disk_free_bytes": disk.free,
  434. "disk_free_formatted": format_bytes(disk.free),
  435. "disk_percent_used": disk.percent,
  436. },
  437. "system": {
  438. "platform": platform.system(),
  439. "platform_release": platform.release(),
  440. "platform_version": platform.version(),
  441. "architecture": platform.machine(),
  442. "hostname": platform.node(),
  443. "python_version": sys.version.split()[0],
  444. "uptime_seconds": uptime_seconds,
  445. "uptime_formatted": format_uptime(uptime_seconds),
  446. "boot_time": boot_time.isoformat(),
  447. },
  448. "memory": {
  449. "total_bytes": memory.total,
  450. "total_formatted": format_bytes(memory.total),
  451. "available_bytes": memory.available,
  452. "available_formatted": format_bytes(memory.available),
  453. "used_bytes": memory.used,
  454. "used_formatted": format_bytes(memory.used),
  455. "percent_used": memory.percent,
  456. },
  457. "cpu": {
  458. "count": psutil.cpu_count(),
  459. "count_logical": psutil.cpu_count(logical=True),
  460. "percent": psutil.cpu_percent(interval=0.1),
  461. },
  462. }
  463. @router.get("/storage-usage")
  464. async def get_storage_usage(
  465. refresh: bool = False,
  466. max_age_seconds: int = STORAGE_USAGE_CACHE_SECONDS,
  467. _: User | None = RequirePermissionIfAuthEnabled(Permission.SYSTEM_READ),
  468. ):
  469. """Get storage usage breakdown for Bambuddy data directories."""
  470. max_age_seconds = max(0, min(max_age_seconds, 3600))
  471. return await _get_storage_usage_cached(refresh=refresh, max_age_seconds=max_age_seconds)