Commit a4fcf9e8 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Merge branch 'domains-table-query-perf' into 'develop'

Fixed slow query on domain list / stats

See merge request funkwhale/funkwhale!506
parents 3f5d363f 9d55320b
...@@ -66,15 +66,6 @@ class DomainQuerySet(models.QuerySet): ...@@ -66,15 +66,6 @@ class DomainQuerySet(models.QuerySet):
def external(self): def external(self):
return self.exclude(pk=settings.FEDERATION_HOSTNAME) return self.exclude(pk=settings.FEDERATION_HOSTNAME)
def with_last_activity_date(self):
activities = Activity.objects.filter(
actor__domain=models.OuterRef("pk")
).order_by("-creation_date")
return self.annotate(
last_activity_date=models.Subquery(activities.values("creation_date")[:1])
)
def with_actors_count(self): def with_actors_count(self):
return self.annotate(actors_count=models.Count("actors", distinct=True)) return self.annotate(actors_count=models.Count("actors", distinct=True))
...@@ -111,7 +102,6 @@ class Domain(models.Model): ...@@ -111,7 +102,6 @@ class Domain(models.Model):
actors=models.Count("actors", distinct=True), actors=models.Count("actors", distinct=True),
outbox_activities=models.Count("actors__outbox_activities", distinct=True), outbox_activities=models.Count("actors__outbox_activities", distinct=True),
libraries=models.Count("actors__libraries", distinct=True), libraries=models.Count("actors__libraries", distinct=True),
uploads=models.Count("actors__libraries__uploads", distinct=True),
received_library_follows=models.Count( received_library_follows=models.Count(
"actors__libraries__received_follows", distinct=True "actors__libraries__received_follows", distinct=True
), ),
...@@ -130,6 +120,7 @@ class Domain(models.Model): ...@@ -130,6 +120,7 @@ class Domain(models.Model):
).count() ).count()
uploads = music_models.Upload.objects.filter(library__actor__domain_id=self.pk) uploads = music_models.Upload.objects.filter(library__actor__domain_id=self.pk)
data["uploads"] = uploads.count()
data["media_total_size"] = uploads.aggregate(v=models.Sum("size"))["v"] or 0 data["media_total_size"] = uploads.aggregate(v=models.Sum("size"))["v"] or 0
data["media_downloaded_size"] = ( data["media_downloaded_size"] = (
uploads.with_file().aggregate(v=models.Sum("size"))["v"] or 0 uploads.with_file().aggregate(v=models.Sum("size"))["v"] or 0
......
...@@ -173,7 +173,6 @@ class ManageInvitationActionSerializer(common_serializers.ActionSerializer): ...@@ -173,7 +173,6 @@ class ManageInvitationActionSerializer(common_serializers.ActionSerializer):
class ManageDomainSerializer(serializers.ModelSerializer): class ManageDomainSerializer(serializers.ModelSerializer):
actors_count = serializers.SerializerMethodField() actors_count = serializers.SerializerMethodField()
last_activity_date = serializers.SerializerMethodField()
outbox_activities_count = serializers.SerializerMethodField() outbox_activities_count = serializers.SerializerMethodField()
class Meta: class Meta:
...@@ -182,7 +181,6 @@ class ManageDomainSerializer(serializers.ModelSerializer): ...@@ -182,7 +181,6 @@ class ManageDomainSerializer(serializers.ModelSerializer):
"name", "name",
"creation_date", "creation_date",
"actors_count", "actors_count",
"last_activity_date",
"outbox_activities_count", "outbox_activities_count",
"nodeinfo", "nodeinfo",
"nodeinfo_fetch_date", "nodeinfo_fetch_date",
...@@ -191,8 +189,5 @@ class ManageDomainSerializer(serializers.ModelSerializer): ...@@ -191,8 +189,5 @@ class ManageDomainSerializer(serializers.ModelSerializer):
def get_actors_count(self, o): def get_actors_count(self, o):
return getattr(o, "actors_count", 0) return getattr(o, "actors_count", 0)
def get_last_activity_date(self, o):
return getattr(o, "last_activity_date", None)
def get_outbox_activities_count(self, o): def get_outbox_activities_count(self, o):
return getattr(o, "outbox_activities_count", 0) return getattr(o, "outbox_activities_count", 0)
...@@ -102,7 +102,6 @@ class ManageDomainViewSet( ...@@ -102,7 +102,6 @@ class ManageDomainViewSet(
lookup_value_regex = r"[a-zA-Z0-9\-\.]+" lookup_value_regex = r"[a-zA-Z0-9\-\.]+"
queryset = ( queryset = (
federation_models.Domain.objects.external() federation_models.Domain.objects.external()
.with_last_activity_date()
.with_actors_count() .with_actors_count()
.with_outbox_activities_count() .with_outbox_activities_count()
.order_by("name") .order_by("name")
...@@ -114,7 +113,7 @@ class ManageDomainViewSet( ...@@ -114,7 +113,7 @@ class ManageDomainViewSet(
ordering_fields = [ ordering_fields = [
"name", "name",
"creation_date", "creation_date",
"last_activity_date", "nodeinfo_fetch_date",
"actors_count", "actors_count",
"outbox_activities_count", "outbox_activities_count",
] ]
......
...@@ -40,11 +40,9 @@ def test_manage_domain_serializer(factories, now): ...@@ -40,11 +40,9 @@ def test_manage_domain_serializer(factories, now):
domain = factories["federation.Domain"]() domain = factories["federation.Domain"]()
setattr(domain, "actors_count", 42) setattr(domain, "actors_count", 42)
setattr(domain, "outbox_activities_count", 23) setattr(domain, "outbox_activities_count", 23)
setattr(domain, "last_activity_date", now)
expected = { expected = {
"name": domain.name, "name": domain.name,
"creation_date": domain.creation_date.isoformat().split("+")[0] + "Z", "creation_date": domain.creation_date.isoformat().split("+")[0] + "Z",
"last_activity_date": now,
"actors_count": 42, "actors_count": 42,
"outbox_activities_count": 23, "outbox_activities_count": 23,
"nodeinfo": {}, "nodeinfo": {},
......
...@@ -38,7 +38,6 @@ ...@@ -38,7 +38,6 @@
<th><translate>Users</translate></th> <th><translate>Users</translate></th>
<th><translate>Received messages</translate></th> <th><translate>Received messages</translate></th>
<th><translate>First seen</translate></th> <th><translate>First seen</translate></th>
<th><translate>Last activity</translate></th>
</template> </template>
<template slot="row-cells" slot-scope="scope"> <template slot="row-cells" slot-scope="scope">
<td> <td>
...@@ -53,10 +52,6 @@ ...@@ -53,10 +52,6 @@
<td> <td>
<human-date :date="scope.obj.creation_date"></human-date> <human-date :date="scope.obj.creation_date"></human-date>
</td> </td>
<td>
<human-date v-if="scope.obj.last_activity_date" :date="scope.obj.last_activity_date"></human-date>
<translate v-else>N/A</translate>
</td>
</template> </template>
</action-table> </action-table>
</div> </div>
...@@ -112,7 +107,6 @@ export default { ...@@ -112,7 +107,6 @@ export default {
orderingOptions: [ orderingOptions: [
['name', 'name'], ['name', 'name'],
['creation_date', 'first_seen'], ['creation_date', 'first_seen'],
['last_activity_date', 'last_activity'],
['actors_count', 'users'], ['actors_count', 'users'],
['outbox_activities_count', 'received_messages'] ['outbox_activities_count', 'received_messages']
] ]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment