1414from functools import reduce
1515from typing import Dict
1616
17+ import xlwt
1718from django .core import validators
18- from django .core .cache import cache , caches
19+ from django .core .cache import caches
1920from django .db import transaction , models
2021from django .db .models import QuerySet , Q
22+ from django .http import HttpResponse
2123from rest_framework import serializers
2224
2325from application .models import Chat , Application , ApplicationDatasetMapping , VoteChoices , ChatRecord
@@ -73,16 +75,17 @@ def get_end_time(self):
7375 def get_query_set (self ):
7476 end_time = self .get_end_time ()
7577 query_set = QuerySet (model = get_dynamics_model (
76- {'application_id' : models .CharField (),
77- 'abstract' : models .CharField (),
78+ {'application_chat. application_id' : models .CharField (),
79+ 'application_chat. abstract' : models .CharField (),
7880 "star_num" : models .IntegerField (),
7981 'trample_num' : models .IntegerField (),
8082 'comparer' : models .CharField (),
81- 'create_time' : models .DateTimeField ()}))
83+ 'application_chat. create_time' : models .DateTimeField ()}))
8284
83- base_query_dict = {'application_id' : self .data .get ("application_id" ), 'create_time__gte' : end_time }
85+ base_query_dict = {'application_chat.application_id' : self .data .get ("application_id" ),
86+ 'application_chat.create_time__gte' : end_time }
8487 if 'abstract' in self .data and self .data .get ('abstract' ) is not None :
85- base_query_dict ['abstract__contains' ] = self .data .get ('abstract' )
88+ base_query_dict ['application_chat. abstract__contains' ] = self .data .get ('abstract' )
8689 base_condition = Q (** base_query_dict )
8790 min_star_query = None
8891 min_trample_query = None
@@ -102,7 +105,7 @@ def get_query_set(self):
102105 condition = base_condition & min_trample_query
103106 else :
104107 condition = base_condition
105- return query_set .filter (condition ).order_by ("-create_time" )
108+ return query_set .filter (condition ).order_by ("-application_chat. create_time" )
106109
107110 def list (self , with_valid = True ):
108111 if with_valid :
@@ -111,6 +114,54 @@ def list(self, with_valid=True):
111114 os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' , 'list_application_chat.sql' )),
112115 with_table_name = False )
113116
117+ @staticmethod
118+ def to_row (row : Dict ):
119+ details = row .get ('details' )
120+ padding_problem_text = details .get ('problem_padding' ).get (
121+ 'padding_problem_text' ) if 'problem_padding' in details and 'padding_problem_text' in details .get (
122+ 'problem_padding' ) else ""
123+ paragraph_list = details .get ('search_step' ).get (
124+ 'paragraph_list' ) if 'search_step' in details and 'paragraph_list' in details .get ('search_step' ) else []
125+ improve_paragraph_list = row .get ('improve_paragraph_list' )
126+ vote_status_map = {'-1' : '未投票' , '0' : '赞同' , '1' : '反对' }
127+ return [str (row .get ('chat_id' )), row .get ('abstract' ), row .get ('problem_text' ), padding_problem_text ,
128+ row .get ('answer_text' ), vote_status_map .get (row .get ('vote_status' )), len (paragraph_list ), "\n " .join (
129+ [f"{ index } 、{ paragraph_list [index ].get ('title' )} \n { paragraph_list [index ].get ('content' )} " for index
130+ in
131+ range (len (paragraph_list ))]),
132+ "\n " .join ([
133+ f"{ improve_paragraph_list [index ].get ('title' )} \n { improve_paragraph_list [index ].get ('content' )} "
134+ for index in range (len (improve_paragraph_list ))]),
135+ row .get ('message_tokens' ) + row .get ('answer_tokens' ), row .get ('run_time' ),
136+ str (row .get ('create_time' ))]
137+
138+ def export (self , with_valid = True ):
139+ if with_valid :
140+ self .is_valid (raise_exception = True )
141+ data_list = native_search (self .get_query_set (), select_string = get_file_content (
142+ os .path .join (PROJECT_DIR , "apps" , "application" , 'sql' , 'export_application_chat.sql' )),
143+ with_table_name = False )
144+
145+ # 创建工作簿对象
146+ workbook = xlwt .Workbook (encoding = 'utf-8' )
147+ # 添加工作表
148+ worksheet = workbook .add_sheet ('Sheet1' )
149+ data = [
150+ ['会话ID' , '摘要' , '用户问题' , '优化后问题' , '回答' , '用户反馈' , '引用分段数' , '分段标题+内容' ,
151+ '标注' , '消耗tokens' , '耗时(s)' , '提问时间' ],
152+ * [self .to_row (row ) for row in data_list ]
153+ ]
154+ # 写入数据到工作表
155+ for row_idx , row in enumerate (data ):
156+ for col_idx , col in enumerate (row ):
157+ worksheet .write (row_idx , col_idx , col )
158+ # 创建HttpResponse对象返回Excel文件
159+ response = HttpResponse (content_type = 'application/vnd.ms-excel' )
160+ response ['Content-Disposition' ] = 'attachment; filename="data.xls"'
161+
162+ workbook .save (response )
163+ return response
164+
114165 def page (self , current_page : int , page_size : int , with_valid = True ):
115166 if with_valid :
116167 self .is_valid (raise_exception = True )
0 commit comments