Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1307 be chaching get filtered requests #1359

Merged
merged 2 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/api/code/lacity_data_api/models/service_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 12 additions & 35 deletions server/api/code/lacity_data_api/services/utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# import hashlib
import os
from datetime import date, timedelta
from ..config import DATA_DIR
Expand All @@ -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()
Expand All @@ -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
}


Expand All @@ -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)