CronJobLog.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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="create_time" label="执行时间" width="180" align="center" />
  132. <el-table-column label="操作" width="100" align="center" fixed="right">
  133. <template #default="{ row }">
  134. <el-button
  135. type="danger"
  136. link
  137. size="small"
  138. @click="handleDelete(row)"
  139. >
  140. 删除
  141. </el-button>
  142. </template>
  143. </el-table-column>
  144. </el-table>
  145. <!-- 分页 -->
  146. <div class="pagination-wrapper">
  147. <el-pagination
  148. v-model:current-page="pagination.page"
  149. v-model:page-size="pagination.list_rows"
  150. :total="pagination.total"
  151. :page-sizes="[10, 15, 30, 50, 100]"
  152. layout="total, sizes, prev, pager, next, jumper"
  153. @size-change="handleSizeChange"
  154. @current-change="handlePageChange"
  155. />
  156. </div>
  157. </el-card>
  158. </div>
  159. </template>
  160. <script setup>
  161. import { ref, onMounted, reactive } from 'vue'
  162. import { ElMessage, ElMessageBox } from 'element-plus'
  163. import { Search, Refresh } from '@element-plus/icons-vue'
  164. import { getCronJobLogList, deleteCronJobLog, batchDeleteCronJobLog, clearAllCronJobLog } from '../api'
  165. const loading = ref(false)
  166. const tableData = ref([])
  167. const selectedIds = ref([])
  168. const searchForm = reactive({
  169. name: '',
  170. success: null,
  171. date_range: []
  172. })
  173. const pagination = reactive({
  174. page: 1,
  175. list_rows: 15,
  176. total: 0
  177. })
  178. const stats = reactive({
  179. total: 0,
  180. success_count: 0,
  181. fail_count: 0,
  182. success_rate: 0
  183. })
  184. const getList = async () => {
  185. loading.value = true
  186. try {
  187. const params = {
  188. page: pagination.page,
  189. list_rows: pagination.list_rows,
  190. name: searchForm.name
  191. }
  192. if (searchForm.success !== null && searchForm.success !== '') {
  193. params.success = searchForm.success
  194. }
  195. if (searchForm.date_range && searchForm.date_range.length === 2) {
  196. params.start_time = searchForm.date_range[0]
  197. params.end_time = searchForm.date_range[1]
  198. }
  199. const res = await getCronJobLogList(params)
  200. if (res.code === 200) {
  201. tableData.value = res.page?.data || []
  202. pagination.total = res.page?.total || 0
  203. const s = res.data || {}
  204. stats.total = s.total || 0
  205. stats.success_count = s.success_count || 0
  206. stats.fail_count = s.fail_count || 0
  207. stats.success_rate = stats.total > 0 ? parseFloat((stats.success_count / stats.total * 100).toFixed(4)) : 0
  208. }
  209. } catch (e) {
  210. console.error('获取日志列表失败:', e)
  211. } finally {
  212. loading.value = false
  213. }
  214. }
  215. const handleSearch = () => {
  216. pagination.page = 1
  217. getList()
  218. }
  219. const handleReset = () => {
  220. searchForm.name = ''
  221. searchForm.success = null
  222. searchForm.date_range = []
  223. pagination.page = 1
  224. getList()
  225. }
  226. const handlePageChange = () => {
  227. getList()
  228. }
  229. const handleSizeChange = () => {
  230. pagination.page = 1
  231. getList()
  232. }
  233. const handleSelectionChange = (selection) => {
  234. selectedIds.value = selection.map(item => item.id)
  235. }
  236. const handleDelete = (row) => {
  237. ElMessageBox.confirm(
  238. `确定要删除任务「${row.name}」的这条执行日志吗?`,
  239. '删除确认',
  240. {
  241. confirmButtonText: '确定',
  242. cancelButtonText: '取消',
  243. type: 'warning'
  244. }
  245. ).then(async () => {
  246. try {
  247. const res = await deleteCronJobLog(row.id)
  248. if (res.code === 200) {
  249. ElMessage.success('删除成功')
  250. getList()
  251. }
  252. } catch (e) {
  253. console.error('删除失败:', e)
  254. }
  255. }).catch(() => {})
  256. }
  257. const handleBatchDelete = () => {
  258. if (selectedIds.value.length === 0) return
  259. ElMessageBox.confirm(
  260. `确定要删除选中的 ${selectedIds.value.length} 条日志吗?`,
  261. '批量删除确认',
  262. {
  263. confirmButtonText: '确定',
  264. cancelButtonText: '取消',
  265. type: 'warning'
  266. }
  267. ).then(async () => {
  268. try {
  269. const res = await batchDeleteCronJobLog(selectedIds.value)
  270. if (res.code === 200) {
  271. ElMessage.success('批量删除成功')
  272. selectedIds.value = []
  273. getList()
  274. }
  275. } catch (e) {
  276. console.error('批量删除失败:', e)
  277. }
  278. }).catch(() => {})
  279. }
  280. const handleClearAll = () => {
  281. ElMessageBox.confirm(
  282. '确定要清空所有定时任务执行日志吗?此操作不可恢复!',
  283. '清空确认',
  284. {
  285. confirmButtonText: '确定清空',
  286. cancelButtonText: '取消',
  287. type: 'warning',
  288. confirmButtonClass: 'el-button--danger'
  289. }
  290. ).then(async () => {
  291. try {
  292. const res = await clearAllCronJobLog()
  293. if (res.code === 200) {
  294. ElMessage.success('清空成功')
  295. pagination.page = 1
  296. getList()
  297. }
  298. } catch (e) {
  299. console.error('清空失败:', e)
  300. }
  301. }).catch(() => {})
  302. }
  303. onMounted(() => {
  304. getList()
  305. })
  306. </script>
  307. <style scoped lang="scss">
  308. .cron-job-log-container {
  309. padding: 0;
  310. }
  311. .stats-row {
  312. margin-bottom: 16px;
  313. .stat-item {
  314. text-align: center;
  315. }
  316. .stat-label {
  317. font-size: 14px;
  318. color: #909399;
  319. margin-bottom: 8px;
  320. }
  321. .stat-value {
  322. font-size: 28px;
  323. font-weight: 600;
  324. color: #303133;
  325. }
  326. .stat-success {
  327. color: #67c23a;
  328. }
  329. .stat-fail {
  330. color: #f56c6c;
  331. }
  332. }
  333. .search-card {
  334. margin-bottom: 16px;
  335. }
  336. .table-card {
  337. .card-header {
  338. display: flex;
  339. justify-content: space-between;
  340. align-items: center;
  341. }
  342. }
  343. .pagination-wrapper {
  344. display: flex;
  345. justify-content: flex-end;
  346. margin-top: 16px;
  347. }
  348. </style>