Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogsilva committed Nov 15, 2024
1 parent 088c933 commit e89ed3f
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 416 deletions.
41 changes: 0 additions & 41 deletions arpav_ppcv/cliapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,47 +71,6 @@ def delete_station(
database.delete_station(session, station_id)


@app.command(name="list-variables")
def list_variables(ctx: typer.Context) -> None:
"""List variables."""
with sqlmodel.Session(ctx.obj["engine"]) as session:
result = [
schemas.VariableRead(**v.model_dump())
for v in database.collect_all_variables(session)
]
print(pydantic_core.to_json(result, indent=_JSON_INDENTATION).decode("utf-8"))


@app.command(name="create-variable")
def create_variable(
ctx: typer.Context,
name: str,
description: str,
unit: Optional[str] = "",
) -> None:
variable_create = schemas.VariableCreate(
name=name, description=description, unit=unit
)
"""Create a new variable."""
with sqlmodel.Session(ctx.obj["engine"]) as session:
db_variable = database.create_variable(session, variable_create)
print(
schemas.VariableRead(**db_variable.model_dump()).model_dump_json(
indent=_JSON_INDENTATION
)
)


@app.command(name="delete-variable")
def delete_variable(
ctx: typer.Context,
variable_id: uuid.UUID,
) -> None:
"""Delete a variable."""
with sqlmodel.Session(ctx.obj["engine"]) as session:
database.delete_variable(session, variable_id)


@app.command(name="list-monthly-measurements")
def list_monthly_measurements(ctx: typer.Context) -> None:
"""List monthly measurements."""
Expand Down
65 changes: 36 additions & 29 deletions arpav_ppcv/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import shapely.io
import sqlmodel
from anyio.from_thread import start_blocking_portal
from arpav_ppcv.schemas.base import CoreConfParamName

from dateutil.parser import isoparse
from geoalchemy2.shape import to_shape
from pandas.core.indexes.datetimes import DatetimeIndex
Expand All @@ -40,6 +40,7 @@
coverages,
observations,
)
from .schemas.base import CoreConfParamName
from .thredds import (
crawler,
ncss,
Expand Down Expand Up @@ -1018,27 +1019,21 @@ def list_coverage_identifiers_by_param_values(
limit: int = 20,
offset: int = 0,
) -> list[str]:
valid_variables = _filter_configuration_parameter_values(
valid_climatic_indicators = _list_possible_climatic_indicators(
session,
climatological_variable_filter or [],
CoreConfParamName.CLIMATOLOGICAL_VARIABLE.value,
measure_filter or [],
aggregation_period_filter or [],
)
logger.debug(f"{locals()=}")
valid_climatological_models = _filter_configuration_parameter_values(
session,
climatological_model_filter or [],
CoreConfParamName.CLIMATOLOGICAL_MODEL.value,
)
valid_aggregation_periods = _filter_configuration_parameter_values(
session,
aggregation_period_filter or [],
CoreConfParamName.AGGREGATION_PERIOD.value,
)
valid_scenarios = _filter_configuration_parameter_values(
session, scenario_filter or [], CoreConfParamName.SCENARIO.value
)
valid_measures = _filter_configuration_parameter_values(
session, measure_filter or [], CoreConfParamName.MEASURE.value
)
valid_year_periods = _filter_configuration_parameter_values(
session, year_period_filter or [], CoreConfParamName.YEAR_PERIOD.value
)
Expand All @@ -1053,40 +1048,25 @@ def list_coverage_identifiers_by_param_values(
valid_time_window_names = [tw.name for tw in valid_time_windows if tw is not None]
coverage_identifiers = set()
for combination in itertools.product(
valid_variables,
valid_aggregation_periods,
valid_climatic_indicators,
valid_climatological_models,
valid_scenarios,
valid_measures,
valid_year_periods,
valid_time_windows,
):
(
variable,
aggregation_period,
model,
scenario,
measure,
year_period,
time_window,
) = combination
climatic_indicator, model, scenario, year_period, time_window = combination
logger.debug(
f"getting links for variable: {variable.name!r}, "
f"aggregation_period: {aggregation_period.name!r}, "
f"getting links for climatic_indicator: {climatic_indicator.identifier!r}, "
f"model: {model.name!r}, scenario: {scenario.name!r}, "
f"measure: {measure.name!r}, "
f"year_period: {year_period.name!r}, "
f"time_window: {time_window.name if time_window else time_window!r}..."
)
param_values_filter = [
database.get_configuration_parameter_value_by_names(
session, CoreConfParamName.ARCHIVE.value, "forecast"
),
variable,
aggregation_period,
model,
scenario,
measure,
year_period,
time_window,
]
Expand All @@ -1096,6 +1076,7 @@ def list_coverage_identifiers_by_param_values(
limit=limit,
offset=offset,
configuration_parameter_values_filter=filter_,
climatic_indicator_filter=climatic_indicator,
)
for cov_conf in cov_confs:
identifiers = database.generate_coverage_identifiers(
Expand All @@ -1119,3 +1100,29 @@ def list_coverage_identifiers_by_param_values(
if len(coverage_identifiers) > (offset + limit):
break
return sorted(coverage_identifiers)[offset : offset + limit]


def _list_possible_climatic_indicators(
session: sqlmodel.Session,
variable_names: Sequence[str],
measure_types: Sequence[str],
aggregation_periods: Sequence[str],
) -> list[climaticindicators.ClimaticIndicator]:
filtered = database.collect_all_climatic_indicators(session)
if len(variable_names) > 0:
filtered = [i for i in filtered if i.name in variable_names]
if len(measure_types) > 0:
filtered = [
i for i in filtered if i.measure_type.value.lower() in measure_types
]
if len(aggregation_periods) > 0:
periods = []
for p in aggregation_periods:
if p == "30yr":
periods.append("thirty_year")
else:
periods.append(p)
filtered = [
i for i in filtered if i.aggregation_period.value.lower() in periods
]
return filtered
25 changes: 16 additions & 9 deletions arpav_ppcv/prefect/flows/observations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime as dt
from typing import Sequence
from typing import (
Sequence,
Union,
)

import httpx
import sqlmodel
Expand Down Expand Up @@ -552,39 +555,43 @@ def _build_created_measurements_table(


def build_monthly_measurement_id(
measurement: observations.MonthlyMeasurement
| observations.MonthlyMeasurementCreate,
measurement: Union[
observations.MonthlyMeasurement, observations.MonthlyMeasurementCreate
],
) -> str:
return "-".join(
(
str(measurement.station_id),
str(measurement.variable_id),
str(measurement.climatic_indicator_id),
measurement.date.strftime("%Y%m"),
)
)


def build_seasonal_measurement_id(
measurement: observations.SeasonalMeasurement
| observations.SeasonalMeasurementCreate,
measurement: Union[
observations.SeasonalMeasurement, observations.SeasonalMeasurementCreate
],
) -> str:
return "-".join(
(
str(measurement.station_id),
str(measurement.variable_id),
str(measurement.climatic_indicator_id),
str(measurement.year),
measurement.season.value,
)
)


def build_yearly_measurement_id(
measurement: observations.YearlyMeasurement | observations.YearlyMeasurementCreate,
measurement: Union[
observations.YearlyMeasurement, observations.YearlyMeasurementCreate
],
) -> str:
return "-".join(
(
str(measurement.station_id),
str(measurement.variable_id),
str(measurement.climatic_indicator_id),
str(measurement.year),
)
)
Expand Down
3 changes: 1 addition & 2 deletions arpav_ppcv/schemas/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ class MonthlyMeasurement(MonthlyMeasurementBase, table=True):

class MonthlyMeasurementCreate(sqlmodel.SQLModel):
station_id: pydantic.UUID4
# variable_id: pydantic.UUID4
climatic_indicator_id: int
value: float
date: dt.date
Expand Down Expand Up @@ -361,7 +360,7 @@ class SeasonalMeasurement(sqlmodel.SQLModel, table=True):

class SeasonalMeasurementCreate(sqlmodel.SQLModel):
station_id: pydantic.UUID4
climatic_indicator_id: pydantic.UUID4
climatic_indicator_id: int
value: float
year: int
season: base.Season
Expand Down
18 changes: 5 additions & 13 deletions arpav_ppcv/webapp/api_v2/routers/coverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,25 +228,17 @@ def list_coverage_identifiers(
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}"
)
conf_param_values_filter, climatic_indicator = _retrieve_climatic_indicator_filter(
db_session, possible_value or []
)
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,
configuration_parameter_values_filter=conf_param_values_filter,
climatic_indicator_filter=climatic_indicator,
)
_, unfiltered_total = db.list_coverage_identifiers(
db_session, limit=1, offset=0, include_total=True
Expand Down
18 changes: 10 additions & 8 deletions arpav_ppcv/webapp/api_v2/routers/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def list_monthly_measurements(
db_session: Annotated[Session, Depends(dependencies.get_db_session)],
list_params: Annotated[dependencies.CommonListFilterParameters, Depends()],
station_code: str | None = None,
variable_name: str | None = None,
climatic_indicator_identifier: str | None = None,
month: Annotated[int | None, fastapi.Query(le=1, ge=12)] = None,
):
"""List known monthly measurements."""
Expand All @@ -143,20 +143,22 @@ def list_monthly_measurements(
raise ValueError("Invalid station code")
else:
station_id = None
if variable_name is not None:
db_variable = db.get_variable_by_name(db_session, variable_name)
if db_variable is not None:
variable_id = db_variable.id
if climatic_indicator_identifier is not None:
db_climatic_indicator = db.get_climatic_indicator_by_identifier(
db_session, climatic_indicator_identifier
)
if db_climatic_indicator is not None:
climatic_indicator_id = db_climatic_indicator.id
else:
raise ValueError("Invalid variable name")
raise ValueError("Invalid climatic indicator identifier")
else:
variable_id = None
climatic_indicator_id = None
monthly_measurements, filtered_total = db.list_monthly_measurements(
db_session,
limit=list_params.limit,
offset=list_params.offset,
station_id_filter=station_id,
variable_id_filter=variable_id,
climatic_indicator_id_filter=climatic_indicator_id,
month_filter=month,
include_total=True,
)
Expand Down
12 changes: 6 additions & 6 deletions arpav_ppcv/webapp/api_v2/schemas/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def from_db_instance(

class MonthlyMeasurementReadListItem(observations.MonthlyMeasurementBase):
url: pydantic.AnyHttpUrl
variable_name: str
climatic_indicator_identifier: str
station_code: str

@classmethod
Expand All @@ -80,7 +80,7 @@ def from_db_instance(
) -> "MonthlyMeasurementReadListItem":
return cls(
**instance.model_dump(),
variable_name=instance.variable.name,
climatic_indicator_identifier=instance.climatic_indicator.identifier,
station_code=instance.station.code,
url=str(
request.url_for(
Expand All @@ -92,7 +92,7 @@ def from_db_instance(

class SeasonalMeasurementReadListItem(pydantic.BaseModel):
url: pydantic.AnyHttpUrl
variable_name: str
climatic_indicator_identifier: str
station_code: str
year: int
season: Season
Expand All @@ -106,7 +106,7 @@ def from_db_instance(
) -> "SeasonalMeasurementReadListItem":
return cls(
**instance.model_dump(),
variable_name=instance.variable.name,
climatic_indicator_identifier=instance.climatic_indicator.identifier,
station_code=instance.station.code,
url=str(
request.url_for(
Expand All @@ -118,7 +118,7 @@ def from_db_instance(

class YearlyMeasurementReadListItem(pydantic.BaseModel):
url: pydantic.AnyHttpUrl
variable_name: str
climatic_indicator_identifier: str
station_code: str
year: int
value: float
Expand All @@ -131,7 +131,7 @@ def from_db_instance(
) -> "YearlyMeasurementReadListItem":
return cls(
**instance.model_dump(),
variable_name=instance.variable.name,
climatic_indicator_identifier=instance.climatic_indicator.identifier,
station_code=instance.station.code,
url=str(
request.url_for(
Expand Down
Loading

0 comments on commit e89ed3f

Please sign in to comment.