潘志宝
2025-02-26 b3a43e63d2c2fa854d676676d3f8072c0d943d13
提交 | 用户 | 时间
6bf4f9 1 <template>
H 2   <ContentWrap>
3     <!-- 搜索工作栏 -->
4     <el-form
5       class="-mb-15px"
6       :model="queryParams"
7       ref="queryFormRef"
8       :inline="true"
9       label-width="68px"
10     >
11       <!--      <el-form-item label="主机名称" prop="hostName">-->
12       <!--        <el-input-->
13       <!--          v-model="queryParams.hostName"-->
14       <!--          placeholder="请输入主机名称"-->
15       <!--          clearable-->
16       <!--          @keyup.enter="handleQuery"-->
17       <!--          class="!w-240px"-->
18       <!--        />-->
19       <!--      </el-form-item>-->
20       <el-form-item label="服务器IP" prop="hostIp">
21         <el-input
22           v-model="queryParams.hostIp"
23           placeholder="请输入服务器IP"
24           clearable
25           @keyup.enter="handleQuery"
26           class="!w-120px"
27         />
28       </el-form-item>
29       <!--      <el-form-item label="盘符" prop="disk">-->
30       <!--        <el-input-->
31       <!--          v-model="queryParams.disk"-->
32       <!--          placeholder="请输入盘符"-->
33       <!--          clearable-->
34       <!--          @keyup.enter="handleQuery"-->
35       <!--          class="!w-240px"-->
36       <!--        />-->
37       <!--      </el-form-item>-->
38       <el-form-item label="磁盘名" prop="diskName">
39         <el-input
40           v-model="queryParams.diskName"
41           placeholder="请输入磁盘名"
42           clearable
43           @keyup.enter="handleQuery"
44           class="!w-120px"
45         />
46       </el-form-item>
47       <el-form-item label="创建时间" prop="createTime">
48         <el-date-picker
49           v-model="queryParams.createTime"
50           value-format="YYYY-MM-DD HH:mm:ss"
51           type="datetimerange"
52           start-placeholder="开始日期"
53           end-placeholder="结束日期"
54           :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
55           class="!w-360px"
56         />
57       </el-form-item>
58       <el-form-item>
59         <el-button @click="handleQuery">
60           <Icon icon="ep:search" class="mr-5px"/>
61           搜索
62         </el-button>
63         <el-button @click="resetQuery">
64           <Icon icon="ep:refresh" class="mr-5px"/>
65           重置
66         </el-button>
67         <el-button
68           type="primary"
69           plain
70           @click="openForm('create')"
71           v-hasPermi="['infra:monitor-disk:create']"
72         >
73           <Icon icon="ep:plus" class="mr-5px"/>
74           新增
75         </el-button>
76         <el-button
77           type="success"
78           plain
79           @click="handleExport"
80           :loading="exportLoading"
81           v-hasPermi="['infra:monitor-disk:export']"
82         >
83           <Icon icon="ep:download" class="mr-5px"/>
84           导出
85         </el-button>
86       </el-form-item>
87       <el-form-item style="float: right">
88         <el-button
89           v-if="showType == 'chart'"
90           type="warning"
91           style="font-weight: bold"
92           plain
93           @click="switchShow('data')">
94           <Icon icon="fa-solid:th-list" class="mr-5px"/>
95           列表展示
96         </el-button>
97         <el-button
98           v-if="showType == 'data'"
99           type="danger"
100           style="font-weight: bold"
101           plain
102           @click="switchShow('chart')">
103           <Icon icon="fa-solid:chart-pie" class="mr-5px"/>
104           图例展示
105         </el-button>
106       </el-form-item>
107     </el-form>
108   </ContentWrap>
109
110   <ContentWrap v-if="showType == 'chart'">
111     <!-- 磁盘使用率折线图 -->
112     <el-skeleton :loading="echartsLoading" animated>
113       <Echart :height="320" :options="diskChartOptions"/>
114     </el-skeleton>
115     <!-- 磁盘使用率饼图 -->
116     <h3 style="margin-top: 20px; margin-bottom: 10px">主机磁盘使用率</h3>
117     <div v-for="host in hostList" :key="host.name" class="host">
118       <div class="host-child">
119         <h4>主机名:{{ host.name }}&nbsp;&nbsp;&nbsp;&nbsp;主机IP:{{ host.ip }}</h4>
120         <el-skeleton :loading="echartsLoading" animated>
121           <div class="disks">
122             <div v-for="disk in host.disks" :key="disk.name" class="disk">
123               <h4 id="diskTitle">{{ disk.disk }}</h4>
124               <PieChart :used="disk.used" :total="disk.total" />
125               <div class="disk-info">
126                 <div style="margin-bottom: 6px; font-size: 16px"><span style="color: #b9292b ;font-weight: bolder">{{ disk.total != 0 ? ((disk.used / disk.total) * 100).toFixed(1) : 0.0 }}% </span>已使用</div>
127                 <div style="font-weight: bolder">{{ disk.used }}GB / {{ disk.total }}GB</div>
128               </div>
129             </div>
130           </div>
131         </el-skeleton>
132       </div>
133     </div>
134 <!--    <div v-for="(host, hostIndex) in hostList" :key="hostIndex">-->
135 <!--      <div style="margin-top: 10px">-->
136 <!--        <el-skeleton :loading="echartsLoading" animated>-->
137 <!--          {{ hostIndex }} &nbsp;&nbsp;&nbsp;&nbsp;主机名: {{ host.name }}&nbsp;&nbsp;&nbsp;&nbsp;-->
138 <!--          服务器IP:{{ host.ip }}-->
139 <!--          <div v-for="(disk, diskIndex) in host.disks" :key="diskIndex">-->
140 <!--            <h3>{{ disk.name }}</h3>-->
141 <!--            <div :ref="el => chartRefs[hostIndex][diskIndex] = el"-->
142 <!--                 :style="{ width: '300px', height: '300px' }"></div>-->
143 <!--          </div>-->
144 <!--        </el-skeleton>-->
145 <!--      </div>-->
146 <!--    </div>-->
147   </ContentWrap>
148
149   <!-- 列表 -->
150   <ContentWrap v-if="showType == 'data'">
151     <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
152       <el-table-column label="主机名称" align="center" prop="hostName"/>
153       <el-table-column label="服务器ip" align="center" prop="hostIp"/>
154       <el-table-column label="盘符" align="center" prop="disk"/>
155       <el-table-column label="磁盘名" align="center" prop="diskName"/>
156       <el-table-column label="总空间" align="center" prop="spaceTotal"/>
157       <el-table-column label="已用空间" align="center" prop="spaceUsed"/>
158       <el-table-column label="可用空间" align="center" prop="spaceUsable"/>
159       <el-table-column label="空间使用比例" align="center" prop="spaceRatio"/>
160       <el-table-column
161         label="创建时间"
162         align="center"
163         prop="createTime"
164         :formatter="dateFormatter"
165         width="180px"
166       />
167       <el-table-column label="操作" align="center">
168         <template #default="scope">
169           <el-button
170             link
171             type="primary"
172             @click="openForm('update', scope.row.id)"
173             v-hasPermi="['infra:monitor-disk:query']"
174           >
175             详情
176           </el-button>
177           <el-button
178             link
179             type="danger"
180             @click="handleDelete(scope.row.id)"
181             v-hasPermi="['infra:monitor-disk:delete']"
182           >
183             删除
184           </el-button>
185         </template>
186       </el-table-column>
187     </el-table>
188     <!-- 分页 -->
189     <Pagination
190       :total="total"
191       v-model:page="queryParams.pageNo"
192       v-model:limit="queryParams.pageSize"
193       @pagination="getList"
194     />
195   </ContentWrap>
196
197   <!-- 表单弹窗:添加/修改 -->
198   <MonitorDiskForm ref="formRef" @success="getList"/>
199 </template>
200
201 <script setup lang="ts">
202 import {dateFormatter} from '@/utils/formatTime'
203 import download from '@/utils/download'
204 import {MonitorDiskApi, MonitorDiskVO} from '@/api/infra/monitordisk'
205 import MonitorDiskForm from './MonitorDiskForm.vue'
206 import {EChartsOption} from "echarts";
207 import * as echarts from 'echarts';
208 import {formatTime} from "@/utils";
209 import {formatDate} from "@vueuse/core";
210 import PieChart from '@/components/MonitorDiskPie/PieChart.vue';
211
212 /** 磁盘监控日志 列表 */
213 defineOptions({name: 'MonitorDisk'})
214
215 const message = useMessage() // 消息弹窗
216 const {t} = useI18n() // 国际化
217
218 const loading = ref(true) // 列表的加载中
219 const list = ref<MonitorDiskVO[]>([]) // 列表的数据
220 const total = ref(0) // 列表的总页数
221 const queryParams = reactive({
222   pageNo: 1,
223   pageSize: 10,
224   hostName: undefined,
225   hostIp: undefined,
226   disk: undefined,
227   diskName: undefined,
228   spaceTotal: undefined,
229   spaceUsed: undefined,
230   spaceUsable: undefined,
231   spaceRatio: undefined,
232   createTime: [],
233 })
234 const queryFormRef = ref() // 搜索的表单
235 const exportLoading = ref(false) // 导出的加载中
236 const echartsLoading = ref(true) // 图表加载中
237 const showType = ref() //展示类型(chart-图例,data-数据)
238
239 const hostList = ref([
240   {
241     name: 'Thinkpad-E14',
242     ip: '172.16.216.133',
243     disks: [
244       {disk: '磁盘C', used: 70, total: 200},
245       {disk: '磁盘D', used: 40, total: 60}
246     ]
247   },
248   {
249     name: 'Thinkpad-E16',
250     ip: '172.16.216.133',
251     disks: [
252       {disk: '磁盘C', used: 80, total: 500},
253       {disk: '磁盘D', used: 20, total: 500}
254     ]
255   }
256 ]);
257
258 const chartRefs = ref([]);
259
260 /** 查询列表 */
261 const getList = async () => {
262   loading.value = true
263   try {
264     const data = await MonitorDiskApi.getMonitorDiskPage(queryParams)
265     list.value = data.list
266     total.value = data.total
267   } finally {
268     loading.value = false
269   }
270 }
271
272 /** 搜索按钮操作 */
273 const handleQuery = () => {
274   queryParams.pageNo = 1
275   if (showType.value == 'data') {
276     getList()
277   } else {
278     getMonitorDiskDataList()
279     usedDiskInstance()
280   }
281 }
282
283 /** 重置按钮操作 */
284 const resetQuery = () => {
285   queryFormRef.value.resetFields()
286   handleQuery()
287 }
288
289 /** 添加/修改操作 */
290 const formRef = ref()
291 const openForm = (type: string, id?: number) => {
292   formRef.value.open(type, id)
293 }
294
295 /** 删除按钮操作 */
296 const handleDelete = async (id: number) => {
297   try {
298     // 删除的二次确认
299     await message.delConfirm()
300     // 发起删除
301     await MonitorDiskApi.deleteMonitorDisk(id)
302     message.success(t('common.delSuccess'))
303     // 刷新列表
304     await getList()
305   } catch {
306   }
307 }
308
309 /** 导出按钮操作 */
310 const handleExport = async () => {
311   try {
312     // 导出的二次确认
313     await message.exportConfirm()
314     // 发起导出
315     exportLoading.value = true
316     const data = await MonitorDiskApi.exportMonitorDisk(queryParams)
317     download.excel(data, '磁盘监控日志.xls')
318   } catch {
319   } finally {
320     exportLoading.value = false
321   }
322 }
323
324 /** 堆叠面积图配置 */
325 const diskChartOptions = reactive<EChartsOption>({
326   title: {
327     text: '磁盘空间折线图'
328   },
329   dataset: {
330     dimensions: [],
331     source: []
332   },
333   grid: {
334     left: 30,
335     right: 20,
336     bottom: 10,
337     top: 70,
338     containLabel: true
339   },
340   legend: {
341     top: 0
342   },
343   series: [
344     {
345       name: 'disk', type: 'line',
346       emphasis: {
347         focus: 'series'
348       }, smooth: false
349     }
350   ],
351   toolbox: {
352     feature: {
353       // 数据区域缩放
354       dataZoom: {
355         yAxisIndex: false // Y轴不缩放
356       },
357       brush: {
358         type: ['lineX', 'clear'] // 区域缩放按钮、还原按钮
359       },
360       saveAsImage: {show: true, name: '物理内存日志图片'} // 保存为图片
361     }
362   },
363   tooltip: {
364     trigger: 'axis',
365     axisPointer: {
366       type: 'cross',
367       label: {
368         backgroundColor: '#6a7985'
369       }
370     },
371     padding: [5, 10]
372   },
373   xAxis: {
374     type: 'category',
375     boundaryGap: false,
376     axisTick: {
377       show: false
378     }
379   },
380   yAxis: {
381     name: "单位(百分比)",
382     nameTextStyle: {
383       color: "#aaa",
384       nameLocation: "start",
385     },
386   },
387 }) as EChartsOption
388
389 /** 查询统计数据列表 */
390 const getMonitorDiskDataList = async () => {
391   const list = await MonitorDiskApi.getMonitorDiskList(queryParams)
392   if (list != null && list != undefined && list.length > 0) {
393     diskChartOptions.dataset['dimensions'] = Object.keys(list[0])
394     diskChartOptions.series = diskChartOptions.dataset['dimensions'].map(item => ({
395       name: item.name,
396       type: 'line',
397       emphasis: {
398         focus: 'series'
399       },
400       smooth: false
401     }));
402     diskChartOptions.series.splice(0, 1)
403     for (let item of list) {
404       item.createTime = formatTime(item.createTime, 'yyyy-MM-dd HH:mm:ss')
405     }
406   }
407   // 更新 Echarts 数据
408   diskChartOptions.dataset['source'] = list
409   echartsLoading.value = false
410 }
411
412 const usedDiskInstance = async () => {
413   const list = await MonitorDiskApi.getMonitorDiskInfo(queryParams)
414   hostList.value = list
415   // 仪表盘详情,用于显示数据。
416 }
417
418 /** 切换展示方式 */
419 const switchShow = (type: String) => {
420   showType.value = type
421   if (showType.value == 'data') {
422     getList()
423   } else {
424     getMonitorDiskDataList()
425     usedDiskInstance()
426   }
427 }
428
429 let intervalId;
430 /** 初始化 **/
431 onMounted(() => {
432   showType.value = 'data';
433   const currentDate = new Date();
434   const previousDate = new Date(currentDate);
435   previousDate.setDate(currentDate.getDate() - 1);
436   queryParams.createTime[0] = formatDate(previousDate, 'YYYY-MM-DD HH:mm:ss');
437   queryParams.createTime[1] = formatDate(currentDate, 'YYYY-MM-DD HH:mm:ss');
438   intervalId = setInterval(() => {
439     if (showType.value == 'data') {
440       getList()
441     } else {
442       getMonitorDiskDataList()
443       usedDiskInstance()
444     }
445   }, 30000);
446 })
447
448 onUnmounted(() => {
449   clearInterval(intervalId);
450 });
451 </script>
452 <style>
453   .host {
454     margin-bottom: 20px;
455     margin-right: 20px;
456     border-radius: 8px;
457   }
458   .host-child {
459     background: rgba(200, 200, 200, 0.3);
460     border-radius: 8px;
461     padding: 10px;
462   }
463   .disks {
464     display: grid;
465     grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
466     gap: 20px;
467     margin-top: 20px;
468   }
469   .disk {
470     width: 250px;
471     background: rgba(100, 100, 150, 0.2);
472     padding: 15px;
473     border-radius: 16px;
474     text-align: center;
475     box-shadow: 0 2px 4px rgba(0,0,0,0.1);
476   }
477
478   #diskTitle {
479     margin-bottom: 10px
480   }
481
482   .disk-info {
483     margin-top: 10px;
484     font-size: 0.9em;
485     color: #666;
486   }
487 </style>