optimize homepage queries

main
eric 2023-09-06 12:49:18 -04:00
parent ddd7746ef0
commit a552feae3f
3 changed files with 25 additions and 7 deletions

View File

@ -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),
),
]

View File

@ -78,7 +78,7 @@ class Check(models.Model):
''' The results of a link check ''' ''' The results of a link check '''
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
link = models.ForeignKey("Link", related_name='checks', on_delete=models.CASCADE) 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) content_type = models.CharField(max_length=255, null=True)
location = models.ForeignKey("Link", related_name='redirects_from', null=True, location = models.ForeignKey("Link", related_name='redirects_from', null=True,
on_delete=models.SET_NULL) on_delete=models.SET_NULL)

View File

@ -14,15 +14,15 @@ class HomepageView(generic.TemplateView):
template_name = 'index.html' template_name = 'index.html'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
codes = Link.objects.filter(recent_check__isnull=False).order_by( active_links = Link.objects.filter(recent_check__isnull=False).only(
'-recent_check__return_code').values( 'recent_check').order_by('-recent_check__return_code').distinct()
codes = active_links.values(
'recent_check__return_code').distinct() 'recent_check__return_code').distinct()
num_checked = Link.objects.filter( num_checked = active_links.count()
recent_check__return_code__isnull=False).distinct().count()
for code in codes: for code in codes:
code['count'] = Link.objects.filter( code['count'] = active_links.filter(
recent_check__return_code=code['recent_check__return_code'], recent_check__return_code=code['recent_check__return_code'],
).distinct().count() ).count()
code['percent'] = '{:.2%}'.format(code['count'] / num_checked) code['percent'] = '{:.2%}'.format(code['count'] / num_checked)
return {'num_checked': num_checked, 'codes': codes} return {'num_checked': num_checked, 'codes': codes}