CronJobLog.vue 9.5 KB

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