Skip to content

Commit

Permalink
Merge pull request #147 from hydroserver2/33-cache-improvements
Browse files Browse the repository at this point in the history
Added django simple history models, and added modified_since param
  • Loading branch information
kjlippold authored Oct 27, 2023
2 parents dc5df9b + 84649c3 commit 60210cc
Show file tree
Hide file tree
Showing 11 changed files with 574 additions and 92 deletions.
22 changes: 21 additions & 1 deletion core/endpoints/datastream/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils import timezone
from uuid import UUID
from typing import List, Optional
from datetime import datetime
from functools import reduce
from core.models import Person, Datastream, ThingAssociation, Observation, ResultQualifier
from core.endpoints.thing.utils import check_thing_by_id
Expand Down Expand Up @@ -60,6 +61,20 @@ def apply_datastream_auth_rules(
return datastream_query, result_exists


def apply_recent_datastream_filter(
datastream_query: QuerySet,
modified_since: datetime
) -> QuerySet:

datastream_history_filter = Q(log__history_date__gt=modified_since)

datastream_query = datastream_query.filter(
datastream_history_filter
)

return datastream_query


def query_datastreams(
user: Optional[Person],
check_result_exists: bool = False,
Expand All @@ -71,7 +86,8 @@ def query_datastreams(
thing_ids: Optional[List[UUID]] = None,
sensor_ids: Optional[List[UUID]] = None,
data_source_ids: Optional[List[UUID]] = None,
observed_property_ids: Optional[List[UUID]] = None
observed_property_ids: Optional[List[UUID]] = None,
modified_since: Optional[datetime] = None
) -> (QuerySet, bool):

datastream_query = Datastream.objects
Expand All @@ -95,6 +111,10 @@ def query_datastreams(
'processing_level', 'unit', 'intended_time_spacing_units', 'time_aggregation_interval_units'
)

if modified_since:
datastream_query = datastream_query.prefetch_related('log')
datastream_query = apply_recent_datastream_filter(datastream_query, modified_since)

datastream_query, result_exists = apply_datastream_auth_rules(
user=user,
datastream_query=datastream_query,
Expand Down
6 changes: 4 additions & 2 deletions core/endpoints/datastream/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from ninja import Path
from uuid import UUID
from typing import Optional
from datetime import datetime
from django.db import transaction, IntegrityError
from django.http import StreamingHttpResponse
from accounts.auth.jwt import JWTAuth
Expand All @@ -16,14 +18,14 @@


@router.dm_list('', response=DatastreamGetResponse)
def get_datastreams(request):
def get_datastreams(request, modified_since: Optional[datetime] = None):
"""
Get a list of Datastreams
This endpoint returns a list of public Datastreams and Datastreams owned by the authenticated user if there is one.
"""

datastream_query, _ = query_datastreams(user=request.authenticated_user)
datastream_query, _ = query_datastreams(user=request.authenticated_user, modified_since=modified_since)

return [
build_datastream_response(datastream) for datastream in datastream_query.all()
Expand Down
20 changes: 19 additions & 1 deletion core/endpoints/thing/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import operator
from datetime import datetime
from ninja.errors import HttpError
from django.db.models import Q, Count, Prefetch
from django.db.models.query import QuerySet
Expand Down Expand Up @@ -54,6 +55,18 @@ def apply_thing_auth_rules(
return thing_query, result_exists


def apply_recent_thing_filter(
thing_query: QuerySet,
modified_since: datetime
) -> QuerySet:

thing_query = thing_query.filter(
log__history_date__gt=modified_since
)

return thing_query


def query_things(
user: Optional[Person],
check_result_exists: bool = False,
Expand All @@ -63,7 +76,8 @@ def query_things(
ignore_privacy: bool = False,
thing_ids: Optional[List[UUID]] = None,
prefetch_photos: bool = False,
prefetch_datastreams: bool = False
prefetch_datastreams: bool = False,
modified_since: Optional[datetime] = None
) -> (QuerySet, bool):

thing_query = Thing.objects
Expand All @@ -83,6 +97,10 @@ def query_things(
if prefetch_datastreams:
thing_query = thing_query.prefetch_related('datastreams')

if modified_since:
thing_query = thing_query.prefetch_related('log')
thing_query = apply_recent_thing_filter(thing_query, modified_since)

thing_query, result_exists = apply_thing_auth_rules(
user=user,
thing_query=thing_query,
Expand Down
7 changes: 4 additions & 3 deletions core/endpoints/thing/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ninja import Path
from typing import List
from typing import List, Optional
from uuid import UUID
from datetime import datetime
from django.db import transaction, IntegrityError
from accounts.auth.jwt import JWTAuth
from accounts.auth.basic import BasicAuth
Expand All @@ -24,14 +25,14 @@


@router.dm_list('', response=ThingGetResponse)
def get_things(request):
def get_things(request, modified_since: Optional[datetime] = None):
"""
Get a list of Things
This endpoint returns a list of public Things and Things owned by the authenticated user if there is one.
"""

thing_query, _ = query_things(user=request.authenticated_user)
thing_query, _ = query_things(user=request.authenticated_user, modified_since=modified_since)

return [
build_thing_response(request.authenticated_user, thing) for thing in thing_query.all()
Expand Down
Loading

0 comments on commit 60210cc

Please sign in to comment.