1111from scan_explorer_service .utils .utils import url_for_proxy
1212import re
1313import io
14- import cProfile
15- import pstats
1614import sys
1715
1816bp_proxy = Blueprint ('proxy' , __name__ , url_prefix = '/image' )
@@ -29,14 +27,8 @@ def image_proxy(path):
2927 req_headers ['X-Forwarded-Path' ] = current_app .config .get ('PROXY_PREFIX' ).rstrip ('/' ) + '/image'
3028
3129 encoded_url = re .sub (r"[+&]" , "%2B" , req_url )
32-
33- current_app .logger .debug (f'req_url: { encoded_url } , params: { request .args } , headers: { req_headers } , data: { request .form } ' )
34-
3530 r = requests .request (request .method , encoded_url , params = request .args , stream = True ,
36- headers = req_headers , allow_redirects = False , data = request .form )
37-
38- current_app .logger .debug (f"Response status code: { r .status_code } " )
39-
31+ headers = req_headers , allow_redirects = False , data = request .form )
4032 excluded_headers = ['content-encoding' ,'content-length' , 'transfer-encoding' , 'connection' ]
4133 headers = [(name , value ) for (name , value ) in r .headers .items () if name .lower () not in excluded_headers ]
4234
@@ -53,23 +45,15 @@ def image_proxy_thumbnail():
5345 """Helper to generate the correct url for a thumbnail given an ID and type"""
5446 try :
5547 id = request .args .get ('id' ).replace (" " , "+" )
56- current_app .logger .debug (f'id { id } ' )
5748 type = request .args .get ('type' )
58- current_app .logger .debug (f'type { type } ' )
5949
6050 with current_app .session_scope () as session :
61- thumbnail_path = item_thumbnail (session , id , type )
62-
63- current_app .logger .debug (f'thumbnail path { thumbnail_path } ' )
64-
51+ thumbnail_path = item_thumbnail (session , id , type )
6552 path = urlparse .urlparse (thumbnail_path ).path
66- current_app .logger .debug (f'path { path } ' )
6753
6854 remove = urlparse .urlparse (url_for_proxy ('proxy.image_proxy' , path = '' )).path
69- current_app .logger .debug (f'remove { remove } ' )
7055
7156 path = path .replace (remove , '' )
72- current_app .logger .debug (f'replace { path } ' )
7357
7458 return image_proxy (path )
7559 except Exception as e :
@@ -81,10 +65,8 @@ def get_item(session, id):
8165 session .query (Article ).filter (Article .id == id ).one_or_none ()
8266 or session .query (Collection ).filter (Collection .id == id ).one_or_none ())
8367 if not item :
84- current_app .logger .debug (f'Item with id { id } not found' )
8568 raise Exception ("ID: " + id + " not found" )
8669
87- current_app .logger .debug (f'Item retrieved successfully { item } ' )
8870 return item
8971
9072
@@ -98,7 +80,6 @@ def get_pages(item, session, page_start, page_end, page_limit):
9880 query = session .query (Page ).filter (Page .collection_id == item .id ,
9981 Page .volume_running_page_num >= page_start ,
10082 Page .volume_running_page_num <= page_end ).order_by (Page .volume_running_page_num ).limit (page_limit )
101- current_app .logger .info (f"Got pages { page_start } -{ page_end } : { query } " )
10283 return query
10384
10485
@@ -112,7 +93,6 @@ def fetch_images(session, item, page_start, page_end, page_limit, memory_limit):
11293 n_pages += 1
11394
11495 current_app .logger .debug (f"Generating image for page: { n_pages } " )
115- current_app .logger .debug (f'Id: { page .id } , Volume_page: { page .volume_running_page_num } , memory: { memory_sum } ' )
11696 if n_pages > page_limit :
11797 break
11898 if memory_sum > memory_limit :
@@ -122,37 +102,28 @@ def fetch_images(session, item, page_start, page_end, page_limit, memory_limit):
122102 object_name = '/' .join (image_path )
123103 object_name += format
124104
125- current_app .logger .debug (f"Image path: { object_name } " )
126105 im_data = fetch_object (object_name , 'AWS_BUCKET_NAME_IMAGE' )
127106 memory_sum += sys .getsizeof (im_data )
128107
129108 yield im_data
130109
131110
132111def fetch_object (object_name , bucket_name ):
133- current_app .logger .debug (f"Using bucket: { bucket_name } " )
134112 file_content = S3Provider (current_app .config , bucket_name ).read_object_s3 (object_name )
135- current_app .logger .debug (f"File content type: { type (file_content )} , length: { len (file_content ) if file_content else 'None' } " )
136113 if not file_content :
137114 current_app .logger .error (f"Failed to fetch content for { object_name } . File might be empty." )
138115 raise ValueError (f"File content is empty for { object_name } " )
139- current_app .logger .debug (f"Successfully fetched object from S3 bucket: { object_name } " )
140116
141117 return file_content
142118
143119
144120def fetch_article (item , memory_limit ):
145121 try :
146- current_app .logger .debug (f"Item is an article: { item .id } " )
147-
148122 object_name = f'{ item .id } .pdf' .lower ()
149- current_app .logger .debug (f"object name: { object_name } " )
150123
151124 full_path = f'pdfs/{ object_name } '
152- current_app .logger .debug (f"full path: { full_path } " )
153125
154126 file_content = fetch_object (full_path , 'AWS_BUCKET_NAME_PDF' )
155- current_app .logger .debug (f"File content type in fetch_article: { type (file_content )} , length: { len (file_content ) if file_content else 'None' } " )
156127
157128 if len (file_content ) > memory_limit :
158129 current_app .logger .error (f"Memory limit reached: { len (file_content )} > { memory_limit } " )
@@ -173,8 +144,6 @@ def fetch_article(item, memory_limit):
173144def generate_pdf (item , session , page_start , page_end , page_limit , memory_limit ):
174145 if isinstance (item , Article ):
175146 response = fetch_article (item , memory_limit )
176- current_app .logger .debug (f"Item is an article" )
177- current_app .logger .debug (f"response fetch article: { response } " )
178147 if response :
179148 return response
180149 else :
@@ -189,38 +158,18 @@ def generate_pdf(item, session, page_start, page_end, page_limit, memory_limit):
189158def pdf_save ():
190159 """Generate a PDF from pages"""
191160 try :
192- profiler = cProfile .Profile ()
193- profiler .enable ()
194-
195-
196161 id = request .args .get ('id' )
197162 page_start = request .args .get ('page_start' , 1 , int )
198163 page_end = request .args .get ('page_end' , math .inf , int )
199164 memory_limit = current_app .config .get ("IMAGE_PDF_MEMORY_LIMIT" )
200165 page_limit = current_app .config .get ("IMAGE_PDF_PAGE_LIMIT" )
201166
202- current_app .logger .debug (f"pdf ID: { id } , page_start: { page_start } , page_end: { page_end } , memory_limit: { memory_limit } , page_limit: { page_limit } " )
203-
204167 with current_app .session_scope () as session :
205168
206169 item = get_item (session , id )
207170 current_app .logger .debug (f"Item retrieved successfully: { item .id } " )
208171
209172 response = generate_pdf (item , session , page_start , page_end , page_limit , memory_limit )
210- current_app .logger .debug (f"Response pdf save: { response } " )
211-
212- profiler .disable ()
213-
214- # Log the profiling information
215- log_buffer = io .StringIO ()
216- profiler_stats = pstats .Stats (profiler , stream = log_buffer )
217- profiler_stats .strip_dirs ().sort_stats ('cumulative' , 'calls' ).print_stats (20 )
218-
219- formatted_stats = log_buffer .getvalue ().splitlines ()
220-
221- current_app .logger .debug (f'==================Profiling information========================: \n ' )
222- for line in formatted_stats :
223- current_app .logger .debug (line )
224173
225174 return response
226175 except Exception as e :
0 commit comments