Hashtag stats are only computed when a Post is edited or updated (#299)

This commit is contained in:
Michael Manfre 2022-12-27 21:42:38 -05:00 committed by GitHub
parent 26f64bfc06
commit cf8c5476e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 12 deletions

View File

@ -44,13 +44,18 @@ class HashtagAdmin(admin.ModelAdmin):
readonly_fields = ["created", "updated", "stats_updated"]
actions = ["force_execution"]
actions = ["force_state_outdated", "force_state_updated"]
@admin.action(description="Force Execution")
def force_execution(self, request, queryset):
@admin.action(description="Force State: outdated")
def force_state_outdated(self, request, queryset):
for instance in queryset:
instance.transition_perform("outdated")
@admin.action(description="Force State: updated")
def force_state_updated(self, request, queryset):
for instance in queryset:
instance.transition_perform("updated")
@admin.register(Emoji)
class EmojiAdmin(admin.ModelAdmin):

View File

@ -13,7 +13,7 @@ from stator.models import State, StateField, StateGraph, StatorModel
class HashtagStates(StateGraph):
outdated = State(try_interval=300, force_initial=True)
updated = State(try_interval=3600, attempt_immediately=False)
updated = State(externally_progressed=True)
outdated.transitions_to(updated)
updated.transitions_to(outdated)
@ -23,13 +23,16 @@ class HashtagStates(StateGraph):
"""
Computes the stats and other things for a Hashtag
"""
from time import time
from .post import Post
start = time()
posts_query = Post.objects.local_public().tagged_with(instance)
total = await posts_query.acount()
today = timezone.now().date()
# TODO: single query
total_today = await posts_query.filter(
created__gte=today,
created__lte=today + timedelta(days=1),
@ -55,13 +58,9 @@ class HashtagStates(StateGraph):
instance.stats_updated = timezone.now()
await sync_to_async(instance.save)()
print(f"Updated hashtag {instance.hashtag} in {time() - start:.5f} seconds")
return cls.updated
@classmethod
async def handle_updated(cls, instance: "Hashtag"):
if instance.state_age > Config.system.hashtag_stats_max_age:
return cls.outdated
class HashtagQuerySet(models.QuerySet):
def public(self):

View File

@ -13,7 +13,7 @@ from django.utils import text, timezone
from activities.models.emoji import Emoji
from activities.models.fan_out import FanOut
from activities.models.hashtag import Hashtag
from activities.models.hashtag import Hashtag, HashtagStates
from activities.models.post_types import (
PostTypeData,
PostTypeDataDecoder,
@ -503,9 +503,10 @@ class Post(StatorModel):
# Ensure hashtags
if self.hashtags:
for hashtag in self.hashtags:
await Hashtag.objects.aget_or_create(
tag, _ = await Hashtag.objects.aget_or_create(
hashtag=hashtag,
)
await tag.atransition_perform(HashtagStates.outdated)
### ActivityPub (outbound) ###