Improve performance and reduce SQL queries
Signed-off-by: Alfredos-Panagiotis Damkalis <fredy@fredy.gr>spacecruft
parent
771aa22ecd
commit
41cb47f9ff
|
@ -238,13 +238,14 @@ class Satellite(models.Model):
|
|||
:returns: dict with most recent DemodData for this Satellite
|
||||
"""
|
||||
data = DemodData.objects.filter(satellite=self.id).order_by('-id')[:1]
|
||||
latest_datum = data[0]
|
||||
return {
|
||||
'data_id': data[0].data_id,
|
||||
'payload_frame': data[0].payload_frame,
|
||||
'timestamp': data[0].timestamp,
|
||||
'is_decoded': data[0].is_decoded,
|
||||
'station': data[0].station,
|
||||
'observer': data[0].observer,
|
||||
'data_id': latest_datum.data_id,
|
||||
'payload_frame': latest_datum.payload_frame,
|
||||
'timestamp': latest_datum.timestamp,
|
||||
'is_decoded': latest_datum.is_decoded,
|
||||
'station': latest_datum.station,
|
||||
'observer': latest_datum.observer,
|
||||
}
|
||||
|
||||
@property
|
||||
|
|
|
@ -11,6 +11,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin, \
|
|||
from django.contrib.auth.models import User
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.core.paginator import Paginator
|
||||
from django.db import OperationalError
|
||||
from django.db.models import Count, Max, Prefetch, Q
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
|
@ -42,26 +43,41 @@ def home(request):
|
|||
|
||||
:returns: base/home.html
|
||||
"""
|
||||
newest_sats = Satellite.objects.all().order_by('-id')[:5].prefetch_related(
|
||||
Prefetch(
|
||||
'transmitter_entries',
|
||||
queryset=Transmitter.objects.all(),
|
||||
to_attr='approved_transmitters'
|
||||
),
|
||||
Prefetch(
|
||||
'transmitter_entries',
|
||||
queryset=TransmitterSuggestion.objects.all(),
|
||||
to_attr='suggested_transmitters'
|
||||
)
|
||||
prefetch_approved = Prefetch(
|
||||
'transmitter_entries', queryset=Transmitter.objects.all(), to_attr='approved_transmitters'
|
||||
)
|
||||
latest_data = Satellite.objects.annotate(latest=Max('telemetry_data__pk')
|
||||
).order_by('-latest')[:5].prefetch_related(
|
||||
Prefetch(
|
||||
'transmitter_entries',
|
||||
queryset=Transmitter.objects.all(),
|
||||
to_attr='approved_transmitters'
|
||||
)
|
||||
) # noqa: E126 flake8 and yapf disagree
|
||||
prefetch_suggested = Prefetch(
|
||||
'transmitter_entries',
|
||||
queryset=TransmitterSuggestion.objects.all(),
|
||||
to_attr='suggested_transmitters'
|
||||
)
|
||||
|
||||
newest_sats = Satellite.objects.all().order_by('-id')[:5].prefetch_related(
|
||||
prefetch_approved, prefetch_suggested
|
||||
)
|
||||
# Calculate latest contributors
|
||||
latest_data_satellites = []
|
||||
found = False
|
||||
date_from = timezone.now() - timedelta(days=1)
|
||||
data_list = DemodData.objects.filter(timestamp__gte=date_from).order_by('-pk')
|
||||
paginator = Paginator(data_list, 50)
|
||||
page = paginator.page(1)
|
||||
while not found:
|
||||
for data in page.object_list:
|
||||
if data.satellite.id in latest_data_satellites:
|
||||
continue
|
||||
latest_data_satellites.append(data.satellite.id)
|
||||
if len(latest_data_satellites) > 5:
|
||||
found = True
|
||||
break
|
||||
if page.has_next():
|
||||
page = paginator.page(page.next_page_number())
|
||||
else:
|
||||
break
|
||||
|
||||
latest_data = Satellite.objects.filter(
|
||||
pk__in=latest_data_satellites
|
||||
).prefetch_related(prefetch_approved, prefetch_suggested)
|
||||
|
||||
# Calculate latest contributors
|
||||
date_from = timezone.now() - timedelta(days=1)
|
||||
|
|
|
@ -41,45 +41,49 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{% if satellite.telemetry_data_count %}
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Data frames">
|
||||
<i class="fas fa-database"></i>
|
||||
{% with data_count=satellite.telemetry_data_count %}
|
||||
{% if data_count %}
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Data frames">
|
||||
<i class="fas fa-database"></i>
|
||||
</div>
|
||||
<div class="d-inline-flex">
|
||||
{{ data_count }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-inline-flex">
|
||||
{{ satellite.telemetry_data_count }}
|
||||
{% with latest_data=satellite.latest_data %}
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Latest data timestamp">
|
||||
<i class="fas fa-clock"></i>
|
||||
</div>
|
||||
<div class="d-inline-flex text-truncate">
|
||||
{{ latest_data.timestamp }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Latest data timestamp">
|
||||
<i class="fas fa-clock"></i>
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Latest data submitter">
|
||||
<i class="fas fa-user"></i>
|
||||
</div>
|
||||
<div class="d-inline-flex text-truncate">
|
||||
{{ latest_data.station }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-inline-flex text-truncate">
|
||||
{{ satellite.latest_data.timestamp }}
|
||||
{% endwith %}
|
||||
{% else %}
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Data frames">
|
||||
<i class="fas fa-database"></i>
|
||||
</div>
|
||||
<div class="d-inline-flex">
|
||||
No Data
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Latest data submitter">
|
||||
<i class="fas fa-user"></i>
|
||||
</div>
|
||||
<div class="d-inline-flex text-truncate">
|
||||
{{ satellite.latest_data.station }}
|
||||
</div>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="d-flex flex-row">
|
||||
<div class="d-flex satellite-card-body-row align-self-center justify-content-center" data-toggle="tooltip"
|
||||
title="Data frames">
|
||||
<i class="fas fa-database"></i>
|
||||
</div>
|
||||
<div class="d-inline-flex">
|
||||
No Data
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
</div>
|
||||
<div class="float-right d-flex flex-column align-content-center">
|
||||
<img class="rounded img-fluid" src="{{ satellite.get_image }}" style="max-height: 82.5px;" />
|
||||
|
|
Loading…
Reference in New Issue