1
0
Fork 0

Merge branch 'fix-left-join-queries' into 'master'

Change annotated queries for better performance

See merge request librespacefoundation/satnogs/satnogs-network!790
merge-requests/790/merge
Alfredos-Panagiotis Damkalis 2019-11-23 20:24:38 +00:00
commit 3a68237177
3 changed files with 18 additions and 6 deletions

View File

@ -40,6 +40,14 @@ def percentagerest(value):
return 0
@register.filter
def get_count_from_id(dictionary, key):
"""Returns observations count from dictionary"""
if key in dictionary.keys():
return dictionary[key]
return 0
@register.filter
def sortdemoddata(demoddata):
"""Returns a date sorted list of DemodData"""

View File

@ -189,8 +189,8 @@ class ObservationListView(ListView):
"""
context = super(ObservationListView, self).get_context_data(**kwargs)
context['satellites'] = Satellite.objects.all()
context['authors'] = User.objects.annotate(obs_count=Count('observations')) \
.filter(obs_count__gt=0) \
observers_ids = list(set(Observation.objects.values_list('author_id', flat=True)))
context['authors'] = User.objects.filter(id__in=observers_ids) \
.order_by('first_name', 'last_name', 'username')
context['stations'] = Station.objects.all().order_by('id')
norad_cat_id = self.request.GET.get('norad', None)
@ -546,7 +546,11 @@ def observation_vet(request, observation_id):
def stations_list(request):
"""View to render Stations page."""
stations = Station.objects.annotate(total_obs=Count('observations'))
stations = Station.objects.all()
stations_total_obs = {
x['id']: x['total_obs']
for x in Station.objects.values('id').annotate(total_obs=Count('observations'))
}
form = StationForm()
antennas = Antenna.objects.all()
online = stations.filter(status=2).count()
@ -555,6 +559,7 @@ def stations_list(request):
return render(
request, 'base/stations.html', {
'stations': stations,
'total_obs': stations_total_obs,
'form': form,
'antennas': antennas,
'online': online,

View File

@ -29,7 +29,6 @@
</button>
</div>
</h2>
<div class="row">
<div class="col-md-12">
<table class="table table-hover">
@ -43,7 +42,7 @@
<th>Owner</th>
</thead>
<tbody>
{% for station in stations.all %}
{% for station in stations %}
<tr class="station-row clickable-row" data-href="{% url 'base:station_view' station_id=station.id %}">
<td>
<a href="{% url 'base:station_view' station_id=station.id %}">
@ -85,7 +84,7 @@
{% endif %}
<td>
<a href="{% url 'base:observations_list' %}?station={{ station.id }}" class="badge total-pill" data-toggle="tooltip" data-placement="bottom" title={% if not station.success_rate %} "No data" {%else %} "{{station.success_rate}}% Success" {% endif %}>
{{ station.total_obs }}
{{ total_obs|get_count_from_id:station.id }}
</a>
</td>
<td>