App.vue 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. <script setup lang="ts">
  2. import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
  3. import { ElMessageBox } from 'element-plus'
  4. import { createAnnotation, deleteAnnotation, deletePretrain, fetchAnnotationConfig, fetchAnnotationsByIds, fetchPeriodDetail, fetchPretrainStatus, fetchQueryOptions, fetchTimePoints, fetchTspluseRuler, fetchWaveWindow, getToken, login as apiLogin, logout as apiLogout, recordPretrain } from './api'
  5. import PeriodModal from './components/PeriodModal.vue'
  6. import TimePointStrip from './components/TimePointStrip.vue'
  7. import WaveChart from './components/WaveChart.vue'
  8. import { STOPPED_COLOR, statusGradientColor } from './statusColor'
  9. import { MEASUREMENT_TYPES, type Annotation, type AnnotationLabel, type Cycle, type MeasurementType, type PeriodDetail, type QueryOption, type TimePoint, type QueryOptionsResponse, type WaveWindowFile, type WaveWindowResponse } from './types'
  10. const queryMeta = ref<QueryOptionsResponse | null>(null)
  11. const selectedPointName = ref('')
  12. const selectedTypes = ref<MeasurementType[]>(['压力'])
  13. const minTime = ref('')
  14. const maxTime = ref('')
  15. const windowSize = ref(4)
  16. const maxPoints = ref(200000)
  17. const noSampling = ref(false)
  18. const includeStopped = ref(false)
  19. const abnormalOnly = ref(false)
  20. const noCycleOnly = ref(false)
  21. const minStatus = ref<number | null>(null)
  22. const ruler = ref<{ min: number; max: number } | null>(null)
  23. const timePoints = ref<TimePoint[]>([])
  24. const referencePoints = ref<TimePoint[]>([])
  25. const startIndex = ref(0)
  26. const waveData = ref<WaveWindowResponse | null>(null)
  27. const chartMode = ref<'split' | 'merge'>('split')
  28. const chartDirty = ref(false)
  29. const queryLoading = ref(false)
  30. const timePointsLoading = ref(false)
  31. const waveLoading = ref(false)
  32. const errorMessage = ref('')
  33. const initialized = ref(false)
  34. const periodDetail = ref<PeriodDetail | null>(null)
  35. const periodVisible = ref(false)
  36. const periodLoading = ref(false)
  37. const annotationWidth = ref(10)
  38. const annotations = ref<Annotation[]>([])
  39. const annotationLoading = ref(false)
  40. const loggedIn = ref(false)
  41. const loginLoading = ref(false)
  42. const loginError = ref('')
  43. const loginForm = reactive({ username: '', password: '' })
  44. const waveChartRef = ref<InstanceType<typeof WaveChart> | null>(null)
  45. const recordedFileIds = ref<number[]>([])
  46. const recordingFileId = ref<number | null>(null)
  47. let queryDebounce: ReturnType<typeof setTimeout> | undefined
  48. let waveRequestId = 0
  49. const pointNames = computed(() => queryMeta.value?.pointNames ?? [])
  50. const pointCounts = computed<Record<string, { pre: number | null; ab: number | null }>>(() => {
  51. const map: Record<string, { pre: number | null; ab: number | null }> = {}
  52. for (const name of pointNames.value) {
  53. map[name] = {
  54. pre: queryMeta.value?.pretrainCounts?.[name] ?? null,
  55. ab: queryMeta.value?.abnormalCounts?.[name] ?? null,
  56. }
  57. }
  58. return map
  59. })
  60. function pointLabel(pointName: string): string {
  61. const { pre, ab } = pointCounts.value[pointName] ?? { pre: null, ab: null }
  62. const inner: string[] = []
  63. if (pre != null) inner.push(String(pre))
  64. if (ab != null) inner.push(String(ab))
  65. return inner.length ? `${pointName} (${inner.join(' · ')})` : pointName
  66. }
  67. const selectedOptionRows = computed<QueryOption[]>(() => (
  68. queryMeta.value?.options.filter((row) => (
  69. row.pointName === selectedPointName.value && selectedTypes.value.includes(row.measurementType)
  70. )) ?? []
  71. ))
  72. const selectedWindowPoints = computed(() => {
  73. const slice = timePoints.value.slice(startIndex.value, startIndex.value + stripWindowSize.value)
  74. return hasReference.value ? [...referencePoints.value, ...slice] : slice
  75. })
  76. const hasReference = computed(() => referencePoints.value.length > 0)
  77. const stripWindowSize = computed(() => Math.max(0, windowSize.value - (hasReference.value ? 1 : 0)))
  78. const maxStart = computed(() => Math.max(0, timePoints.value.length - stripWindowSize.value))
  79. const endIndex = computed(() => Math.min(timePoints.value.length, startIndex.value + stripWindowSize.value))
  80. const statusFilter = computed(() => {
  81. const list: string[] = []
  82. if (abnormalOnly.value) list.push('abnormal')
  83. if (noCycleOnly.value) list.push('no_cycle')
  84. return list
  85. })
  86. const currentCycles = computed(() => waveData.value?.cycles ?? [])
  87. const currentSource = computed(() => waveData.value?.source ?? timePointsSource.value)
  88. const timePointsSource = ref<'database' | 'demo'>('demo')
  89. const availableTypeCount = computed(() => selectedOptionRows.value.reduce((total, row) => total + row.fileCount, 0))
  90. const annotatedFileIds = computed(() => [...new Set(annotations.value.map((item) => item.waveFileId))])
  91. function toInputTime(value: string) {
  92. if (!value) return ''
  93. return value.replace('T', ' ').slice(0, 19)
  94. }
  95. function toApiTime(value: string) {
  96. return value ? value.replace('T', ' ') + (value.length === 16 ? ':00' : '') : undefined
  97. }
  98. function boundsForSelection() {
  99. const rows = selectedOptionRows.value
  100. if (!rows.length) return null
  101. const min = rows.reduce((latest, row) => (row.minTime > latest ? row.minTime : latest), rows[0].minTime)
  102. const max = rows.reduce((earliest, row) => (row.maxTime < earliest ? row.maxTime : earliest), rows[0].maxTime)
  103. return { min: toInputTime(min), max: toInputTime(max) }
  104. }
  105. function syncTimeBounds(force = false) {
  106. const bounds = boundsForSelection()
  107. if (!bounds) return
  108. if (force || !minTime.value || minTime.value < bounds.min || minTime.value > bounds.max) minTime.value = bounds.min
  109. if (force || !maxTime.value || maxTime.value > bounds.max || maxTime.value < bounds.min) maxTime.value = bounds.max
  110. if (minTime.value > maxTime.value) {
  111. minTime.value = bounds.min
  112. maxTime.value = bounds.max
  113. }
  114. }
  115. function formatDateTime(value: string | undefined) {
  116. if (!value) return '未选择'
  117. return value.replace('T', ' ').slice(0, 16)
  118. }
  119. function statusText() {
  120. if (timePointsLoading.value) return '正在读取时间点'
  121. if (waveLoading.value) return '正在整理波形'
  122. if (!timePoints.value.length) return '等待查询'
  123. return '窗口已就绪'
  124. }
  125. async function loadOptions() {
  126. queryLoading.value = true
  127. errorMessage.value = ''
  128. try {
  129. const result = await fetchQueryOptions()
  130. queryMeta.value = result
  131. selectedPointName.value = result.pointNames[0] ?? ''
  132. syncTimeBounds(true)
  133. initialized.value = true
  134. await loadTimePoints()
  135. } catch (error) {
  136. errorMessage.value = error instanceof Error ? error.message : '查询条件读取失败'
  137. } finally {
  138. queryLoading.value = false
  139. }
  140. }
  141. async function loadTspluseRuler() {
  142. try {
  143. ruler.value = await fetchTspluseRuler()
  144. } catch {
  145. ruler.value = null
  146. }
  147. }
  148. function firstRunningIndex(): number {
  149. const index = timePoints.value.findIndex((point) =>
  150. selectedTypes.value.some((type) => (point.files[type]?.rpm ?? 0) > 0),
  151. )
  152. return index >= 0 ? index : 0
  153. }
  154. async function loadTimePoints() {
  155. if (!selectedPointName.value || !selectedTypes.value.length) return
  156. timePointsLoading.value = true
  157. waveData.value = null
  158. errorMessage.value = ''
  159. try {
  160. const result = await fetchTimePoints({
  161. pointName: selectedPointName.value,
  162. measurementTypes: selectedTypes.value,
  163. minTime: toApiTime(minTime.value),
  164. maxTime: toApiTime(maxTime.value),
  165. includeStopped: includeStopped.value,
  166. minStatus: noCycleOnly.value ? null : minStatus.value,
  167. statusFilter: statusFilter.value,
  168. })
  169. timePoints.value = result.points
  170. referencePoints.value = result.referencePoints ?? []
  171. timePointsSource.value = result.source
  172. startIndex.value = Math.max(
  173. 0,
  174. Math.min(firstRunningIndex(), Math.max(0, timePoints.value.length - stripWindowSize.value)),
  175. )
  176. chartDirty.value = true
  177. void loadAnnotations()
  178. } catch (error) {
  179. timePoints.value = []
  180. referencePoints.value = []
  181. errorMessage.value = error instanceof Error ? error.message : '时间点读取失败'
  182. } finally {
  183. timePointsLoading.value = false
  184. }
  185. }
  186. async function loadWaveWindowNow() {
  187. const points = selectedWindowPoints.value
  188. if (!points.length || !selectedPointName.value) {
  189. waveData.value = null
  190. chartDirty.value = false
  191. return
  192. }
  193. const requestId = ++waveRequestId
  194. waveLoading.value = true
  195. try {
  196. const result = await fetchWaveWindow({
  197. pointName: selectedPointName.value,
  198. measurementTypes: selectedTypes.value,
  199. points,
  200. maxPoints: maxPoints.value,
  201. noSampling: noSampling.value,
  202. })
  203. if (requestId === waveRequestId) {
  204. waveData.value = result
  205. chartDirty.value = false
  206. }
  207. } catch (error) {
  208. if (requestId === waveRequestId) errorMessage.value = error instanceof Error ? error.message : '波形读取失败'
  209. } finally {
  210. if (requestId === waveRequestId) waveLoading.value = false
  211. }
  212. }
  213. function markChartDirty() {
  214. chartDirty.value = true
  215. }
  216. function scheduleTimePoints() {
  217. if (!initialized.value) return
  218. if (queryDebounce) clearTimeout(queryDebounce)
  219. queryDebounce = setTimeout(() => void loadTimePoints(), 180)
  220. }
  221. function onPointChange() {
  222. syncTimeBounds(true)
  223. scheduleTimePoints()
  224. }
  225. function onTypesChange() {
  226. if (!selectedTypes.value.length) {
  227. selectedTypes.value = ['压力']
  228. return
  229. }
  230. syncTimeBounds(true)
  231. scheduleTimePoints()
  232. }
  233. function onTimeChange() {
  234. if (minTime.value > maxTime.value) {
  235. errorMessage.value = '开始时间不能晚于结束时间'
  236. return
  237. }
  238. scheduleTimePoints()
  239. }
  240. function onStartIndexChange(value: number) {
  241. startIndex.value = Math.max(0, Math.min(value, maxStart.value))
  242. markChartDirty()
  243. }
  244. function onWindowSizeChange(value: number) {
  245. windowSize.value = Math.max(1, Math.min(200, Math.round(value || 1)))
  246. startIndex.value = Math.min(startIndex.value, maxStart.value)
  247. markChartDirty()
  248. }
  249. function resetWindow() {
  250. startIndex.value = 0
  251. markChartDirty()
  252. }
  253. function pageWindow(direction: -1 | 1) {
  254. const next = startIndex.value + direction * stripWindowSize.value
  255. startIndex.value = Math.max(0, Math.min(next, maxStart.value))
  256. markChartDirty()
  257. }
  258. function jumpToIndex(index: number) {
  259. onStartIndexChange(Number(index))
  260. }
  261. const jumpOptions = computed(() => timePoints.value.map((point, index) => {
  262. const file = point.files['压力']
  263. const text = point.sampleTime.replace('T', ' ')
  264. const label = file && file.status !== undefined ? `${text} (${file.status})` : text
  265. const color = file ? (file.rpm <= 0 ? STOPPED_COLOR : statusGradientColor(file.status, ruler.value)) : undefined
  266. return { value: index, label, color }
  267. }))
  268. function sampleTimeStyle(file: WaveWindowFile) {
  269. if (file.measurementType !== '压力') return undefined
  270. const color = file.rpm <= 0 ? STOPPED_COLOR : statusGradientColor(file.status, ruler.value)
  271. return { color }
  272. }
  273. function refresh() {
  274. void loadOptions()
  275. }
  276. async function openPeriod(cycle: Cycle) {
  277. periodVisible.value = true
  278. periodLoading.value = true
  279. periodDetail.value = null
  280. try {
  281. periodDetail.value = await fetchPeriodDetail(cycle.waveFileId, cycle.periodNo)
  282. } catch (error) {
  283. errorMessage.value = error instanceof Error ? error.message : '周期详情读取失败'
  284. periodVisible.value = false
  285. } finally {
  286. periodLoading.value = false
  287. }
  288. }
  289. function closePeriod() {
  290. periodVisible.value = false
  291. }
  292. async function loadAnnotationConfig() {
  293. try {
  294. const result = await fetchAnnotationConfig()
  295. annotationWidth.value = result.annotationWidth
  296. } catch {
  297. annotationWidth.value = 10
  298. }
  299. }
  300. async function loadAnnotations() {
  301. const ids = [...new Set(
  302. timePoints.value.flatMap((point) => (
  303. selectedTypes.value
  304. .map((type) => point.files[type]?.id)
  305. .filter((id): id is number => id != null)
  306. )),
  307. )]
  308. if (!ids.length) {
  309. annotations.value = []
  310. return
  311. }
  312. try {
  313. const result = await fetchAnnotationsByIds(ids)
  314. annotations.value = result.annotations
  315. } catch {
  316. annotations.value = []
  317. }
  318. }
  319. type AnnotationBlock = {
  320. waveFileId: number
  321. periodStart: number
  322. periodEnd: number
  323. sampleIndexStart: number
  324. sampleIndexEnd: number
  325. }
  326. function annotationBlock(cycle: Cycle): AnnotationBlock | null {
  327. const width = Math.max(1, annotationWidth.value)
  328. const fileCycles = (waveData.value?.cycles ?? []).filter((item) => item.waveFileId === cycle.waveFileId)
  329. const maxPeriod = Math.max(1, ...fileCycles.map((item) => item.periodNo))
  330. let leftBound = 1
  331. let rightBound = maxPeriod
  332. const existing = annotations.value
  333. .filter((item) => item.waveFileId === cycle.waveFileId)
  334. .sort((a, b) => a.periodStart - b.periodStart)
  335. for (const item of existing) {
  336. if (item.periodEnd < cycle.periodNo) {
  337. leftBound = Math.max(leftBound, item.periodEnd + 1)
  338. } else if (item.periodStart > cycle.periodNo) {
  339. rightBound = Math.min(rightBound, item.periodStart - 1)
  340. } else {
  341. return null
  342. }
  343. }
  344. if (leftBound > rightBound) return null
  345. let start = Math.max(leftBound, cycle.periodNo - Math.floor((width - 1) / 2))
  346. let end = Math.min(rightBound, start + width - 1)
  347. start = Math.max(leftBound, end - width + 1)
  348. const startCycle = fileCycles.find((item) => item.periodNo === start)
  349. const endCycle = fileCycles.find((item) => item.periodNo === end)
  350. return {
  351. waveFileId: cycle.waveFileId,
  352. periodStart: start,
  353. periodEnd: end,
  354. sampleIndexStart: startCycle?.startSampleIndex ?? cycle.startSampleIndex,
  355. sampleIndexEnd: endCycle?.endSampleIndex ?? cycle.endSampleIndex,
  356. }
  357. }
  358. async function chooseLabel(block: AnnotationBlock): Promise<AnnotationLabel | null> {
  359. try {
  360. await ElMessageBox.confirm(
  361. `将在周期 ${block.periodStart} — ${block.periodEnd} 创建标注,请选择样本类型`,
  362. '创建标注',
  363. {
  364. confirmButtonText: '正常样本',
  365. cancelButtonText: '异常样本',
  366. distinguishCancelAndClose: true,
  367. type: 'info',
  368. closeOnClickModal: false,
  369. },
  370. )
  371. return '正常'
  372. } catch (action) {
  373. if (action === 'cancel') return '异常'
  374. return null
  375. }
  376. }
  377. async function onAnnotationToggle(cycle: Cycle) {
  378. if (annotationLoading.value) return
  379. const existing = annotations.value.find((item) => (
  380. item.waveFileId === cycle.waveFileId && cycle.periodNo >= item.periodStart && cycle.periodNo <= item.periodEnd
  381. ))
  382. if (existing) {
  383. try {
  384. await ElMessageBox.confirm(
  385. `取消该标注(${existing.label},周期 ${existing.periodStart} — ${existing.periodEnd})?`,
  386. '取消标注',
  387. { confirmButtonText: '取消标注', cancelButtonText: '保留', type: 'warning', closeOnClickModal: false },
  388. )
  389. } catch {
  390. return
  391. }
  392. annotationLoading.value = true
  393. try {
  394. await deleteAnnotation(existing.id)
  395. await loadAnnotations()
  396. } catch (error) {
  397. errorMessage.value = error instanceof Error ? error.message : '标注取消失败'
  398. } finally {
  399. annotationLoading.value = false
  400. }
  401. return
  402. }
  403. const block = annotationBlock(cycle)
  404. if (!block) return
  405. const label = await chooseLabel(block)
  406. if (!label) return
  407. annotationLoading.value = true
  408. try {
  409. await createAnnotation({
  410. wave_file_id: block.waveFileId,
  411. label,
  412. period_start: block.periodStart,
  413. period_end: block.periodEnd,
  414. sample_index_start: block.sampleIndexStart,
  415. sample_index_end: block.sampleIndexEnd,
  416. })
  417. await loadAnnotations()
  418. } catch (error) {
  419. errorMessage.value = error instanceof Error ? error.message : '标注创建失败'
  420. } finally {
  421. annotationLoading.value = false
  422. }
  423. }
  424. watch(selectedPointName, () => {
  425. if (initialized.value) onPointChange()
  426. })
  427. watch(selectedTypes, () => {
  428. if (initialized.value) onTypesChange()
  429. }, { deep: true })
  430. watch([minTime, maxTime], () => {
  431. if (initialized.value) onTimeChange()
  432. })
  433. watch(windowSize, () => {
  434. if (initialized.value) onWindowSizeChange(windowSize.value)
  435. })
  436. watch(noSampling, () => {
  437. if (initialized.value && selectedWindowPoints.value.length) loadWaveWindowNow()
  438. })
  439. watch(includeStopped, () => {
  440. if (initialized.value) scheduleTimePoints()
  441. })
  442. watch(abnormalOnly, () => {
  443. if (initialized.value) scheduleTimePoints()
  444. })
  445. watch(noCycleOnly, () => {
  446. if (initialized.value) scheduleTimePoints()
  447. })
  448. watch(minStatus, () => {
  449. if (initialized.value) scheduleTimePoints()
  450. })
  451. watch(waveData, () => void refreshPretrainStatus(), { deep: false })
  452. async function refreshPretrainStatus() {
  453. const ids = waveData.value?.files.map((file) => file.id) ?? []
  454. if (!ids.length) {
  455. recordedFileIds.value = []
  456. return
  457. }
  458. try {
  459. const result = await fetchPretrainStatus(ids)
  460. recordedFileIds.value = result.recorded
  461. } catch {
  462. recordedFileIds.value = []
  463. }
  464. }
  465. function locateFile(file: WaveWindowFile) {
  466. waveChartRef.value?.locateFile(file.pointIndex)
  467. }
  468. async function recordFile(file: WaveWindowFile) {
  469. if (recordedFileIds.value.includes(file.id) || recordingFileId.value != null) return
  470. recordingFileId.value = file.id
  471. try {
  472. const result = await recordPretrain({
  473. id: file.id,
  474. point_name: file.pointName,
  475. measurement_type: file.measurementType,
  476. rpm: file.rpm,
  477. sample_time: file.sampleTime,
  478. file_name: file.fileName,
  479. })
  480. if (result.recorded || result.already) {
  481. recordedFileIds.value = [...new Set([...recordedFileIds.value, file.id])]
  482. }
  483. } catch (error) {
  484. errorMessage.value = error instanceof Error ? error.message : '记录失败'
  485. } finally {
  486. recordingFileId.value = null
  487. }
  488. }
  489. async function deleteRecordedFile(file: WaveWindowFile) {
  490. if (!recordedFileIds.value.includes(file.id) || recordingFileId.value != null) return
  491. recordingFileId.value = file.id
  492. try {
  493. await deletePretrain(file.id)
  494. recordedFileIds.value = recordedFileIds.value.filter((id) => id !== file.id)
  495. } catch (error) {
  496. errorMessage.value = error instanceof Error ? error.message : '删除失败'
  497. } finally {
  498. recordingFileId.value = null
  499. }
  500. }
  501. async function handleLogin() {
  502. if (!loginForm.username || !loginForm.password) {
  503. loginError.value = '请输入账号和密码'
  504. return
  505. }
  506. loginLoading.value = true
  507. loginError.value = ''
  508. try {
  509. await apiLogin(loginForm.username, loginForm.password)
  510. loggedIn.value = true
  511. loginForm.password = ''
  512. await loadOptions()
  513. void loadTspluseRuler()
  514. void loadAnnotationConfig()
  515. } catch (error) {
  516. loginError.value = error instanceof Error ? error.message : '登录失败'
  517. } finally {
  518. loginLoading.value = false
  519. }
  520. }
  521. async function handleLogout() {
  522. await apiLogout()
  523. loggedIn.value = false
  524. waveData.value = null
  525. timePoints.value = []
  526. annotations.value = []
  527. }
  528. function onAuthExpired() {
  529. loggedIn.value = false
  530. waveData.value = null
  531. timePoints.value = []
  532. annotations.value = []
  533. }
  534. onMounted(() => {
  535. window.addEventListener('auth-expired', onAuthExpired)
  536. loggedIn.value = getToken() != null
  537. if (loggedIn.value) {
  538. void loadOptions()
  539. void loadTspluseRuler()
  540. void loadAnnotationConfig()
  541. }
  542. })
  543. onBeforeUnmount(() => {
  544. window.removeEventListener('auth-expired', onAuthExpired)
  545. if (queryDebounce) clearTimeout(queryDebounce)
  546. })
  547. </script>
  548. <template>
  549. <div v-if="!loggedIn" class="login-shell">
  550. <form class="login-card" @submit.prevent="handleLogin">
  551. <div class="login-brand">
  552. <div class="brand-mark"><span></span><span></span><span></span></div>
  553. <div>
  554. <div class="brand-kicker">COMPRESSOR / WAVE LAB</div>
  555. <h1>故障预测波形工作台</h1>
  556. </div>
  557. </div>
  558. <p class="login-hint">请登录后继续使用</p>
  559. <el-input v-model="loginForm.username" class="login-input" size="large" placeholder="账号" clearable @input="loginError = ''" />
  560. <el-input v-model="loginForm.password" class="login-input" size="large" type="password" show-password placeholder="密码" @keyup.enter="handleLogin" @input="loginError = ''" />
  561. <el-alert v-if="loginError" class="login-error" type="error" :closable="false" show-icon :title="loginError" />
  562. <el-button class="login-button" type="primary" size="large" :loading="loginLoading" native-type="submit">登 录</el-button>
  563. </form>
  564. </div>
  565. <div v-else class="app-shell">
  566. <header class="app-header">
  567. <div class="brand-lockup">
  568. <div class="brand-mark"><span></span><span></span><span></span></div>
  569. <div>
  570. <div class="brand-kicker">COMPRESSOR / WAVE LAB</div>
  571. <h1>故障预测波形工作台</h1>
  572. </div>
  573. </div>
  574. <div class="header-meta">
  575. <span class="live-indicator"><i></i> 浏览模式</span>
  576. <span class="header-date">{{ selectedPointName || '未选择采样点' }}</span>
  577. <el-button class="header-refresh" type="primary" plain :loading="queryLoading" @click="refresh">刷新</el-button>
  578. <el-button class="header-logout" plain @click="handleLogout">退出登录</el-button>
  579. </div>
  580. </header>
  581. <main class="workspace">
  582. <section class="query-panel panel">
  583. <div class="panel-heading compact-heading">
  584. <div>
  585. <h2>查询条件</h2>
  586. </div>
  587. <el-tag class="connection-badge" :type="currentSource === 'database' ? 'success' : 'warning'" effect="plain">
  588. {{ currentSource === 'database' ? '数据库已连接' : '演示数据' }}
  589. </el-tag>
  590. </div>
  591. <div class="query-grid">
  592. <label class="field field-point">
  593. <span class="field-label">机组与部位</span>
  594. <el-select
  595. v-model="selectedPointName"
  596. class="query-control"
  597. size="large"
  598. filterable
  599. :disabled="queryLoading"
  600. placeholder="输入机组、气缸或部位关键词"
  601. filter-placeholder="搜索采样点"
  602. >
  603. <el-option v-for="pointName in pointNames" :key="pointName" :label="pointLabel(pointName)" :value="pointName">
  604. <span>{{ pointName }}</span>
  605. <template v-if="pointCounts[pointName]?.pre !== null || pointCounts[pointName]?.ab !== null">
  606. <span class="point-counts">
  607. (<span v-if="pointCounts[pointName]?.pre !== null">{{ pointCounts[pointName]?.pre }}</span><template v-if="pointCounts[pointName]?.pre !== null && pointCounts[pointName]?.ab !== null"> · </template><span v-if="pointCounts[pointName]?.ab !== null" class="abnormal-count">{{ pointCounts[pointName]?.ab }}</span>)
  608. </span>
  609. </template>
  610. </el-option>
  611. </el-select>
  612. </label>
  613. <div class="field field-types">
  614. <span class="field-label">数据名称</span>
  615. <el-select
  616. v-model="selectedTypes"
  617. class="query-control"
  618. size="large"
  619. multiple
  620. collapse-tags
  621. collapse-tags-tooltip
  622. :max-collapse-tags="2"
  623. placeholder="选择数据名称"
  624. >
  625. <el-option v-for="type in MEASUREMENT_TYPES" :key="type" :label="type" :value="type" />
  626. </el-select>
  627. </div>
  628. <label class="field">
  629. <span class="field-label">开始时间</span>
  630. <el-date-picker
  631. v-model="minTime"
  632. class="query-control"
  633. size="large"
  634. type="datetime"
  635. value-format="YYYY-MM-DD HH:mm:ss"
  636. format="YYYY-MM-DD HH:mm:ss"
  637. :disabled="queryLoading"
  638. :disabled-date="(date: Date) => date < new Date(`${boundsForSelection()?.min ?? '1970-01-01 00:00:00'}`) || date > new Date(`${boundsForSelection()?.max ?? '2999-12-31 23:59:59'}`)"
  639. placeholder="选择开始时间"
  640. />
  641. </label>
  642. <label class="field">
  643. <span class="field-label">结束时间</span>
  644. <el-date-picker
  645. v-model="maxTime"
  646. class="query-control"
  647. size="large"
  648. type="datetime"
  649. value-format="YYYY-MM-DD HH:mm:ss"
  650. format="YYYY-MM-DD HH:mm:ss"
  651. :disabled="queryLoading"
  652. :disabled-date="(date: Date) => date < new Date(`${boundsForSelection()?.min ?? '1970-01-01 00:00:00'}`) || date > new Date(`${boundsForSelection()?.max ?? '2999-12-31 23:59:59'}`)"
  653. placeholder="选择结束时间"
  654. />
  655. </label>
  656. <label class="field window-field">
  657. <span class="field-label">时间窗口 <em>文件数量</em></span>
  658. <el-input-number
  659. v-model="windowSize"
  660. class="query-control window-number"
  661. size="large"
  662. :min="1"
  663. :max="200"
  664. controls-position="right"
  665. :disabled="queryLoading"
  666. />
  667. </label>
  668. <el-button class="query-action query-button" type="primary" size="large" :loading="timePointsLoading" :disabled="!selectedPointName" @click="loadTimePoints">
  669. 查询时间点
  670. </el-button>
  671. <div class="query-row-2">
  672. <label class="field field-check">
  673. <span class="field-label">运行状态</span>
  674. <el-checkbox v-model="includeStopped" class="query-checkbox" size="large">停机</el-checkbox>
  675. <el-checkbox v-model="abnormalOnly" class="query-checkbox" size="large">异常</el-checkbox>
  676. <el-checkbox v-model="noCycleOnly" class="query-checkbox" size="large">无周期</el-checkbox>
  677. </label>
  678. <label class="field field-status">
  679. <span class="field-label">质心距离</span>
  680. <el-input-number
  681. v-model="minStatus"
  682. class="query-control"
  683. size="large"
  684. :min="0"
  685. :max="4"
  686. :step="1"
  687. :step-strictly="true"
  688. controls-position="right"
  689. :disabled="queryLoading || noCycleOnly"
  690. clearable
  691. placeholder="可空"
  692. :title="noCycleOnly ? '勾选无周期时忽略质心距离' : undefined"
  693. />
  694. </label>
  695. </div>
  696. </div>
  697. <el-alert v-if="errorMessage" class="data-alert" type="error" :closable="false" show-icon :title="errorMessage" />
  698. </section>
  699. <section class="selection-panel panel">
  700. <TimePointStrip
  701. :points="timePoints"
  702. :measurement-types="selectedTypes"
  703. :start-index="startIndex"
  704. :window-size="stripWindowSize"
  705. :loading="timePointsLoading"
  706. :min-time="minTime"
  707. :max-time="maxTime"
  708. :annotated-file-ids="annotatedFileIds"
  709. :ruler="ruler"
  710. @update:start-index="onStartIndexChange"
  711. />
  712. <div class="selection-controls">
  713. <div class="selection-readout">
  714. <span class="selection-accent"></span>
  715. <span>当前窗口覆盖 <strong>{{ selectedWindowPoints.length }}</strong> 个时间点</span>
  716. <span v-if="hasReference" class="reference-badge" title="始终保留一个 status=0 的正常数据用于对比,固定不随翻页变化">
  717. 正常对比:{{ formatDateTime(referencePoints[0]?.sampleTime) }}
  718. </span>
  719. </div>
  720. <div class="selection-actions">
  721. <el-button class="ghost-button" plain :disabled="!startIndex" @click="resetWindow">回到起点</el-button>
  722. <el-button class="ghost-button" plain :disabled="startIndex <= 0" @click="pageWindow(-1)">上一页</el-button>
  723. <el-button class="ghost-button" plain :disabled="startIndex >= timePoints.length - windowSize" @click="pageWindow(1)">下一页</el-button>
  724. <el-select-v2
  725. class="jump-select"
  726. :options="jumpOptions"
  727. :model-value="startIndex"
  728. :disabled="!timePoints.length"
  729. size="small"
  730. filterable
  731. placeholder="跳转"
  732. @update:model-value="jumpToIndex"
  733. >
  734. <template #default="{ item }">
  735. <span :style="item.color ? { color: item.color } : undefined">{{ item.label }}</span>
  736. </template>
  737. </el-select-v2>
  738. <el-button
  739. class="ghost-button chart-query"
  740. :class="{ 'is-dirty': chartDirty }"
  741. type="primary"
  742. plain
  743. :loading="waveLoading"
  744. :disabled="waveLoading || !selectedWindowPoints.length"
  745. @click="loadWaveWindowNow"
  746. >查询图表</el-button>
  747. </div>
  748. </div>
  749. <div v-if="waveData?.files.length" class="window-files">
  750. <div class="window-files-title">当前窗口 wave_file 记录</div>
  751. <table class="window-files-table">
  752. <thead>
  753. <tr>
  754. <th>id</th>
  755. <th>point_name</th>
  756. <th>measurement_type</th>
  757. <th>rpm</th>
  758. <th>sample_time</th>
  759. <th>tspluse_status</th>
  760. <th>file_name</th>
  761. <th>操作</th>
  762. </tr>
  763. </thead>
  764. <tbody>
  765. <tr v-for="file in waveData.files" :key="file.id">
  766. <td class="mono">{{ file.id }}</td>
  767. <td>{{ file.pointName }}</td>
  768. <td>{{ file.measurementType }}</td>
  769. <td class="mono">{{ file.rpm }}</td>
  770. <td class="mono" :style="sampleTimeStyle(file)">{{ file.sampleTime.replace('T', ' ') }}</td>
  771. <td class="mono">{{ file.status ?? '' }}</td>
  772. <td class="file-name" :title="file.fileName">{{ file.fileName || '—' }}</td>
  773. <td class="actions">
  774. <el-button class="file-action" size="small" plain :disabled="!waveData" @click="locateFile(file)">定位</el-button>
  775. <el-button
  776. v-if="!recordedFileIds.includes(file.id)"
  777. class="file-action"
  778. size="small"
  779. type="primary"
  780. plain
  781. :loading="recordingFileId === file.id"
  782. @click="recordFile(file)"
  783. >记录</el-button>
  784. <el-button
  785. v-else
  786. class="file-action"
  787. size="small"
  788. type="danger"
  789. plain
  790. :loading="recordingFileId === file.id"
  791. @click="deleteRecordedFile(file)"
  792. >删除</el-button>
  793. </td>
  794. </tr>
  795. </tbody>
  796. </table>
  797. </div>
  798. </section>
  799. <section class="chart-panel panel">
  800. <div class="panel-heading chart-heading">
  801. <div>
  802. <h2>波形预览</h2>
  803. </div>
  804. <div class="chart-actions">
  805. <span class="annotation-width">标度宽度:<strong>{{ annotationWidth }}</strong></span>
  806. <el-radio-group v-model="chartMode" class="mode-switch" size="small">
  807. <el-radio-button label="split">分图显示</el-radio-button>
  808. <el-radio-button label="merge">归一合并</el-radio-button>
  809. </el-radio-group>
  810. <el-switch
  811. v-model="noSampling"
  812. class="sampling-switch"
  813. size="small"
  814. active-text="不采样"
  815. inactive-text="采样"
  816. :disabled="waveLoading"
  817. />
  818. <div class="chart-status"><i :class="{ busy: waveLoading }"></i>{{ statusText() }}</div>
  819. </div>
  820. </div>
  821. <WaveChart
  822. ref="waveChartRef"
  823. :data="waveData"
  824. :points="selectedWindowPoints"
  825. :mode="chartMode"
  826. :loading="waveLoading"
  827. :annotations="annotations"
  828. @period-dblclick="openPeriod"
  829. @annotation-toggle="onAnnotationToggle"
  830. />
  831. </section>
  832. <aside class="insight-panel panel">
  833. <div class="panel-heading compact-heading">
  834. <div><h2>当前窗口</h2></div>
  835. <span class="readout-number">{{ String(selectedWindowPoints.length).padStart(2, '0') }}</span>
  836. </div>
  837. <div class="readout-grid">
  838. <div class="readout-card">
  839. <span>时间跨度</span>
  840. <div class="readout-value">
  841. <strong>{{ formatDateTime(selectedWindowPoints[0]?.sampleTime) }}</strong>
  842. <small>至 {{ formatDateTime(selectedWindowPoints[selectedWindowPoints.length - 1]?.sampleTime) }}</small>
  843. </div>
  844. </div>
  845. <div class="readout-card"><span>检测周期</span><strong>{{ currentCycles.length }}</strong></div>
  846. <div class="readout-card"><span>选择范围</span><strong>#{{ startIndex + 1 }} — #{{ endIndex }}</strong></div>
  847. <div class="readout-card"><span>显示策略</span><strong>MIN / MAX</strong></div>
  848. </div>
  849. <div class="data-footprint">
  850. <div><span>数据名称</span><strong>{{ selectedTypes.join(' / ') }}</strong></div>
  851. <div><span>可用文件</span><strong>{{ availableTypeCount.toLocaleString() }}</strong></div>
  852. <div><span>抽样上限</span><strong>{{ maxPoints.toLocaleString() }} 点</strong></div>
  853. </div>
  854. </aside>
  855. </main>
  856. <PeriodModal :visible="periodVisible" :detail="periodDetail" :loading="periodLoading" @close="closePeriod" />
  857. </div>
  858. </template>