* Return the global Top N files by size from the snapshot. * * @param int $limit * @return array */
(int $limit = 100, string $sourceId = '')
| 889 | * @return array |
| 890 | */ |
| 891 | public static function getTopFiles(int $limit = 100, string $sourceId = ''): array |
| 892 | { |
| 893 | $ctx = self::resolveSourceContext($sourceId); |
| 894 | if (empty($ctx['ok'])) { |
| 895 | return [ |
| 896 | 'ok' => false, |
| 897 | 'error' => $ctx['error'] ?? 'invalid_source', |
| 898 | 'message' => $ctx['message'] ?? 'Invalid source.', |
| 899 | ]; |
| 900 | } |
| 901 | |
| 902 | $snapshot = self::loadSnapshot($sourceId); |
| 903 | if ($snapshot === null) { |
| 904 | return [ |
| 905 | 'ok' => false, |
| 906 | 'error' => 'no_snapshot', |
| 907 | ]; |
| 908 | } |
| 909 | |
| 910 | $rootBytes = (int)($snapshot['root_bytes'] ?? 0); |
| 911 | $files = is_array($snapshot['files'] ?? null) ? $snapshot['files'] : []; |
| 912 | |
| 913 | if ($limit > 0 && count($files) > $limit) { |
| 914 | $files = array_slice($files, 0, $limit); |
| 915 | } |
| 916 | |
| 917 | $out = []; |
| 918 | foreach ($files as $file) { |
| 919 | $bytes = (int)($file['bytes'] ?? 0); |
| 920 | $pct = ($rootBytes > 0) ? ($bytes * 100.0 / $rootBytes) : 0.0; |
| 921 | $out[] = [ |
| 922 | 'folder' => (string)($file['folder'] ?? 'root'), |
| 923 | 'name' => (string)($file['name'] ?? ''), |
| 924 | 'path' => (string)($file['path'] ?? ($file['name'] ?? '')), |
| 925 | 'bytes' => $bytes, |
| 926 | 'mtime' => (int)($file['mtime'] ?? 0), |
| 927 | 'percentOfTotal' => $pct, |
| 928 | ]; |
| 929 | } |
| 930 | |
| 931 | return [ |
| 932 | 'ok' => true, |
| 933 | 'files' => $out, |
| 934 | ]; |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Helper: derive the parent folder key ("root" -> null, "foo/bar" -> "foo"). |
no test coverage detected