log.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <template>
  2. <div class="strategy-log-container">
  3. <!-- 页面头部 -->
  4. <div class="page-header">
  5. <h1 class="page-title">
  6. <i class="el-icon-document"></i>
  7. 策略执行日志
  8. </h1>
  9. <div class="header-actions">
  10. <el-date-picker
  11. v-model="dateRange"
  12. type="daterange"
  13. range-separator="至"
  14. start-placeholder="开始日期"
  15. end-placeholder="结束日期"
  16. value-format="yyyy-MM-dd"
  17. @change="handleDateChange"
  18. style="margin-right: 12px"
  19. />
  20. <el-button icon="el-icon-refresh" @click="refreshList">刷新</el-button>
  21. </div>
  22. </div>
  23. <div class="main-content">
  24. <!-- 左侧筛选面板 -->
  25. <div class="filter-panel">
  26. <div class="filter-section">
  27. <div class="filter-title">策略筛选</div>
  28. <el-select
  29. v-model="queryParams.strategyCode"
  30. placeholder="选择策略"
  31. filterable
  32. clearable
  33. style="width: 100%"
  34. @change="getList"
  35. >
  36. <el-option
  37. v-for="strategy in strategyList"
  38. :key="strategy.strategyCode"
  39. :label="strategy.strategyName"
  40. :value="strategy.strategyCode"
  41. />
  42. </el-select>
  43. </div>
  44. <div class="filter-section">
  45. <div class="filter-title">执行状态</div>
  46. <div class="status-tags">
  47. <el-tag
  48. v-for="status in execStatusOptions"
  49. :key="status.value"
  50. :effect="queryParams.execStatus === status.value ? 'dark' : 'plain'"
  51. :type="status.type"
  52. @click="handleStatusFilter(status.value)"
  53. class="status-tag"
  54. >
  55. {{ status.label }}
  56. </el-tag>
  57. </div>
  58. </div>
  59. <div class="filter-section">
  60. <div class="filter-title">触发类型</div>
  61. <el-radio-group v-model="queryParams.triggerType" @change="getList">
  62. <el-radio label="">全部</el-radio>
  63. <el-radio label="EVENT">事件</el-radio>
  64. <el-radio label="TIME">定时</el-radio>
  65. <el-radio label="MANUAL">手动</el-radio>
  66. <el-radio label="CONDITION">条件</el-radio>
  67. <el-radio label="ATTR">属性</el-radio>
  68. </el-radio-group>
  69. </div>
  70. <div class="filter-section">
  71. <div class="filter-title">执行统计</div>
  72. <div class="stats-summary">
  73. <div class="stat-item">
  74. <span class="stat-value">{{ stats.total || 0 }}</span>
  75. <span class="stat-label">总执行</span>
  76. </div>
  77. <div class="stat-item success">
  78. <span class="stat-value">{{ stats.success || 0 }}</span>
  79. <span class="stat-label">成功</span>
  80. </div>
  81. <div class="stat-item fail">
  82. <span class="stat-value">{{ stats.fail || 0 }}</span>
  83. <span class="stat-label">失败</span>
  84. </div>
  85. <div class="stat-item timeout">
  86. <span class="stat-value">{{ stats.timeout || 0 }}</span>
  87. <span class="stat-label">超时</span>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. <!-- 右侧日志列表 -->
  93. <div class="log-panel">
  94. <el-table :data="logList" v-loading="loading" border stripe>
  95. <el-table-column label="执行ID" prop="execId" width="200" show-overflow-tooltip>
  96. <template slot-scope="{ row }">
  97. <el-button type="text" @click="viewLogDetail(row)">
  98. {{ row.execId.slice(0, 8) }}...
  99. </el-button>
  100. </template>
  101. </el-table-column>
  102. <el-table-column label="策略" min-width="150">
  103. <template slot-scope="{ row }">
  104. <div class="strategy-info">
  105. <span class="strategy-name">{{ row.strategyName || row.strategyCode }}</span>
  106. <span class="strategy-code">{{ row.strategyCode }}</span>
  107. </div>
  108. </template>
  109. </el-table-column>
  110. <el-table-column label="触发类型" width="100" align="center">
  111. <template slot-scope="{ row }">
  112. <el-tag size="small" :type="getTriggerTagType(row.triggerType)">
  113. {{ getTriggerTypeName(row.triggerType) }}
  114. </el-tag>
  115. </template>
  116. </el-table-column>
  117. <el-table-column label="触发源" prop="triggerSource" width="120" show-overflow-tooltip />
  118. <el-table-column label="执行状态" width="100" align="center">
  119. <template slot-scope="{ row }">
  120. <el-tag :type="getExecStatusType(row.execStatus)" size="small">
  121. <i :class="getExecStatusIcon(row.execStatus)"></i>
  122. {{ getExecStatusText(row.execStatus) }}
  123. </el-tag>
  124. </template>
  125. </el-table-column>
  126. <el-table-column label="开始时间" width="160">
  127. <template slot-scope="{ row }">
  128. {{ parseTime(row.startTime) }}
  129. </template>
  130. </el-table-column>
  131. <el-table-column label="耗时" width="100" align="center">
  132. <template slot-scope="{ row }">
  133. <span :class="getDurationClass(row.duration)">
  134. {{ formatDuration(row.duration) }}
  135. </span>
  136. </template>
  137. </el-table-column>
  138. <el-table-column label="执行人" prop="execBy" width="100" align="center">
  139. <template slot-scope="{ row }">
  140. {{ row.execBy || '-' }}
  141. </template>
  142. </el-table-column>
  143. <el-table-column label="操作" width="120" align="center" fixed="right">
  144. <template slot-scope="{ row }">
  145. <el-button size="mini" type="text" @click="viewLogDetail(row)">
  146. <i class="el-icon-view"></i> 详情
  147. </el-button>
  148. </template>
  149. </el-table-column>
  150. </el-table>
  151. <!-- 分页 -->
  152. <div class="pagination-wrapper">
  153. <el-pagination
  154. background
  155. layout="total, sizes, prev, pager, next, jumper"
  156. :total="total"
  157. :page-size.sync="queryParams.pageSize"
  158. :current-page.sync="queryParams.pageNum"
  159. :page-sizes="[20, 50, 100]"
  160. @size-change="getList"
  161. @current-change="getList"
  162. />
  163. </div>
  164. </div>
  165. </div>
  166. <!-- 日志详情抽屉 -->
  167. <el-drawer
  168. title="执行详情"
  169. :visible.sync="detailDrawerVisible"
  170. size="60%"
  171. direction="rtl"
  172. >
  173. <div class="log-detail" v-if="currentLog">
  174. <!-- 基本信息 -->
  175. <div class="detail-section">
  176. <h3 class="section-title">基本信息</h3>
  177. <el-descriptions :column="2" border size="small">
  178. <el-descriptions-item label="执行ID">{{ currentLog.execId }}</el-descriptions-item>
  179. <el-descriptions-item label="策略代码">{{ currentLog.strategyCode }}</el-descriptions-item>
  180. <el-descriptions-item label="策略名称">{{ currentLog.strategyName || '-' }}</el-descriptions-item>
  181. <el-descriptions-item label="触发类型">{{ getTriggerTypeName(currentLog.triggerType) }}</el-descriptions-item>
  182. <el-descriptions-item label="触发源">{{ currentLog.triggerSource || '-' }}</el-descriptions-item>
  183. <el-descriptions-item label="执行状态">
  184. <el-tag :type="getExecStatusType(currentLog.execStatus)" size="small">
  185. {{ getExecStatusText(currentLog.execStatus) }}
  186. </el-tag>
  187. </el-descriptions-item>
  188. <el-descriptions-item label="执行人">{{ currentLog.execBy || '系统' }}</el-descriptions-item>
  189. <el-descriptions-item label="开始时间">{{ parseTime(currentLog.startTime) }}</el-descriptions-item>
  190. <el-descriptions-item label="结束时间">{{ parseTime(currentLog.endTime) || '-' }}</el-descriptions-item>
  191. <el-descriptions-item label="执行耗时">{{ formatDuration(currentLog.duration) }}</el-descriptions-item>
  192. </el-descriptions>
  193. </div>
  194. <!-- 错误信息 -->
  195. <div class="detail-section" v-if="currentLog.errorMessage">
  196. <h3 class="section-title error-title">
  197. <i class="el-icon-warning"></i> 错误信息
  198. </h3>
  199. <div class="error-content">
  200. {{ currentLog.errorMessage }}
  201. </div>
  202. </div>
  203. <!-- 步骤执行时间线 -->
  204. <div class="detail-section">
  205. <h3 class="section-title">执行步骤</h3>
  206. <el-timeline v-if="stepLogs.length > 0">
  207. <el-timeline-item
  208. v-for="step in stepLogs"
  209. :key="step.id"
  210. :type="getStepTimelineType(step.execStatus)"
  211. :icon="getStepTimelineIcon(step.execStatus)"
  212. >
  213. <div class="step-log-item">
  214. <div class="step-header">
  215. <span class="step-name">{{ step.stepName }}</span>
  216. <span class="step-index">#{{ step.stepIndex }}</span>
  217. <el-tag :type="getExecStatusType(step.execStatus)" size="mini">
  218. {{ getExecStatusText(step.execStatus) }}
  219. </el-tag>
  220. <span class="step-duration">{{ formatDuration(step.duration) }}</span>
  221. </div>
  222. <div class="step-time">
  223. {{ parseTime(step.startTime) }} - {{ parseTime(step.endTime) || '执行中' }}
  224. </div>
  225. <div class="step-params" v-if="step.inputParam">
  226. <el-collapse>
  227. <el-collapse-item title="输入参数">
  228. <pre>{{ formatJson(step.inputParam) }}</pre>
  229. </el-collapse-item>
  230. </el-collapse>
  231. </div>
  232. <div class="step-result" v-if="step.outputResult">
  233. <el-collapse>
  234. <el-collapse-item title="输出结果">
  235. <pre>{{ formatJson(step.outputResult) }}</pre>
  236. </el-collapse-item>
  237. </el-collapse>
  238. </div>
  239. <div class="step-error" v-if="step.errorMessage">
  240. <span class="error-label">错误:</span>
  241. {{ step.errorMessage }}
  242. </div>
  243. </div>
  244. </el-timeline-item>
  245. </el-timeline>
  246. <el-empty v-else description="暂无步骤执行记录" />
  247. </div>
  248. <!-- ✅ 已删除"上下文数据"区块 -->
  249. </div>
  250. </el-drawer>
  251. </div>
  252. </template>
  253. <script>
  254. import { listEnergyStrategy, getExecLogList, getExecLog, getStepExecLog } from '@/api/mgr/energyStrategy';
  255. export default {
  256. name: 'StrategyLog',
  257. data() {
  258. return {
  259. loading: false,
  260. logList: [],
  261. total: 0,
  262. strategyList: [],
  263. dateRange: [],
  264. queryParams: {
  265. pageNum: 1,
  266. pageSize: 20,
  267. strategyCode: '',
  268. execStatus: null,
  269. triggerType: '',
  270. startTimeBegin: '',
  271. startTimeEnd: ''
  272. },
  273. stats: {
  274. total: 0,
  275. success: 0,
  276. fail: 0,
  277. timeout: 0
  278. },
  279. execStatusOptions: [
  280. { value: null, label: '全部', type: '' },
  281. { value: 0, label: '执行中', type: 'info' },
  282. { value: 1, label: '成功', type: 'success' },
  283. { value: 2, label: '失败', type: 'danger' },
  284. { value: 3, label: '超时', type: 'warning' }
  285. ],
  286. detailDrawerVisible: false,
  287. currentLog: null,
  288. stepLogs: []
  289. };
  290. },
  291. created() {
  292. this.loadStrategies();
  293. this.getList();
  294. },
  295. methods: {
  296. // 加载策略列表(用于筛选)
  297. async loadStrategies() {
  298. try {
  299. const response = await listEnergyStrategy({ pageSize: 1000 });
  300. this.strategyList = response.rows || [];
  301. } catch (error) {
  302. console.error('加载策略列表失败', error);
  303. this.$message.error('加载策略列表失败');
  304. }
  305. },
  306. // 获取日志列表
  307. async getList() {
  308. this.loading = true;
  309. try {
  310. const params = { ...this.queryParams };
  311. // 处理日期范围
  312. if (this.dateRange && this.dateRange.length === 2) {
  313. params.startTimeBegin = this.dateRange[0] + ' 00:00:00';
  314. params.startTimeEnd = this.dateRange[1] + ' 23:59:59';
  315. } else {
  316. params.startTimeBegin = '';
  317. params.startTimeEnd = '';
  318. }
  319. // 清理空值参数
  320. Object.keys(params).forEach(key => {
  321. if (params[key] === '' || params[key] === null) {
  322. delete params[key];
  323. }
  324. });
  325. const response = await getExecLogList(params);
  326. this.logList = response.rows || [];
  327. this.total = response.total || 0;
  328. // 计算统计数据
  329. this.calculateStats();
  330. } catch (error) {
  331. console.error('获取日志列表失败', error);
  332. this.$message.error('获取日志列表失败');
  333. this.logList = [];
  334. this.total = 0;
  335. } finally {
  336. this.loading = false;
  337. }
  338. },
  339. calculateStats() {
  340. this.stats = {
  341. total: this.total,
  342. success: this.logList.filter(l => l.execStatus === 1).length,
  343. fail: this.logList.filter(l => l.execStatus === 2).length,
  344. timeout: this.logList.filter(l => l.execStatus === 3).length
  345. };
  346. },
  347. refreshList() {
  348. this.queryParams.pageNum = 1;
  349. this.getList();
  350. this.$message.success('刷新成功');
  351. },
  352. handleDateChange() {
  353. this.queryParams.pageNum = 1;
  354. this.getList();
  355. },
  356. handleStatusFilter(status) {
  357. this.queryParams.execStatus = this.queryParams.execStatus === status ? null : status;
  358. this.queryParams.pageNum = 1;
  359. this.getList();
  360. },
  361. // 查看日志详情
  362. async viewLogDetail(log) {
  363. this.currentLog = log;
  364. this.detailDrawerVisible = true;
  365. this.stepLogs = [];
  366. // 加载步骤执行日志
  367. try {
  368. const response = await getStepExecLog(log.execId);
  369. this.stepLogs = (response.data || []).sort((a, b) => a.stepIndex - b.stepIndex);
  370. } catch (error) {
  371. console.error('加载步骤日志失败', error);
  372. this.$message.warning('步骤日志加载失败');
  373. }
  374. },
  375. // 辅助方法
  376. getTriggerTypeName(type) {
  377. const map = {
  378. 'EVENT': '事件',
  379. 'TIME': '定时',
  380. 'MANUAL': '手动',
  381. 'CONDITION': '条件',
  382. 'ATTR': '属性' // ✅ 新增属性触发类型
  383. };
  384. return map[type] || type || '-';
  385. },
  386. getTriggerTagType(type) {
  387. const map = {
  388. 'EVENT': 'danger',
  389. 'TIME': '',
  390. 'MANUAL': 'info',
  391. 'CONDITION': 'success',
  392. 'ATTR': 'warning' // ✅ 新增属性触发类型
  393. };
  394. return map[type] || '';
  395. },
  396. getExecStatusType(status) {
  397. const map = { 0: 'info', 1: 'success', 2: 'danger', 3: 'warning' };
  398. return map[status] || '';
  399. },
  400. getExecStatusText(status) {
  401. const map = { 0: '执行中', 1: '成功', 2: '失败', 3: '超时' };
  402. return map[status] || '-';
  403. },
  404. getExecStatusIcon(status) {
  405. const map = { 0: 'el-icon-loading', 1: 'el-icon-success', 2: 'el-icon-error', 3: 'el-icon-time' };
  406. return map[status] || '';
  407. },
  408. getStepTimelineType(status) {
  409. const map = { 0: 'info', 1: 'success', 2: 'danger', 3: 'warning' };
  410. return map[status] || 'info';
  411. },
  412. getStepTimelineIcon(status) {
  413. const map = { 0: 'el-icon-loading', 1: 'el-icon-check', 2: 'el-icon-close', 3: 'el-icon-time' };
  414. return map[status] || 'el-icon-more';
  415. },
  416. getDurationClass(duration) {
  417. if (!duration) return '';
  418. if (duration > 10000) return 'duration-slow';
  419. if (duration > 5000) return 'duration-normal';
  420. return 'duration-fast';
  421. },
  422. formatDuration(ms) {
  423. if (!ms && ms !== 0) return '-';
  424. if (ms < 1000) return ms + 'ms';
  425. if (ms < 60000) return (ms / 1000).toFixed(1) + 's';
  426. return (ms / 60000).toFixed(1) + 'min';
  427. },
  428. parseTime(time) {
  429. if (!time) return '';
  430. return new Date(time).toLocaleString('zh-CN', { hour12: false });
  431. },
  432. formatJson(str) {
  433. if (!str) return '';
  434. try {
  435. const obj = typeof str === 'string' ? JSON.parse(str) : str;
  436. return JSON.stringify(obj, null, 2);
  437. } catch (e) {
  438. return str;
  439. }
  440. }
  441. }
  442. };
  443. </script>
  444. <style lang="scss" scoped>
  445. .strategy-log-container {
  446. min-height: 100vh;
  447. background: #f0f2f5;
  448. padding: 20px;
  449. }
  450. .page-header {
  451. display: flex;
  452. justify-content: space-between;
  453. align-items: center;
  454. margin-bottom: 20px;
  455. padding: 20px 24px;
  456. background: #fff;
  457. border-radius: 12px;
  458. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  459. .page-title {
  460. margin: 0;
  461. font-size: 20px;
  462. font-weight: 600;
  463. i {
  464. margin-right: 10px;
  465. color: #409eff;
  466. }
  467. }
  468. }
  469. .main-content {
  470. display: flex;
  471. gap: 20px;
  472. }
  473. .filter-panel {
  474. width: 260px;
  475. flex-shrink: 0;
  476. background: #fff;
  477. border-radius: 12px;
  478. padding: 20px;
  479. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  480. .filter-section {
  481. margin-bottom: 24px;
  482. &:last-child {
  483. margin-bottom: 0;
  484. }
  485. .filter-title {
  486. font-size: 14px;
  487. font-weight: 600;
  488. color: #303133;
  489. margin-bottom: 12px;
  490. }
  491. }
  492. .status-tags {
  493. display: flex;
  494. flex-wrap: wrap;
  495. gap: 8px;
  496. .status-tag {
  497. cursor: pointer;
  498. transition: all 0.2s;
  499. &:hover {
  500. transform: scale(1.05);
  501. }
  502. }
  503. }
  504. .stats-summary {
  505. display: grid;
  506. grid-template-columns: repeat(2, 1fr);
  507. gap: 12px;
  508. .stat-item {
  509. text-align: center;
  510. padding: 12px;
  511. background: #f5f7fa;
  512. border-radius: 8px;
  513. .stat-value {
  514. display: block;
  515. font-size: 24px;
  516. font-weight: 600;
  517. color: #303133;
  518. }
  519. .stat-label {
  520. font-size: 12px;
  521. color: #909399;
  522. }
  523. &.success .stat-value { color: #67c23a; }
  524. &.fail .stat-value { color: #f56c6c; }
  525. &.timeout .stat-value { color: #e6a23c; }
  526. }
  527. }
  528. }
  529. .log-panel {
  530. flex: 1;
  531. background: #fff;
  532. border-radius: 12px;
  533. padding: 20px;
  534. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  535. .strategy-info {
  536. .strategy-name {
  537. display: block;
  538. font-weight: 500;
  539. }
  540. .strategy-code {
  541. font-size: 12px;
  542. color: #909399;
  543. }
  544. }
  545. .duration-fast { color: #67c23a; }
  546. .duration-normal { color: #e6a23c; }
  547. .duration-slow { color: #f56c6c; }
  548. }
  549. .pagination-wrapper {
  550. display: flex;
  551. justify-content: flex-end;
  552. margin-top: 20px;
  553. }
  554. // 日志详情
  555. .log-detail {
  556. padding: 0 20px 20px;
  557. .detail-section {
  558. margin-bottom: 24px;
  559. .section-title {
  560. font-size: 16px;
  561. font-weight: 600;
  562. color: #303133;
  563. margin-bottom: 16px;
  564. padding-bottom: 8px;
  565. border-bottom: 1px solid #ebeef5;
  566. &.error-title {
  567. color: #f56c6c;
  568. }
  569. }
  570. }
  571. .error-content {
  572. padding: 12px 16px;
  573. background: #fef0f0;
  574. border-radius: 6px;
  575. color: #f56c6c;
  576. font-family: monospace;
  577. white-space: pre-wrap;
  578. word-break: break-all;
  579. }
  580. .step-log-item {
  581. .step-header {
  582. display: flex;
  583. align-items: center;
  584. gap: 8px;
  585. margin-bottom: 8px;
  586. .step-name {
  587. font-weight: 600;
  588. color: #303133;
  589. }
  590. .step-index {
  591. font-size: 12px;
  592. color: #909399;
  593. }
  594. .step-duration {
  595. margin-left: auto;
  596. font-size: 12px;
  597. color: #909399;
  598. }
  599. }
  600. .step-time {
  601. font-size: 12px;
  602. color: #909399;
  603. margin-bottom: 8px;
  604. }
  605. .step-error {
  606. padding: 8px 12px;
  607. background: #fef0f0;
  608. border-radius: 4px;
  609. font-size: 12px;
  610. color: #f56c6c;
  611. margin-top: 8px;
  612. .error-label {
  613. font-weight: 600;
  614. }
  615. }
  616. pre {
  617. background: #f5f7fa;
  618. padding: 12px;
  619. border-radius: 6px;
  620. font-size: 12px;
  621. overflow-x: auto;
  622. }
  623. }
  624. }
  625. </style>