88from .. import db , config
99
1010
11- def legacy_fix (type_ , row ):
12- session_id = str (row ['CommitteeSessionID' ])
13- document_session_id = str (row ['DocumentCommitteeSessionID' ])
14- ext = 'txt' if type_ == 'text' else 'csv'
15- basepath = os .path .join (
16- config .KNESSET_PIPELINES_DATA_PATH ,
17- 'committees' , f'meeting_protocols_{ type_ } ' , 'files' ,
18- )
19- legacy_file = os .path .join (
20- basepath , session_id [0 ], session_id [1 ], f'{ session_id } .{ ext } '
21- )
22- legacy_hash_file = f'{ legacy_file } .hash'
23- legacy_hash_retry_file = f'{ legacy_hash_file } .retry'
24- new_file = os .path .join (
25- basepath , document_session_id [0 ], document_session_id [1 ], f'{ document_session_id } .{ ext } '
26- )
27- new_hash_file = f'{ new_file } .hash'
28- new_relfile = os .path .join (
29- 'files' , document_session_id [0 ], document_session_id [1 ], f'{ document_session_id } .{ ext } '
30- )
31- if os .path .exists (legacy_file ):
32- row [f'{ type_ } _parsed_filename' ] = new_relfile
33- os .makedirs (os .path .dirname (new_file ), exist_ok = True )
34- shutil .move (legacy_file , new_file )
35- if os .path .exists (legacy_hash_file ):
36- shutil .move (legacy_hash_file , new_hash_file )
37- if os .path .exists (legacy_hash_file ):
38- os .remove (legacy_hash_file )
39- if os .path .exists (legacy_hash_retry_file ):
40- os .remove (legacy_hash_retry_file )
41-
42-
43- def parse_retry (type_ , error , document_session_id , retry_report ):
11+ def parse_retry_error (type_ , error , document_session_id , group_type_id , application_desc , stats ):
4412 document_session_id = str (document_session_id )
13+ group_type_id = str (group_type_id )
14+ application_desc = str (application_desc )
4515 if error :
4616 ext = 'txt' if type_ == 'text' else 'csv'
47- hash_file = os .path .join (
17+ download_filepath = os .path .join (
18+ config .KNESSET_PIPELINES_DATA_PATH ,
19+ 'committees' , 'download_document_committee_session' , 'files' , group_type_id ,
20+ document_session_id [0 ], document_session_id [1 ], f'{ document_session_id } .{ application_desc } '
21+ )
22+ parsed_filepath = os .path .join (
4823 config .KNESSET_PIPELINES_DATA_PATH ,
4924 'committees' , f'meeting_protocols_{ type_ } ' , 'files' ,
5025 document_session_id [0 ], document_session_id [1 ], f'{ document_session_id } .{ ext } .hash'
5126 )
52- if os .path .exists (hash_file ):
53- hash_retry_file = f'{ hash_file } .retry'
54- if os .path .exists (hash_retry_file ):
55- with open (hash_retry_file ) as f :
56- retry = int (f .read ().strip ())
57- else :
58- retry = 0
59- retry += 1
60- if retry > 10 :
61- print (f'document session { document_session_id } { type_ } exceeded max retries' )
62- retry_report .append (f'session { document_session_id } { type_ } exceeded max retries' )
63- else :
64- with open (hash_retry_file , 'w' ) as f :
65- f .write (str (retry ))
66- os .remove (hash_file )
67- print (f'document session { document_session_id } { type_ } retry { retry } ' )
27+ hash_filepath = f'{ parsed_filepath } .hash'
28+ retry_filepath = f'{ parsed_filepath } .retry'
29+ if os .path .exists (retry_filepath ):
30+ with open (retry_filepath , 'r' ) as f :
31+ retry_num = int (f .read ().strip ())
32+ else :
33+ retry_num = 0
34+ if retry_num >= 10 :
35+ print (f'parse_retry_error({ type_ } ): document session id { document_session_id } exceeded max retries' )
36+ stats [f'parse_retry_error({ type_ } ): exceeded max retries' ] += 1
37+ else :
38+ retry_num += 1
39+ print (f'parse_retry_error({ type_ } ): document session id { document_session_id } retry { retry_num } ' )
40+ stats [f'parse_retry_error({ type_ } ): will retry' ] += 1
41+ for filepath in [download_filepath , parsed_filepath , hash_filepath ]:
42+ if os .path .exists (filepath ):
43+ os .remove (filepath )
44+ with open (retry_filepath , 'w' ) as f :
45+ f .write (str (retry_num ))
6846
6947
7048def update_row_stats (row , stats ):
@@ -89,7 +67,6 @@ def update_row_stats(row, stats):
8967
9068def process_rows (rows ):
9169 stats = defaultdict (int )
92- retry_report = []
9370 protocol_session_rows = {}
9471 for row in rows :
9572 stats ['total_rows' ] += 1
@@ -102,17 +79,15 @@ def process_rows(rows):
10279 else :
10380 stats ['protocol_rows' ] += 1
10481 update_row_stats (row , stats )
105- # parse_retry ('text', row['text_error'], row['DocumentCommitteeSessionID'], retry_report )
106- # parse_retry ('parts', row['parts_error'], row['DocumentCommitteeSessionID'], retry_report )
82+ parse_retry_error ('text' , row ['text_error' ], row ['DocumentCommitteeSessionID' ], row [ 'GroupTypeID' ], row [ 'ApplicationDesc' ], stats )
83+ parse_retry_error ('parts' , row ['parts_error' ], row ['DocumentCommitteeSessionID' ], row [ 'GroupTypeID' ], row [ 'ApplicationDesc' ], stats )
10784 protocol_session_rows .setdefault (row ['CommitteeSessionID' ], []).append (row )
10885 for session_id , rows in protocol_session_rows .items ():
10986 if len (rows ) > 1 :
11087 good_rows = [row for row in rows if row ['text_filesize' ] > 0 ]
11188 if len (good_rows ) > 0 :
11289 rows = good_rows
11390 row = rows [0 ]
114- # legacy_fix('text', row)
115- # legacy_fix('parts', row)
11691 stats ['yielded_protocol_rows' ] += 1
11792 yield row
11893 for k , v in stats .items ():
0 commit comments