diff --git a/activities/admin.py b/activities/admin.py index 76505af..5417505 100644 --- a/activities/admin.py +++ b/activities/admin.py @@ -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): diff --git a/activities/models/hashtag.py b/activities/models/hashtag.py index afe2d94..3bf424f 100644 --- a/activities/models/hashtag.py +++ b/activities/models/hashtag.py @@ -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): diff --git a/activities/models/post.py b/activities/models/post.py index f7e3293..e9bc885 100644 --- a/activities/models/post.py +++ b/activities/models/post.py @@ -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) ###