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