0001-cache-health-checks-results-26836.patch
hobo/rest/models.py | ||
---|---|---|
1 |
from django.core.cache import cache |
|
2 | ||
3 | ||
4 |
class ServiceBaseStatus(object): |
|
5 | ||
6 |
def __init__(self, service): |
|
7 |
self.service = service |
|
8 |
self.base_url = service.base_url |
|
9 |
self.is_resolvable = self.cache(service.is_resolvable, 120) |
|
10 |
self.has_certificate = self.cache(service.has_valid_certificate, 3600) |
|
11 |
self.is_running = self.cache(service.is_running, 60) |
|
12 |
self.is_operational = self.cache(service.is_operational, 60) |
|
13 |
self.slug = service.slug |
|
14 | ||
15 |
def cache(self, fn, sec): |
|
16 |
idx = '%s_%s' % (self.service.__class__.__name__, fn.__name__) |
|
17 |
val = cache.get(idx) |
|
18 |
if not val: |
|
19 |
val = fn() |
|
20 |
cache.set(idx, val, sec) |
|
21 |
return(val) |
hobo/rest/serializers.py | ||
---|---|---|
17 | 17 |
from rest_framework import serializers |
18 | 18 | |
19 | 19 | |
20 |
class HealthBaseSerializer(serializers.Serializer): |
|
21 |
title = serializers.CharField() |
|
20 |
class StatusSerializer(serializers.Serializer): |
|
22 | 21 |
slug = serializers.SlugField() |
23 |
base_url = serializers.CharField() |
|
22 |
has_certificate = serializers.BooleanField() |
|
23 |
is_operational = serializers.BooleanField() |
|
24 | 24 |
is_resolvable = serializers.BooleanField() |
25 |
has_valid_certificate = serializers.BooleanField() |
|
26 | 25 |
is_running = serializers.BooleanField() |
27 |
is_operational = serializers.BooleanField() |
hobo/rest/views.py | ||
---|---|---|
14 | 14 |
# You should have received a copy of the GNU General Public License |
15 | 15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 |
from rest_framework import generics |
|
18 |
from rest_framework import permissions |
|
19 |
from rest_framework.response import Response |
|
17 |
from django.http import JsonResponse |
|
18 |
from django.views.generic import View |
|
20 | 19 | |
21 | 20 |
from hobo.environment.utils import get_installed_services |
22 |
from hobo.rest.serializers import HealthBaseSerializer |
|
21 |
from hobo.rest.serializers import StatusSerializer |
|
22 |
from hobo.rest.models import ServiceBaseStatus |
|
23 | 23 | |
24 | 24 | |
25 |
class HealthList(generics.ListAPIView): |
|
26 |
serializer_class = HealthBaseSerializer |
|
27 |
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) |
|
28 | ||
29 |
def get_queryset(self): |
|
30 |
return [x for x in get_installed_services() if not x.secondary] |
|
31 | ||
32 |
def list(self, request, *args, **kwargs): |
|
33 |
""" |
|
34 |
Custom dictionnary object |
|
35 |
""" |
|
36 |
self.object_list = self.filter_queryset(self.get_queryset()) |
|
37 | ||
38 |
# Switch between paginated or standard style responses |
|
39 |
page = self.paginate_queryset(self.object_list) |
|
40 |
if page is not None: |
|
41 |
serializer = self.get_pagination_serializer(page) |
|
42 |
else: |
|
43 |
serializer = self.get_serializer(self.object_list, many=True) |
|
25 |
class HealthList(View): |
|
44 | 26 | |
27 |
def get(self, request, *args, **kwargs): |
|
28 |
qs = [ServiceBaseStatus(x) for x in get_installed_services() if not x.secondary] |
|
29 |
serializer = StatusSerializer(qs, many=True) |
|
45 | 30 |
response = {d['slug']: d for d in serializer.data} |
46 |
return Response(response) |
|
31 |
return JsonResponse(response) |
|
47 |
- |