From 868bb4563d5c735e9ad4e8b4594d934eb35215c4 Mon Sep 17 00:00:00 2001 From: Nicholas Kwon Date: Tue, 13 Sep 2022 10:14:57 -0700 Subject: [PATCH 1/2] include num_requests_last_week in build_cache return value --- .../lacity_data_api/services/utilities.py | 47 +++++-------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/server/api/code/lacity_data_api/services/utilities.py b/server/api/code/lacity_data_api/services/utilities.py index d7b591ce5..640fc337d 100644 --- a/server/api/code/lacity_data_api/services/utilities.py +++ b/server/api/code/lacity_data_api/services/utilities.py @@ -1,4 +1,3 @@ -# import hashlib import os from datetime import date, timedelta from ..config import DATA_DIR @@ -10,6 +9,11 @@ async def build_cache(): + """Builds the cache by calling the set of functions we want cached. + + Returns: + A dict mapping cache entry string names to their counts. + """ from ..models.geometry import Geometry # avoiding circular imports open_requests = await service_request.get_open_requests() @@ -19,31 +23,27 @@ async def build_cache(): types = await request_type.get_types_dict() geojson = await Geometry.get_council_geojson() - # for i in councils: - # await council.get_open_request_counts(i) - - # get results for past week + # Get results for past week. + num_requests_last_week = 0 for day in range(7): - await service_request.get_filtered_requests( + num_requests_last_week += len(await service_request.get_filtered_requests( date.today() - timedelta(days=day), date.today() - timedelta(days=day), - limit=GET_FILTERED_REQUESTS_LIMIT + limit=GET_FILTERED_REQUESTS_LIMIT) ) - # delete any cached CSV files + # Delete any cached CSV files. for file in os.scandir(DATA_DIR): os.remove(file.path) - # import lacity_data_api.services.reports as rpts - # await rpts.make_csv_cache("service_requests") - return { "open_requests": len(open_requests), "open_requests_counts": len(open_requests_counts), "types": len(types), "councils": len(councils), "regions": len(regions), - "geojson": len(geojson) + "geojson": len(geojson), + "num_requests_last_week": num_requests_last_week } @@ -63,26 +63,3 @@ def cache_key(f, *args, **kwargs): '.' + str(f.__qualname__) + str(args) ) - - -# TODO: maybe recessitate this for object keys (e.g. filters) -# def hashed_cache_key(f, *args, **kwargs): -# """ -# Utility function to create hashed key for pins based on filters -# """ - -# # want to sort the values for types and councils -# for i in args: -# if type(i) == list: -# i.sort() - -# object_key = str(args).encode('utf-8') # need a b-string for hashing -# hashed_key = hashlib.md5(object_key).hexdigest() - -# # use unhashed string if in DEBUG mode -# if DEBUG: -# # this should match the default aiocache key format -# return format(str(f.__module__) + str(f.__name__) + str(args)) -# else: -# # caching without the module and function to potentially make reusable -# return format(hashed_key) From d24dd15b914b8bf1b63dd482b3143da9b4100dc7 Mon Sep 17 00:00:00 2001 From: Nicholas Kwon Date: Tue, 13 Sep 2022 10:16:58 -0700 Subject: [PATCH 2/2] add cached decorator back --- server/api/code/lacity_data_api/models/service_request.py | 1 + 1 file changed, 1 insertion(+) diff --git a/server/api/code/lacity_data_api/models/service_request.py b/server/api/code/lacity_data_api/models/service_request.py index 4760fa257..9482f7fd9 100644 --- a/server/api/code/lacity_data_api/models/service_request.py +++ b/server/api/code/lacity_data_api/models/service_request.py @@ -162,6 +162,7 @@ async def get_open_request_counts(): return result +@cached(alias="default") async def get_filtered_requests( start_date: datetime.date = None, end_date: datetime.date = None,