1313from shared .reports .exceptions import LabelIndexNotFoundError , LabelNotFoundError
1414from shared .reports .filtered import FilteredReport
1515from shared .reports .reportfile import ReportFile
16- from shared .reports .serde import orjson_option , report_default
17- from shared .reports .types import TOTALS_MAP , ReportHeader , ReportTotals
16+ from shared .reports .types import ReportHeader , ReportTotals
1817from shared .utils .flare import report_to_flare
1918from shared .utils .make_network_file import make_network_file
2019from shared .utils .migrate import migrate_totals
2120from shared .utils .sessions import Session , SessionType
2221from shared .utils .totals import agg_totals
2322
24- from .serde import _encode_chunk , serialize_report
23+ from .serde import END_OF_CHUNK , END_OF_HEADER , serialize_report
2524
2625log = logging .getLogger (__name__ )
2726
@@ -37,21 +36,6 @@ def unique_everseen(iterable):
3736 yield element
3837
3938
40- END_OF_CHUNK = "\n <<<<< end_of_chunk >>>>>\n "
41- END_OF_HEADER = "\n <<<<< end_of_header >>>>>\n "
42-
43-
44- def chunks_from_storage_contains_header (chunks : str ) -> bool :
45- try :
46- first_line_end = chunks .index ("\n " )
47- second_line_end = chunks .index ("\n " , first_line_end + 1 )
48- except ValueError :
49- return False
50- # If the header is present then the END_OF_HEADER marker
51- # is in the 2nd line of the report
52- return chunks [first_line_end : second_line_end + 1 ] == END_OF_HEADER
53-
54-
5539class Report :
5640 sessions : dict [int , Session ]
5741 _header : ReportHeader
@@ -80,14 +64,14 @@ def __init__(
8064 for sid , session in sessions .items ()
8165 }
8266
83- _chunks : list [bytes ] = []
67+ _chunks : list [str ] = []
8468 if chunks :
85- if isinstance (chunks , str ):
86- chunks = chunks .encode ()
8769 if isinstance (chunks , bytes ):
88- splits = chunks .split (b"\n <<<<< end_of_header >>>>>\n " , maxsplit = 1 )
70+ chunks = chunks .decode ()
71+ if isinstance (chunks , str ):
72+ splits = chunks .split (END_OF_HEADER , maxsplit = 1 )
8973 if len (splits ) > 1 :
90- _header = orjson .loads (splits [0 ] or b "{}" )
74+ _header = orjson .loads (splits [0 ] or "{}" )
9175 self ._header = ReportHeader (
9276 labels_index = {
9377 int (k ): v
@@ -96,7 +80,7 @@ def __init__(
9680 )
9781 chunks = splits [1 ]
9882
99- _chunks = chunks .split (b" \n <<<<< end_of_chunk >>>>> \n " )
83+ _chunks = chunks .split (END_OF_CHUNK )
10084 else :
10185 _chunks = chunks
10286
@@ -112,8 +96,7 @@ def __init__(
11296 file_diff_totals = None
11397
11498 try :
115- _lines = _chunks [chunks_index ]
116- lines = _lines .decode () if isinstance (_lines , bytes ) else _lines
99+ lines = _chunks [chunks_index ]
117100 except IndexError :
118101 lines = ""
119102
@@ -345,42 +328,12 @@ def is_empty(self):
345328 def __bool__ (self ):
346329 return self .is_empty () is False
347330
348- @sentry_sdk .trace
349- def to_archive (self , with_header = True ):
350- # TODO: confirm removing encoding here is fine
351- chunks = END_OF_CHUNK .join (
352- _encode_chunk (chunk ).decode () for chunk in self ._files .values ()
353- )
354- if with_header :
355- # When saving to database we want this
356- return END_OF_HEADER .join (
357- [orjson .dumps (self ._header , option = orjson_option ).decode (), chunks ]
358- )
359- # This is helpful to build ReadOnlyReport
360- # Because Rust can't parse the header. It doesn't need it either,
361- # So it's simpler to just never sent it.
362- return chunks
363-
364- @sentry_sdk .trace
365- def to_database (self ):
366- """returns (totals, report) to be stored in database"""
367- totals = dict (zip (TOTALS_MAP , self .totals ))
368- totals ["diff" ] = self .diff_totals
369-
370- files = {
371- file .name : [i , file .totals , None , file .diff_totals ]
372- for i , file in enumerate (self ._files .values ())
373- }
374- return (
375- totals ,
376- orjson .dumps (
377- {"files" : files , "sessions" : self .sessions },
378- default = report_default ,
379- option = orjson_option ,
380- ).decode (),
381- )
382-
383331 def serialize (self , with_totals = True ) -> tuple [bytes , bytes , ReportTotals | None ]:
332+ """
333+ Serializes a report as `(report_json, chunks, totals)`.
334+
335+ The `totals` is either a `ReportTotals`, or `None`, depending on the `with_totals` flag.
336+ """
384337 return serialize_report (self , with_totals )
385338
386339 @sentry_sdk .trace
@@ -584,10 +537,6 @@ def get_uploaded_flags(self):
584537 flags .update (sess .flags )
585538 return flags
586539
587- @sentry_sdk .trace
588- def repack (self ):
589- pass
590-
591540 def delete_labels (
592541 self , sessionids : list [int ] | set [int ], labels_to_delete : list [int ] | set [int ]
593542 ):
0 commit comments