From 6eff8fde74e01fa3527585ec1fbd3629d51bc97a Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 9 Jun 2018 01:45:25 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B5=E3=83=BC=E3=83=90=E3=83=BC=E3=81=AE?= =?UTF-8?q?=E7=B5=B1=E8=A8=88=E6=83=85=E5=A0=B1=E3=82=92=E3=83=A1=E3=83=A2?= =?UTF-8?q?=E3=83=AA=E3=81=AB=E8=A8=98=E6=86=B6=E3=81=99=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B=E3=81=AA=E3=81=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- .../views/widgets/server.cpu-memory.vue | 9 +++++++++ src/index.ts | 4 ++-- src/{utils/stats.ts => server-stats.ts} | 13 ++++++++++-- src/server/api/stream/server.ts | 20 +++++++++++++++++-- 5 files changed, 41 insertions(+), 7 deletions(-) rename src/{utils/stats.ts => server-stats.ts} (70%) diff --git a/package.json b/package.json index 36c9d8e5f3..b0ad03aa6e 100644 --- a/package.json +++ b/package.json @@ -218,6 +218,6 @@ "webpack-cli": "2.1.4", "websocket": "1.0.26", "ws": "5.2.0", - "xev": "2.0.0" + "xev": "2.0.1" } } diff --git a/src/client/app/common/views/widgets/server.cpu-memory.vue b/src/client/app/common/views/widgets/server.cpu-memory.vue index fbd36b255a..6bf998c249 100644 --- a/src/client/app/common/views/widgets/server.cpu-memory.vue +++ b/src/client/app/common/views/widgets/server.cpu-memory.vue @@ -76,9 +76,15 @@ export default Vue.extend({ }, mounted() { this.connection.on('stats', this.onStats); + this.connection.on('statsLog', this.onStatsLog); + this.connection.send({ + type: 'requestLog', + id: Math.random().toString() + }); }, beforeDestroy() { this.connection.off('stats', this.onStats); + this.connection.off('statsLog', this.onStatsLog); }, methods: { onStats(stats) { @@ -94,6 +100,9 @@ export default Vue.extend({ this.cpuP = (stats.cpu_usage * 100).toFixed(0); this.memP = (stats.mem.used / stats.mem.total * 100).toFixed(0); + }, + onStatsLog(statsLog) { + statsLog.forEach(stats => this.onStats(stats)); } } }); diff --git a/src/index.ts b/src/index.ts index bcd6561691..42a4f484e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ import ProgressBar from './utils/cli/progressbar'; import EnvironmentInfo from './utils/environmentInfo'; import MachineInfo from './utils/machineInfo'; import DependencyInfo from './utils/dependencyInfo'; -import stats from './utils/stats'; +import serverStats from './server-stats'; import loadConfig from './config/load'; import { Config } from './config/types'; @@ -49,7 +49,7 @@ function main() { masterMain(opt); ev.mount(); - stats(); + serverStats(); } else { workerMain(opt); } diff --git a/src/utils/stats.ts b/src/server-stats.ts similarity index 70% rename from src/utils/stats.ts rename to src/server-stats.ts index cfb710f5e1..85aa85b682 100644 --- a/src/utils/stats.ts +++ b/src/server-stats.ts @@ -9,10 +9,16 @@ const ev = new Xev(); * Report stats regularly */ export default function() { + const log = []; + + ev.on('requestServerStatsLog', id => { + ev.emit('serverStatsLog:' + id, log); + }); + setInterval(() => { osUtils.cpuUsage(cpuUsage => { const disk = diskusage.checkSync(os.platform() == 'win32' ? 'c:' : '/'); - ev.emit('stats', { + const stats = { cpu_usage: cpuUsage, mem: { total: os.totalmem(), @@ -21,7 +27,10 @@ export default function() { disk, os_uptime: os.uptime(), process_uptime: process.uptime() - }); + }; + ev.emit('serverStats', stats); + log.push(stats); + if (log.length > 50) log.shift(); }); }, 1000); } diff --git a/src/server/api/stream/server.ts b/src/server/api/stream/server.ts index 4ca2ad1b10..342170a21e 100644 --- a/src/server/api/stream/server.ts +++ b/src/server/api/stream/server.ts @@ -11,9 +11,25 @@ export default function(request: websocket.request, connection: websocket.connec })); }; - ev.addListener('stats', onStats); + connection.on('message', async data => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'requestLog': + ev.once('serverStatsLog:' + msg.id, statsLog => { + connection.send(JSON.stringify({ + type: 'statsLog', + body: statsLog + })); + }); + ev.emit('requestServerStatsLog', msg.id); + break; + } + }); + + ev.addListener('serverStats', onStats); connection.on('close', () => { - ev.removeListener('stats', onStats); + ev.removeListener('serverStats', onStats); }); }