index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <template>
  2. <div>
  3. <div class="app-container">
  4. <el-form
  5. :model="queryParams"
  6. ref="queryForm"
  7. :inline="true"
  8. v-show="showSearch"
  9. size="small"
  10. >
  11. <!-- <el-form-item prop="domainName">-->
  12. <!-- <el-select-->
  13. <!-- v-model="queryParams.domainName"-->
  14. <!-- placeholder="请选择域名"-->
  15. <!-- clearable-->
  16. <!-- style="width: 150px"-->
  17. <!-- >-->
  18. <!-- <el-option-->
  19. <!-- v-for="item in options"-->
  20. <!-- :key="item.label"-->
  21. <!-- :label="item.label"-->
  22. <!-- :value="item.label"-->
  23. <!-- >-->
  24. <!-- </el-option>-->
  25. <!-- </el-select>-->
  26. <!-- </el-form-item>-->
  27. <el-form-item>
  28. <span :class="timeType == 0 ? 'type active' : 'type'" @click="handleTimeType(0)">今日</span>
  29. <span :class="timeType == 1 ? 'type active' : 'type'" @click="handleTimeType(1)">昨日</span>
  30. <span :class="timeType == 2 ? 'type active' : 'type'" @click="handleTimeType(2)">7天内</span>
  31. <span :class="timeType == 3 ? 'type active' : 'type'" @click="handleTimeType(3)">30天内</span>
  32. <el-date-picker
  33. v-model="datetime"
  34. type="datetimerange"
  35. value-format="yyyy-MM-dd HH:mm:ss"
  36. range-separator="至"
  37. start-placeholder="开始日时间"
  38. end-placeholder="结束时间"
  39. :clearable="false"
  40. :picker-options="pickerOptions"
  41. @change="timePickerchange"
  42. >
  43. </el-date-picker>
  44. </el-form-item>
  45. <el-form-item label="粒度" prop="interval">
  46. <el-select v-model="queryParams.interval" placeholder="请选择粒度" >
  47. <el-option label="5分钟" :value="0"></el-option>
  48. <el-option label="1小时" :value="1"></el-option>
  49. <el-option label="1天" :value="2"></el-option>
  50. </el-select>
  51. </el-form-item>
  52. <el-form-item>
  53. <el-button type="primary" icon="el-icon-search" size="mini" @click="getChartData">搜索</el-button>
  54. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  55. </el-form-item>
  56. </el-form>
  57. <!-- <el-row>-->
  58. <!-- <right-toolbar-->
  59. <!-- :showSearch.sync="showSearch"-->
  60. <!-- @queryTable="getChartData"-->
  61. <!-- ></right-toolbar>-->
  62. <!-- </el-row>-->
  63. </div>
  64. <div class="app-container" >
  65. <div class="chart-title">流量趋势<span>总量:{{ summary.flux }} MB</span></div>
  66. <div ref="trafficTrends" style="width: 100%; height: 400px;"></div>
  67. </div>
  68. <!-- 带宽趋势图 -->
  69. <div class="app-container">
  70. <div class="chart-title">峰值带宽趋势 <span>峰值:{{ summary.bw }} Mbps</span></div>
  71. <div ref="bwTrends" style="width: 100%; height: 400px;"></div>
  72. </div>
  73. <!-- 请求数量趋势图 -->
  74. <div class="app-container">
  75. <div class="chart-title">请求数量 <span>总量:{{ summary.req_num }} 次</span></div>
  76. <div ref="reqNumTrends" style="width: 100%; height: 400px;"></div>
  77. </div>
  78. <!-- 请求命中率趋势图 -->
  79. <div class="app-container">
  80. <div class="chart-title">请求命中率
  81. <span>命中率:{{ summary.hit_rate }} %</span>
  82. <span>命中次数:{{ summary.hit_num }} 次</span>
  83. </div>
  84. <div ref="hitRateTrends" style="width: 100%; height: 400px;"></div>
  85. </div>
  86. </div>
  87. </template>
  88. <script>
  89. import {parseTime} from "@/utils/common";
  90. import {cdnStatistics} from "@/api/common";
  91. import * as echarts from "echarts";
  92. export default {
  93. data() {
  94. return {
  95. flux:[],
  96. bw: [],
  97. reqNum:[],
  98. hitRate:[],
  99. summary: {
  100. flux:null,
  101. req_num:null,
  102. bw:null,
  103. hit_num:null,
  104. hit_rate:null,
  105. },
  106. statTypeOptions:[
  107. { value:'bw',
  108. label:"带宽"
  109. },
  110. { value:'flux',
  111. label:"流量"
  112. }
  113. ],
  114. options: [{
  115. label:"obs.shouzhujue.com"
  116. }],
  117. today: new Date(),
  118. tomorrow: new Date().setDate(new Date().getDate() + 1),
  119. yesterday: new Date().setDate(new Date().getDate() - 1),
  120. weekday: new Date().setDate(new Date().getDate() - 6),
  121. thirtyDaysAgo: new Date().setDate(new Date().getDate() - 29),
  122. // 查询参数
  123. queryParams: {
  124. startTime: undefined,
  125. endTime: undefined,
  126. domainName: "obs.jy.cc",
  127. statType: "flux,bw,req_num,hit_num",
  128. interval: 0,
  129. },
  130. // 时间范围类型
  131. timeType: 0,
  132. datetime: [],
  133. // 显示搜索条件
  134. showSearch: true,
  135. chart: null,
  136. bwChart: null,
  137. reqNumChart: null,
  138. hitRateChart: null,
  139. chartOptions: {
  140. tooltip: {
  141. trigger: "axis",
  142. },
  143. grid: {
  144. bottom: "10%",
  145. left: "5%",
  146. top: "15%",
  147. right: "5%",
  148. },
  149. xAxis: {
  150. name: "时间",
  151. axisLine: {
  152. show: true,
  153. },
  154. axisTick: {
  155. show: true,
  156. },
  157. splitLine: {
  158. show: false,
  159. },
  160. axisLabel: {
  161. formatter: (value) => {
  162. return this.parseTime(value, "{h}:{i}");
  163. },
  164. },
  165. },
  166. yAxis: {
  167. name: "流量(MB)",
  168. type: "value",
  169. axisLine: {
  170. show: false,
  171. },
  172. splitLine: {
  173. show: true,
  174. lineStyle: {
  175. type: "dashed",
  176. },
  177. },
  178. axisTick: {
  179. show: true,
  180. },
  181. },
  182. series: [
  183. {
  184. data: [],
  185. name: "流量",
  186. type: "line",
  187. smooth: true,
  188. symbol: "none",
  189. lineStyle: {
  190. color: "#409eff", //改变折线颜色
  191. },
  192. emphasis: {
  193. focus: "series",
  194. },
  195. },
  196. ],
  197. },
  198. bwChartOptions: {
  199. tooltip: {
  200. trigger: "axis",
  201. },
  202. grid: {
  203. bottom: "10%",
  204. left: "5%",
  205. top: "15%",
  206. right: "5%",
  207. },
  208. xAxis: {
  209. name: "时间",
  210. axisLine: {
  211. show: true,
  212. },
  213. axisTick: {
  214. show: true,
  215. },
  216. splitLine: {
  217. show: false,
  218. },
  219. axisLabel: {
  220. formatter: (value) => {
  221. return this.parseTime(value, "{h}:{i}");
  222. },
  223. },
  224. },
  225. yAxis: {
  226. name: "带宽(Mbps)",
  227. type: "value",
  228. axisLine: {
  229. show: false,
  230. },
  231. splitLine: {
  232. show: true,
  233. lineStyle: {
  234. type: "dashed",
  235. },
  236. },
  237. axisTick: {
  238. show: true,
  239. },
  240. },
  241. series: [
  242. {
  243. data: [],
  244. name: "峰值带宽",
  245. type: "line",
  246. smooth: true,
  247. symbol: "none",
  248. lineStyle: {
  249. color: "#409eff", //改变折线颜色
  250. },
  251. emphasis: {
  252. focus: "series",
  253. },
  254. },
  255. ],
  256. },
  257. reqNumChartOptions: {
  258. tooltip: {
  259. trigger: "axis",
  260. },
  261. grid: {
  262. bottom: "10%",
  263. left: "5%",
  264. top: "15%",
  265. right: "5%",
  266. },
  267. xAxis: {
  268. name: "时间",
  269. axisLine: {
  270. show: true,
  271. },
  272. axisTick: {
  273. show: true,
  274. },
  275. splitLine: {
  276. show: false,
  277. },
  278. axisLabel: {
  279. formatter: (value) => {
  280. return this.parseTime(value, "{h}:{i}");
  281. },
  282. },
  283. },
  284. yAxis: {
  285. name: "请求数量(次)",
  286. type: "value",
  287. axisLine: {
  288. show: false,
  289. },
  290. splitLine: {
  291. show: true,
  292. lineStyle: {
  293. type: "dashed",
  294. },
  295. },
  296. axisTick: {
  297. show: true,
  298. },
  299. },
  300. series: [
  301. {
  302. data: [],
  303. name: "请求数量",
  304. type: "line",
  305. smooth: true,
  306. symbol: "none",
  307. lineStyle: {
  308. color: "#409eff", //改变折线颜色
  309. },
  310. emphasis: {
  311. focus: "series",
  312. },
  313. },
  314. ],
  315. },
  316. hitRateChartOptions: {
  317. tooltip: {
  318. trigger: "axis",
  319. },
  320. grid: {
  321. bottom: "10%",
  322. left: "5%",
  323. top: "15%",
  324. right: "5%",
  325. },
  326. xAxis: {
  327. name: "时间",
  328. axisLine: {
  329. show: true,
  330. },
  331. axisTick: {
  332. show: true,
  333. },
  334. splitLine: {
  335. show: false,
  336. },
  337. axisLabel: {
  338. formatter: (value) => {
  339. return this.parseTime(value, "{h}:{i}");
  340. },
  341. },
  342. },
  343. yAxis: {
  344. name: "命中率(%)",
  345. type: "value",
  346. axisLine: {
  347. show: false,
  348. },
  349. splitLine: {
  350. show: true,
  351. lineStyle: {
  352. type: "dashed",
  353. },
  354. },
  355. axisTick: {
  356. show: true,
  357. },
  358. },
  359. series: [
  360. {
  361. data: [],
  362. name: "请求命中率",
  363. type: "line",
  364. smooth: true,
  365. symbol: "none",
  366. lineStyle: {
  367. color: "#409eff", //改变折线颜色
  368. },
  369. emphasis: {
  370. focus: "series",
  371. },
  372. },
  373. ],
  374. },
  375. };
  376. },
  377. computed: {
  378. pickerOptions() {
  379. if(this.timeType == 1) {
  380. return {
  381. disabledDate: time => {
  382. return time.getTime() > Date.now() - 8.64e7 || time.getTime() < Date.now() - 2 * 24 * 60 * 60 * 1000;
  383. }
  384. }
  385. } else if (this.timeType == 2) {
  386. return {
  387. disabledDate: time => {
  388. return time.getTime() > Date.now() || time.getTime() < Date.now() - 7 * 24 * 60 * 60 * 1000
  389. }
  390. }
  391. } else if (this.timeType == 3) {
  392. return {
  393. disabledDate: time => {
  394. return time.getTime() > Date.now() || time.getTime() < Date.now() - 30 * 24 * 60 * 60 * 1000
  395. }
  396. }
  397. } else {
  398. return {
  399. disabledDate: time => {
  400. return time.getTime() < Date.now() - 8.64e7 || time.getTime() > Date.now() - 8.64e7
  401. }
  402. }
  403. }
  404. }
  405. },
  406. created() {
  407. this.resetQuery()
  408. },
  409. methods: {
  410. timePickerchange() {
  411. this.queryParams.startTime = this.datetime&&this.datetime.length > 0 ? this.datetime[0] : undefined
  412. this.queryParams.endTime = this.datetime&&this.datetime.length > 0 ? this.datetime[1] : undefined
  413. },
  414. handleTimeType(type) {
  415. this.timeType = type
  416. if(this.timeType == 1) { // 昨天
  417. this.queryParams.startTime = `${parseTime(this.yesterday, "{y}-{m}-{d}")} 00:00:00`
  418. this.queryParams.endTime = `${parseTime(this.today, "{y}-{m}-{d}")} 00:00:00`
  419. } else if (this.timeType == 2) { // 7天内
  420. this.queryParams.startTime = `${parseTime(this.weekday, "{y}-{m}-{d}")} 00:00:00`
  421. this.queryParams.endTime = `${parseTime(this.today, "{y}-{m}-{d} {h}:{i}:{s}")}`
  422. } else if (this.timeType == 3) { // 30天内
  423. this.queryParams.startTime = `${parseTime(this.thirtyDaysAgo, "{y}-{m}-{d}")} 00:00:00`
  424. this.queryParams.endTime = `${parseTime(this.today, "{y}-{m}-{d} {h}:{i}:{s}")}`
  425. } else { // 今天
  426. this.queryParams.startTime = `${parseTime(this.today, "{y}-{m}-{d}")} 00:00:00`
  427. this.queryParams.endTime = `${parseTime(this.today, "{y}-{m}-{d} {h}:{i}:{s}")}`
  428. }
  429. this.datetime = [this.queryParams.startTime,this.queryParams.endTime]
  430. },
  431. initChart(data) {
  432. const chartElement = this.$refs.trafficTrends;
  433. if (!this.chart) {
  434. this.chart = echarts.init(chartElement);
  435. }
  436. this.seChartOptions(data.flux || [])
  437. },
  438. initBwChart(data) {
  439. const chartElement = this.$refs.bwTrends;
  440. if (!this.bwChart) {
  441. this.bwChart = echarts.init(chartElement);
  442. }
  443. this.seBwChartOptions(data.bw||[])
  444. },
  445. initReqNumChart(data) {
  446. const chartElement = this.$refs.reqNumTrends;
  447. if (!this.reqNumChart) {
  448. this.reqNumChart = echarts.init(chartElement);
  449. }
  450. this.seReqNumChartOptions(data.reqNum||[])
  451. },
  452. initHitRateChart(data) {
  453. const chartElement = this.$refs.hitRateTrends;
  454. if (!this.hitRateChart) {
  455. this.hitRateChart = echarts.init(chartElement);
  456. }
  457. this.seHitRateChartOptions(data.hitRate||[])
  458. },
  459. seChartOptions(data) {
  460. const minute = this.queryParams.interval == 1 ? 60 : this.queryParams.interval == 2 ? 1440 : 5
  461. this.chartOptions.xAxis.data = this.resetXAxis(minute,this.queryParams.startTime,this.queryParams.endTime)
  462. this.chartOptions.series[0].data = data;
  463. this.chart.setOption(this.chartOptions);
  464. },
  465. seBwChartOptions(data) {
  466. const minute = this.queryParams.interval == 1 ? 60 : this.queryParams.interval == 2 ? 1440 : 5
  467. this.bwChartOptions.xAxis.data = this.resetXAxis(minute,this.queryParams.startTime,this.queryParams.endTime)
  468. this.bwChartOptions.series[0].data = data;
  469. this.bwChart.setOption(this.bwChartOptions);
  470. },
  471. seReqNumChartOptions(data) {
  472. const minute = this.queryParams.interval == 1 ? 60 : this.queryParams.interval == 2 ? 1440 : 5
  473. this.reqNumChartOptions.xAxis.data = this.resetXAxis(minute,this.queryParams.startTime,this.queryParams.endTime)
  474. this.reqNumChartOptions.series[0].data = data;
  475. this.reqNumChart.setOption(this.reqNumChartOptions);
  476. },
  477. seHitRateChartOptions(data) {
  478. const minute = this.queryParams.interval == 1 ? 60 : this.queryParams.interval == 2 ? 1440 : 5
  479. this.hitRateChartOptions.xAxis.data = this.resetXAxis(minute,this.queryParams.startTime,this.queryParams.endTime)
  480. this.hitRateChartOptions.series[0].data = data;
  481. this.hitRateChart.setOption(this.hitRateChartOptions);
  482. },
  483. resetXAxis(minute,start,end) {
  484. const startTime = new Date(start);
  485. const endTime = new Date(end);
  486. const intervals = [];
  487. let currentTime = startTime;
  488. while (currentTime <= endTime) {
  489. intervals.push(parseTime(currentTime, "{y}-{m}-{d} {h}:{i}:{s}"));
  490. currentTime = new Date(currentTime.getTime() + minute * 60 * 1000);
  491. }
  492. if (intervals[intervals.length - 1] !== parseTime(endTime, "{y}-{m}-{d} {h}:{i}:{s}")) {
  493. intervals.push(parseTime(endTime, "{y}-{m}-{d} {h}:{i}:{s}"));
  494. }
  495. return intervals
  496. },
  497. resetQuery() {
  498. this.resetForm("queryForm");
  499. this.timeType = 0
  500. this.queryParams.interval = 0
  501. this.queryParams.startTime = `${parseTime(this.today, "{y}-{m}-{d}")} 00:00:00`
  502. this.queryParams.endTime = `${parseTime(this.today, "{y}-{m}-{d} {h}:{i}:{s}")}`
  503. this.datetime = [this.queryParams.startTime,this.queryParams.endTime]
  504. this.getChartData();
  505. },
  506. getChartData() {
  507. const params = {
  508. startTime: new Date(this.queryParams.startTime).getTime(),
  509. endTime: new Date(this.queryParams.endTime).getTime(),
  510. domainName: this.queryParams.domainName,
  511. statType: this.queryParams.statType,
  512. interval: this.queryParams.interval == 1 ? 3600 : this.queryParams.interval == 2 ? 86400 : 300,
  513. }
  514. cdnStatistics(params).then(response => {
  515. this.summary.flux = (response.summary.result.flux /(1024 *1024)).toFixed(2);
  516. this.summary.req_num = response.summary.result.req_num;
  517. this.summary.hit_num = response.summary.result.hit_num;
  518. this.summary.bw = (response.summary.result.bw /(1024 *1024)).toFixed(2);
  519. if (response.summary.result.req_num != null && response.summary.result.hit_num != null) {
  520. this.summary.hit_rate = ((response.summary.result.hit_num / response.summary.result.req_num) * 100).toFixed(2) ;
  521. }
  522. if (response.detail.result.flux!=null){
  523. this.flux = response.detail.result.flux.map(num => (num / (1024 * 1024)).toFixed(2));
  524. }
  525. // 处理带宽数据
  526. if (response.detail.result.bw != null) {
  527. this.bw = response.detail.result.bw.map(num => (num / (1024 * 1024)).toFixed(2));
  528. }
  529. // 处理请求数据
  530. if (response.detail.result.req_num != null) {
  531. this.reqNum = response.detail.result.req_num;
  532. }
  533. if (response.detail.result.req_num != null && response.detail.result.hit_num != null) {
  534. // 初始化命中率数组
  535. this.hitRate = response.detail.result.req_num.map((req, index) => {
  536. const hit = response.detail.result.hit_num[index];
  537. return req > 0 ? ((hit / req) * 100).toFixed(2) : 0;
  538. });
  539. }
  540. // 初始化两个图表
  541. this.$nextTick(() => {
  542. this.initChart({ flux: this.flux });
  543. this.initBwChart({ bw: this.bw });
  544. this.initReqNumChart({ reqNum: this.reqNum });
  545. this.initHitRateChart({ hitRate: this.hitRate });
  546. });
  547. });
  548. },
  549. },
  550. };
  551. </script>
  552. <style lang="scss" scoped>
  553. .app-container {
  554. border-radius: 8px;
  555. .type {
  556. background: #f0f2f5;
  557. width: 80px;
  558. display: inline-block;
  559. text-align: center;
  560. margin-right: 5px;
  561. border-radius: 5px;
  562. cursor: pointer;
  563. }
  564. .active {
  565. color: #fff;
  566. background: #409eff;
  567. }
  568. .chart-title {
  569. font-size: 18px;
  570. font-weight: bold;
  571. span {
  572. font-size: 14px;
  573. font-weight: normal;
  574. color: #999;
  575. margin-left: 20px;
  576. }
  577. }
  578. }
  579. </style>