mock-replay 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. from sentry.runner import configure
  3. configure()
  4. import datetime
  5. import pathlib
  6. import click
  7. import requests
  8. from django.conf import settings
  9. from sentry.models import File, Organization, Project
  10. from sentry.replays.models import ReplayRecordingSegment
  11. from sentry.replays.testutils import mock_replay
  12. def store_replay(replay):
  13. response = requests.post(settings.SENTRY_SNUBA + "/tests/replays/insert", json=[replay])
  14. assert response.status_code == 200
  15. def create_recording_segment(replay_id, project_id, filename, segment_id):
  16. with open(filename, "rb") as f:
  17. file = File.objects.create(name=filename, type="application/octet-stream")
  18. file.putfile(f)
  19. ReplayRecordingSegment.objects.create(
  20. replay_id=replay_id.replace("-", ""),
  21. project_id=project_id,
  22. segment_id=segment_id,
  23. file_id=file.id,
  24. )
  25. def make_filename(filename: str) -> str:
  26. parent_dir = pathlib.Path(__file__).parent.resolve()
  27. return f"{parent_dir}/rrweb-output/{filename}"
  28. def main():
  29. project_name = "Replay Test"
  30. if settings.SENTRY_SINGLE_ORGANIZATION:
  31. org = Organization.get_default()
  32. click.echo(f"Mocking org {org.name}")
  33. else:
  34. click.echo("Mocking org {}".format("Default"))
  35. org, _ = Organization.objects.get_or_create(slug="default")
  36. click.echo(f" > Mocking project {project_name}")
  37. project, _ = Project.objects.get_or_create(
  38. name=project_name,
  39. defaults={
  40. "organization": org,
  41. "flags": Project.flags.has_releases,
  42. },
  43. )
  44. replay_id = "44c586f7-bd12-4c1b-b609-189344a19e92"
  45. seq1_timestamp = datetime.datetime.now() - datetime.timedelta(seconds=22)
  46. seq2_timestamp = datetime.datetime.now() - datetime.timedelta(seconds=5)
  47. click.echo("Creating Clickhouse entries...")
  48. store_replay(mock_replay(seq1_timestamp, project.id, replay_id, segment_id=0))
  49. store_replay(mock_replay(seq2_timestamp, project.id, replay_id, segment_id=1))
  50. click.echo("Creating Postgres entries...")
  51. create_recording_segment(replay_id, project.id, make_filename("rrweb-1658770770892.json"), 0)
  52. create_recording_segment(replay_id, project.id, make_filename("rrweb-1658770772903.json"), 1)
  53. if __name__ == "__main__":
  54. main()