-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from Police-Data-Accessibility-Project/mc_v1_…
…agency_search_cache ⚡️ feat(middleware): Add functionality to cache homepage search results
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import psycopg2 | ||
from flask import Response, make_response | ||
|
||
SQL_GET_AGENCIES_WITHOUT_HOMEPAGE_URLS = """ | ||
SELECT | ||
SUBMITTED_NAME, | ||
JURISDICTION_TYPE, | ||
STATE_ISO, | ||
MUNICIPALITY, | ||
COUNTY_NAME, | ||
AIRTABLE_UID, | ||
COUNT_DATA_SOURCES, | ||
ZIP_CODE, | ||
NO_WEB_PRESENCE -- Relevant | ||
FROM | ||
PUBLIC.AGENCIES | ||
WHERE | ||
approved = true | ||
AND homepage_url is null | ||
AND NOT EXISTS ( | ||
SELECT 1 FROM PUBLIC.AGENCY_URL_SEARCH_CACHE | ||
WHERE PUBLIC.AGENCIES.AIRTABLE_UID = PUBLIC.AGENCY_URL_SEARCH_CACHE.agency_airtable_uid | ||
) | ||
ORDER BY COUNT_DATA_SOURCES DESC | ||
LIMIT 100 -- Limiting to 100 in acknowledgment of the search engine quota | ||
""" | ||
|
||
SQL_UPDATE_CACHE = """ | ||
INSERT INTO public.agency_url_search_cache | ||
(agency_airtable_uid, search_result) | ||
VALUES (%s, %s) | ||
""" | ||
|
||
|
||
def get_agencies_without_homepage_urls(cursor: psycopg2.extensions.cursor) -> Response: | ||
cursor.execute(SQL_GET_AGENCIES_WITHOUT_HOMEPAGE_URLS) | ||
results = cursor.fetchall() | ||
output = [] | ||
for result in results: | ||
dict_result = dict(zip([i[0] for i in cursor.description], result)) | ||
output.append(dict_result) | ||
return make_response(output, 200) | ||
|
||
|
||
def update_search_cache( | ||
cursor: psycopg2.extensions.cursor, agency_uid: str, search_results: list[str] | ||
) -> Response: | ||
for result in search_results: | ||
cursor.execute(SQL_UPDATE_CACHE, (agency_uid, result)) | ||
return make_response("Search Cache Updated", 200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from flask import request | ||
|
||
from middleware.homepage_search_cache import ( | ||
get_agencies_without_homepage_urls, | ||
update_search_cache, | ||
) | ||
from middleware.security import api_required | ||
from resources.PsycopgResource import PsycopgResource | ||
|
||
|
||
class HomepageSearchCache(PsycopgResource): | ||
|
||
@api_required | ||
def get(self): | ||
""" | ||
Retrieve 100 agencies without homepage urls | ||
:return: | ||
""" | ||
with self.psycopg2_connection.cursor() as cursor: | ||
return get_agencies_without_homepage_urls(cursor) | ||
|
||
@api_required | ||
def post(self): | ||
""" | ||
Update search cache | ||
:return: | ||
""" | ||
with self.psycopg2_connection.cursor() as cursor: | ||
return update_search_cache( | ||
cursor=cursor, | ||
agency_uid=request.json["agency_uid"], | ||
search_results=request.json["search_results"], | ||
) |