1
0

index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="p-4">
  3. <a-card>
  4. <!-- Redis 信息实时监控 -->
  5. <a-row :gutter="8">
  6. <a-col :sm="24" :xl="12">
  7. <div ref="chartRef" style="width: 100%; height: 300px"></div>
  8. </a-col>
  9. <a-col :sm="24" :xl="12">
  10. <div ref="chartRef2" style="width: 100%; height: 300px"></div>
  11. </a-col>
  12. </a-row>
  13. </a-card>
  14. <BasicTable @register="registerTable" :api="getInfo" />
  15. </div>
  16. </template>
  17. <script lang="ts" name="monitor-redis" setup>
  18. import { onMounted, ref, reactive, Ref, onUnmounted } from 'vue';
  19. import { BasicTable, useTable, TableAction } from '/@/components/Table';
  20. import { getInfo, getRedisInfo } from './redis.api';
  21. import dayjs from 'dayjs';
  22. import { columns } from './redis.data';
  23. import { useMessage } from '/@/hooks/web/useMessage';
  24. import { useECharts } from '/@/hooks/web/useECharts';
  25. const dataSource = ref([]);
  26. const chartRef = ref<HTMLDivElement | null>(null);
  27. const chartRef2 = ref<HTMLDivElement | null>(null);
  28. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  29. const { setOptions: setOptions2, echarts: echarts2 } = useECharts(chartRef2 as Ref<HTMLDivElement>);
  30. const loading = ref(false);
  31. let timer = null;
  32. const { createMessage } = useMessage();
  33. const key = reactive({
  34. title: {
  35. text: 'Redis Key 实时数量(个)',
  36. },
  37. xAxis: {
  38. type: 'category',
  39. boundaryGap: false,
  40. data: [],
  41. },
  42. yAxis: {
  43. type: 'value',
  44. },
  45. series: [
  46. {
  47. data: [],
  48. type: 'line',
  49. areaStyle: {
  50. color: '#ff6987',
  51. },
  52. lineStyle: {
  53. color: '#dc143c',
  54. width: 10,
  55. type: 'solid',
  56. },
  57. },
  58. ],
  59. });
  60. const memory = reactive({
  61. title: {
  62. text: 'Redis 内存实时占用情况(KB)',
  63. },
  64. xAxis: {
  65. type: 'category',
  66. boundaryGap: false,
  67. data: [],
  68. },
  69. yAxis: {
  70. type: 'value',
  71. },
  72. series: [
  73. {
  74. data: [],
  75. type: 'line',
  76. areaStyle: {
  77. color: '#74bcff',
  78. },
  79. lineStyle: {
  80. color: '#1890ff',
  81. width: 10,
  82. type: 'solid',
  83. },
  84. },
  85. ],
  86. });
  87. const [registerTable, { reload }] = useTable({
  88. columns,
  89. showIndexColumn: false,
  90. pagination: false,
  91. bordered: true,
  92. });
  93. // 获取一组数据中最大和最小的值
  94. function getMaxAndMin(dataSource, field) {
  95. let maxValue = null,
  96. minValue = null;
  97. dataSource.forEach((item) => {
  98. let value = Number.parseInt(item[field]);
  99. // max
  100. if (maxValue == null) {
  101. maxValue = value;
  102. } else if (value > maxValue) {
  103. maxValue = value;
  104. }
  105. // min
  106. if (minValue == null) {
  107. minValue = value;
  108. } else if (value < minValue) {
  109. minValue = value;
  110. }
  111. });
  112. return [maxValue, minValue];
  113. }
  114. function loadRedisInfo() {
  115. getInfo().then((res) => {
  116. dataSource.value = res.result;
  117. });
  118. }
  119. function initCharts() {
  120. setOptions(memory);
  121. setOptions2(key);
  122. }
  123. /** 开启定时器 */
  124. function openTimer() {
  125. loadData();
  126. closeTimer();
  127. timer = setInterval(() => {
  128. loadData();
  129. }, 5000);
  130. }
  131. /** 关闭定时器 */
  132. function closeTimer() {
  133. if (timer) clearInterval(timer);
  134. }
  135. function loadData() {
  136. getRedisInfo()
  137. .then((res) => {
  138. let time = dayjs().format('hh:mm:ss');
  139. let [{ dbSize: currentSize }, memoryInfo] = res;
  140. let currentMemory = memoryInfo.used_memory / 1000;
  141. // push 数据
  142. key.xAxis.data.push(time);
  143. key.series[0].data.push(currentSize);
  144. memory.xAxis.data.push(time);
  145. memory.series[0].data.push(currentMemory);
  146. // 最大长度为6
  147. if (key.series[0].data.length > 6) {
  148. key.xAxis.data.splice(0, 1);
  149. key.series[0].data.splice(0, 1);
  150. memory.xAxis.data.splice(0, 1);
  151. memory.series[0].data.splice(0, 1);
  152. }
  153. setOptions(memory, false);
  154. setOptions2(key, false);
  155. // 计算 Key 最大最小值
  156. //let keyPole = getMaxAndMin(key.dataSource, 'y');
  157. //key.max = Math.floor(keyPole[0]) + 10;
  158. //key.min = Math.floor(keyPole[1]) - 10;
  159. //if (key.min < 0) this.key.min = 0;
  160. // 计算 Memory 最大最小值
  161. //let memoryPole = getMaxAndMin(memory.dataSource, 'y');
  162. //memory.max = Math.floor(memoryPole[0]) + 100;
  163. //memory.min = Math.floor(memoryPole[1]) - 100;
  164. //if (memory.min < 0) memory.min = 0;
  165. })
  166. .catch((e) => {
  167. //closeTimer()
  168. });
  169. }
  170. onMounted(() => {
  171. initCharts();
  172. openTimer();
  173. setTimeout(() => {
  174. loadData();
  175. }, 1000);
  176. });
  177. // update-begin--author:liaozhiyang---date:220230719---for:【issues-615】系统监控中的REDIS监控页面打开,再关闭后,没有关闭计时器
  178. onUnmounted(() => {
  179. closeTimer();
  180. });
  181. // update-end--author:liaozhiyang---date:220230719---for:【issues-615】系统监控中的REDIS监控页面打开,再关闭后,没有关闭计时器
  182. </script>