0004_auto_20240312_1344.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Generated by Django 5.0.2 on 2024-03-12 13:44
  2. import os
  3. from datetime import timedelta
  4. from django.db import connection, migrations
  5. from django.db.models import Value
  6. from django.conf import settings
  7. from django.utils.timezone import now
  8. from django.contrib.postgres.search import SearchVector
  9. from apps.issue_events.constants import IssueEventType
  10. MIGRATION_LIMIT = os.getenv("ISSUE_EVENT_MIGRATION_LIMIT", 50000)
  11. MIGRATION_LIMIT = int(MIGRATION_LIMIT)
  12. EVENT_LIMIT = os.getenv("EVENT_PER_ISSUE_MIGRATION_LIMIT", 1000)
  13. EVENT_LIMIT = int(EVENT_LIMIT)
  14. def reformat_data(data):
  15. if "exception" in data and data["exception"]:
  16. if "values" in data["exception"]:
  17. data["exception"] = data["exception"]["values"]
  18. if "breadcrumbs" in data and data["breadcrumbs"]:
  19. if "values" in data["breadcrumbs"]:
  20. data["breadcrumbs"] = data["breadcrumbs"]["values"]
  21. return data
  22. def daterange(start_date, end_date):
  23. for n in range(int((end_date - start_date).days)):
  24. yield start_date + timedelta(n)
  25. def migrate_issue_events(apps, schema_editor):
  26. if settings.TESTING:
  27. return
  28. today = now()
  29. days_ago = timedelta(days=settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS + 1)
  30. with connection.cursor() as cursor:
  31. for single_date in daterange(today - days_ago, today):
  32. str_date = single_date.strftime("%Y_%b_%d").lower()
  33. from_date = single_date.date() # strftime("%Y-%m-%d")
  34. to_date = (single_date + timedelta(days=1)).date() # strftime("%Y-%m-%d")
  35. table_name = "issue_events_issueevent_" + str_date
  36. sql = f"""CREATE TABLE IF NOT EXISTS {table_name} PARTITION OF issue_events_issueevent
  37. FOR VALUES FROM (%s) TO (%s);
  38. COMMENT ON TABLE {table_name} IS 'psqlextra_auto_partitioned';
  39. """
  40. cursor.execute(sql, [from_date, to_date])
  41. OldIssue = apps.get_model("issues", "Issue")
  42. Event = apps.get_model("events", "Event")
  43. Issue = apps.get_model("issue_events", "Issue")
  44. IssueEvent = apps.get_model("issue_events", "IssueEvent")
  45. oldest_event = Event.objects.order_by("-created")[
  46. MIGRATION_LIMIT : MIGRATION_LIMIT + 1
  47. ].first()
  48. start_migration_date = oldest_event.created if oldest_event else None
  49. old_issues = OldIssue.objects.all().defer("search_vector", "tags")
  50. if start_migration_date:
  51. old_issues = old_issues.filter(last_seen__gt=start_migration_date)
  52. for old_issue in old_issues:
  53. issue = Issue.objects.create(
  54. culprit=old_issue.culprit[:1024],
  55. level=old_issue.level,
  56. metadata=old_issue.metadata,
  57. project=old_issue.project,
  58. title=old_issue.title,
  59. type=old_issue.type,
  60. status=old_issue.status,
  61. short_id=old_issue.short_id,
  62. search_vector=SearchVector(Value(old_issue.title)),
  63. count=old_issue.count,
  64. first_seen=old_issue.created,
  65. last_seen=old_issue.last_seen,
  66. )
  67. events = old_issue.event_set.order_by("-created")
  68. if start_migration_date:
  69. events = events.filter(created__gt=start_migration_date)
  70. IssueEvent.objects.bulk_create(
  71. [
  72. IssueEvent(
  73. id=event.event_id,
  74. type=IssueEventType.from_string(event.data.get("type", "default")),
  75. timestamp=event.timestamp if event.timestamp else event.created,
  76. received=event.created,
  77. title=event.data.get("title"),
  78. transaction=event.data.get("culprit")[:200],
  79. level=event.level,
  80. data=reformat_data(event.data),
  81. tags=event.tags,
  82. issue=issue,
  83. )
  84. for event in events[:EVENT_LIMIT]
  85. ]
  86. )
  87. class Migration(migrations.Migration):
  88. dependencies = [
  89. ("events", "0001_squashed_0003_auto_20210219_1951"),
  90. ("issues", "0013_alter_comment_options_alter_issue_unique_together_and_more"),
  91. ("issue_events", "0001_initial"),
  92. ]
  93. operations = [
  94. migrations.RunPython(migrate_issue_events, migrations.RunPython.noop),
  95. ]