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

Add errorhandling for missing cache in ranked statistics #2563

Merged
merged 6 commits into from
Mar 22, 2023
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
31 changes: 27 additions & 4 deletions python/nav/web/sortedstats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,27 @@
from django.shortcuts import render
from django.core.cache import caches
from django.conf import settings
from django.core.cache.backends.base import InvalidCacheBackendError

from .forms import ViewForm
from . import CLASSMAP, TIMEFRAMES
from nav.metrics.errors import GraphiteUnreachableError

GRAPHITE_TIME_FORMAT = "%H:%M_%Y%m%d"
_logger = logging.getLogger(__name__)
cache = caches['sortedstats']


def get_cache():
return caches['sortedstats']


def cache_is_misconfigured():
try:
get_cache()
except InvalidCacheBackendError:
return True
else:
return False


def index(request):
Expand Down Expand Up @@ -64,6 +77,7 @@ def index(request):
'graphite_unreachable': graphite_unreachable,
'from_cache': from_cache,
'duration': duration,
'cache_misconfigured': cache_is_misconfigured(),
}

return render(request, 'sortedstats/sortedstats.html', context)
Expand All @@ -78,8 +92,13 @@ def process_form(form):
rows = form.cleaned_data['rows']
cache_key = get_cache_key(view, timeframe, rows)
if form.cleaned_data['use_cache']:
result = cache.get(cache_key)
if result and not result.data:
try:
cache = get_cache()
result = cache.get(cache_key)
if result and not result.data:
result = None
except InvalidCacheBackendError as e:
_logger.error("Error accessing cache for ranked statistics: %s", e)
result = None
if not result:
result = collect_result(view, timeframe, rows)
Expand All @@ -106,7 +125,11 @@ def collect_result(view, timeframe, rows):
cache_key = get_cache_key(view, timeframe, rows)
result = get_result(view, start, end, rows)
result.collect()
cache.set(cache_key, result, timeout=timeout)
try:
cache = get_cache()
cache.set(cache_key, result, timeout=timeout)
except InvalidCacheBackendError as e:
_logger.error("Error accessing cache for ranked statistics: %s", e)
return result


Expand Down
5 changes: 5 additions & 0 deletions python/nav/web/templates/sortedstats/sortedstats.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
{% include 'nav_header.html' %}
{% endwith %}

{% if cache_misconfigured %}
<div class="alert-box error">
The cache for Ranked Statistics is not configured correctly.
</div>
{% endif %}

{% crispy form %}

Expand Down
12 changes: 6 additions & 6 deletions tests/unittests/web/sortedstats/sortedstats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def test_cache_key_is_correct(self):
cache_key = views.get_cache_key(view, timeframe, rows)
self.assertEqual(cache_key, expected_cache_key)

@patch('nav.web.sortedstats.views.cache')
@patch('nav.web.sortedstats.views.get_cache')
def test_process_form_returns_cache_value_if_cache_exists(self, cache_mock):
data = "cached"
cache_mock.get.return_value.data = data
cache_mock.return_value.get.return_value.data = data
fake_form = MagicMock()
fake_form.cleaned_data = {
'view': 'uptime',
Expand All @@ -42,12 +42,12 @@ def test_process_form_returns_cache_value_if_cache_exists(self, cache_mock):
self.assertEqual(result.data, data)

@patch('nav.web.sortedstats.views.collect_result')
@patch('nav.web.sortedstats.views.cache')
@patch('nav.web.sortedstats.views.get_cache')
def test_cache_not_used_if_empty_and_use_cache_is_on(
self, cache_mock, collect_mock
):
data = "new"
cache_mock.get.return_value.data = ""
cache_mock.return_value.get.return_value.data = ""
collect_mock.return_value.data = data
fake_form = MagicMock()
fake_form.cleaned_data = {
Expand All @@ -61,12 +61,12 @@ def test_cache_not_used_if_empty_and_use_cache_is_on(
self.assertEqual(result.data, data)

@patch('nav.web.sortedstats.views.collect_result')
@patch('nav.web.sortedstats.views.cache')
@patch('nav.web.sortedstats.views.get_cache')
def test_cache_not_used_if_empty_and_use_cache_is_off(
self, cache_mock, collect_mock
):
data = "new"
cache_mock.get.return_value.data = ""
cache_mock.return_value.get.return_value.data = ""
collect_mock.return_value.data = data
fake_form = MagicMock()
fake_form.cleaned_data = {
Expand Down