fix duplicated stats

This commit is contained in:
Son 2022-02-28 10:13:19 +01:00
parent 6a520e110c
commit 3d1a960702
2 changed files with 19 additions and 45 deletions

56
cron.py
View file

@ -465,7 +465,7 @@ def alias_creation_report() -> List[Tuple[str, int]]:
return res return res
def growth_stats(): def stats():
"""send admin stats everyday""" """send admin stats everyday"""
if not ADMIN_EMAIL: if not ADMIN_EMAIL:
LOG.w("ADMIN_EMAIL not set, nothing to do") LOG.w("ADMIN_EMAIL not set, nothing to do")
@ -480,7 +480,7 @@ def growth_stats():
today = arrow.now().format() today = arrow.now().format()
report = f""" growth_stats = f"""
Growth Stats for {today} Growth Stats for {today}
nb_user: {stats_today.nb_user} - {increase_percent(stats_yesterday.nb_user, stats_today.nb_user)} nb_user: {stats_today.nb_user} - {increase_percent(stats_yesterday.nb_user, stats_today.nb_user)}
@ -507,32 +507,16 @@ nb_referred_user: {stats_today.nb_referred_user} - {increase_percent(stats_yeste
nb_referred_user_upgrade: {stats_today.nb_referred_user_paid} - {increase_percent(stats_yesterday.nb_referred_user_paid, stats_today.nb_referred_user_paid)} nb_referred_user_upgrade: {stats_today.nb_referred_user_paid} - {increase_percent(stats_yesterday.nb_referred_user_paid, stats_today.nb_referred_user_paid)}
""" """
LOG.d("report email: %s", report) LOG.d("growth_stats email: %s", growth_stats)
send_email( send_email(
ADMIN_EMAIL, ADMIN_EMAIL,
subject=f"SimpleLogin Growth Stats for {today}", subject=f"SimpleLogin Growth Stats for {today}",
plaintext=report, plaintext=growth_stats,
retries=3, retries=3,
) )
monitoring_report = f"""
def daily_monitoring_report():
"""send monitoring stats of the previous day"""
if not MONITORING_EMAIL:
LOG.w("MONITORING_EMAIL not set, nothing to do")
return
stats_today = compute_metric2()
stats_yesterday = (
Metric2.filter(Metric2.date < stats_today.date)
.order_by(Metric2.date.desc())
.first()
)
today = arrow.now().format()
report = f"""
Monitoring Stats for {today} Monitoring Stats for {today}
nb_alias: {stats_today.nb_alias} - {increase_percent(stats_yesterday.nb_alias, stats_today.nb_alias)} nb_alias: {stats_today.nb_alias} - {increase_percent(stats_yesterday.nb_alias, stats_today.nb_alias)}
@ -545,32 +529,32 @@ nb_total_bounced_last_24h: {stats_today.nb_total_bounced_last_24h} - {increase_p
""" """
report += "\n====================================\n" monitoring_report += "\n====================================\n"
report += f""" monitoring_report += f"""
# Account bounce report: # Account bounce report:
""" """
for email, bounces in bounce_report(): for email, bounces in bounce_report():
report += f"{email}: {bounces}\n" monitoring_report += f"{email}: {bounces}\n"
report += f"""\n monitoring_report += f"""\n
# Alias creation report: # Alias creation report:
""" """
for email, nb_alias, date in alias_creation_report(): for email, nb_alias, date in alias_creation_report():
report += f"{email}, {date}: {nb_alias}\n" monitoring_report += f"{email}, {date}: {nb_alias}\n"
report += f"""\n monitoring_report += f"""\n
# Full bounce detail report: # Full bounce detail report:
""" """
report += all_bounce_report() monitoring_report += all_bounce_report()
LOG.d("report email: %s", report) LOG.d("monitoring_report email: %s", monitoring_report)
send_email( send_email(
MONITORING_EMAIL, MONITORING_EMAIL,
subject=f"SimpleLogin Monitoring Report for {today}", subject=f"SimpleLogin Monitoring Report for {today}",
plaintext=report, plaintext=monitoring_report,
retries=3, retries=3,
) )
@ -1040,8 +1024,7 @@ if __name__ == "__main__":
help="Choose a cron job to run", help="Choose a cron job to run",
type=str, type=str,
choices=[ choices=[
"growth_stats", "stats",
"daily_monitoring_report",
"notify_trial_end", "notify_trial_end",
"notify_manual_subscription_end", "notify_manual_subscription_end",
"notify_premium_end", "notify_premium_end",
@ -1057,12 +1040,9 @@ if __name__ == "__main__":
args = parser.parse_args() args = parser.parse_args()
# wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc # wrap in an app context to benefit from app setup like database cleanup, sentry integration, etc
with create_light_app().app_context(): with create_light_app().app_context():
if args.job == "growth_stats": if args.job == "stats":
LOG.d("Compute growth Stats") LOG.d("Compute growth and daily monitoring stats")
growth_stats() stats()
if args.job == "daily_monitoring_report":
LOG.d("Send out daily monitoring stats")
daily_monitoring_report()
elif args.job == "notify_trial_end": elif args.job == "notify_trial_end":
LOG.d("Notify users with trial ending soon") LOG.d("Notify users with trial ending soon")
notify_trial_end() notify_trial_end()

View file

@ -1,12 +1,6 @@
jobs: jobs:
- name: SimpleLogin growth stats - name: SimpleLogin growth stats
command: python /code/cron.py -j growth_stats command: python /code/cron.py -j stats
shell: /bin/bash
schedule: "0 1 * * *"
captureStderr: true
- name: SimpleLogin monitoring stats
command: python /code/cron.py -j daily_monitoring_report
shell: /bin/bash shell: /bin/bash
schedule: "0 0 * * *" schedule: "0 0 * * *"
captureStderr: true captureStderr: true