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