mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-24 00:07:02 -04:00
Cache the JSON in Redis
The endpoints are probably fast enough, but it seems risky to have a public URL which causes a new query every time the page is loaded.
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
from app.notify_client import NotifyAdminAPIClient
|
from app.notify_client import NotifyAdminAPIClient, cache
|
||||||
|
|
||||||
|
|
||||||
class PerformanceDashboardAPIClient(NotifyAdminAPIClient):
|
class PerformanceDashboardAPIClient(NotifyAdminAPIClient):
|
||||||
|
|
||||||
|
@cache.set('performance-stats-{start_date}-to-{end_date}')
|
||||||
def get_performance_dashboard_stats(
|
def get_performance_dashboard_stats(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from datetime import date
|
from datetime import date
|
||||||
|
from unittest.mock import call
|
||||||
|
|
||||||
from app.notify_client.performance_dashboard_api_client import (
|
from app.notify_client.performance_dashboard_api_client import (
|
||||||
PerformanceDashboardAPIClient,
|
PerformanceDashboardAPIClient,
|
||||||
@@ -6,8 +7,9 @@ from app.notify_client.performance_dashboard_api_client import (
|
|||||||
|
|
||||||
|
|
||||||
def test_get_aggregate_platform_stats(mocker):
|
def test_get_aggregate_platform_stats(mocker):
|
||||||
|
mocker.patch('app.extensions.RedisClient.get', return_value=None)
|
||||||
client = PerformanceDashboardAPIClient()
|
client = PerformanceDashboardAPIClient()
|
||||||
mock = mocker.patch.object(client, 'get')
|
mock = mocker.patch.object(client, 'get', return_value={})
|
||||||
|
|
||||||
client.get_performance_dashboard_stats(
|
client.get_performance_dashboard_stats(
|
||||||
start_date=date(2021, 3, 1),
|
start_date=date(2021, 3, 1),
|
||||||
@@ -18,3 +20,66 @@ def test_get_aggregate_platform_stats(mocker):
|
|||||||
'start_date': '2021-03-01',
|
'start_date': '2021-03-01',
|
||||||
'end_date': '2021-03-31'
|
'end_date': '2021-03-31'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def test_sets_value_in_cache(mocker):
|
||||||
|
client = PerformanceDashboardAPIClient()
|
||||||
|
|
||||||
|
mock_redis_get = mocker.patch(
|
||||||
|
'app.extensions.RedisClient.get',
|
||||||
|
return_value=None,
|
||||||
|
)
|
||||||
|
mock_api_get = mocker.patch(
|
||||||
|
'app.notify_client.NotifyAdminAPIClient.get',
|
||||||
|
return_value={'data_from': 'api'},
|
||||||
|
)
|
||||||
|
mock_redis_set = mocker.patch(
|
||||||
|
'app.extensions.RedisClient.set',
|
||||||
|
)
|
||||||
|
|
||||||
|
assert client.get_performance_dashboard_stats(
|
||||||
|
start_date=date(2021, 1, 1),
|
||||||
|
end_date=date(2022, 2, 2),
|
||||||
|
) == {'data_from': 'api'}
|
||||||
|
|
||||||
|
assert mock_redis_get.call_args_list == [
|
||||||
|
call('performance-stats-2021-01-01-to-2022-02-02'),
|
||||||
|
]
|
||||||
|
assert mock_api_get.call_args_list == [
|
||||||
|
call('/performance-dashboard', params={
|
||||||
|
'start_date': '2021-01-01', 'end_date': '2022-02-02'
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
assert mock_redis_set.call_args_list == [
|
||||||
|
call(
|
||||||
|
'performance-stats-2021-01-01-to-2022-02-02',
|
||||||
|
'{"data_from": "api"}',
|
||||||
|
ex=604800,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_returns_value_from_cache(mocker):
|
||||||
|
client = PerformanceDashboardAPIClient()
|
||||||
|
|
||||||
|
mock_redis_get = mocker.patch(
|
||||||
|
'app.extensions.RedisClient.get',
|
||||||
|
return_value=b'{"data_from": "cache"}',
|
||||||
|
)
|
||||||
|
mock_api_get = mocker.patch(
|
||||||
|
'app.notify_client.NotifyAdminAPIClient.get',
|
||||||
|
)
|
||||||
|
mock_redis_set = mocker.patch(
|
||||||
|
'app.extensions.RedisClient.set',
|
||||||
|
)
|
||||||
|
|
||||||
|
assert client.get_performance_dashboard_stats(
|
||||||
|
start_date=date(2021, 1, 1),
|
||||||
|
end_date=date(2022, 2, 2),
|
||||||
|
) == {'data_from': 'cache'}
|
||||||
|
|
||||||
|
assert mock_redis_get.call_args_list == [
|
||||||
|
call('performance-stats-2021-01-01-to-2022-02-02'),
|
||||||
|
]
|
||||||
|
assert mock_api_get.called is False
|
||||||
|
assert mock_redis_set.called is False
|
||||||
|
|||||||
Reference in New Issue
Block a user