CronJobLog.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <div class="cron-job-log-container">
  3. <!-- 搜索区域 -->
  4. <el-card shadow="never" class="search-card">
  5. <el-form :model="searchForm" inline>
  6. <el-form-item label="任务名称">
  7. <el-input
  8. v-model="searchForm.name"
  9. placeholder="请输入任务名称"
  10. clearable
  11. style="width: 200px"
  12. @keyup.enter="handleSearch"
  13. />
  14. </el-form-item>
  15. <el-form-item label="执行状态">
  16. <el-select
  17. v-model="searchForm.success"
  18. placeholder="请选择状态"
  19. clearable
  20. style="width: 150px"
  21. >
  22. <el-option label="成功" :value="1" />
  23. <el-option label="失败" :value="0" />
  24. </el-select>
  25. </el-form-item>
  26. <el-form-item label="执行时间">
  27. <el-date-picker
  28. v-model="searchForm.date_range"
  29. type="daterange"
  30. range-separator="-"
  31. start-placeholder="开始日期"
  32. end-placeholder="结束日期"
  33. value-format="YYYY-MM-DD HH:mm:ss"
  34. :default-time="[new Date(2000, 0, 1, 0, 0, 0), new Date(2000, 0, 1, 23, 59, 59)]"
  35. style="width: 360px"
  36. />
  37. </el-form-item>
  38. <el-form-item>
  39. <el-button type="primary" @click="handleSearch">
  40. <el-icon><Search /></el-icon>
  41. 搜索
  42. </el-button>
  43. <el-button @click="handleReset">
  44. <el-icon><Refresh /></el-icon>
  45. 重置
  46. </el-button>
  47. </el-form-item>
  48. </el-form>
  49. </el-card>
  50. <!-- 统计卡片 -->
  51. <el-row :gutter="16" class="stats-row">
  52. <el-col :span="6">
  53. <el-card shadow="never">
  54. <div class="stat-item">
  55. <div class="stat-label">总执行次数</div>
  56. <div class="stat-value">{{ stats.total }}</div>
  57. </div>
  58. </el-card>
  59. </el-col>
  60. <el-col :span="6">
  61. <el-card shadow="never">
  62. <div class="stat-item">
  63. <div class="stat-label">成功次数</div>
  64. <div class="stat-value stat-success">{{ stats.success_count }}</div>
  65. </div>
  66. </el-card>
  67. </el-col>
  68. <el-col :span="6">
  69. <el-card shadow="never">
  70. <div class="stat-item">
  71. <div class="stat-label">失败次数</div>
  72. <div class="stat-value stat-fail">{{ stats.fail_count }}</div>
  73. </div>
  74. </el-card>
  75. </el-col>
  76. <el-col :span="6">
  77. <el-card shadow="never">
  78. <div class="stat-item">
  79. <div class="stat-label">成功率</div>
  80. <div class="stat-value">{{ stats.success_rate }}%</div>
  81. </div>
  82. </el-card>
  83. </el-col>
  84. </el-row>
  85. <!-- 表格区域 -->
  86. <el-card shadow="never" class="table-card">
  87. <template #header>
  88. <div class="card-header">
  89. <span>日志列表</span>
  90. <div class="header-actions">
  91. <el-button
  92. type="danger"
  93. :disabled="selectedIds.length === 0"
  94. @click="handleBatchDelete"
  95. >
  96. 批量删除
  97. </el-button>
  98. <el-button
  99. type="danger"
  100. plain
  101. @click="handleClearAll"
  102. >
  103. 清空所有日志
  104. </el-button>
  105. </div>
  106. </div>
  107. </template>
  108. <el-table
  109. v-loading="loading"
  110. :data="tableData"
  111. border
  112. stripe
  113. @selection-change="handleSelectionChange"
  114. >
  115. <el-table-column type="selection" width="50" align="center" />
  116. <el-table-column prop="id" label="ID" width="80" align="center" />
  117. <el-table-column prop="name" label="任务名称" min-width="200" show-overflow-tooltip />
  118. <el-table-column prop="rule" label="Cron规则" width="180" />
  119. <el-table-column prop="duration" label="执行耗时" width="120" align="center">
  120. <template #default="{ row }">
  121. {{ row.duration ? row.duration + 's' : '-' }}
  122. </template>
  123. </el-table-column>
  124. <el-table-column prop="success" label="执行结果" width="100" align="center">
  125. <template #default="{ row }">
  126. <el-tag :type="row.success ? 'success' : 'danger'" size="small">
  127. {{ row.success ? '成功' : '失败' }}
  128. </el-tag>
  129. </template>
  130. </el-table-column>
  131. <el-table-column prop="exception" label="异常信息" min-width="200" show-overflow-tooltip>
  132. <template #default="{ row }">
  133. <span v-if="row.exception" class="exception-text">{{ row.exception }}</span>
  134. <span v-else>-</span>
  135. </template>
  136. </el-table-column>
  137. <el-table-column prop="create_time" label="执行时间" width="180" align="center" />
  138. <el-table-column label="操作" width="100" align="center" fixed="right">
  139. <template #default="{ row }">
  140. <el-button
  141. type="danger"
  142. link
  143. size="small"
  144. @click="handleDelete(row)"
  145. >
  146. 删除
  147. </el-button>
  148. </template>
  149. </el-table-column>
  150. </el-table>
  151. <!-- 分页 -->
  152. <div class="pagination-wrapper">
  153. <el-pagination
  154. v-model:current-page="pagination.page"
  155. v-model:page-size="pagination.list_rows"
  156. :total="pagination.total"
  157. :page-sizes="[10, 15, 30, 50, 100]"
  158. layout="total, sizes, prev, pager, next, jumper"
  159. @size-change="handleSizeChange"
  160. @current-change="handlePageChange"
  161. />
  162. </div>
  163. </el-card>
  164. </div>
  165. </template>
  166. <script setup>
  167. import { ref, onMounted, reactive } from 'vue'
  168. import { ElMessage, ElMessageBox } from 'element-plus'
  169. import { Search, Refresh } from '@element-plus/icons-vue'
  170. import { getCronJobLogList, deleteCronJobLog, batchDeleteCronJobLog, clearAllCronJobLog } from '../api'
  171. const loading = ref(false)
  172. const tableData = ref([])
  173. const selectedIds = ref([])
  174. const searchForm = reactive({
  175. name: '',
  176. success: null,
  177. date_range: []
  178. })
  179. const pagination = reactive({
  180. page: 1,
  181. list_rows: 15,
  182. total: 0
  183. })
  184. const stats = reactive({
  185. total: 0,
  186. success_count: 0,
  187. fail_count: 0,
  188. success_rate: 0
  189. })
  190. const getList = async () => {
  191. loading.value = true
  192. try {
  193. const params = {
  194. page: pagination.page,
  195. list_rows: pagination.list_rows,
  196. name: searchForm.name
  197. }
  198. if (searchForm.success !== null && searchForm.success !== '') {
  199. params.success = searchForm.success
  200. }
  201. if (searchForm.date_range && searchForm.date_range.length === 2) {
  202. params.start_time = searchForm.date_range[0]
  203. params.end_time = searchForm.date_range[1]
  204. }
  205. const res = await getCronJobLogList(params)
  206. if (res.code === 200) {
  207. tableData.value = res.page?.data || []
  208. pagination.total = res.page?.total || 0
  209. const s = res.data || {}
  210. stats.total = s.total || 0
  211. stats.success_count = s.success_count || 0
  212. stats.fail_count = s.fail_count || 0
  213. stats.success_rate = stats.total > 0 ? parseFloat((stats.success_count / stats.total * 100).toFixed(4)) : 0
  214. }
  215. } catch (e) {
  216. console.error('获取日志列表失败:', e)
  217. } finally {
  218. loading.value = false
  219. }
  220. }
  221. const handleSearch = () => {
  222. pagination.page = 1
  223. getList()
  224. }
  225. const handleReset = () => {
  226. searchForm.name = ''
  227. searchForm.success = null
  228. searchForm.date_range = []
  229. pagination.page = 1
  230. getList()
  231. }
  232. const handlePageChange = () => {
  233. getList()
  234. }
  235. const handleSizeChange = () => {
  236. pagination.page = 1
  237. getList()
  238. }
  239. const handleSelectionChange = (selection) => {
  240. selectedIds.value = selection.map(item => item.id)
  241. }
  242. const handleDelete = (row) => {
  243. ElMessageBox.confirm(
  244. `确定要删除任务「${row.name}」的这条执行日志吗?`,
  245. '删除确认',
  246. {
  247. confirmButtonText: '确定',
  248. cancelButtonText: '取消',
  249. type: 'warning'
  250. }
  251. ).then(async () => {
  252. try {
  253. const res = await deleteCronJobLog(row.id)
  254. if (res.code === 200) {
  255. ElMessage.success('删除成功')
  256. getList()
  257. }
  258. } catch (e) {
  259. console.error('删除失败:', e)
  260. }
  261. }).catch(() => {})
  262. }
  263. const handleBatchDelete = () => {
  264. if (selectedIds.value.length === 0) return
  265. ElMessageBox.confirm(
  266. `确定要删除选中的 ${selectedIds.value.length} 条日志吗?`,
  267. '批量删除确认',
  268. {
  269. confirmButtonText: '确定',
  270. cancelButtonText: '取消',
  271. type: 'warning'
  272. }
  273. ).then(async () => {
  274. try {
  275. const res = await batchDeleteCronJobLog(selectedIds.value)
  276. if (res.code === 200) {
  277. ElMessage.success('批量删除成功')
  278. selectedIds.value = []
  279. getList()
  280. }
  281. } catch (e) {
  282. console.error('批量删除失败:', e)
  283. }
  284. }).catch(() => {})
  285. }
  286. const handleClearAll = () => {
  287. ElMessageBox.confirm(
  288. '确定要清空所有定时任务执行日志吗?此操作不可恢复!',
  289. '清空确认',
  290. {
  291. confirmButtonText: '确定清空',
  292. cancelButtonText: '取消',
  293. type: 'warning',
  294. confirmButtonClass: 'el-button--danger'
  295. }
  296. ).then(async () => {
  297. try {
  298. const res = await clearAllCronJobLog()
  299. if (res.code === 200) {
  300. ElMessage.success('清空成功')
  301. pagination.page = 1
  302. getList()
  303. }
  304. } catch (e) {
  305. console.error('清空失败:', e)
  306. }
  307. }).catch(() => {})
  308. }
  309. onMounted(() => {
  310. getList()
  311. })
  312. </script>
  313. <style scoped lang="scss">
  314. .cron-job-log-container {
  315. padding: 0;
  316. }
  317. .stats-row {
  318. margin-bottom: 16px;
  319. .stat-item {
  320. text-align: center;
  321. }
  322. .stat-label {
  323. font-size: 14px;
  324. color: #909399;
  325. margin-bottom: 8px;
  326. }
  327. .stat-value {
  328. font-size: 28px;
  329. font-weight: 600;
  330. color: #303133;
  331. }
  332. .stat-success {
  333. color: #67c23a;
  334. }
  335. .stat-fail {
  336. color: #f56c6c;
  337. }
  338. }
  339. .search-card {
  340. margin-bottom: 16px;
  341. }
  342. .table-card {
  343. .card-header {
  344. display: flex;
  345. justify-content: space-between;
  346. align-items: center;
  347. }
  348. }
  349. .pagination-wrapper {
  350. display: flex;
  351. justify-content: flex-end;
  352. margin-top: 16px;
  353. }
  354. .exception-text {
  355. color: #f56c6c;
  356. }
  357. </style>