CronJobLog.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. // 获取列表
  185. const getList = async () => {
  186. loading.value = true
  187. try {
  188. const params = {
  189. page: pagination.page,
  190. list_rows: pagination.list_rows,
  191. name: searchForm.name
  192. }
  193. if (searchForm.success !== null && searchForm.success !== '') {
  194. params.success = searchForm.success
  195. }
  196. if (searchForm.date_range && searchForm.date_range.length === 2) {
  197. params.start_time = searchForm.date_range[0]
  198. params.end_time = searchForm.date_range[1]
  199. }
  200. const res = await getCronJobLogList(params)
  201. if (res.code === 200) {
  202. tableData.value = res.page?.data || []
  203. pagination.total = res.page?.total || 0
  204. // 统计数据
  205. const s = res.data || {}
  206. stats.total = s.total || 0
  207. stats.success_count = s.success_count || 0
  208. stats.fail_count = s.fail_count || 0
  209. stats.success_rate = stats.total > 0 ? parseFloat((stats.success_count / stats.total * 100).toFixed(4)) : 0
  210. }
  211. } catch (e) {
  212. console.error('获取日志列表失败:', e)
  213. } finally {
  214. loading.value = false
  215. }
  216. }
  217. // 搜索
  218. const handleSearch = () => {
  219. pagination.page = 1
  220. getList()
  221. }
  222. // 重置
  223. const handleReset = () => {
  224. searchForm.name = ''
  225. searchForm.success = null
  226. searchForm.date_range = []
  227. pagination.page = 1
  228. getList()
  229. }
  230. // 分页
  231. const handlePageChange = () => {
  232. getList()
  233. }
  234. const handleSizeChange = () => {
  235. pagination.page = 1
  236. getList()
  237. }
  238. // 选择变化
  239. const handleSelectionChange = (selection) => {
  240. selectedIds.value = selection.map(item => item.id)
  241. }
  242. // 删除单条
  243. const handleDelete = (row) => {
  244. ElMessageBox.confirm(
  245. `确定要删除任务「${row.name}」的这条执行日志吗?`,
  246. '删除确认',
  247. {
  248. confirmButtonText: '确定',
  249. cancelButtonText: '取消',
  250. type: 'warning'
  251. }
  252. ).then(async () => {
  253. try {
  254. const res = await deleteCronJobLog(row.id)
  255. if (res.code === 200) {
  256. ElMessage.success('删除成功')
  257. getList()
  258. }
  259. } catch (e) {
  260. console.error('删除失败:', e)
  261. }
  262. }).catch(() => {})
  263. }
  264. // 批量删除
  265. const handleBatchDelete = () => {
  266. if (selectedIds.value.length === 0) return
  267. ElMessageBox.confirm(
  268. `确定要删除选中的 ${selectedIds.value.length} 条日志吗?`,
  269. '批量删除确认',
  270. {
  271. confirmButtonText: '确定',
  272. cancelButtonText: '取消',
  273. type: 'warning'
  274. }
  275. ).then(async () => {
  276. try {
  277. const res = await batchDeleteCronJobLog(selectedIds.value)
  278. if (res.code === 200) {
  279. ElMessage.success('批量删除成功')
  280. selectedIds.value = []
  281. getList()
  282. }
  283. } catch (e) {
  284. console.error('批量删除失败:', e)
  285. }
  286. }).catch(() => {})
  287. }
  288. // 清空所有
  289. const handleClearAll = () => {
  290. ElMessageBox.confirm(
  291. '确定要清空所有定时任务执行日志吗?此操作不可恢复!',
  292. '清空确认',
  293. {
  294. confirmButtonText: '确定清空',
  295. cancelButtonText: '取消',
  296. type: 'warning',
  297. confirmButtonClass: 'el-button--danger'
  298. }
  299. ).then(async () => {
  300. try {
  301. const res = await clearAllCronJobLog()
  302. if (res.code === 200) {
  303. ElMessage.success('清空成功')
  304. pagination.page = 1
  305. getList()
  306. }
  307. } catch (e) {
  308. console.error('清空失败:', e)
  309. }
  310. }).catch(() => {})
  311. }
  312. onMounted(() => {
  313. getList()
  314. })
  315. </script>
  316. <style scoped lang="scss">
  317. .cron-job-log-container {
  318. padding: 0;
  319. }
  320. .page-header {
  321. margin-bottom: 16px;
  322. h2 {
  323. margin: 0;
  324. font-size: 20px;
  325. font-weight: 600;
  326. color: #303133;
  327. }
  328. }
  329. .stats-row {
  330. margin-bottom: 16px;
  331. .stat-item {
  332. text-align: center;
  333. }
  334. .stat-label {
  335. font-size: 14px;
  336. color: #909399;
  337. margin-bottom: 8px;
  338. }
  339. .stat-value {
  340. font-size: 28px;
  341. font-weight: 600;
  342. color: #303133;
  343. }
  344. .stat-success {
  345. color: #67c23a;
  346. }
  347. .stat-fail {
  348. color: #f56c6c;
  349. }
  350. }
  351. .search-card {
  352. margin-bottom: 16px;
  353. }
  354. .table-card {
  355. .card-header {
  356. display: flex;
  357. justify-content: space-between;
  358. align-items: center;
  359. }
  360. }
  361. .pagination-wrapper {
  362. display: flex;
  363. justify-content: flex-end;
  364. margin-top: 16px;
  365. }
  366. </style>