Skip to content

Commit

Permalink
Merge pull request #168 from ricardogsilva/162-use-a-coverage-configu…
Browse files Browse the repository at this point in the history
…rations-possible-values-as-a-way-to-describe-it

Use a coverage configuration's possible values as a way to describe it
  • Loading branch information
francbartoli authored Jul 22, 2024
2 parents 7e9a1f4 + 17e32a1 commit 1f02a4a
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 336 deletions.
244 changes: 190 additions & 54 deletions arpav_ppcv/database.py

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions arpav_ppcv/schemas/coverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,13 @@ class CoverageConfiguration(sqlmodel.SQLModel, table=True):
@pydantic.computed_field()
@property
def coverage_id_pattern(self) -> str:
id_parts = ["{name}"]
for match_obj in re.finditer(r"(\{\w+\})", self.thredds_url_pattern):
id_parts.append(match_obj.group(1))
return "-".join(id_parts)
other_parts = set()
for pv in self.possible_values:
other_parts.add(
pv.configuration_parameter_value.configuration_parameter.name
)
all_parts = ["name"] + sorted(list(other_parts))
return "-".join(f"{{{part}}}" for part in all_parts)

def get_thredds_url_fragment(self, coverage_identifier: str) -> str:
try:
Expand Down Expand Up @@ -269,7 +272,9 @@ def build_coverage_identifier(
id_parts.append(conf_param_value.name)
break
else:
raise ValueError(f"Invalid param_name {param_name!r}")
raise ValueError(
f"Could not find suitable value for {param_name!r}"
)
else:
continue
return "-".join(id_parts)
Expand All @@ -293,15 +298,17 @@ def retrieve_used_values(
raise ValueError(f"Invalid parameter/value pair: {(param_name, value)}")
return result

def retrieve_configuration_parameters(self, coverage_identifier) -> dict[str, str]:
def retrieve_configuration_parameters(
self, coverage_identifier: str
) -> dict[str, str]:
pattern_parts = re.finditer(
r"\{(\w+)\}", self.coverage_id_pattern.partition("-")[-1]
)
id_parts = coverage_identifier.split("-")[1:]
result = {}
for index, pattern_match_obj in enumerate(pattern_parts):
id_part = id_parts[index]
configuration_parameter_name = pattern_match_obj.group(1)
id_part = id_parts[index]
result[configuration_parameter_name] = id_part
return result

Expand Down
2 changes: 2 additions & 0 deletions arpav_ppcv/webapp/admin/views/coverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ async def find_all(
database.list_configuration_parameters,
limit=limit,
offset=skip,
name_filter=str(where) if where not in (None, "") else None,
include_total=False,
)
db_conf_params, _ = await anyio.to_thread.run_sync(
Expand Down Expand Up @@ -439,6 +440,7 @@ async def find_all(
database.list_coverage_configurations,
limit=limit,
offset=skip,
name_filter=str(where) if where not in (None, "") else None,
include_total=False,
)
db_cov_confs, _ = await anyio.to_thread.run_sync(
Expand Down
4 changes: 3 additions & 1 deletion arpav_ppcv/webapp/admin/views/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ async def find_all(
db.list_variables,
limit=limit,
offset=skip,
name_filter=str(where) if where not in (None, "") else None,
include_total=False,
)
db_vars, _ = await anyio.to_thread.run_sync(
Expand All @@ -319,7 +320,7 @@ class StationView(ModelView):
fields.UuidField("id"),
starlette_admin.StringField("name", required=True),
starlette_admin.StringField("code", required=True),
starlette_admin.StringField("type", required=True),
starlette_admin.StringField("type_", required=True),
starlette_admin.FloatField("longitude", required=True),
starlette_admin.FloatField("latitude", required=True),
starlette_admin.DateField("active_since"),
Expand Down Expand Up @@ -448,6 +449,7 @@ async def find_all(
limit=limit,
offset=skip,
include_total=False,
name_filter=str(where) if where not in (None, "") else None,
)
db_stations, _ = await anyio.to_thread.run_sync(
list_stations, request.state.session
Expand Down
49 changes: 49 additions & 0 deletions arpav_ppcv/webapp/api_v2/routers/coverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ async def list_configuration_parameters(
request: Request,
db_session: Annotated[Session, Depends(dependencies.get_db_session)],
list_params: Annotated[dependencies.CommonListFilterParameters, Depends()],
name_contains: str | None = None,
):
"""List configuration parameters."""
config_params, filtered_total = db.list_configuration_parameters(
db_session,
limit=list_params.limit,
offset=list_params.offset,
include_total=True,
name_filter=name_contains,
)
_, unfiltered_total = db.list_configuration_parameters(
db_session, limit=1, offset=0, include_total=True
Expand All @@ -81,6 +83,15 @@ async def list_coverage_configurations(
request: Request,
db_session: Annotated[Session, Depends(dependencies.get_db_session)],
list_params: Annotated[dependencies.CommonListFilterParameters, Depends()],
possible_value: Annotated[
list[
Annotated[
str,
pydantic.StringConstraints(pattern=r"^[0-9a-zA-Z_]+:[0-9a-zA-Z_]+$"),
]
],
Query(),
] = None,
):
"""### List coverage configurations.
Expand Down Expand Up @@ -117,11 +128,24 @@ async def list_coverage_configurations(
endpoint.
"""
conf_param_values_filter = []
for possible in possible_value or []:
param_name, param_value = possible.partition(":")[::2]
db_parameter_value = db.get_configuration_parameter_value_by_names(
db_session, param_name, param_value
)
if db_parameter_value is not None:
conf_param_values_filter.append(db_parameter_value)
else:
logger.debug(
f"ignoring unknown parameter/value pair {param_name}:{param_value}"
)
coverage_configurations, filtered_total = db.list_coverage_configurations(
db_session,
limit=list_params.limit,
offset=list_params.offset,
include_total=True,
configuration_parameter_values_filter=conf_param_values_filter or None,
)
_, unfiltered_total = db.list_coverage_configurations(
db_session, limit=1, offset=0, include_total=True
Expand Down Expand Up @@ -156,6 +180,9 @@ def get_coverage_configuration(
)


# PossibleValue: pydantic.StringConstraints(pattern="^[\w-_]+:[\w-_]+$")


@router.get(
"/coverage-identifiers",
response_model=coverage_schemas.CoverageIdentifierList,
Expand All @@ -166,13 +193,35 @@ def list_coverage_identifiers(
db_session: Annotated[Session, Depends(dependencies.get_db_session)],
list_params: Annotated[dependencies.CommonListFilterParameters, Depends()],
name_contains: Annotated[list[str], Query()] = None,
possible_value: Annotated[
list[
Annotated[
str,
pydantic.StringConstraints(pattern=r"^[0-9a-zA-Z_]+:[0-9a-zA-Z_]+$"),
]
],
Query(),
] = None,
):
conf_param_values_filter = []
for possible in possible_value or []:
param_name, param_value = possible.partition(":")[::2]
db_parameter_value = db.get_configuration_parameter_value_by_names(
db_session, param_name, param_value
)
if db_parameter_value is not None:
conf_param_values_filter.append(db_parameter_value)
else:
logger.debug(
f"ignoring unknown parameter/value pair {param_name}:{param_value}"
)
cov_internals, filtered_total = db.list_coverage_identifiers(
db_session,
limit=list_params.limit,
offset=list_params.offset,
include_total=True,
name_filter=name_contains,
configuration_parameter_values_filter=conf_param_values_filter or None,
)
_, unfiltered_total = db.list_coverage_identifiers(
db_session, limit=1, offset=0, include_total=True
Expand Down
35 changes: 35 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,41 @@ def sample_configuration_parameters(arpav_db_session):
return db_conf_params


@pytest.fixture()
def sample_coverage_configurations(
arpav_db_session, sample_configuration_parameters
) -> list[coverages.CoverageConfiguration]:
db_cov_confs = []
for i in range(10):
params_to_use = random.sample(sample_configuration_parameters, k=2)
param_values_to_use = []
for param in params_to_use:
possible_value = coverages.ConfigurationParameterPossibleValue(
configuration_parameter_value=random.choice(param.allowed_values)
)
param_values_to_use.append(possible_value)
db_cov_confs.append(
coverages.CoverageConfiguration(
name=f"coverage_configuration{i}",
netcdf_main_dataset_name="some-dataset-name",
thredds_url_pattern=(
f"the_thredds-param_"
f"{{"
f"{random.choice(param_values_to_use).configuration_parameter_value.configuration_parameter.name}"
f"}}"
),
palette="fake",
possible_values=param_values_to_use,
)
)
for db_cov_conf in db_cov_confs:
arpav_db_session.add(db_cov_conf)
arpav_db_session.commit()
for db_cov_conf in db_cov_confs:
arpav_db_session.refresh(db_cov_conf)
return db_cov_confs


def _override_get_settings():
standard_settings = config.get_settings()
return standard_settings
Expand Down
Loading

0 comments on commit 1f02a4a

Please sign in to comment.