Skip to content

Commit

Permalink
refactor: levels filtering to a nested function
Browse files Browse the repository at this point in the history
  • Loading branch information
fjugipe committed Nov 22, 2024
1 parent 779c6a4 commit f119e88
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions api/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async def add_request_parameters(
request.filter["standard_name"].values.extend(split_and_strip(standard_names))

if levels:
request.filter["level"].values.extend(get_z_levels_or_range(levels))
request.filter["level"].values.extend(get_levels_values(levels))

if methods:
request.filter["function"].values.extend(split_and_strip(methods))
Expand All @@ -180,35 +180,29 @@ def get_periods_or_range(periods: str) -> list[str]:
raise HTTPException(status_code=400, detail=f"{err}")


def get_z_levels_or_range(z: str) -> list[str]:
def get_levels_values(levels: str) -> list[str]:
"""
Function for getting the levels filters as a list of levels, ranges
or range intervals
Function for getting the levels filters as a list of levels, ranges,
range intervals or combination of the previous
"""
# it can be z=value1,value2,value3: z=2,10,80
# or z=minimum value/maximum value: z=10/100
# or z=Rn/min height/height interval: z=R20/100/50
# or a combination of the above: z=10,30/100,200,300,R20/100/50

values = [level_or_range.split("/") for level_or_range in split_and_strip(z)]
def get_level_or_range(z: str) -> list[str]:
try:
split_on_slash = z.split("/")
if len(split_on_slash) == 2:
return get_z_values_from_range(split_on_slash)
elif len(split_on_slash) > 2:
return get_z_values_from_interval(split_on_slash)
else:
return [convert_m_to_cm(z)]
except ValueError:
raise HTTPException(status_code=400, detail=f"Invalid levels value: {z}")

try:
nested_filters = [
(
get_z_values_from_interval(level_or_range)
if len(level_or_range) > 2
else (
get_z_values_from_range(level_or_range)
if len(level_or_range) == 2
else [convert_m_to_cm(level_or_range[0])]
)
)
for level_or_range in values
]
# Return the flattened list of level filters
return list(chain(*nested_filters))
except ValueError:
raise HTTPException(status_code=400, detail=f"Invalid levels value: {z}")
return list(chain(*[get_level_or_range(z) for z in split_and_strip(levels)]))


def get_z_values_from_range(range: list[str]) -> list[str]:
Expand Down

1 comment on commit f119e88

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Unit Test Coverage Report
FileStmtsMissCoverMissing
__init__.py00100% 
datastore_pb2.py614821%34–81
datastore_pb2_grpc.py542750%15–16, 19, 65–80, 121–123, 128–130, 135–137, 142–144, 148–173, 219, 246, 273, 300
export_metrics.py100100% 
grpc_getter.py201145%15–19, 23–26, 30–32, 36–38
locustfile.py15150%1–31
main.py43784%45, 50, 60, 70–71, 81–82
metadata_endpoints.py663252%45–54, 58, 85, 100–220, 224
response_classes.py50100% 
utilities.py1803978%21, 39, 46, 68–71, 79–90, 95–102, 122, 126, 128, 156, 162, 180, 238–244, 248–250, 280, 284, 306, 311
custom_geo_json
   edr_feature_collection.py60100% 
formatters
   __init__.py110100% 
   covjson.py60198%91
   geojson.py21290%27, 52
openapi
   custom_dimension_examples.py40100% 
   edr_query_parameter_descriptions.py110100% 
   openapi_examples.py130100% 
routers
   __init__.py00100% 
   edr.py101496%348–349, 438–439
   feature.py471960%99–132, 148–153, 159–181
TOTAL72820572% 

Title Coverage Tests Skipped Failures Errors Time
API Unit Tests Coverage 32 0 💤 0 ❌ 0 🔥 1.903s ⏱️
Ingest Unit Tests Coverage 16 0 💤 0 ❌ 0 🔥 1m 43s ⏱️

Please sign in to comment.