()
| 2424 | |
| 2425 | |
| 2426 | def update_termux_usage_stats(): |
| 2427 | now = time.time() |
| 2428 | stats = load_termux_usage_stats() |
| 2429 | first_scan = float(stats.get("first_scan") or now) |
| 2430 | previous_snapshot = stats.get("snapshot") if isinstance(stats.get("snapshot"), dict) else {} |
| 2431 | |
| 2432 | current_snapshot, language_counts, language_bytes, folder_counts, newest_files = collect_termux_file_snapshot() |
| 2433 | current_paths = set(current_snapshot.keys()) |
| 2434 | previous_paths = set(previous_snapshot.keys()) |
| 2435 | |
| 2436 | is_first_scan = not bool(previous_snapshot) |
| 2437 | created = sorted(current_paths - previous_paths) if not is_first_scan else [] |
| 2438 | deleted = sorted(previous_paths - current_paths) if not is_first_scan else [] |
| 2439 | edited = [] |
| 2440 | if not is_first_scan: |
| 2441 | for rel_path in sorted(current_paths & previous_paths): |
| 2442 | old = previous_snapshot.get(rel_path) or {} |
| 2443 | new = current_snapshot.get(rel_path) or {} |
| 2444 | if old.get("size") != new.get("size") or old.get("mtime") != new.get("mtime"): |
| 2445 | edited.append(rel_path) |
| 2446 | |
| 2447 | totals = stats.get("totals") if isinstance(stats.get("totals"), dict) else {} |
| 2448 | totals["created"] = int(totals.get("created", 0)) + len(created) |
| 2449 | totals["deleted"] = int(totals.get("deleted", 0)) + len(deleted) |
| 2450 | totals["edited"] = int(totals.get("edited", 0)) + len(edited) |
| 2451 | |
| 2452 | total_commands, top_commands = summarize_bash_history() |
| 2453 | stats.update({ |
| 2454 | "first_scan": first_scan, |
| 2455 | "last_scan": now, |
| 2456 | "scan_count": int(stats.get("scan_count", 0)) + 1, |
| 2457 | "snapshot": current_snapshot, |
| 2458 | "totals": totals, |
| 2459 | "latest": { |
| 2460 | "created": created[:20], |
| 2461 | "deleted": deleted[:20], |
| 2462 | "edited": edited[:20], |
| 2463 | "is_first_scan": is_first_scan, |
| 2464 | }, |
| 2465 | "language_counts": language_counts, |
| 2466 | "language_bytes": language_bytes, |
| 2467 | "folder_counts": folder_counts, |
| 2468 | "newest_files": newest_files, |
| 2469 | "total_commands": total_commands, |
| 2470 | "top_commands": top_commands, |
| 2471 | }) |
| 2472 | save_termux_usage_stats(stats) |
| 2473 | return stats |
| 2474 | |
| 2475 | |
| 2476 | def _record_termux_settings_session_time(): |
| 2477 | try: |
| 2478 | stats = load_termux_usage_stats() |
| 2479 | runtime = max(0, time.time() - SETTINGS_SESSION_START) |
| 2480 | stats["settings_runtime_seconds"] = float(stats.get("settings_runtime_seconds", 0.0)) + runtime |
| 2481 | if "first_scan" not in stats: |
| 2482 | stats["first_scan"] = SETTINGS_SESSION_START |
| 2483 | stats["last_seen"] = time.time() |
no test coverage detected