Skip to content

Commit

Permalink
Only fetch latest metrics to reduce payload size sent in response
Browse files Browse the repository at this point in the history
  • Loading branch information
cmplx-xyttmt committed Oct 20, 2023
1 parent 85b6ead commit af42aba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions devices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from taggit.managers import TaggableManager
from taggit.models import GenericUUIDTaggedItemBase, TaggedItemBase

from datetime import datetime, timedelta


class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):
class Meta:
Expand Down Expand Up @@ -71,7 +73,6 @@ def get_metrics(self):
def get_metric_files(self):
return self.metricstextfile_set.order_by("-time_uploaded")[:20]


def get_absolute_url(self):
return reverse("device_detail", args=[str(self.id)])

Expand Down Expand Up @@ -129,6 +130,10 @@ class Category(models.TextChoices):
device = models.OneToOneField(Device, on_delete=models.CASCADE, null=True)
category = models.CharField(max_length=50, choices=Category.choices, default=Category.E)

@property
def device_name(self):
return self.device.device_id

@property
def location_description(self):
return location_category_information[self.category]['description']
Expand All @@ -147,11 +152,15 @@ def day_limit(self):

@property
def location_hourly_metrics(self):
return self.device.hourlyaggregate_set.order_by("-date")
return (self.device.hourlyaggregate_set
.filter(date__range=[datetime.today() - timedelta(days=2), datetime.today()])
.order_by("-date"))

@property
def location_daily_metrics(self):
return self.device.dailyaggregate_set.order_by("-date")
return (self.device.dailyaggregate_set
.filter(date__range=[datetime.today() - timedelta(weeks=4), datetime.today()])
.order_by("-date"))

@property
def location_recordings(self):
Expand Down
2 changes: 1 addition & 1 deletion devices/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LocationMetricsSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = [
'id', 'location_hourly_metrics', 'location_daily_metrics'
'id', 'device_name', 'location_hourly_metrics', 'location_daily_metrics'
]


Expand Down

0 comments on commit af42aba

Please sign in to comment.