Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacommonsorg/website in…
Browse files Browse the repository at this point in the history
…to ct
  • Loading branch information
shifucun committed May 16, 2024
2 parents 568f48d + 08a20fd commit c7d4404
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
9 changes: 2 additions & 7 deletions deploy/helm_charts/envs/unsdg_staging.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,8 @@ nl:
enabled: true
embeddingsSpec:
defaultIndex: "sdg_ft"
enabledIndexes: [
"bio_ft"
"medium_ft",
"sdg_ft",
"undata_ft",
"undata_ilo_ft",
]
enabledIndexes:
["bio_ft", "medium_ft", "sdg_ft", "undata_ft", "undata_ilo_ft"]

serviceGroups:
recon: null
Expand Down
13 changes: 13 additions & 0 deletions server/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import urllib

from flask import make_response
from flask import request
from google.protobuf import text_format

from server.config import subject_page_pb2
Expand Down Expand Up @@ -646,3 +647,15 @@ def get_vertex_ai_models():
vertex_ai_indexes = model_loader.load_indexes()
reranking_models = model_loader.load_models('RERANKING')
return dict(vertex_ai_indexes, **reranking_models)


def post_body_cache_key():
"""
Builds flask cache key for POST requests using the request path and
JSON-encoded post body
"""
body_object = request.get_json()
full_path = request.full_path
post_body = json.dumps(body_object, sort_keys=True)
cache_key = f'{full_path},{post_body}'
return cache_key
5 changes: 0 additions & 5 deletions server/routes/explore/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from flask import current_app
from flask import request

from server.lib.cache import cache
from server.lib.nl.common import serialize
import server.lib.nl.common.constants as constants
import server.lib.nl.common.counters as ctr
Expand All @@ -36,7 +35,6 @@
from server.lib.nl.explore.params import DCNames
from server.lib.nl.explore.params import Params
from server.lib.util import get_nl_disaster_config
from server.routes import TIMEOUT
from server.routes.explore import helpers
import server.services.bigtable as bt

Expand All @@ -47,7 +45,6 @@
# The detection endpoint.
#
@bp.route('/detect', methods=['POST'])
@cache.cached(timeout=TIMEOUT, query_string=True)
def detect():
debug_logs = {}
client = request.args.get(Params.CLIENT.value, Clients.DEFAULT.value)
Expand Down Expand Up @@ -84,7 +81,6 @@ def detect():
# - childEntityType: A type of child entity (optional)
#
@bp.route('/fulfill', methods=['POST'])
@cache.cached(timeout=TIMEOUT, query_string=True)
def fulfill():
"""Data handler."""
debug_logs = {}
Expand All @@ -96,7 +92,6 @@ def fulfill():
# The detect and fulfill endpoint.
#
@bp.route('/detect-and-fulfill', methods=['POST'])
@cache.cached(timeout=TIMEOUT, query_string=True)
def detect_and_fulfill():
debug_logs = {}

Expand Down
2 changes: 1 addition & 1 deletion server/routes/explore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,4 @@ def abort(error_message: str,
def _set_blocked(err_json: Dict):
err_json['blocked'] = True
if err_json.get('debug'):
err_json['debug']['blocked'] = True
err_json['debug']['blocked'] = True
3 changes: 3 additions & 0 deletions server/routes/shared_api/choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ def geojson():


@bp.route('/node-geojson', methods=['POST'])
@cache.cached(timeout=TIMEOUT,
query_string=True,
make_cache_key=lib_util.post_body_cache_key)
def node_geojson():
"""Gets geoJson data for a list of nodes and a specified property to use to
get the geoJson data"""
Expand Down
30 changes: 30 additions & 0 deletions server/tests/lib/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# limitations under the License.

import datetime
import json
import unittest

import server.lib.util as lib_util
from web_app import app


class TestParseDate(unittest.TestCase):
Expand All @@ -29,3 +31,31 @@ def test(self):
for input in data:
assert lib_util.parse_date(input).replace(
tzinfo=datetime.timezone.utc).timestamp() == data[input]


class TestPostBodyCacheKey(unittest.TestCase):

def test_post_body_cache_key(self):
test_data = {'key1': 'value1', 'key3': 'value3', 'key2': 'value2'}

with app.test_request_context('/test', method='POST', json=test_data):
# Get the expected cache key
expected_cache_key = f"/test?," + json.dumps(test_data, sort_keys=True)

# Ensure calculated cache value matches
cache_key = lib_util.post_body_cache_key()
self.assertEqual(cache_key, expected_cache_key)

def test_post_body_with_query_params_cache_key(self):
test_data = {'key1': 'value1', 'key3': 'value3', 'key2': 'value2'}

with app.test_request_context('/test?a=b&c=d',
method='POST',
json=test_data):
# Get the expected cache key
expected_cache_key = f"/test?a=b&c=d," + json.dumps(test_data,
sort_keys=True)

# Ensure calculated cache value matches
cache_key = lib_util.post_body_cache_key()
self.assertEqual(cache_key, expected_cache_key)

0 comments on commit c7d4404

Please sign in to comment.