feat: týdenní porovnání statistik s minulým týdnem
This commit is contained in:
+32
-5
@@ -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,
|
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}
|
stats = {m["key"]: int(m["total"]) for m in measures_data}
|
||||||
new_users = stats.get("new_users", 0)
|
new_users = stats.get("new_users", 0)
|
||||||
active_users = stats.get("active_users", 0)
|
active_users = stats.get("active_users", 0)
|
||||||
interactions = stats.get("interactions", 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é)"
|
hashtags = " ".join(f"#{t['name']}" for t in tags[:3]) if tags else "(žádné)"
|
||||||
|
|
||||||
tip = TIPS[week_number % len(TIPS)]
|
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"🐘 Týdenní přehled Mamutovo.cz\n"
|
||||||
f"📅 {date_from_str} – {date_to_str} {year}\n"
|
f"📅 {date_from_str} – {date_to_str} {year}\n"
|
||||||
f"\n"
|
f"\n"
|
||||||
f"👥 Noví uživatelé: {new_users}\n"
|
f"👥 Noví uživatelé: {new_users}{fmt_diff(new_users, 'new_users')}\n"
|
||||||
f"✅ Aktivní uživatelé: {active_users}\n"
|
f"✅ Aktivní uživatelé: {active_users}{fmt_diff(active_users, 'active_users')}\n"
|
||||||
f"📝 Interakce: {interactions}\n"
|
f"📝 Interakce: {interactions}{fmt_diff(interactions, 'interactions')}\n"
|
||||||
f"📝 Tooty týdne: {total_count}\n"
|
f"📝 Tooty týdne: {total_count}\n"
|
||||||
f"\n"
|
f"\n"
|
||||||
f"🔥 Populární hashtagy:\n"
|
f"🔥 Populární hashtagy:\n"
|
||||||
@@ -461,6 +469,14 @@ def main():
|
|||||||
instance_info = {}
|
instance_info = {}
|
||||||
user_count = instance_info.get("stats", {}).get("user_count", 0)
|
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)
|
top_tooty = load_tooty_from_data(date_to, 7)
|
||||||
total_count = load_total_count_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)
|
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)
|
top_links = load_top_links_from_data(date_to, 7)
|
||||||
toot = build_toot(
|
toot = build_toot(
|
||||||
measures_data, tags, top_tooty, date_from, date_to, week_number,
|
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:
|
if args.dry_run:
|
||||||
@@ -485,6 +501,17 @@ def main():
|
|||||||
except Exception:
|
except Exception:
|
||||||
sys.exit(1)
|
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)
|
cutoff = date_to - timedelta(days=60)
|
||||||
data_dir = "data"
|
data_dir = "data"
|
||||||
if os.path.isdir(data_dir):
|
if os.path.isdir(data_dir):
|
||||||
|
|||||||
Reference in New Issue
Block a user