Skip to content

Commit

Permalink
talky talky talky
Browse files Browse the repository at this point in the history
  • Loading branch information
gassc committed Jul 2, 2021
1 parent 22d4d26 commit ad4bd2c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
14 changes: 12 additions & 2 deletions trwwapi/rainfall/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pdb
import objgraph

from django.utils.timezone import localtime
from django.utils.timezone import localtime, now
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from rest_framework import status
Expand Down Expand Up @@ -392,7 +392,6 @@ def _get_latest(model_class, timestamp_field="timestamp"):
except (model_class.DoesNotExist, AttributeError):
return None


def get_latest_garrobservation():
return _get_latest(GarrObservation)

Expand All @@ -407,3 +406,14 @@ def get_latest_rtrgobservation():

def get_latest_rainfallevent():
return _get_latest(RainfallEvent, 'start_dt')

def get_rainfall_total_for(postgres_table_model, sensor_ids, back_to: timedelta):

end_dt = localtime(now(), TZ)
start_dt = end_dt - back_to

rows = query_pgdb(postgres_table_model, sensor_ids, [start_dt, end_dt])
if rows:
return round(sum(x['val'] for x in rows if x['val']), 1)
else:
return None
6 changes: 6 additions & 0 deletions trwwapi/rainfall/templates/speech.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head></head>
<body>
"{{text}}"
</body>
</html>
9 changes: 8 additions & 1 deletion trwwapi/rainfall/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
RtrrObservationViewset,
RtrgbservationViewset,
RainfallEventViewset,
LatestObservationTimestampsSummary
LatestObservationTimestampsSummary,
get_myrain_24hours,
get_myrain_48hours,
get_myrain_pastweek
)

# -----------------------------------------------
Expand Down Expand Up @@ -77,6 +80,10 @@
# --------------------------
# custom routes (for function-based views)
# path('v2/latest-observations/', LatestObservationTimestampsSummary.as_view({'get': 'list'})),

path('v3/myrain/24hours/', get_myrain_24hours),
path('v3/myrain/48hours/', get_myrain_48hours),
path('v3/myrain/pastweek/', get_myrain_pastweek),

# --------------------------
# low-level DRF-registered routes
Expand Down
51 changes: 49 additions & 2 deletions trwwapi/rainfall/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from datetime import timedelta
# from django.contrib.auth.models import User, Group
from django.utils.safestring import mark_safe
from django.conf import settings
from django_filters import filters
from django.contrib.gis.geos import Point
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
Expand All @@ -10,6 +13,8 @@
from rest_framework.pagination import PageNumberPagination
from django_filters.rest_framework import FilterSet, DjangoFilterBackend



from ..common.config import TZI
# from .api_v2.config import TZI

Expand All @@ -26,7 +31,8 @@
get_latest_gaugeobservation,
get_latest_rainfallevent,
get_latest_rtrgobservation,
get_latest_rtrrobservation
get_latest_rtrrobservation,
get_rainfall_total_for
)
from .models import (
GarrObservation,
Expand Down Expand Up @@ -208,4 +214,45 @@ def list(self, request, format=None):
raw_summary.items()
}

return Response(summary)
return Response(summary)


def _get_myrain_for(request, back_to: timedelta, back_to_text: str):
text ="That didn't work."

lat = request.GET.get('lat')
lng = request.GET.get('lng')
srid = request.GET.get('srid')

if all([lat, lng]):

p = Point(float(lng), float(lat)) #, srid=srid if srid else 4326)
p.srid = srid if srid else 4326
pixels = Pixel.objects.filter(geom__contains=p)

if len(list(pixels)) > 0:

total = get_rainfall_total_for(RtrrObservation, [pixel.pixel_id for pixel in pixels], timedelta(days=2))

if total:
text = """According to 3 Rivers Wet Weather, your location received approximately {0} inches of rainfall {1}.""".format(total, back_to_text)
else:
text = "Sorry, it looks like rainfall data is unavailable for your location for that timeframe. Check back soon!"
else:
text = "Sorry, we can't get detailed rainfall data for your location."
else:
text = "Sorry, you didn't provide enough location data to answer your question."

# text += " For more information about rainfall and infrastructure in the greater Pittsburgh area, visit w w w dot 3 rivers wet weather dot org."
text += " For more information about rainfall and infrastructure in the greater Pittsburgh area, visit www.3riverswetweather.org"

return render(request, 'speech.html', {"text": text})

def get_myrain_24hours(request):
return _get_myrain_for(request, timedelta(days=1), "over the past 24 hours")

def get_myrain_48hours(request):
return _get_myrain_for(request, timedelta(days=2), "over the past 48 hours")

def get_myrain_pastweek(request):
return _get_myrain_for(request, timedelta(days=7), "over the past week")

0 comments on commit ad4bd2c

Please sign in to comment.