diff --git a/pyproject.toml.unversioned b/pyproject.toml.unversioned index af082812..82658417 100644 --- a/pyproject.toml.unversioned +++ b/pyproject.toml.unversioned @@ -191,6 +191,7 @@ packages = [ "delete_feature.py", "upload_sync_small.py", "collection.py", + "load_query.py", ] "spatialprofilingtoolbox.db.data_model" = [ "metaschema.sql", @@ -198,6 +199,8 @@ packages = [ "create_roles.sql", "create_views.sql", "drop_views.sql", + "load_query.sql", + "load_query_breakdown.sql", "fields.tsv", "grant_on_tables.sql", "performance_tweaks.sql", diff --git a/spatialprofilingtoolbox/db/data_model/load_query.sql b/spatialprofilingtoolbox/db/data_model/load_query.sql new file mode 100644 index 00000000..d7dd537b --- /dev/null +++ b/spatialprofilingtoolbox/db/data_model/load_query.sql @@ -0,0 +1,21 @@ +SELECT +SUM( + ( + xpath( + '/row/c/text()', + query_to_xml( + format( + 'select count(*) as c from %I.%I', + schema_name, + 'quantitative_feature_value_queue' + ), + FALSE, + TRUE, + '' + ) + ) + )[1]::text::int +) as total_jobs_in_queue +FROM + default_study_lookup.study_lookup +; diff --git a/spatialprofilingtoolbox/db/data_model/load_query_breakdown.sql b/spatialprofilingtoolbox/db/data_model/load_query_breakdown.sql new file mode 100644 index 00000000..d495b3e2 --- /dev/null +++ b/spatialprofilingtoolbox/db/data_model/load_query_breakdown.sql @@ -0,0 +1,20 @@ +SELECT + schema_name, + ( + xpath( + '/row/c/text()', + query_to_xml( + format( + 'select count(*) as c from %I.%I', + schema_name, + 'quantitative_feature_value_queue' + ), + FALSE, + TRUE, + '' + ) + ) + )[1]::text::int AS number_jobs_in_queue +FROM + default_study_lookup.study_lookup +; diff --git a/spatialprofilingtoolbox/db/scripts/load_query.py b/spatialprofilingtoolbox/db/scripts/load_query.py new file mode 100644 index 00000000..e067e15b --- /dev/null +++ b/spatialprofilingtoolbox/db/scripts/load_query.py @@ -0,0 +1,53 @@ +"""Utility to print the SQL query that returns the number of computation jobs in the queue.""" + +import argparse +from importlib.resources import as_file +from importlib.resources import files + + +def _get_data_model_file(filename: str) -> str: + source_package = 'spatialprofilingtoolbox.db.data_model' + with as_file(files(source_package).joinpath(filename)) as path: + with open(path, encoding='utf-8') as file: + script = file.read() + return script + +def get_load_query() -> str: + return _get_data_model_file('load_query.sql') + + +def get_load_query_breakdown() -> str: + return _get_data_model_file('load_query_breakdown.sql') + + +def parse_args(): + parser = argparse.ArgumentParser( + prog='spt db load-query', + description='Get a SQL query which returns the size of the computation job queue, suitable as a measure of load.', + ) + group = parser.add_mutually_exclusive_group(required=True) + group.add_argument( + '--all', + action='store_true', + default=False, + help='If selected, the SQL query will be the one which sums all job queue sizes for a single total.', + ) + group.add_argument( + '--breakdown-by-dataset', + action='store_true', + default=False, + help='If selected, the SQL query will return the job queue sizes broken down by dataset.', + ) + return parser.parse_args() + + +def main(): + args = parse_args() + if args.all: + print(get_load_query()) + if args.breakdown_by_dataset: + print(get_load_query_breakdown()) + + +if __name__=='__main__': + main()