feat: týdenní porovnání statistik s minulým týdnem

This commit is contained in:
2026-06-15 19:06:22 +02:00
parent 9d03bdc737
commit 4da5e6790c
+32 -5
View File
@@ -194,12 +194,20 @@ def build_monthly_toot(measures_data, tags, top_tooty, date_to, prev_stats, inst
)
def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number,
total_count=0, top_author=None, newest_account=None, user_count=0, top_links=None):
total_count=0, top_author=None, newest_account=None, user_count=0, top_links=None,
prev_stats=None):
stats = {m["key"]: int(m["total"]) for m in measures_data}
new_users = stats.get("new_users", 0)
active_users = stats.get("active_users", 0)
interactions = stats.get("interactions", 0)
def fmt_diff(current, key):
if not prev_stats or key not in prev_stats:
return ""
d = current - prev_stats[key]
sign = "+" if d >= 0 else ""
return f" ({sign}{d})"
hashtags = " ".join(f"#{t['name']}" for t in tags[:3]) if tags else "(žádné)"
tip = TIPS[week_number % len(TIPS)]
@@ -235,9 +243,9 @@ def build_toot(measures_data, tags, top_tooty, date_from, date_to, week_number,
f"🐘 Týdenní přehled Mamutovo.cz\n"
f"📅 {date_from_str} {date_to_str} {year}\n"
f"\n"
f"👥 Noví uživatelé: {new_users}\n"
f"✅ Aktivní uživatelé: {active_users}\n"
f"📝 Interakce: {interactions}\n"
f"👥 Noví uživatelé: {new_users}{fmt_diff(new_users, 'new_users')}\n"
f"✅ Aktivní uživatelé: {active_users}{fmt_diff(active_users, 'active_users')}\n"
f"📝 Interakce: {interactions}{fmt_diff(interactions, 'interactions')}\n"
f"📝 Tooty týdne: {total_count}\n"
f"\n"
f"🔥 Populární hashtagy:\n"
@@ -461,6 +469,14 @@ def main():
instance_info = {}
user_count = instance_info.get("stats", {}).get("user_count", 0)
weekly_stats_path = os.path.join("data", "weekly_stats.json")
prev_stats = None
try:
with open(weekly_stats_path, encoding="utf-8") as f:
prev_stats = json.load(f)
except FileNotFoundError:
pass
top_tooty = load_tooty_from_data(date_to, 7)
total_count = load_total_count_from_data(date_to, 7)
top_author = load_top_author_from_data(date_to, 7)
@@ -468,7 +484,7 @@ def main():
top_links = load_top_links_from_data(date_to, 7)
toot = build_toot(
measures_data, tags, top_tooty, date_from, date_to, week_number,
total_count, top_author, newest_account, user_count, top_links,
total_count, top_author, newest_account, user_count, top_links, prev_stats,
)
if args.dry_run:
@@ -485,6 +501,17 @@ def main():
except Exception:
sys.exit(1)
cur_stats = {m["key"]: int(m["total"]) for m in measures_data}
os.makedirs("data", exist_ok=True)
with open(weekly_stats_path, "w", encoding="utf-8") as f:
json.dump({
"date": date_to.strftime("%Y-%m-%d"),
"new_users": cur_stats.get("new_users", 0),
"active_users": cur_stats.get("active_users", 0),
"interactions": cur_stats.get("interactions", 0),
}, f, ensure_ascii=False, indent=2)
print("Týdenní statistiky uloženy.")
cutoff = date_to - timedelta(days=60)
data_dir = "data"
if os.path.isdir(data_dir):