ReportCollectorTrainer.vue 13 KB

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