From 63198d4095d04df9b49a48bb54c2d5b51b3f8b9f Mon Sep 17 00:00:00 2001 From: MC_Myth <1440126177@qq.com> Date: Sat, 20 Feb 2021 13:50:21 +0800 Subject: [PATCH] :art: improve code style --- module/js/ccsun.js | 65 +++++++------ web/static/js/ccsun.js | 206 ++++++++++++++++++++--------------------- 2 files changed, 135 insertions(+), 136 deletions(-) diff --git a/module/js/ccsun.js b/module/js/ccsun.js index a86ea1c..929f120 100644 --- a/module/js/ccsun.js +++ b/module/js/ccsun.js @@ -1,7 +1,6 @@ - -if(process.argv.length <= 3) { - console.log('Missing parameters.The parameter is " [offline|online]"') - return +if (process.argv.length <= 3) { + console.log('Missing parameters.The parameter is " [offline|online]"') + return } const puppeteer = require('puppeteer'); @@ -16,38 +15,38 @@ const url = `http://127.0.0.1:${port}/chart?day=${day}&offline=${String((offline if (!fs.existsSync('./temp/')) fs.mkdirSync('./temp/'); (async () => { - const browser = await puppeteer.launch({ - headless: true, - }); - const page = await browser.newPage(); - try { - await page.goto(url, {'waitUntil': 'networkidle2'}); - } catch (err) { - await browser.close(); - } - const header = await page.$('html'); + const browser = await puppeteer.launch({ + headless: true, + }); + const page = await browser.newPage(); + try { + await page.goto(url, {'waitUntil': 'networkidle2'}); + } catch (err) { + await browser.close(); + } + const header = await page.$('html'); - const doc = await page.evaluate((header) => { - const {x, y, width, height} = header.getBoundingClientRect(); - return {x, y, width, height}; - }, header); + const doc = await page.evaluate((header) => { + const {x, y, width, height} = header.getBoundingClientRect(); + return {x, y, width, height}; + }, header); - let pageWidth = day * 35 - if (pageWidth < 500) pageWidth = 500; - await page.setViewport({width: Math.round(pageWidth), height: Math.round(doc.height)}); - const waitForSelectorOptions = { - timeout: 1600 - } - await setTimeout(async () => { - await page.waitForSelector('#container .highcharts-container', waitForSelectorOptions).then(async () => { - await page.waitForSelector('#container2 .highcharts-container', waitForSelectorOptions).then(async () => { - await page.screenshot({path: filename, quality: 76}) - }) + let pageWidth = day * 35 + if (pageWidth < 500) pageWidth = 500; + await page.setViewport({width: Math.round(pageWidth), height: Math.round(doc.height)}); + const waitForSelectorOptions = { + timeout: 1600 + } + await setTimeout(async () => { + await page.waitForSelector('#container .highcharts-container', waitForSelectorOptions).then(async () => { + await page.waitForSelector('#container2 .highcharts-container', waitForSelectorOptions).then(async () => { + await page.screenshot({path: filename, quality: 76}) + }) }).catch(async reject => { - console.log('[Error waitForSelector]') - console.log(reject) - await browser.close(); + console.log('[Error waitForSelector]') + console.log(reject) + await browser.close(); }) await browser.close(); - }, 600) + }, 600) })(); \ No newline at end of file diff --git a/web/static/js/ccsun.js b/web/static/js/ccsun.js index 32867c9..a5cba56 100644 --- a/web/static/js/ccsun.js +++ b/web/static/js/ccsun.js @@ -1,116 +1,116 @@ const port = '8881' $(function () { - $.ajax({ - type: "GET", - contentType: "application/json;charset=UTF-8", - url: `http://127.0.0.1:${port}/api/ccsun${window.location.search}`, - success: function (result) { - loaded(JSON.parse(result)); - } - }); + $.ajax({ + type: "GET", + contentType: "application/json;charset=UTF-8", + url: `http://127.0.0.1:${port}/api/ccsun${window.location.search}`, + success: function (result) { + loaded(JSON.parse(result)); + } + }); }); function loaded(json_src) { - let notice = '' - if(json_src["status"] === "data_acquisition_failed") { - notice = "获取实时流量数据失败" - } - const plotOptions = { - line: { - animation: false, - dataLabels: { - enabled: true - } - } + let notice = '' + if (json_src["status"] === "data_acquisition_failed") { + notice = "获取实时流量数据失败" + } + const plotOptions = { + line: { + animation: false, + dataLabels: { + enabled: true + } } - const chart1 = Highcharts.chart('container', { - chart: { - type: 'line', - animation: false - }, - credits: {enabled: false}, - title: { - text: '当日流量' - }, - subtitle: { - text: notice - }, - xAxis: {}, - yAxis: { - title: { - text: '流量 (GB)' - } - }, - plotOptions - }); - let date = []; - let download = []; - let upload = []; - // let toal = 0; - for (let i = 0; i < json_src["data"].length; i++) { - const _date = json_src["data"][i]["date"].slice(5) - date.push(_date); - chart1.xAxis[0].setCategories(date); - let _download = parseFloat(json_src["data"][i]["download"]); - let _upload = parseFloat(json_src["data"][i]["upload"]); - download.push(_download); - upload.push(_upload); - // toal += _download + _upload - } - // chart1.setTitle(null, { text: `合计:${String(toal.toFixed(2))}GB`}); - chart1.addSeries({ - name: '下载', - data: download - }); + } + const chart1 = Highcharts.chart('container', { + chart: { + type: 'line', + animation: false + }, + credits: {enabled: false}, + title: { + text: '当日流量' + }, + subtitle: { + text: notice + }, + xAxis: {}, + yAxis: { + title: { + text: '流量 (GB)' + } + }, + plotOptions + }); + let date = []; + let download = []; + let upload = []; + // let toal = 0; + for (let i = 0; i < json_src["data"].length; i++) { + const _date = json_src["data"][i]["date"].slice(5) + date.push(_date); + chart1.xAxis[0].setCategories(date); + let _download = parseFloat(json_src["data"][i]["download"]); + let _upload = parseFloat(json_src["data"][i]["upload"]); + download.push(_download); + upload.push(_upload); + // toal += _download + _upload + } + // chart1.setTitle(null, { text: `合计:${String(toal.toFixed(2))}GB`}); + chart1.addSeries({ + name: '下载', + data: download + }); - chart1.addSeries({ - name: '上传', - data: upload - }); + chart1.addSeries({ + name: '上传', + data: upload + }); - const chart2 = Highcharts.chart('container2', { - chart: { - type: 'line', - animation: false - }, - credits: {enabled: false}, - title: { - text: '总计流量' - }, - subtitle: { - text: notice - }, - xAxis: {}, - yAxis: { - title: { - text: '流量 (GB)' - } - }, - plotOptions - }); + const chart2 = Highcharts.chart('container2', { + chart: { + type: 'line', + animation: false + }, + credits: {enabled: false}, + title: { + text: '总计流量' + }, + subtitle: { + text: notice + }, + xAxis: {}, + yAxis: { + title: { + text: '流量 (GB)' + } + }, + plotOptions + }); - date = []; - download = []; - upload = []; - // toal = 0; - for (let i = 0; i < json_src["data"].length; i++) { - const _date = json_src["data"][i]["date"].slice(5) - date.push(_date); - chart2.xAxis[0].setCategories(date); - let _download = parseFloat(json_src["data"][i]["used"]["download"]); - let _upload = parseFloat(json_src["data"][i]["used"]["upload"]); - download.push(_download); - upload.push(_upload); - } - chart2.addSeries({ - name: '下载', - data: download - }); + date = []; + download = []; + upload = []; + // toal = 0; + for (let i = 0; i < json_src["data"].length; i++) { + const _date = json_src["data"][i]["date"].slice(5) + date.push(_date); + chart2.xAxis[0].setCategories(date); + let _download = parseFloat(json_src["data"][i]["used"]["download"]); + let _upload = parseFloat(json_src["data"][i]["used"]["upload"]); + download.push(_download); + upload.push(_upload); + } + chart2.addSeries({ + name: '下载', + data: download + }); - chart2.addSeries({ - name: '上传', - data: upload - }); + chart2.addSeries({ + name: '上传', + data: upload + }); } /*