Skip to content

Commit

Permalink
refine stats response models
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeifer committed Apr 22, 2024
1 parent a8731db commit 89a93d8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
26 changes: 13 additions & 13 deletions snodas/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def build_links(self: Self, request: HttpRequest, api: NinjaAPI) -> Self:


class SnodasStats(BaseModel):
date: date
swe: float
depth: float
runoff: float
Expand Down Expand Up @@ -343,20 +344,19 @@ def __str__(self) -> str:
class PourPointStats(BaseModel):
pourpoint: PourPoint
query: PourPointQuery
results: dict[str, SnodasStats] = Field(
results: list[SnodasStats] = Field(
...,
examples=[{
'2008-12-14': {
'swe': 0.018373034138853925,
'depth': 0.12781884243276667,
'runoff': 0.0000057251843327792756,
'sublimation': -0.00012737501598261594,
'average_temp': 266.3164421865995,
'precip_solid': 6.18786387077508,
'precip_liquid': 0.03673017090738538,
'sublimation_blowing': -6.307803776158206e-8,
},
}],
examples=[[{
'date': '2008-12-14',
'swe': 0.018373034138853925,
'depth': 0.12781884243276667,
'runoff': 0.0000057251843327792756,
'sublimation': -0.00012737501598261594,
'average_temp': 266.3164421865995,
'precip_solid': 6.18786387077508,
'precip_liquid': 0.03673017090738538,
'sublimation_blowing': -6.307803776158206e-8,
}]],
)
links: list[Link] = []

Expand Down
15 changes: 15 additions & 0 deletions snodas/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.urls import path, reverse

from ninja import NinjaAPI
from ninja.errors import HttpError

from snodas import types
from snodas.views import (
Expand Down Expand Up @@ -156,6 +157,13 @@ def id_stat_range_query(
request,
api,
)

if not pourpoint.properties.area_meters:
raise HttpError(
status_code=409,
message='Pourpoint does not have an AOI polygon',
)

query = types.DateRangeQuery(
start_date=start_date,
end_date=end_date,
Expand Down Expand Up @@ -187,6 +195,13 @@ def id_stat_doy_query(
request,
api,
)

if not pourpoint.properties.area_meters:
raise HttpError(
status_code=409,
message='Pourpoint does not have an AOI polygon',
)

query = types.DOYQuery(
month=month,
day=day,
Expand Down
14 changes: 5 additions & 9 deletions snodas/views/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,17 @@ def raw_stat_query_csv(request, cursor, filename, stat_query):
def get_pourpoint_stats(
pourpoint_id: int,
query: types.DateQuery,
) -> dict[str, types.SnodasStats]:
) -> list[types.SnodasStats]:
with connection.cursor() as cursor:
cursor.execute(
query.stat_query(pourpoint_id).as_string(cursor.connection),
)
columns = (x.name for x in cursor.description)
rows = cursor.fetchall()
rtn: dict[str, types.SnodasStats] = {}

for row in rows:
result = dict(zip(columns, row))
date = result.pop('date')
rtn[str(date)] = types.SnodasStats(**result)

return rtn
return [
types.SnodasStats(**dict(zip(columns, row)))
for row in rows
]


def get_csv_statistics(
Expand Down

0 comments on commit 89a93d8

Please sign in to comment.