Skip to content

Commit

Permalink
Merge pull request #2260 from WikiWatershed/tt/bigcz-cache-cuahsi-req…
Browse files Browse the repository at this point in the history
…uests

BiG-CZ: Cache CUAHSI Requests

Connects #1932
  • Loading branch information
rajadain committed Sep 20, 2017
2 parents ac2f981 + 332f5d1 commit fe10d23
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/mmw/apps/bigcz/clients/cuahsi/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from operator import attrgetter

from suds.client import Client
from suds.sudsobject import asdict
from rest_framework.exceptions import ValidationError
from django.contrib.gis.geos import Point

from django.core.cache import cache
from django.conf import settings

from apps.bigcz.models import ResourceLink, ResourceList, BBox
Expand Down Expand Up @@ -42,6 +44,27 @@
client = Client(CATALOG_URL, timeout=settings.BIGCZ_CLIENT_TIMEOUT)


def recursive_asdict(d):
"""
Convert Suds object into serializable format, so it can be cached.
From https://gist.github.com/robcowie/a6a56cf5b17a86fdf461
"""
out = {}
for k, v in asdict(d).iteritems():
if hasattr(v, '__keylist__'):
out[k] = recursive_asdict(v)
elif isinstance(v, list):
out[k] = []
for item in v:
if hasattr(item, '__keylist__'):
out[k].append(recursive_asdict(item))
else:
out[k].append(item)
else:
out[k] = v
return out


def filter_networkIDs(services, gridded=False):
"""
Transforms list of services to list of ServiceIDs, with respect to
Expand All @@ -52,8 +75,8 @@ def filter_networkIDs(services, gridded=False):
If no filters apply, we return an empty list to disable filtering.
"""
if not gridded:
return [str(s.ServiceID) for s in services
if s.NetworkName not in GRIDDED]
return [str(s['ServiceID']) for s in services
if s['NetworkName'] not in GRIDDED]

return []

Expand Down Expand Up @@ -171,9 +194,17 @@ def group_series_by_location(series):
return records


def make_request(request, **kwargs):
def make_request(request, expiry, **kwargs):
key = 'bigcz_{}_{}'.format(request.method.name,
hash(frozenset(kwargs.items())))
cached = cache.get(key)
if cached:
return cached

try:
return request(**kwargs)
response = recursive_asdict(request(**kwargs))
cache.set(key, response, timeout=expiry)
return response
except URLError, e:
if isinstance(e.reason, timeout):
raise RequestTimedOutError()
Expand All @@ -185,6 +216,7 @@ def make_request(request, **kwargs):

def get_services_in_box(box):
result = make_request(client.service.GetServicesInBox2,
604800, # Cache for one week
xmin=box.xmin,
xmax=box.xmax,
ymin=box.ymin,
Expand All @@ -205,6 +237,7 @@ def get_series_catalog_in_box(box, from_date, to_date, networkIDs):
to_date = to_date or DATE_MAX

result = make_request(client.service.GetSeriesCatalogForBox2,
300, # Cache for 5 minutes
xmin=box.xmin,
xmax=box.xmax,
ymin=box.ymin,
Expand Down

0 comments on commit fe10d23

Please sign in to comment.