text: # Generated by Django 3.2.12 on 2022-04-19 10:27
  from dateutil.relativedelta import relativedelta
  from django.db import migrations, models
  from django.utils import timezone
  
  
  def add_schedules(apps, schema_editor):
      Schedule = apps.get_model("django_q", "schedule")
      now = timezone.now()
  
      # Create schedule for calculating the daily active users.
      # We schedule it on the next day at 3 o'clock.
      Schedule.objects.create(
          func="user_statistics.services.calculate_daily_active_users",
          schedule_type="D",
          next_run=timezone.datetime(
              year=now.year, month=now.month, day=now.day + 1, hour=3
          ),
      )
  
      # Create schedule for calculating the monthly active users.
      # We schedule it on the first day of the next month at 3 o'clock.
      Schedule.objects.create(
          func="user_statistics.services.calculate_monthly_active_users",
          schedule_type="M",
          next_run=timezone.datetime(year=now.year, month=now.month + 1, day=1, hour=3),
      )
  
      # Create schedule for deleting user visits.
      # We schedule this on the 5th day of the next month to allow for error mitigation
      #  in case that calculation of the daily and monthly visits failed.
      Schedule.objects.create(
          func="user_statistics.services.delete_past_user_visits",
          schedule_type="M",
          next_run=timezone.datetime(year=now.year, month=now.month + 1, day=5, hour=3),
      )
  
  
  class Migration(migrations.Migration):
  
      initial = True
  
      dependencies = []
  
      operations = [
          migrations.CreateModel(
              name="DailyActiveUsers",
              fields=[
                  ("date", models.DateField(primary_key=True)),
                  ("count", models.PositiveIntegerField()),
                  ("fraction", models.DecimalField(decimal_places=2, max_digits=3)),
              ],
          ),
          migrations.CreateModel(
              name="MonthlyActiveUsers",
              fields=[
                  ("date", models.DateField(primary_key=True)),
                  ("count", models.PositiveIntegerField()),
                  ("fraction", models.DecimalField(decimal_places=2, max_digits=3)),
              ],
          ),
          migrations.RunPython(add_schedules),
      ]
  