mock-replay 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python
  2. from io import BytesIO
  3. from zlib import compress
  4. from sentry.runner import configure
  5. from sentry.utils.json import dumps_htmlsafe
  6. configure()
  7. import pathlib
  8. import uuid
  9. from datetime import datetime, timedelta
  10. import click
  11. import requests
  12. from django.conf import settings
  13. from sentry.models import File, Organization, Project, Team
  14. from sentry.replays.models import ReplayRecordingSegment
  15. from sentry.replays.testutils import (
  16. mock_replay,
  17. mock_rrweb_div_helloworld,
  18. mock_segment_click,
  19. mock_segment_console,
  20. mock_segment_fullsnapshot,
  21. mock_segment_init,
  22. mock_segment_nagivation,
  23. mock_segment_rageclick,
  24. )
  25. def store_replay(replay):
  26. response = requests.post(
  27. settings.SENTRY_SNUBA + "/tests/entities/replays/insert", json=[replay]
  28. )
  29. assert response.status_code == 200
  30. def create_recording(replay_id, project_id, timestamp):
  31. segments = [
  32. mock_segment_init(timestamp),
  33. mock_segment_fullsnapshot(timestamp, [mock_rrweb_div_helloworld()]),
  34. mock_segment_console(timestamp),
  35. mock_segment_nagivation(timestamp + timedelta(seconds=1), hrefFrom="/", hrefTo="/home/"),
  36. mock_segment_nagivation(
  37. timestamp + timedelta(seconds=2), hrefFrom="/home/", hrefTo="/profile/"
  38. ),
  39. mock_segment_rageclick(
  40. timestamp + timedelta(seconds=2),
  41. "nav.app-65yvxw.e1upz5ks6[aria-label='Primary Navigation'] > div.app-1v175cc.e1upz5ks4",
  42. "sidebar-item-performance",
  43. "a",
  44. 24,
  45. ),
  46. mock_segment_click(
  47. timestamp + timedelta(seconds=2),
  48. "nav.app-65yvxw.e1upz5ks6[aria-label='Primary Navigation'] > div.app-1v175cc.e1upz5ks4",
  49. "sidebar-item-performance",
  50. "a",
  51. ),
  52. mock_segment_nagivation(
  53. timestamp + timedelta(seconds=6),
  54. hrefFrom="/profile/",
  55. hrefTo="/performance/",
  56. ),
  57. mock_segment_rageclick(
  58. timestamp + timedelta(seconds=6),
  59. "nav.app-65yvxw.e1upz5ks6[aria-label='Primary Navigation'] > div.app-1v175cc.e1upz5ks4",
  60. "sidebar-item-performance",
  61. "a",
  62. 24,
  63. ),
  64. ]
  65. for (segment_id, segment) in enumerate(segments):
  66. store_replay_segments(replay_id, project_id, segment_id, segment)
  67. def store_replay_segments(replay_id: str, project_id: str, segment_id: int, segment):
  68. f = File.objects.create(name="rr:{segment_id}", type="replay.recording")
  69. f.putfile(BytesIO(compress(dumps_htmlsafe(segment).encode())))
  70. ReplayRecordingSegment.objects.create(
  71. replay_id=replay_id,
  72. project_id=project_id,
  73. segment_id=segment_id,
  74. file_id=f.id,
  75. )
  76. def make_filename(filename: str) -> str:
  77. parent_dir = pathlib.Path(__file__).parent.resolve()
  78. return f"{parent_dir}/rrweb-output/{filename}"
  79. def main():
  80. project_name = "Replay Test Project"
  81. if not settings.SENTRY_FEATURES["organizations:session-replay"]:
  82. click.echo(
  83. 'Session Replays is currently turned off! \nTo enable, add the following line to your local sentry.conf.py file: \nSENTRY_FEATURES["organizations:session-replay"] = True'
  84. )
  85. exit()
  86. if settings.SENTRY_SINGLE_ORGANIZATION:
  87. org = Organization.get_default()
  88. click.echo(f"Mocking org {org.name}")
  89. else:
  90. click.echo("Mocking org {}".format("Default"))
  91. org, _ = Organization.objects.get_or_create(slug="default")
  92. click.echo(f" > Mocking project {project_name}")
  93. team, _ = Team.objects.get_or_create(
  94. organization=org, slug="sentry", defaults={"name": "Sentry"}
  95. )
  96. project, _ = Project.objects.get_or_create(
  97. name=project_name,
  98. defaults={
  99. "organization": org,
  100. "flags": Project.flags.has_replays,
  101. },
  102. platform="javascript",
  103. )
  104. project.add_team(team)
  105. replay_id = uuid.uuid4().hex
  106. seq1_timestamp = datetime.now() - timedelta(seconds=15)
  107. seq2_timestamp = datetime.now() - timedelta(seconds=5)
  108. click.echo("Creating Replay events entries...")
  109. store_replay(mock_replay(seq1_timestamp, project.id, replay_id, segment_id=0))
  110. store_replay(mock_replay(seq2_timestamp, project.id, replay_id, segment_id=1))
  111. click.echo("Creating Replay recording entries...")
  112. create_recording(replay_id, project.id, seq1_timestamp)
  113. if __name__ == "__main__":
  114. main()