lib.itmens/takahe/jobs.py

65 lines
2.2 KiB
Python
Raw Permalink Normal View History

2024-05-19 17:14:03 -04:00
from datetime import timedelta
from django.core.cache import cache
from django.utils import timezone
from loguru import logger
from common.models import BaseJob, JobManager
from journal.models import Comment, Review, ShelfMember
from takahe.models import Domain, Identity, Post
@JobManager.register
class TakaheStats(BaseJob):
interval = timedelta(hours=6)
max_unreachable_days = 31
def active_users(self, d: int) -> int:
return (
ShelfMember.objects.filter(
created_time__gte=timezone.now() - timedelta(days=d), local=True
)
.values("owner_id")
.distinct()
.count()
)
def run(self):
logger.info("Updating Tahake stats.")
# for /api/v1/instance
stats = {
"user_count": Identity.objects.filter(
local=True, deleted__isnull=True
).count(),
"status_count": Post.objects.filter(local=True)
.exclude(state__in=["deleted", "deleted_fanned_out"])
.count(),
"domain_count": Domain.objects.count(),
}
2024-05-20 17:08:26 -04:00
cache.set("instance_info_stats", stats, timeout=None)
2024-05-20 15:49:46 -04:00
logger.debug(f"/api/v1/instance {stats}")
2024-05-19 17:14:03 -04:00
# for /api/v2/instance
usage = {
"users": {
"active_month": self.active_users(30),
}
}
2024-05-20 17:08:26 -04:00
cache.set("instance_info_usage", usage, timeout=None)
2024-05-20 15:49:46 -04:00
logger.debug(f"/api/v2/instance {usage}")
2024-05-19 17:14:03 -04:00
# for NodeInfo
nodeinfo_usage = {
"users": {
"total": stats["user_count"],
2024-05-20 15:49:46 -04:00
"activeMonth": usage["users"]["active_month"],
"activeHalfyear": self.active_users(180),
2024-05-19 17:14:03 -04:00
},
"localPosts": ShelfMember.objects.filter(local=True).count(),
"localComments": Comment.objects.filter(local=True).count()
+ Review.objects.filter(local=True).count(),
}
2024-05-20 17:08:26 -04:00
cache.set("nodeinfo_usage", nodeinfo_usage, timeout=None)
2024-05-20 15:49:46 -04:00
logger.debug(f"/nodeinfo/2.0/ {nodeinfo_usage}")
2025-01-05 09:43:52 -05:00
# disable /api/v1/instance/activity for now as it's slow
cache.set("instance_activity_stats", [], timeout=None)
2024-05-20 15:49:46 -04:00
logger.info("Tahake stats updated.")