|
| 1 | +import os |
1 | 2 | from contextlib import nullcontext |
2 | 3 | from functools import cached_property |
3 | 4 | from time import time |
4 | 5 | from typing import List |
5 | 6 |
|
6 | 7 | import datajoint as dj |
| 8 | +import humanize |
7 | 9 | from datajoint.condition import make_condition |
8 | 10 | from datajoint.errors import DataJointError |
9 | 11 | from datajoint.expression import QueryExpression |
|
16 | 18 | from spyglass.utils.database_settings import SHARED_MODULES |
17 | 19 | from spyglass.utils.dj_helper_fn import ( |
18 | 20 | NonDaemonPool, |
| 21 | + _quick_get_analysis_path, |
19 | 22 | ensure_names, |
20 | 23 | fetch_nwb, |
21 | 24 | get_nwb_table, |
@@ -863,6 +866,31 @@ def check_threads(self, detailed=False, all_threads=False) -> DataFrame: |
863 | 866 |
|
864 | 867 | return df |
865 | 868 |
|
| 869 | + # --------------------------- Check disc usage ------------------------------ |
| 870 | + def get_table_storage_usage(self): |
| 871 | + """Total size of all analysis files in the table. |
| 872 | + Uses the analysis_file_name field to find the file paths and sum their |
| 873 | + sizes. |
| 874 | +
|
| 875 | + Returns |
| 876 | + ------- |
| 877 | + tuple |
| 878 | + (human-readable string, total size in bytes) |
| 879 | + """ |
| 880 | + if "analysis_file_name" not in self.heading.names: |
| 881 | + logger.warning( |
| 882 | + f"{self.full_table_name} does not have an analysis_file_name field." |
| 883 | + ) |
| 884 | + return "0 Mib", 0 |
| 885 | + file_names = self.fetch("analysis_file_name") |
| 886 | + file_paths = [ |
| 887 | + _quick_get_analysis_path(file_name) for file_name in file_names |
| 888 | + ] |
| 889 | + file_paths = [path for path in file_paths if path is not None] |
| 890 | + file_sizes = [os.stat(path).st_size for path in file_paths] |
| 891 | + total_size = sum(file_sizes) |
| 892 | + return humanize.naturalsize(total_size, binary=True), total_size |
| 893 | + |
866 | 894 |
|
867 | 895 | class SpyglassMixinPart(SpyglassMixin, dj.Part): |
868 | 896 | """ |
|
0 commit comments