Files
fangke/server/tracker/management/commands/refresh_stats.py
T
浅念 cc1ea6805b feat: 日聚合服务与命令 + 索引迁移打包(Task3 审查结论)
- refresh_daily:按上海自然日聚合 pv/uv/sessions/new_visitors,update_or_create 幂等
- refresh_stats 管理命令:回刷近 N 天(生产由 1Panel 每日 00:10 调度,见 Task 22)
- 索引打包(0002):Event/VisitSession 增加 (site, 时间) 日聚合索引;
  删 5 个冗余单列 FK 索引(Event.site/session、VisitSession.site/visitor、DailyStat.site),
  保留 Event.visitor/Visitor.site;sqlite PRAGMA 实证级联索引完整、EXPLAIN 走新索引
- test_models:IntegrityError 断言收紧 + Site 级联删除测试

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-15 13:38:01 +08:00

25 lines
927 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import timedelta
from django.core.management.base import BaseCommand
from django.utils import timezone
from tracker.models import Site
from tracker.services.stats import refresh_daily
class Command(BaseCommand):
# 生产调度:1Panel 计划任务每日 00:10 执行
# docker exec <web容器> python manage.py refresh_stats --days 2
# (回刷含今天在内的近 N 天,详见 Task 22 README)
help = "回刷近 N 天日聚合(含今天)"
def add_arguments(self, parser):
parser.add_argument("--days", type=int, default=2)
def handle(self, *args, **opts):
today = timezone.localdate()
total = 0
for site in Site.objects.filter(is_active=True):
for i in range(opts["days"]):
stat = refresh_daily(site, today - timedelta(days=i))
total += 1
self.stdout.write(f"refreshed {total} site-days")