optimize homepage queries
parent
ddd7746ef0
commit
a552feae3f
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.1.7 on 2023-09-06 16:06
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('doab_check', '0004_auto_20230427_1818'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='check',
|
||||
name='return_code',
|
||||
field=models.IntegerField(db_index=True),
|
||||
),
|
||||
]
|
|
@ -78,7 +78,7 @@ class Check(models.Model):
|
|||
''' The results of a link check '''
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
link = models.ForeignKey("Link", related_name='checks', on_delete=models.CASCADE)
|
||||
return_code = models.IntegerField()
|
||||
return_code = models.IntegerField(db_index=True)
|
||||
content_type = models.CharField(max_length=255, null=True)
|
||||
location = models.ForeignKey("Link", related_name='redirects_from', null=True,
|
||||
on_delete=models.SET_NULL)
|
||||
|
|
|
@ -14,15 +14,15 @@ class HomepageView(generic.TemplateView):
|
|||
template_name = 'index.html'
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
codes = Link.objects.filter(recent_check__isnull=False).order_by(
|
||||
'-recent_check__return_code').values(
|
||||
active_links = Link.objects.filter(recent_check__isnull=False).only(
|
||||
'recent_check').order_by('-recent_check__return_code').distinct()
|
||||
codes = active_links.values(
|
||||
'recent_check__return_code').distinct()
|
||||
num_checked = Link.objects.filter(
|
||||
recent_check__return_code__isnull=False).distinct().count()
|
||||
num_checked = active_links.count()
|
||||
for code in codes:
|
||||
code['count'] = Link.objects.filter(
|
||||
code['count'] = active_links.filter(
|
||||
recent_check__return_code=code['recent_check__return_code'],
|
||||
).distinct().count()
|
||||
).count()
|
||||
code['percent'] = '{:.2%}'.format(code['count'] / num_checked)
|
||||
return {'num_checked': num_checked, 'codes': codes}
|
||||
|
||||
|
|
Loading…
Reference in New Issue