Skip to content

Commit 568ee45

Browse files
authored
feat: Granules Sort by time or other properties (#532)
* breaking: using latest uds-lib + update docker * feat: use latest uds-lib * chore: allow local ES * fix: add es type * fix: adding a no-ssl option * feat: optionally omitting cumulus in collection creation * feat: split archive api to a different file to simplify documentation * feat: move one more archive api * fix: add granule addition (w/o cumulus) * feat: allow cross collection query + bbox * feat: Api documentation (#522) * chore: add documentation * chore: update names * chore: update tag * fix: boto3 s3 transfer lib issue * feat: add sort by argument * update keyword to match ogc * fix: bbox need to check for None type * chore: dummy
1 parent 3ece102 commit 568ee45

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Questions about our project? Please see our: [FAQ]([INSERT LINK TO FAQ / DISCUSS
111111
1. Question 1
112112
- Answer to question 1.
113113
2. Question 2
114-
- Answer to question 2
114+
- Answer to question 2.
115115
-->
116116

117117
<!-- example FAQ inline with no questions yet>

cumulus_lambda_functions/uds_api/dapa/granules_dapa_query_es.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
class GranulesDapaQueryEs:
23-
def __init__(self, collection_id, limit, offset, input_datetime, filter_input, pagination_link_obj: PaginationLinksGenerator, base_url, bbox=None):
23+
def __init__(self, collection_id, limit, offset, input_datetime, filter_input, pagination_link_obj: PaginationLinksGenerator, base_url, bbox=None, sort_by=None):
2424
self.__collection_cnm_lambda_name = os.environ.get('COLLECTION_CREATION_LAMBDA_NAME', '').strip()
2525
self.__pagination_link_obj = pagination_link_obj
2626
self.__input_datetime = input_datetime
@@ -31,6 +31,35 @@ def __init__(self, collection_id, limit, offset, input_datetime, filter_input, p
3131
self.__filter_input = filter_input
3232
self.__granules_index = GranulesDbIndex()
3333
self.__bbox = bbox
34+
self.__sort_by = sort_by
35+
36+
def get_sorting_arguments(self):
37+
if self.__sort_by is None or self.__sort_by == '':
38+
return [
39+
{'properties.datetime': {'order': 'desc'}},
40+
{'id': {'order': 'asc'}}
41+
]
42+
sorting_dict = {}
43+
sort_keys = [k.strip() for k in self.__sort_by.split(',')]
44+
for each_key in sort_keys:
45+
if each_key.startswith('+'):
46+
sorting_dict[each_key[1:]] = {'order': 'asc'}
47+
elif each_key.startswith('-'):
48+
sorting_dict[each_key[1:]] = {'order': 'desc'}
49+
else:
50+
sorting_dict[each_key] = {'order': 'asc'}
51+
if 'properties.datetime' not in sorting_dict:
52+
sorting_dict['properties.datetime'] = {'order': 'desc'}
53+
if 'id' not in sorting_dict:
54+
sorting_dict['id'] = {'order': 'asc'}
55+
56+
sorting_array = [
57+
{'properties.datetime': sorting_dict.pop('properties.datetime')},
58+
{'id': sorting_dict.pop('id')},
59+
]
60+
for k, v in sorting_dict.items():
61+
sorting_array.append({k: v})
62+
return sorting_array
3463

3564
def __generate_es_dsl(self):
3665
query_terms = []
@@ -52,10 +81,7 @@ def __generate_es_dsl(self):
5281
'track_total_hits': self.__offset is None,
5382
'size': self.__limit,
5483
# "collapse": {"field": "id"},
55-
'sort': [
56-
{'properties.datetime': {'order': 'desc'}},
57-
{'id': {'order': 'asc'}}
58-
],
84+
'sort': self.get_sorting_arguments(),
5985
'query': {
6086
'bool': {
6187
'must': query_terms

cumulus_lambda_functions/uds_api/granules_api.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ async def get_granules_dapa(request: Request, collection_id: str=Path(descriptio
7575
offset: Union[str, None] = Query(None, description='Pagination Item from current page to get the next page'),
7676
datetime: Union[str, None] = Query(None, description='Example: 2018-02-12T23:20:50Z'),
7777
filter: Union[str, None] = Query(None, description="OGC CQL filters: https://portal.ogc.org/files/96288#rc_cql-text -- Example: id in (g1,g2,g3) and tags::core = 'level-3' and (time1 < 34 or time1 > 14)"),
78-
bbox: Union[str, None]=Query(None, description='Bounding box in minx,miny,maxx,maxy -- Example: bbox=12.3,0.3,14.4,2.3')):
78+
bbox: Union[str, None]=Query(None, description='Bounding box in minx,miny,maxx,maxy -- Example: bbox=12.3,0.3,14.4,2.3'),
79+
sortby: Union[str, None]=Query(None, description='Sort the results based on the comma separated parameters, each sorting key can be started with + / - for ascending / descending order. missing operator is assumed "+". Example: sortby=+id,-properties.created'),
80+
):
81+
# https://docs.ogc.org/DRAFTS/24-030.html#sortby-parameter
82+
# https://docs.ogc.org/DRAFTS/24-030.html#_declaring_default_sort_order
7983
authorizer: UDSAuthorizorAbstract = UDSAuthorizerFactory() \
8084
.get_instance(UDSAuthorizerFactory.cognito,
8185
es_url=os.getenv('ES_URL'),
@@ -95,8 +99,8 @@ async def get_granules_dapa(request: Request, collection_id: str=Path(descriptio
9599
try:
96100
pagination_links = PaginationLinksGenerator(request)
97101
api_base_prefix = FastApiUtils.get_api_base_prefix()
98-
bbox_array = [float(k) for k in bbox.split(',')]
99-
granules_dapa_query = GranulesDapaQueryEs(collection_id, limit, offset, datetime, filter, pagination_links, f'{pagination_links.base_url}/{api_base_prefix}', bbox_array)
102+
bbox_array = [float(k) for k in bbox.split(',')] if bbox is not None else None
103+
granules_dapa_query = GranulesDapaQueryEs(collection_id, limit, offset, datetime, filter, pagination_links, f'{pagination_links.base_url}/{api_base_prefix}', bbox_array, sortby)
100104
granules_result = granules_dapa_query.start()
101105
except Exception as e:
102106
LOGGER.exception('failed during get_granules_dapa')

0 commit comments

Comments
 (0)