mock-replay 2.3 KB

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