2424from rest_framework import serializers
2525
2626from application .models import Chat , Application , ChatRecord
27- from common .db .search import get_dynamics_model , native_search , native_page_search
27+ from common .db .search import get_dynamics_model , native_search , native_page_search , native_page_handler
2828from common .exception .app_exception import AppApiException
2929from common .utils .common import get_file_content
3030from maxkb .conf import PROJECT_DIR
@@ -95,7 +95,8 @@ def get_query_set(self, select_ids=None):
9595 'trample_num' : models .IntegerField (),
9696 'comparer' : models .CharField (),
9797 'application_chat.update_time' : models .DateTimeField (),
98- 'application_chat.id' : models .UUIDField (), }))
98+ 'application_chat.id' : models .UUIDField (),
99+ 'application_chat_record_temp.id' : models .UUIDField ()}))
99100
100101 base_query_dict = {'application_chat.application_id' : self .data .get ("application_id" ),
101102 'application_chat.update_time__gte' : start_time ,
@@ -106,7 +107,6 @@ def get_query_set(self, select_ids=None):
106107 if 'username' in self .data and self .data .get ('username' ) is not None :
107108 base_query_dict ['application_chat.asker__username__icontains' ] = self .data .get ('username' )
108109
109-
110110 if select_ids is not None and len (select_ids ) > 0 :
111111 base_query_dict ['application_chat.id__in' ] = select_ids
112112 base_condition = Q (** base_query_dict )
@@ -180,25 +180,26 @@ def to_row(row: Dict):
180180 str (row .get ('create_time' ).astimezone (pytz .timezone (TIME_ZONE )).strftime ('%Y-%m-%d %H:%M:%S' )
181181 if row .get ('create_time' ) is not None else None )]
182182
183+ @staticmethod
184+ def reset_value (value ):
185+ if isinstance (value , str ):
186+ value = re .sub (ILLEGAL_CHARACTERS_RE , '' , value )
187+ if isinstance (value , datetime .datetime ):
188+ eastern = pytz .timezone (TIME_ZONE )
189+ c = datetime .timezone (eastern ._utcoffset )
190+ value = value .astimezone (c )
191+ return value
192+
183193 def export (self , data , with_valid = True ):
184194 if with_valid :
185195 self .is_valid (raise_exception = True )
186196 ApplicationChatRecordExportRequest (data = data ).is_valid (raise_exception = True )
187197
188- data_list = native_search (self .get_query_set (data .get ('select_ids' )),
189- select_string = get_file_content (
190- os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' ,
191- ('export_application_chat_ee.sql' if ['PE' , 'EE' ].__contains__ (
192- edition ) else 'export_application_chat.sql' ))),
193- with_table_name = False )
194-
195- batch_size = 500
196-
197198 def stream_response ():
198- workbook = openpyxl .Workbook ()
199- worksheet = workbook .active
200- worksheet . title = 'Sheet1'
201-
199+ workbook = openpyxl .Workbook (write_only = True )
200+ worksheet = workbook .create_sheet ( title = 'Sheet1' )
201+ current_page = 1
202+ page_size = 500
202203 headers = [gettext ('Conversation ID' ), gettext ('summary' ), gettext ('User Questions' ),
203204 gettext ('Problem after optimization' ),
204205 gettext ('answer' ), gettext ('User feedback' ),
@@ -207,24 +208,22 @@ def stream_response():
207208 gettext ('Annotation' ), gettext ('USER' ), gettext ('Consuming tokens' ),
208209 gettext ('Time consumed (s)' ),
209210 gettext ('Question Time' )]
210- for col_idx , header in enumerate (headers , 1 ):
211- cell = worksheet .cell (row = 1 , column = col_idx )
212- cell .value = header
213-
214- for i in range (0 , len (data_list ), batch_size ):
215- batch_data = data_list [i :i + batch_size ]
216-
217- for row_idx , row in enumerate (batch_data , start = i + 2 ):
218- for col_idx , value in enumerate (self .to_row (row ), 1 ):
219- cell = worksheet .cell (row = row_idx , column = col_idx )
220- if isinstance (value , str ):
221- value = re .sub (ILLEGAL_CHARACTERS_RE , '' , value )
222- if isinstance (value , datetime .datetime ):
223- eastern = pytz .timezone (TIME_ZONE )
224- c = datetime .timezone (eastern ._utcoffset )
225- value = value .astimezone (c )
226- cell .value = value
227-
211+ worksheet .append (headers )
212+ for data_list in native_page_handler (page_size , self .get_query_set (data .get ('select_ids' )),
213+ primary_key = 'application_chat_record_temp.id' ,
214+ primary_queryset = 'default_queryset' ,
215+ get_primary_value = lambda item : item .get ('id' ),
216+ select_string = get_file_content (
217+ os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' ,
218+ ('export_application_chat_ee.sql' if ['PE' ,
219+ 'EE' ].__contains__ (
220+ edition ) else 'export_application_chat.sql' ))),
221+ with_table_name = False ):
222+
223+ for item in data_list :
224+ row = [self .reset_value (v ) for v in self .to_row (item )]
225+ worksheet .append (row )
226+ current_page = current_page + 1
228227 output = BytesIO ()
229228 workbook .save (output )
230229 output .seek (0 )
0 commit comments