Skip to content

Commit

Permalink
Merge pull request #464 from bento-platform/chore/rm-page-caches
Browse files Browse the repository at this point in the history
chore: remove hard-coded page caching
  • Loading branch information
davidlougheed authored Dec 12, 2023
2 parents 2e9ef0d + 0949e0c commit 4d153a5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 37 deletions.
4 changes: 3 additions & 1 deletion chord_metadata_service/chord/views_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ async def data_type_metadata_schema(_request: HttpRequest, data_type: str):
@permission_classes([OverrideOrSuperUserOnly | ReadOnly])
async def dataset_data_type(request: HttpRequest, dataset_id: str, data_type: str):
if data_type not in QUERYSET_FN:
return Response(errors.bad_request_error, status=status.HTTP_400_BAD_REQUEST)
return Response(
errors.bad_request_error(f"Data type {data_type} doesn't exist"), status=status.HTTP_400_BAD_REQUEST)

qs = QUERYSET_FN[data_type](dataset_id)

if request.method == "DELETE":
Expand Down
15 changes: 5 additions & 10 deletions chord_metadata_service/chord/views_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
from django.db.models.functions import Coalesce
from django.contrib.postgres.aggregates import ArrayAgg
from django.conf import settings
from django.http import HttpRequest
from django.views.decorators.cache import cache_page
from psycopg2 import sql
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.request import Request as DrfRequest
from rest_framework.response import Response
from rest_framework import status

Expand Down Expand Up @@ -298,8 +297,6 @@ def search(request, internal_data=False):
}, start))


# Cache page for the requested url
@cache_page(60 * 60 * 2)
@api_view(["GET", "POST"])
@permission_classes([AllowAny])
def chord_search(request):
Expand All @@ -313,8 +310,6 @@ def chord_search(request):

# Mounted on /private/, so will get protected anyway; this allows for access from federation service
# TODO: Ugly and misleading permissions
# Cache page for the requested url
@cache_page(60 * 60 * 2)
@api_view(["GET", "POST"])
@permission_classes([AllowAny])
def chord_private_search(request):
Expand Down Expand Up @@ -549,7 +544,7 @@ def chord_dataset_representation(dataset: Dataset):
}


def dataset_search(request: HttpRequest, dataset_id: str, internal=False):
def dataset_search(request: DrfRequest, dataset_id: str, internal=False):
start = datetime.now()
search_params, err = get_chord_search_parameters(request=request)
if err:
Expand All @@ -564,19 +559,19 @@ def dataset_search(request: HttpRequest, dataset_id: str, internal=False):

@api_view(["GET", "POST"])
@permission_classes([OverrideOrSuperUserOnly | ReadOnly])
def public_dataset_search(request: HttpRequest, dataset_id: str):
def public_dataset_search(request: DrfRequest, dataset_id: str):
return dataset_search(request=request, dataset_id=dataset_id)


@api_view(["GET", "POST"])
@permission_classes([OverrideOrSuperUserOnly | ReadOnly])
def private_dataset_search(request: HttpRequest, dataset_id: str):
def private_dataset_search(request: DrfRequest, dataset_id: str):
return dataset_search(request=request, dataset_id=dataset_id, internal=True)


@api_view(["GET"])
@permission_classes([OverrideOrSuperUserOnly | ReadOnly])
def dataset_summary(request: HttpRequest, dataset_id: str):
def dataset_summary(request: DrfRequest, dataset_id: str):
dataset = Dataset.objects.get(identifier=dataset_id)
return Response({
DATA_TYPE_PHENOPACKET: phenopacket_dataset_summary(dataset=dataset),
Expand Down
7 changes: 2 additions & 5 deletions chord_metadata_service/experiments/api_views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets, mixins
from rest_framework.settings import api_settings
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django_filters.rest_framework import DjangoFilterBackend

from .serializers import ExperimentSerializer, ExperimentResultSerializer
from .models import Experiment, ExperimentResult
Expand Down Expand Up @@ -99,7 +97,7 @@ def get_queryset(self):
.order_by("id")
)

def create(self, request, *args, **kwargs):
def create(self, request, *_args, **_kwargs):
ids_list = request.data.get('id', [])
request.data["id"] = ids_list
queryset = self.get_queryset()
Expand All @@ -125,7 +123,6 @@ class ExperimentResultViewSet(viewsets.ModelViewSet):
filterset_class = ExperimentResultFilter

# Cache page for the requested url for 2 hours
@method_decorator(cache_page(60 * 60 * 2))
def dispatch(self, *args, **kwargs):
return super(ExperimentResultViewSet, self).dispatch(*args, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion chord_metadata_service/experiments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from chord_metadata_service.patients.models import Individual


__all__ = ["ExperimentSerializer"]
__all__ = ["ExperimentSerializer", "ExperimentResultSerializer", "InstrumentSerializer"]


class ExperimentResultSerializer(GenericSerializer):
Expand Down
8 changes: 0 additions & 8 deletions chord_metadata_service/patients/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from rest_framework.settings import api_settings
from rest_framework.views import APIView
from django.conf import settings
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django_filters.rest_framework import DjangoFilterBackend
from django.core.exceptions import ValidationError
from django.db.models import Count, F, Q
Expand Down Expand Up @@ -69,12 +67,6 @@ class IndividualViewSet(viewsets.ModelViewSet):
*(f"phenopackets__{p}" for p in PHENOPACKET_PREFETCH if p != "subject"),
).order_by("id")

# Cache page for the requested url, default to 2 hours.

@method_decorator(cache_page(settings.CACHE_TIME))
def dispatch(self, *args, **kwargs):
return super(IndividualViewSet, self).dispatch(*args, **kwargs)

def list(self, request, *args, **kwargs):
if request.query_params.get("format") == OUTPUT_FORMAT_BENTO_SEARCH_RESULT:
start = datetime.now()
Expand Down
13 changes: 1 addition & 12 deletions chord_metadata_service/phenopackets/api_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from django.conf import settings
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.settings import api_settings
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django_filters.rest_framework import DjangoFilterBackend

from chord_metadata_service.restapi.api_renderers import (PhenopacketsRenderer, FHIRRenderer,
BiosamplesCSVRenderer, ARGORenderer,
Expand All @@ -23,11 +20,6 @@ class PhenopacketsModelViewSet(viewsets.ModelViewSet):
renderer_classes = (*api_settings.DEFAULT_RENDERER_CLASSES, PhenopacketsRenderer)
pagination_class = LargeResultsSetPagination

# Cache response to the requested URL, default to 2 hours.
@method_decorator(cache_page(settings.CACHE_TIME))
def dispatch(self, *args, **kwargs):
return super(PhenopacketsModelViewSet, self).dispatch(*args, **kwargs)


class ExtendedPhenopacketsModelViewSet(PhenopacketsModelViewSet):
renderer_classes = (*PhenopacketsModelViewSet.renderer_classes, FHIRRenderer)
Expand Down Expand Up @@ -97,9 +89,6 @@ class BiosampleViewSet(ExtendedPhenopacketsModelViewSet):
post:
Create a new biosample
"""
queryset = m.Biosample.objects.all() \
.prefetch_related(*BIOSAMPLE_PREFETCH) \
.order_by("id")
serializer_class = s.BiosampleSerializer
filter_backends = [DjangoFilterBackend]
filterset_class = f.BiosampleFilter
Expand Down

0 comments on commit 4d153a5

Please sign in to comment.