Create schedulers for delete old runs and reports

Related_prod: PRODX-42822

Change-Id: I91e1d9f24202f883047efdfdfaa8bd30e047e367
diff --git a/testrail_bot/control/celery_tasks/schedules_pipeline.py b/testrail_bot/control/celery_tasks/schedules_pipeline.py
index 1ad090a..080519a 100644
--- a/testrail_bot/control/celery_tasks/schedules_pipeline.py
+++ b/testrail_bot/control/celery_tasks/schedules_pipeline.py
@@ -1,5 +1,5 @@
 import os
-from datetime import datetime
+from datetime import datetime, timedelta
 
 from .. import models
 from . import tasks
@@ -81,3 +81,33 @@
         path=path,
         is_testplan=True,
     )
+
+
+def task_to_delete_old_2m_testruns():
+    """
+    Delete test runs after 2 month
+    Clear mechanism 31.12.XXXX - 2 month
+    delete 1-31.10.XXXX and older
+    """
+    today = datetime.today()
+    border_date = (today - timedelta(days=30 * 2)).strftime("%Y-%m-%d")
+    runs = models.TestRailTestRun.objects.all()
+    for run in runs:
+        if run.created_at.strftime("%Y-%m-%d") < border_date:
+            run.delete()
+            print(run.created_at.strftime("%Y-%m-%d"))
+
+
+def task_to_delete_old_2m_reports():
+    """
+    Delete reports after 2 month
+    Clear mechanism 31.12.XXXX - 2 month
+    delete 1-31.10.XXXX and older
+    """
+    today = datetime.today()
+    border_date = (today - timedelta(days=30 * 2)).strftime("%Y-%m-%d")
+    reports = models.TestRailReport.objects.order_by("-created_at").all()
+    for report in reports:
+        if report.created_at.strftime("%Y-%m-%d") < border_date:
+            report.delete()
+            print(report.created_at.strftime("%Y-%m-%d"))
diff --git a/testrail_bot/control/celery_tasks/tasks.py b/testrail_bot/control/celery_tasks/tasks.py
index cd18f6d..3d090c2 100644
--- a/testrail_bot/control/celery_tasks/tasks.py
+++ b/testrail_bot/control/celery_tasks/tasks.py
@@ -66,3 +66,27 @@
     :return:
     """
     schedules_pipeline.task_to_check_testplan(testplan_id)
+
+
+@shared_task
+def delete_old_2m_testruns(*args, **kwargs):
+    """
+    Finds today testplan
+    Creates TestRun with this id
+    Creates Periodic task to analyze created TestRun
+
+    :return:
+    """
+    schedules_pipeline.task_to_delete_old_2m_testruns()
+
+
+@shared_task
+def delete_old_2m_reports(*args, **kwargs):
+    """
+    Finds today testplan
+    Creates TestRun with this id
+    Creates Periodic task to analyze created TestRun
+
+    :return:
+    """
+    schedules_pipeline.task_to_delete_old_2m_reports()
diff --git a/testrail_bot/control/migrations/0013_testrailtestrun_created_at_and_more.py b/testrail_bot/control/migrations/0013_testrailtestrun_created_at_and_more.py
new file mode 100644
index 0000000..bbbee9a
--- /dev/null
+++ b/testrail_bot/control/migrations/0013_testrailtestrun_created_at_and_more.py
@@ -0,0 +1,48 @@
+# Generated by Django 4.2.7 on 2024-06-19 12:59
+
+from django.db import migrations, models
+import django.utils.timezone
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("control", "0012_alter_suitepassrate_suite_id"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="testrailtestrun",
+            name="created_at",
+            field=models.DateTimeField(
+                auto_now_add=True, default=django.utils.timezone.now
+            ),
+            preserve_default=False,
+        ),
+        migrations.AlterField(
+            model_name="cronperiodictask",
+            name="task_name",
+            field=models.CharField(
+                choices=[
+                    (
+                        "control.celery_tasks.tasks.check_today_testplan",
+                        "Check today testplan",
+                    ),
+                    (
+                        "control.celery_tasks.tasks.check_specific_testplan",
+                        "Check specific testplan",
+                    ),
+                    (
+                        "control.celery_tasks.tasks.delete_old_2m_testruns",
+                        "Delete previous 2-month TestRuns(for bot view)",
+                    ),
+                    (
+                        "control.celery_tasks.tasks.delete_old_2m_reports",
+                        "Delete previous 2-month Reports(for bot view)",
+                    ),
+                ],
+                default="control.celery_tasks.tasks.check_today_testplan",
+                max_length=300,
+            ),
+        ),
+    ]
diff --git a/testrail_bot/control/models.py b/testrail_bot/control/models.py
index b438dec..f613292 100644
--- a/testrail_bot/control/models.py
+++ b/testrail_bot/control/models.py
@@ -51,6 +51,7 @@
     filter_last_traceback = models.BooleanField(default=True)
     created_before = models.DateField(default=now)
     created_after = models.DateField(default=default_created_after)
+    created_at = models.DateTimeField(auto_now_add=True)
 
     @property
     def text_filters(self):
@@ -155,6 +156,16 @@
         "Check specific testplan",
         ["testplan_id_arg"],
     ),
+    (
+        "control.celery_tasks.tasks.delete_old_2m_testruns",
+        "Delete previous 2-month TestRuns(for bot view)",
+        [],
+    ),
+    (
+        "control.celery_tasks.tasks.delete_old_2m_reports",
+        "Delete previous 2-month Reports(for bot view)",
+        [],
+    ),
 ]
 
 TASK_CHOICES = list(map(lambda x: x[:-1], TASKS))
diff --git a/testrail_bot/control/templates/control/index.html b/testrail_bot/control/templates/control/index.html
index 8736ef9..ca89024 100644
--- a/testrail_bot/control/templates/control/index.html
+++ b/testrail_bot/control/templates/control/index.html
@@ -21,6 +21,8 @@
             {% else %}
                 Run from {{run.plan_name}} plan in {{run.project_name}} project
             {% endif %}
+            <span class="justify-content-end px-3 mx-2 fw-bold rounded-pill float-sm-end
+                text-dark bg-white">created at {{ run.created_at }}</span>
         </a>
     {% endfor %}
 </div>
diff --git a/testrail_bot/control/templates/control/reports.html b/testrail_bot/control/templates/control/reports.html
index 337349b..f62a614 100644
--- a/testrail_bot/control/templates/control/reports.html
+++ b/testrail_bot/control/templates/control/reports.html
@@ -11,7 +11,8 @@
         {% endif %}
         ">
         {{report.report_name}}
-
+        <span class="justify-content-end px-3 mx-2 fw-bold rounded-pill float-sm-end
+            text-dark bg-white">created at {{ report.created_at }}</span>
         </a>
     {% endfor %}
 </div>