forked from jointakahe/takahe
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always include Identity counts, calculate/store stats
- Loading branch information
Showing
9 changed files
with
158 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.db.models import ( | ||
Count, | ||
DateTimeField, | ||
IntegerField, | ||
Max, | ||
OuterRef, | ||
Subquery, | ||
) | ||
|
||
from activities.models import Post | ||
from core.ld import format_ld_date | ||
from users.models import Follow, Identity | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Recalculates Identity stats" | ||
|
||
def handle(self, *args, **options): | ||
posts = ( | ||
Post.objects.filter(author_id=OuterRef("id")) | ||
.values("author_id") | ||
.annotate(num=Count("id")) | ||
.values("num")[:1] | ||
) | ||
latest = ( | ||
Post.objects.filter(author_id=OuterRef("id")) | ||
.values("author_id") | ||
.annotate(latest=Max("created")) | ||
.values("latest")[:1] | ||
) | ||
followers = ( | ||
Follow.objects.filter(target_id=OuterRef("id")) | ||
.values("target_id") | ||
.annotate(num=Count("id")) | ||
.values("num")[:1] | ||
) | ||
following = ( | ||
Follow.objects.filter(source_id=OuterRef("id")) | ||
.values("source_id") | ||
.annotate(num=Count("id")) | ||
.values("num")[:1] | ||
) | ||
|
||
qs = Identity.objects.annotate( | ||
statuses_count=Subquery(posts, output_field=IntegerField()), | ||
last_status_at=Subquery(latest, output_field=DateTimeField()), | ||
followers_count=Subquery(followers, output_field=IntegerField()), | ||
following_count=Subquery(following, output_field=IntegerField()), | ||
) | ||
|
||
for i in qs: | ||
latest = format_ld_date(i.last_status_at) if i.last_status_at else None | ||
i.stats = { | ||
"statuses_count": i.statuses_count or 0, | ||
"last_status_at": latest, | ||
"followers_count": i.followers_count or 0, | ||
"following_count": i.following_count or 0, | ||
} | ||
i.save(update_fields=["stats"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.11 on 2024-04-30 13:19 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("users", "0026_identity_featured_tags_uri_hashtagfeature"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="identity", | ||
name="stats", | ||
field=models.JSONField(blank=True, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters