cron.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import argparse
  2. import arrow
  3. from app.config import IGNORED_EMAILS, ADMIN_EMAIL
  4. from app.email_utils import send_email, send_trial_end_soon_email
  5. from app.extensions import db
  6. from app.log import LOG
  7. from app.models import (
  8. Subscription,
  9. User,
  10. GenEmail,
  11. ForwardEmailLog,
  12. ForwardEmail,
  13. CustomDomain,
  14. Client,
  15. )
  16. from server import create_app
  17. def notify_trial_end():
  18. for user in User.query.filter(User.trial_end.isnot(None)).all():
  19. if arrow.now().shift(days=3) > user.trial_end >= arrow.now().shift(days=2):
  20. LOG.d("Send trial end email to user %s", user)
  21. send_trial_end_soon_email(user)
  22. def stats():
  23. """send admin stats everyday"""
  24. if not ADMIN_EMAIL:
  25. # nothing to do
  26. return
  27. # nb user
  28. q = User.query
  29. for ie in IGNORED_EMAILS:
  30. q = q.filter(~User.email.contains(ie))
  31. nb_user = q.count()
  32. LOG.d("total number user %s", nb_user)
  33. # nb gen emails
  34. q = db.session.query(GenEmail, User).filter(GenEmail.user_id == User.id)
  35. for ie in IGNORED_EMAILS:
  36. q = q.filter(~User.email.contains(ie))
  37. nb_gen_email = q.count()
  38. LOG.d("total number alias %s", nb_gen_email)
  39. # nb mails forwarded
  40. q = db.session.query(ForwardEmailLog, ForwardEmail, GenEmail, User).filter(
  41. ForwardEmailLog.forward_id == ForwardEmail.id,
  42. ForwardEmail.gen_email_id == GenEmail.id,
  43. GenEmail.user_id == User.id,
  44. )
  45. for ie in IGNORED_EMAILS:
  46. q = q.filter(~User.email.contains(ie))
  47. nb_forward = nb_block = nb_reply = 0
  48. for fel, _, _, _ in q:
  49. if fel.is_reply:
  50. nb_reply += 1
  51. elif fel.blocked:
  52. nb_block += 1
  53. else:
  54. nb_forward += 1
  55. LOG.d("nb forward %s, nb block %s, nb reply %s", nb_forward, nb_block, nb_reply)
  56. nb_premium = Subscription.query.count()
  57. nb_custom_domain = CustomDomain.query.count()
  58. nb_custom_domain_alias = GenEmail.query.filter(
  59. GenEmail.custom_domain_id.isnot(None)
  60. ).count()
  61. nb_disabled_alias = GenEmail.query.filter(GenEmail.enabled == False).count()
  62. nb_app = Client.query.count()
  63. today = arrow.now().format()
  64. send_email(
  65. ADMIN_EMAIL,
  66. subject=f"SimpleLogin Stats for {today}, {nb_user} users, {nb_gen_email} aliases, {nb_forward} forwards",
  67. plaintext="",
  68. html=f"""
  69. Stats for {today} <br>
  70. nb_user: {nb_user} <br>
  71. nb_premium: {nb_premium} <br>
  72. nb_alias: {nb_gen_email} <br>
  73. nb_disabled_alias: {nb_disabled_alias} <br>
  74. nb_custom_domain: {nb_custom_domain} <br>
  75. nb_custom_domain_alias: {nb_custom_domain_alias} <br>
  76. nb_forward: {nb_forward} <br>
  77. nb_reply: {nb_reply} <br>
  78. nb_block: {nb_block} <br>
  79. nb_app: {nb_app} <br>
  80. """,
  81. )
  82. if __name__ == "__main__":
  83. LOG.d("Start running cronjob")
  84. parser = argparse.ArgumentParser()
  85. parser.add_argument(
  86. "-j",
  87. "--job",
  88. help="Choose a cron job to run",
  89. type=str,
  90. choices=["stats", "notify_trial_end",],
  91. )
  92. args = parser.parse_args()
  93. app = create_app()
  94. with app.app_context():
  95. if args.job == "stats":
  96. LOG.d("Compute stats")
  97. stats()
  98. elif args.job == "notify_trial_end":
  99. LOG.d("Notify users with trial ending soon")
  100. notify_trial_end()