system.py 17 KB

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