ReportCollectorTrainer.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <div class="report-trainer admin-page">
  3. <el-row :gutter="16">
  4. <!-- 左侧:聚类列表 -->
  5. <el-col :span="8">
  6. <el-card>
  7. <div slot="header">
  8. <span>报告采集训练</span>
  9. <el-select v-model="statusFilter" size="small" style="width:120px;float:right" @change="fetchClusters">
  10. <el-option label="全部" value="" />
  11. <el-option label="收集中" value="collecting" />
  12. <el-option label="可生成" value="ready" />
  13. <el-option label="已生成" value="generated" />
  14. </el-select>
  15. </div>
  16. <div class="adm-table-wrap">
  17. <el-table :data="clusters" v-loading="loading" border stripe height="100%" @row-click="selectCluster">
  18. <el-table-column prop="id" label="ID" width="50">
  19. <template slot="header">
  20. <span class="sort-header" @click.stop="onSortClick('id', $event)">ID{{ sortIcon('id') }}</span>
  21. </template>
  22. </el-table-column>
  23. <el-table-column prop="clusterKey" label="聚类键" width="120" show-overflow-tooltip>
  24. <template slot="header">
  25. <span class="sort-header" @click.stop="onSortClick('clusterKey', $event)">聚类键<span v-if="sortIndex('clusterKey') !== null" class="sort-index" :class="sortClass('clusterKey') === 'sort-asc' ? 'sort-asc-badge' : 'sort-desc-badge'">{{ sortIndex('clusterKey') }}</span>{{ sortIcon('clusterKey') }}</span>
  26. </template>
  27. </el-table-column>
  28. <el-table-column prop="reportCount" label="报告数" width="70">
  29. <template slot="header">
  30. <span class="sort-header" @click.stop="onSortClick('reportCount', $event)">报告数{{ sortIcon('reportCount') }}</span>
  31. </template>
  32. </el-table-column>
  33. <el-table-column label="状态" width="80">
  34. <template slot-scope="{ row }">
  35. <el-tag :type="clusterStatusType(row.status)" size="small">{{ clusterStatusText(row.status) }}</el-tag>
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="操作" width="80" fixed="right">
  39. <template slot-scope="{ row }">
  40. <el-button size="mini" type="text" @click.stop="handleGenerate(row)" v-if="row.status==='ready'">生成</el-button>
  41. </template>
  42. </el-table-column>
  43. </el-table>
  44. </div>
  45. </el-card>
  46. </el-col>
  47. <!-- 右侧:详情面板 -->
  48. <el-col :span="16">
  49. <el-card v-if="currentCluster">
  50. <div slot="header">
  51. <span>{{ currentCluster.clusterKey }}({{ currentCluster.reportCount }} 份报告)</span>
  52. <el-tag :type="clusterStatusType(currentCluster.status)" size="small" style="margin-left:8px">{{ clusterStatusText(currentCluster.status) }}</el-tag>
  53. </div>
  54. <div class="adm-table-wrap">
  55. <el-table :data="uploads" v-loading="uploadLoading" border stripe height="100%" @row-click="selectUpload">
  56. <el-table-column prop="id" label="ID" width="50" />
  57. <el-table-column label="文件" min-width="200">
  58. <template slot-scope="{ row }">
  59. <a :href="row.fileUrl" target="_blank">{{ row.fileUrl }}</a>
  60. </template>
  61. </el-table-column>
  62. <el-table-column prop="uploadedAt" label="上传时间" width="170">
  63. <template slot-scope="{ row }">{{ formatTime(row.uploadedAt) }}</template>
  64. </el-table-column>
  65. <el-table-column label="标注" width="100">
  66. <template slot-scope="{ row }">
  67. <el-tag v-if="row.annotation" size="small" type="success">已标注</el-tag>
  68. <el-tag v-else size="small" type="info">未标注</el-tag>
  69. </template>
  70. </el-table-column>
  71. </el-card>
  72. <el-card v-else>
  73. <div style="text-align:center;padding:80px;color:#bbb">
  74. <i class="el-icon-document" style="font-size:48px"></i>
  75. <p style="margin-top:16px">请从左侧选择一个聚类</p>
  76. </div>
  77. </el-card>
  78. </el-col>
  79. </el-row>
  80. <!-- 标注对话框 -->
  81. <el-dialog :title="'标注报告 #' + (selectedUpload ? selectedUpload.id : '')" :visible.sync="annotateVisible" width="800px" :close-on-click-modal="false">
  82. <div v-loading="annotateLoading">
  83. <el-descriptions :column="2" border size="small" v-if="selectedUpload">
  84. <el-descriptions-item label="文件">{{ selectedUpload.fileUrl }}</el-descriptions-item>
  85. <el-descriptions-item label="上传时间">{{ formatTime(selectedUpload.uploadedAt) }}</el-descriptions-item>
  86. </el-descriptions>
  87. <el-divider content-position="left">LLM 解析结果</el-divider>
  88. <pre class="llm-result">{{ formatJson(selectedLlmResult) }}</pre>
  89. <el-divider content-position="left">模板预览</el-divider>
  90. <div v-if="previewBlocks.length" class="trainer-preview">
  91. <div v-for="(block, bi) in previewBlocks" :key="bi" class="trainer-block">
  92. <div class="trainer-block-title">{{ block.title }}</div>
  93. <div v-if="block.type === 'score'" class="trainer-score">
  94. <span v-for="item in block.items" :key="item.label" class="trainer-score-item">{{ item.label }}: {{ item.value }}</span>
  95. </div>
  96. <div v-else-if="block.type === 'indicator'" class="trainer-indicator">
  97. <div v-for="(item, ii) in block.items" :key="ii" class="trainer-ind-item">
  98. <span class="t-ind-name">{{ item.name }}</span>
  99. <span class="t-ind-val">{{ item.value }} {{ item.unit }}</span>
  100. </div>
  101. </div>
  102. <div v-else class="trainer-text">{{ block.content || '(空)' }}</div>
  103. </div>
  104. </div>
  105. <div v-else class="empty-hint">暂无模板预览(请先保存该报告类型的模板)</div>
  106. <el-divider content-position="left">手动指定报告类型</el-divider>
  107. <el-form label-width="100px">
  108. <el-form-item label="报告类型">
  109. <el-select v-model="annotateForm.typeId" placeholder="选择类型" style="width:100%" @change="onTypeChange">
  110. <el-option v-for="t in allTypes" :key="t.typeId" :label="t.displayName + ' (' + t.typeId + ')'" :value="t.typeId" />
  111. </el-select>
  112. </el-form-item>
  113. <el-form-item label="确认标注">
  114. <el-switch v-model="annotateForm.confirmed" :active-value="true" :inactive-value="false" />
  115. <span class="hint-text">确认后作为训练数据纳入</span>
  116. </el-form-item>
  117. </el-form>
  118. </div>
  119. <div slot="footer">
  120. <el-button @click="annotateVisible = false">取消</el-button>
  121. <el-button type="primary" :loading="savingAnnotation" @click="saveAnnotation">保存标注</el-button>
  122. </div>
  123. </el-dialog>
  124. </el-table>
  125. </div>
  126. </div></template>
  127. <script>
  128. import { getUnknownClusters, getClusterUploads, generateTypeFromCluster } from '@/api/reportParser'
  129. import { getReportTemplates } from '@/api/reportTemplate'
  130. import SortMixin from '@/mixins/SortMixin'
  131. export default {
  132. name: 'ReportCollectorTrainer',
  133. mixins: [SortMixin],
  134. data() {
  135. return {
  136. loading: false,
  137. uploadLoading: false,
  138. annotateLoading: false,
  139. savingAnnotation: false,
  140. statusFilter: '',
  141. clusters: [],
  142. sortableColumns: { id: 'ID', reportCount: '报告数', clusterKey: '聚类键' },
  143. currentCluster: null,
  144. uploads: [],
  145. selectedUpload: null,
  146. selectedLlmResult: null,
  147. annotateVisible: false,
  148. annotateForm: { typeId: '', confirmed: false },
  149. allTypes: [],
  150. previewBlocks: []
  151. }
  152. },
  153. computed: {
  154. tableHeight() {
  155. return window.innerHeight - 200
  156. }
  157. },
  158. mounted() { this.fetchClusters() },
  159. methods: {
  160. loadList() {
  161. this.fetchClusters()
  162. },
  163. fetchClusters() {
  164. this.loading = true
  165. var params = { page: 1, size: 100, status: this.statusFilter || undefined }
  166. if (this.sortState.length > 0) params.sort = this.sortState
  167. getUnknownClusters(params).then(res => {
  168. this.clusters = res.data && res.data.records ? res.data.records : (res.data || [])
  169. }).catch(() => {}).finally(() => { this.loading = false })
  170. },
  171. fetchAllTypes() {
  172. getReportTemplates({}).then(res => {
  173. this.allTypes = (res.data || []).map(t => ({ typeId: t.reportType, displayName: t.name }))
  174. }).catch(() => {})
  175. },
  176. selectCluster(row) {
  177. this.currentCluster = row
  178. this.uploads = []
  179. this.selectedUpload = null
  180. this.uploadLoading = true
  181. getClusterUploads({ clusterId: row.id }).then(res => {
  182. this.uploads = res.data || []
  183. }).catch(() => {}).finally(() => { this.uploadLoading = false })
  184. },
  185. selectUpload(row) {
  186. this.selectedUpload = row
  187. try {
  188. this.selectedLlmResult = row.llmResult ? JSON.parse(row.llmResult) : {}
  189. } catch (e) {
  190. this.selectedLlmResult = {}
  191. }
  192. this.annotateForm = { typeId: '', confirmed: false }
  193. this.previewBlocks = []
  194. this.annotateVisible = true
  195. this.fetchAllTypes()
  196. },
  197. onTypeChange() {
  198. // 根据选定类型预览模板渲染效果
  199. this.previewBlocks = this.renderPreview(this.annotateForm.typeId, this.selectedLlmResult)
  200. },
  201. renderPreview(reportType, llmResult) {
  202. // 简化的预览渲染
  203. if (!reportType) return []
  204. var blocks = []
  205. var payload = llmResult.payload || llmResult
  206. if (payload.summary) {
  207. var items = []
  208. if (payload.summary.overallScore != null) items.push({ label: '综合', value: payload.summary.overallScore })
  209. if (payload.summary.gutHealthScore != null) items.push({ label: '菌群健康', value: payload.summary.gutHealthScore })
  210. if (items.length) blocks.push({ type: 'score', title: '健康评分', items: items })
  211. }
  212. if (payload.indicators && payload.indicators.length) {
  213. blocks.push({ type: 'indicator', title: '指标详情', items: payload.indicators })
  214. }
  215. return blocks
  216. },
  217. handleGenerate(row) {
  218. this.$confirm('确定由此聚类生成报告类型?', '提示', { type: 'warning' }).then(() => {
  219. generateTypeFromCluster({ clusterId: row.id }).then(res => {
  220. this.$message.success('类型已生成: ' + (res.data && res.data.typeId))
  221. this.fetchClusters()
  222. }).catch(() => { this.$message.error('生成失败') })
  223. }).catch(() => {})
  224. },
  225. saveAnnotation() {
  226. if (!this.selectedUpload) return
  227. this.savingAnnotation = true
  228. var annotation = JSON.stringify({
  229. typeId: this.annotateForm.typeId,
  230. confirmed: this.annotateForm.confirmed,
  231. llmResult: this.selectedLlmResult
  232. })
  233. // 通过 report-parser/unknown-uploads/update 更新标注
  234. this.$http.post('/api/admin/report-parser/unknown-updates/update', {
  235. id: this.selectedUpload.id,
  236. annotation: annotation
  237. }).then(res => {
  238. if (res.code === 200) {
  239. this.$message.success('标注已保存')
  240. this.annotateVisible = false
  241. this.selectCluster(this.currentCluster)
  242. } else {
  243. this.$message.error(res.message || '保存失败')
  244. }
  245. }).catch(() => { this.$message.error('保存失败') }).finally(() => { this.savingAnnotation = false })
  246. },
  247. clusterStatusType(s) {
  248. if (s === 'ready') return 'warning'
  249. if (s === 'generated' || s === 'imported') return 'success'
  250. return 'info'
  251. },
  252. clusterStatusText(s) {
  253. var map = { collecting: '收集中', ready: '可生成', generated: '已生成', imported: '已导入' }
  254. return map[s] || s
  255. },
  256. formatJson(str) {
  257. if (!str) return ''
  258. try { return JSON.stringify(JSON.parse(str), null, 2) } catch (e) { return str }
  259. },
  260. }
  261. }
  262. </script>
  263. <style scoped>
  264. .report-trainer { padding: 16px; }
  265. .llm-result { background: #f5f7fa; padding: 12px; border-radius: 4px; font-size: 13px; max-height: 300px; overflow: auto; white-space: pre-wrap; }
  266. .hint-text { margin-left: 8px; color: #999; font-size: 12px; }
  267. .trainer-preview { border: 1px solid #ebeef5; border-radius: 8px; padding: 12px; margin-top: 8px; }
  268. .trainer-block { margin-bottom: 12px; }
  269. .trainer-block-title { font-weight: bold; font-size: 14px; color: #303133; margin-bottom: 6px; }
  270. .trainer-score { display: flex; flex-wrap: wrap; gap: 8px; }
  271. .trainer-score-item { background: #f0f9ff; padding: 4px 12px; border-radius: 4px; font-size: 13px; }
  272. .trainer-indicator { max-height: 200px; overflow-y: auto; }
  273. .trainer-ind-item { display: flex; padding: 4px 0; border-bottom: 1px solid #f0f0f0; font-size: 13px; }
  274. .t-ind-name { flex: 1; color: #333; }
  275. .t-ind-val { color: #666; margin-left: 12px; }
  276. .trainer-text { font-size: 13px; color: #555; line-height: 1.6; }
  277. .empty-hint { color: #bbb; text-align: center; padding: 20px; }
  278. .sort-header {
  279. cursor: pointer;
  280. user-select: none;
  281. font-weight: 500;
  282. font-size: 12px;
  283. }
  284. .sort-header:hover {
  285. color: #409eff;
  286. }
  287. </style>