Skip to content

Commit 7cb9071

Browse files
authored
Add 'all-keys' REST Query Parameter (#74)
* add AllKeys class, _get_projection(), & _limit_result_list() * use _get_projection() & _limit_result_list(); type-hint, black-fmt, doc * keys=None bug fix * add nack count_files() kwargs * count_files() bug fix * add argbuilder.py: move build_files_query() & make build_keys() * bug fix: kwargs not **kwargs * test "all-keys" argument * Revert "test "all-keys" argument" This reverts commit 8fd2d28. * test "all-keys" argument * add build_limit() * add build_limit() - 2 * add build_limit() - 3 * add build_start() * comments: "id_" -> "_id" * reuse logging messages for Exceptions * body -> args
1 parent 5c68aa1 commit 7cb9071

File tree

5 files changed

+447
-264
lines changed

5 files changed

+447
-264
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- run: |
2525
. env/bin/activate &&
2626
resources/enable_profiling.py &&
27-
pytest --tb=short &&
27+
pytest -vvvvv --tb=short &&
2828
resources/profile_queries.py
2929
deploy:
3030
docker:

file_catalog/argbuilder.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""Builder utility functions for arg/kwargs dicts."""
2+
3+
from typing import Any, Dict
4+
5+
from tornado.escape import json_decode
6+
7+
# local imports
8+
from file_catalog.mongo import AllKeys
9+
10+
11+
def build_limit(kwargs: Dict[str, Any], config: Dict[str, Any]) -> None:
12+
"""Build the `"limit"` argument."""
13+
if "limit" in kwargs:
14+
kwargs["limit"] = int(kwargs["limit"])
15+
if kwargs["limit"] < 1:
16+
raise Exception("limit is not positive")
17+
18+
# check with config
19+
if kwargs["limit"] > config["FC_QUERY_FILE_LIST_LIMIT"]:
20+
kwargs["limit"] = config["FC_QUERY_FILE_LIST_LIMIT"]
21+
else:
22+
# if no limit has been defined, set max limit
23+
kwargs["limit"] = config["FC_QUERY_FILE_LIST_LIMIT"]
24+
25+
26+
def build_start(kwargs: Dict[str, Any]) -> None:
27+
"""Build the `"start"` argument."""
28+
if "start" in kwargs:
29+
kwargs["start"] = int(kwargs["start"])
30+
if kwargs["start"] < 0:
31+
raise Exception("start is negative")
32+
33+
34+
def build_files_query(kwargs: Dict[str, Any]) -> None:
35+
"""Build `"query"` dict with formatted/fully-named arguments.
36+
37+
Pop corresponding shortcut-keys from `kwargs`.
38+
"""
39+
if "query" in kwargs:
40+
# keep whatever was already in here, then add to it
41+
if isinstance(kwargs["query"], (str, bytes)):
42+
query = json_decode(kwargs.pop("query"))
43+
else:
44+
query = kwargs.pop("query")
45+
else:
46+
query = {}
47+
48+
if "locations.archive" not in query:
49+
query["locations.archive"] = None
50+
51+
# shortcut query params
52+
if "logical_name" in kwargs:
53+
query["logical_name"] = kwargs.pop("logical_name")
54+
if "run_number" in kwargs:
55+
query["run.run_number"] = kwargs.pop("run_number")
56+
if "dataset" in kwargs:
57+
query["iceprod.dataset"] = kwargs.pop("dataset")
58+
if "event_id" in kwargs:
59+
e = kwargs.pop("event_id")
60+
query["run.first_event"] = {"$lte": e}
61+
query["run.last_event"] = {"$gte": e}
62+
if "processing_level" in kwargs:
63+
query["processing_level"] = kwargs.pop("processing_level")
64+
if "season" in kwargs:
65+
query["offline_processing_metadata.season"] = kwargs.pop("season")
66+
67+
kwargs["query"] = query
68+
69+
70+
def build_keys(kwargs: Dict[str, Any]) -> None:
71+
"""Build `"keys"` list, potentially using `"all-keys"`.
72+
73+
Pop `"all-keys"`.
74+
"""
75+
use_all_keys = kwargs.pop("all-keys", None) in ["True", "true", 1]
76+
77+
if use_all_keys:
78+
kwargs["keys"] = AllKeys()
79+
elif "keys" in kwargs:
80+
kwargs["keys"] = kwargs["keys"].split("|")

0 commit comments

Comments
 (0)