device.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <template>
  2. <div class="play-dashboard-root">
  3. <div class="play-dashboard-page">
  4. <el-container>
  5. <el-header>
  6. <el-card shadow="hover" style="margin-top: 10px">
  7. <el-row justify="end" align="middle">
  8. <el-col :span="19" style="text-align: right">
  9. <el-radio-group v-model="timeRadio" size="small" @change="handleDateRangeChange">
  10. <!-- <el-radio-button label="今日" value="today" />-->
  11. <el-radio-button label="近7天" value="week"/>
  12. <el-radio-button label="近30天" value="month"/>
  13. <el-radio-button label="自定义" value="diy"/>
  14. </el-radio-group>
  15. </el-col>
  16. <el-col :span="5" style="text-align: right">
  17. <el-date-picker v-model="dateRange" type="daterange" @change="handleDateRangeChange" range-separator="-"
  18. start-placeholder="开始日期"
  19. :disabled="diyFlag" :clearable="false" end-placeholder="结束日期"
  20. style="margin-left: 10px; margin-right: 30px"/>
  21. </el-col>
  22. </el-row>
  23. </el-card>
  24. </el-header>
  25. <el-main style="margin-top: 20px">
  26. <el-row :gutter="20">
  27. <el-col :span="6" v-for="(item, index) in stats" :key="index">
  28. <el-card shadow="hover" class="stat-card">
  29. <h3>{{ item.label }}</h3>
  30. <p :class="['number', item.class]">{{ item.value }}</p>
  31. </el-card>
  32. </el-col>
  33. </el-row>
  34. <el-row :gutter="20" style="margin-top: 20px">
  35. <el-col :span="18">
  36. <el-row :gutter="20">
  37. <el-col :span="8">
  38. <el-card shadow="hover">
  39. <h3>类型占比</h3>
  40. <div ref="typePie" class="chart-placeholder"></div>
  41. </el-card>
  42. </el-col>
  43. <el-col :span="8">
  44. <el-card shadow="hover">
  45. <h3>告警级别</h3>
  46. <div ref="alarmLevel" class="chart-placeholder"></div>
  47. </el-card>
  48. </el-col>
  49. <el-col :span="8">
  50. <el-card shadow="hover">
  51. <h3>告警问题统计</h3>
  52. <div ref="alarmCount" class="chart-placeholder"></div>
  53. </el-card>
  54. </el-col>
  55. </el-row>
  56. <el-row :gutter="20" style="margin-top: 20px">
  57. <el-col :span="12">
  58. <el-card shadow="hover">
  59. <h3>在线时长排行</h3>
  60. <div ref="onlineTime" class="chart-placeholder"></div>
  61. </el-card>
  62. </el-col>
  63. <el-col :span="12">
  64. <el-card shadow="hover">
  65. <h3>告警数量排行</h3>
  66. <div ref="alarmNum" class="chart-placeholder"></div>
  67. </el-card>
  68. </el-col>
  69. </el-row>
  70. </el-col>
  71. <el-col :span="6">
  72. <el-card shadow="hover">
  73. <h3>告警清单</h3>
  74. <el-table v-loading="loading" :data="alarmList" row-key="id">
  75. <el-table-column label="设备名称" align="left" prop="deviceName" width="100" :show-overflow-tooltip="true" />
  76. <el-table-column label="告警等级" align="center" prop="errorLevel" width="80">
  77. <template #default="scope">
  78. <dict-tag :options="smsb_device_error_level" :value="scope.row.errorLevel" />
  79. </template>
  80. </el-table-column>
  81. <el-table-column label="告警类型" align="center" prop="errorType" width="80">
  82. <template #default="scope">
  83. <dict-tag :options="smsb_device_error_type" :value="scope.row.errorType"/>
  84. </template>
  85. </el-table-column>
  86. <el-table-column label="创建时间" align="left" prop="createTime" width="160"/>
  87. </el-table>
  88. </el-card>
  89. </el-col>
  90. </el-row>
  91. </el-main>
  92. </el-container>
  93. </div>
  94. </div>
  95. </template>
  96. <script setup lang="ts">
  97. // console.log('[device.vue] setup called');
  98. import {reactive} from 'vue';
  99. import {deviceStatistics} from '@/api/smsb/device/device';
  100. import * as echarts from 'echarts';
  101. import {DeviceErrorRecordQuery, DeviceErrorRecordVO} from '@/api/smsb/device/errorRecord_type';
  102. import {
  103. alarmCountByLevel,
  104. alarmCountByType,
  105. alarmNumTop,
  106. listDeviceErrorRecord,
  107. onlineTimeTop
  108. } from '@/api/smsb/device/errorRecord';
  109. const alarmList = ref<DeviceErrorRecordVO[]>([]);
  110. const {proxy} = getCurrentInstance() as ComponentInternalInstance;
  111. const {
  112. smsb_device_error_level,
  113. smsb_device_error_type
  114. } = toRefs<any>(proxy?.useDict('smsb_device_error_level', 'smsb_device_error_type'));
  115. const total = ref(0);
  116. const loading = ref(true);
  117. const timeRadio = ref('week');
  118. const dateRange = ref(['2025-01-01', '2025-01-01']);
  119. const typePie = ref();
  120. const alarmLevel = ref();
  121. const alarmCount = ref();
  122. const alarmNum = ref();
  123. const onlineTime = ref();
  124. const diyFlag = ref(true);
  125. const dialogData = reactive<DialogPageData<DeviceErrorRecordQuery>>({
  126. dialogQueryParams: {
  127. pageNum: 1,
  128. pageSize: 14,
  129. deviceId: undefined,
  130. params: {}
  131. }
  132. });
  133. const { dialogQueryParams } = toRefs(dialogData);
  134. const stats = reactive([
  135. { label: '设备总量', value: 0, class: '' },
  136. { label: '已上线设备', value: 0, class: 'success' },
  137. { label: '离线设备', value: 0, class: 'danger' },
  138. { label: '待接入设备', value: 0, class: 'warning' }
  139. ]);
  140. const handleDateRangeChange = () => {
  141. // console.log('[device.vue] handleDateRangeChange called');
  142. const rangeType = timeRadio.value;
  143. const today = new Date();
  144. const startDate = new Date();
  145. const endDate = new Date();
  146. switch (rangeType) {
  147. case 'today':
  148. break;
  149. case 'week':
  150. startDate.setDate(today.getDate() - 7);
  151. dateRange.value = [formatDate(startDate), formatDate(endDate)];
  152. diyFlag.value = true;
  153. break;
  154. case 'month':
  155. startDate.setMonth(today.getMonth() - 1);
  156. dateRange.value = [formatDate(startDate), formatDate(endDate)];
  157. diyFlag.value = true;
  158. break;
  159. case "diy" :
  160. diyFlag.value = false;
  161. dateRange.value = [formatDate(new Date(dateRange.value[0])), formatDate(new Date(dateRange.value[1]))];
  162. break;
  163. default:
  164. break;
  165. // throw new Error('Invalid range type');
  166. }
  167. console.log(dateRange.value);
  168. alarmCountData();
  169. getAlarmLevel();
  170. getAlarmNumTop();
  171. getOnlineTimeTop();
  172. };
  173. const formatDate = (date: Date) => {
  174. const year = date.getFullYear();
  175. const month = String(date.getMonth() + 1).padStart(2, '0');
  176. const day = String(date.getDate()).padStart(2, '0');
  177. return `${year}-${month}-${day}`;
  178. };
  179. const getOnlineTimeTop = async () => {
  180. // console.log('[device.vue] getOnlineTimeTop called');
  181. const params = {
  182. startTime: dateRange.value[0],
  183. endTime: dateRange.value[1]
  184. };
  185. const res = await onlineTimeTop(params);
  186. if (!onlineTime.value) {
  187. // console.log('[onlineTime] ref is null when initializing echarts');
  188. } else {
  189. // console.log('[onlineTime] ref:', onlineTime.value);
  190. echarts.dispose(onlineTime.value);
  191. // console.log('[onlineTime] echarts instance disposed before init');
  192. }
  193. const onlineTimeTopInstance = echarts.init(onlineTime.value, 'macaroons');
  194. // console.log('[onlineTime] echarts instance created:', onlineTimeTopInstance);
  195. onlineTimeTopInstance.setOption({
  196. title: {
  197. text: ''
  198. },
  199. tooltip: {
  200. trigger: 'axis',
  201. axisPointer: {
  202. type: 'shadow'
  203. }
  204. },
  205. legend: {},
  206. grid: {
  207. left: '3%',
  208. right: '4%',
  209. bottom: '3%',
  210. containLabel: true
  211. },
  212. xAxis: {
  213. type: 'value',
  214. boundaryGap: [0, 0.01]
  215. },
  216. yAxis: {
  217. type: 'category',
  218. data: res.data.deviceNameList
  219. },
  220. series: [
  221. {
  222. name: '',
  223. type: 'bar',
  224. data: res.data.onlineTimeList
  225. }
  226. ]
  227. });
  228. };
  229. const getAlarmNumTop = async () => {
  230. // console.log('[device.vue] getAlarmNumTop called');
  231. const params = {
  232. startTime: dateRange.value[0],
  233. endTime: dateRange.value[1]
  234. };
  235. const res = await alarmNumTop(params);
  236. if (!alarmNum.value) {
  237. // console.log('[alarmNum] ref is null when initializing echarts');
  238. } else {
  239. // console.log('[alarmNum] ref:', alarmNum.value);
  240. echarts.dispose(alarmNum.value);
  241. // console.log('[alarmNum] echarts instance disposed before init');
  242. }
  243. const alarmNumTopInstance = echarts.init(alarmNum.value, 'macaroons');
  244. // console.log('[alarmNum] echarts instance created:', alarmNumTopInstance);
  245. alarmNumTopInstance.setOption({
  246. title: {
  247. text: ''
  248. },
  249. tooltip: {
  250. trigger: 'axis',
  251. axisPointer: {
  252. type: 'shadow'
  253. }
  254. },
  255. legend: {},
  256. grid: {
  257. left: '3%',
  258. right: '4%',
  259. bottom: '3%',
  260. containLabel: true
  261. },
  262. xAxis: {
  263. type: 'value',
  264. boundaryGap: [0, 0.01]
  265. },
  266. yAxis: {
  267. type: 'category',
  268. data: res.data.deviceNameList
  269. },
  270. series: [
  271. {
  272. name: '',
  273. type: 'bar',
  274. data: res.data.alarmNumList
  275. }
  276. ]
  277. });
  278. };
  279. const alarmCountData = async () => {
  280. // console.log('[device.vue] alarmCountData called');
  281. const params = {
  282. startTime: dateRange.value[0],
  283. endTime: dateRange.value[1]
  284. };
  285. const res = await alarmCountByType(params);
  286. if (!alarmCount.value) {
  287. // console.log('[alarmCount] ref is null when initializing echarts');
  288. } else {
  289. // console.log('[alarmCount] ref:', alarmCount.value);
  290. echarts.dispose(alarmCount.value);
  291. // console.log('[alarmCount] echarts instance disposed before init');
  292. }
  293. const alarmTypeInstance = echarts.init(alarmCount.value, 'macaroons');
  294. // console.log('[alarmCount] echarts instance created:', alarmTypeInstance);
  295. alarmTypeInstance.setOption({
  296. xAxis: {
  297. type: 'category',
  298. data: res.data.alarmType
  299. },
  300. yAxis: {
  301. type: 'value'
  302. },
  303. series: [
  304. {
  305. data: res.data.alarmCount,
  306. type: 'bar'
  307. }
  308. ]
  309. });
  310. };
  311. const getAlarmLevel = async () => {
  312. // console.log('[device.vue] getAlarmLevel called');
  313. const params = {
  314. startTime: dateRange.value[0],
  315. endTime: dateRange.value[1]
  316. };
  317. const res = await alarmCountByLevel(params);
  318. if (!alarmLevel.value) {
  319. // console.log('[alarmLevel] ref is null when initializing echarts');
  320. } else {
  321. // console.log('[alarmLevel] ref:', alarmLevel.value);
  322. echarts.dispose(alarmLevel.value);
  323. // console.log('[alarmLevel] echarts instance disposed before init');
  324. }
  325. const alarmLevelInstance = echarts.init(alarmLevel.value, 'macaroons');
  326. // console.log('[alarmLevel] echarts instance created:', alarmLevelInstance);
  327. alarmLevelInstance.setOption({
  328. title: {
  329. text: ''
  330. },
  331. tooltip: {
  332. trigger: 'axis'
  333. },
  334. legend: {
  335. data: ['普通', '紧急']
  336. },
  337. grid: {
  338. left: '3%',
  339. right: '4%',
  340. bottom: '3%',
  341. containLabel: true
  342. },
  343. toolbox: {
  344. feature: {
  345. saveAsImage: {}
  346. }
  347. },
  348. xAxis: {
  349. type: 'category',
  350. boundaryGap: false,
  351. data: res.data.alarmDateList
  352. },
  353. yAxis: {
  354. type: 'value'
  355. },
  356. series: [
  357. {
  358. name: '普通',
  359. type: 'line',
  360. stack: 'Total',
  361. data: res.data.normalAlamList
  362. },
  363. {
  364. name: '紧急',
  365. type: 'line',
  366. stack: 'Total',
  367. data: res.data.dangerAlamList
  368. }
  369. ]
  370. });
  371. };
  372. const getDeviceStatistics = async () => {
  373. // console.log('[device.vue] getDeviceStatistics called');
  374. const res = await deviceStatistics();
  375. stats.forEach((item) => {
  376. switch (item.label) {
  377. case '设备总量':
  378. item.value = res.data.totalNum;
  379. break;
  380. case '已上线设备':
  381. item.value = res.data.onlineNum;
  382. break;
  383. case '离线设备':
  384. item.value = res.data.offlineNum;
  385. break;
  386. case '待接入设备':
  387. item.value = res.data.initNum;
  388. break;
  389. }
  390. });
  391. if (!typePie.value) {
  392. // console.log('[typePie] ref is null when initializing echarts');
  393. } else {
  394. // console.log('[typePie] ref:', typePie.value);
  395. echarts.dispose(typePie.value);
  396. // console.log('[typePie] echarts instance disposed before init');
  397. }
  398. const typePieInstance = echarts.init(typePie.value, 'macaroons');
  399. // console.log('[typePie] echarts instance created:', typePieInstance);
  400. typePieInstance.setOption({
  401. title: {
  402. text: '',
  403. subtext: '',
  404. left: 'center'
  405. },
  406. tooltip: {
  407. trigger: 'item'
  408. },
  409. legend: {
  410. orient: 'vertical',
  411. left: 'left'
  412. },
  413. series: [
  414. {
  415. name: '类型占比',
  416. type: 'pie',
  417. radius: '65%',
  418. data: [
  419. { value: (res.data.onlineNum / res.data.totalNum) * 100, name: '在线' },
  420. { value: (res.data.offlineNum / res.data.totalNum) * 100, name: '离线' },
  421. { value: (res.data.initNum / res.data.totalNum) * 100, name: '待接入' }
  422. ],
  423. emphasis: {
  424. itemStyle: {
  425. shadowBlur: 10,
  426. shadowOffsetX: 0,
  427. shadowColor: 'rgba(0, 0, 0, 0.5)'
  428. }
  429. }
  430. }
  431. ]
  432. });
  433. };
  434. const getAlarmList = async () => {
  435. // console.log('[device.vue] getAlarmList called');
  436. loading.value = true;
  437. const res = await listDeviceErrorRecord(dialogQueryParams.value);
  438. alarmList.value = res.rows;
  439. total.value = res.total;
  440. loading.value = false;
  441. };
  442. onMounted(() => {
  443. // console.log('[device.vue] onMounted called');
  444. handleDateRangeChange();
  445. getDeviceStatistics();
  446. getAlarmList();
  447. });
  448. </script>
  449. <style scoped>
  450. .stat-card {
  451. text-align: center;
  452. }
  453. .number {
  454. font-size: 24px;
  455. font-weight: bold;
  456. }
  457. .success {
  458. color: green;
  459. }
  460. .danger {
  461. color: red;
  462. }
  463. .warning {
  464. color: orange;
  465. }
  466. .chart-placeholder {
  467. height: 250px;
  468. /*background: #f5f5f5;*/
  469. border-radius: 8px;
  470. }
  471. .play-dashboard-root {
  472. height: 100vh;
  473. overflow-y: auto;
  474. }
  475. </style>