speed up stream lookup

This commit is contained in:
j 2023-08-18 14:59:45 +02:00
parent 61dd667a71
commit d7f087125e
3 changed files with 24 additions and 5 deletions

View file

@ -0,0 +1,17 @@
# Generated by Django 4.2.3 on 2023-08-18 12:54
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('archive', '0006_alter_file_extension_alter_file_id_alter_file_info_and_more'),
]
operations = [
migrations.AddIndex(
model_name='stream',
index=models.Index(fields=['file', 'source', 'available'], name='archive_str_file_id_69a542_idx'),
),
]

View file

@ -734,6 +734,9 @@ class Stream(models.Model):
class Meta:
unique_together = ("file", "resolution", "format")
indexes = [
models.Index(fields=['file', 'source', 'available'])
]
file = models.ForeignKey(File, related_name='streams', on_delete=models.CASCADE)
resolution = models.IntegerField(default=96)

View file

@ -644,7 +644,7 @@ class Item(models.Model):
i['hasSource'] = self.streams().exclude(file__data='').exists()
streams = self.streams()
i['durations'] = [s.duration for s in streams]
i['durations'] = [s[0] for s in streams.values_list('duration')]
i['duration'] = sum(i['durations'])
i['audioTracks'] = self.audio_tracks()
if not i['audioTracks']:
@ -1309,10 +1309,9 @@ class Item(models.Model):
return sorted(set(tracks))
def streams(self, track=None):
files = self.files.filter(selected=True).filter(Q(is_audio=True) | Q(is_video=True)),
qs = archive.models.Stream.objects.filter(
source=None, available=True, file__item=self, file__selected=True
).filter(
Q(file__is_audio=True) | Q(file__is_video=True)
file__in=files, source=None, available=True
).select_related()
if not track:
tracks = self.audio_tracks()
@ -1561,7 +1560,7 @@ class Item(models.Model):
if not subtitles:
return
# otherwise add empty 5 seconds annotation every minute
duration = sum([s.duration for s in self.streams()])
duration = sum([s[0] for s in self.streams().values_list('duration')])
layer = subtitles['id']
# FIXME: allow annotations from no user instead?
user = User.objects.all().order_by('id')[0]