Verified Commit 9d55320b authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Fixed slow query on domain list / stats

parent 3f5d363f
......@@ -66,15 +66,6 @@ class DomainQuerySet(models.QuerySet):
def external(self):
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):
return self.annotate(actors_count=models.Count("actors", distinct=True))
......@@ -111,7 +102,6 @@ class Domain(models.Model):
actors=models.Count("actors", distinct=True),
outbox_activities=models.Count("actors__outbox_activities", distinct=True),
libraries=models.Count("actors__libraries", distinct=True),
uploads=models.Count("actors__libraries__uploads", distinct=True),
received_library_follows=models.Count(
"actors__libraries__received_follows", distinct=True
),
......@@ -130,6 +120,7 @@ class Domain(models.Model):
).count()
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_downloaded_size"] = (
uploads.with_file().aggregate(v=models.Sum("size"))["v"] or 0
......
......@@ -173,7 +173,6 @@ class ManageInvitationActionSerializer(common_serializers.ActionSerializer):
class ManageDomainSerializer(serializers.ModelSerializer):
actors_count = serializers.SerializerMethodField()
last_activity_date = serializers.SerializerMethodField()
outbox_activities_count = serializers.SerializerMethodField()
class Meta:
......@@ -182,7 +181,6 @@ class ManageDomainSerializer(serializers.ModelSerializer):
"name",
"creation_date",
"actors_count",
"last_activity_date",
"outbox_activities_count",
"nodeinfo",
"nodeinfo_fetch_date",
......@@ -191,8 +189,5 @@ class ManageDomainSerializer(serializers.ModelSerializer):
def get_actors_count(self, o):
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):
return getattr(o, "outbox_activities_count", 0)
......@@ -102,7 +102,6 @@ class ManageDomainViewSet(
lookup_value_regex = r"[a-zA-Z0-9\-\.]+"
queryset = (
federation_models.Domain.objects.external()
.with_last_activity_date()
.with_actors_count()
.with_outbox_activities_count()
.order_by("name")
......@@ -114,7 +113,7 @@ class ManageDomainViewSet(
ordering_fields = [
"name",
"creation_date",
"last_activity_date",
"nodeinfo_fetch_date",
"actors_count",
"outbox_activities_count",
]
......
......@@ -40,11 +40,9 @@ def test_manage_domain_serializer(factories, now):
domain = factories["federation.Domain"]()
setattr(domain, "actors_count", 42)
setattr(domain, "outbox_activities_count", 23)
setattr(domain, "last_activity_date", now)
expected = {
"name": domain.name,
"creation_date": domain.creation_date.isoformat().split("+")[0] + "Z",
"last_activity_date": now,
"actors_count": 42,
"outbox_activities_count": 23,
"nodeinfo": {},
......
......@@ -38,7 +38,6 @@
<th><translate>Users</translate></th>
<th><translate>Received messages</translate></th>
<th><translate>First seen</translate></th>
<th><translate>Last activity</translate></th>
</template>
<template slot="row-cells" slot-scope="scope">
<td>
......@@ -53,10 +52,6 @@
<td>
<human-date :date="scope.obj.creation_date"></human-date>
</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>
</action-table>
</div>
......@@ -112,7 +107,6 @@ export default {
orderingOptions: [
['name', 'name'],
['creation_date', 'first_seen'],
['last_activity_date', 'last_activity'],
['actors_count', 'users'],
['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