Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
wip es6 -> es5
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilson committed Nov 2, 2016
1 parent 0d37356 commit 248e2d1
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 355 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
@@ -1,3 +1,3 @@
---
extends:
- "formidable/configurations/es6-node"
- "formidable/configurations/es5-node"
48 changes: 24 additions & 24 deletions bin/nodejs-dashboard.js
@@ -1,18 +1,18 @@
#!/usr/bin/env node
"use strict";

const SocketIO = require("socket.io");
const spawn = require("cross-spawn");
const commander = require("commander");
const path = require("path");
var SocketIO = require("socket.io");
var spawn = require("cross-spawn");
var commander = require("commander");
var path = require("path");

const Dashboard = require("../lib/dashboard");
const config = require("../lib/config");
const appPkg = require(path.resolve("package.json"));
const pkg = require("../package.json");
var Dashboard = require("../lib/dashboard");
var config = require("../lib/config");
var appPkg = require(path.resolve("package.json"));
var pkg = require("../package.json");

const appName = appPkg.name || "node";
const program = new commander.Command(pkg.name);
var appName = appPkg.name || "node";
var program = new commander.Command(pkg.name);

program.version(pkg.version);
program.option("-p, --port [port]", "Socket listener port");
Expand All @@ -25,46 +25,46 @@ if (!program.args.length) {
return;
}

const command = program.args[0];
const args = program.args.slice(1);
var command = program.args[0];
var args = program.args.slice(1);

const port = program.port || config.PORT;
const eventDelay = program.eventdelay || config.BLOCKED_THRESHOLD;
var port = program.port || config.PORT;
var eventDelay = program.eventdelay || config.BLOCKED_THRESHOLD;

process.env[config.PORT_KEY] = port;
process.env[config.BLOCKED_THRESHOLD_KEY] = eventDelay;

const child = spawn(command, args, {
var child = spawn(command, args, {
env: process.env,
stdio: [null, null, null, null],
detached: true
});

console.log(`Waiting for client connection on ${port}...`); //eslint-disable-line
console.log("Waiting for client connection on %d...", port); //eslint-disable-line

const server = new SocketIO(port);
var server = new SocketIO(port);

const dashboard = new Dashboard({ appName, program });
var dashboard = new Dashboard({ appName: appName, program: program });

server.on("connection", (socket) => {
socket.on("metrics", (data) => {
server.on("connection", function (socket) {
socket.on("metrics", function (data) {
dashboard.onEvent({ type: "metrics", data: JSON.parse(data) });
});

socket.on("error", (err) => {
socket.on("error", function (err) {
console.error("Received error from agent, exiting: ", err); //eslint-disable-line
process.exit(1); //eslint-disable-line
});
});

child.stdout.on("data", (data) => {
child.stdout.on("data", function (data) {
dashboard.onEvent({ type: "stdout", data: data.toString("utf8") });
});

child.stderr.on("data", (data) => {
child.stderr.on("data", function (data) {
dashboard.onEvent({ type: "stderr", data: data.toString("utf8") });
});

process.on("exit", () => {
process.on("exit", function () {
process.kill(process.platform === "win32" ? child.pid : -child.pid);
});
2 changes: 1 addition & 1 deletion index.js
@@ -1,4 +1,4 @@
"use strict";
const dashboardAgent = require("./lib/dashboard-agent");
var dashboardAgent = require("./lib/dashboard-agent");

module.exports = dashboardAgent();
6 changes: 3 additions & 3 deletions lib/config.js
@@ -1,10 +1,10 @@
"use strict";

const pkg = require("../package.json");
var pkg = require("../package.json");

module.exports = {
PORT: 9838,
PORT_KEY: `${pkg.name}_PORT`,
PORT_KEY: pkg.name + "_PORT",
BLOCKED_THRESHOLD: 10,
BLOCKED_THRESHOLD_KEY: `${pkg.name}_BLOCKED_THRESHOLD`
BLOCKED_THRESHOLD_KEY: pkg.name + "_BLOCKED_THRESHOLD"
};
50 changes: 25 additions & 25 deletions lib/dashboard-agent.js
@@ -1,36 +1,36 @@
"use strict";

const SocketIO = require("socket.io-client");
const blocked = require("blocked");
const pusage = require("pidusage");
const os = require("os");
const _ = require("lodash");
const config = require("./config");

const dashboardAgent = () => {
const options = {
var SocketIO = require("socket.io-client");
var blocked = require("blocked");
var pusage = require("pidusage");
var os = require("os");
var _ = require("lodash");
var config = require("./config");

var dashboardAgent = function () {
var options = {
port: process.env[config.PORT_KEY],
blockedThreshold: process.env[config.BLOCKED_THRESHOLD_KEY],
refreshInterval: 1000
};

const socket = new SocketIO(`http://localhost:${options.port}`);
var socket = new SocketIO("http://localhost:" + options.port);

const eventLoop = {
var eventLoop = {
delay: 0,
high: 0
};

const _delayed = (delay) => {
var _delayed = function (delay) {
eventLoop.high = Math.max(eventLoop.high, delay);
eventLoop.delay = delay;
};

blocked(_delayed, { threshold: options.blockedThreshold });

const _getStats = (cb) => {
const metrics = {
eventLoop,
var _getStats = function (cb) {
var metrics = {
eventLoop: eventLoop,
mem: {
systemTotal: os.totalmem()
},
Expand All @@ -41,7 +41,7 @@ const dashboardAgent = () => {

_.merge(metrics.mem, process.memoryUsage());

pusage.stat(process.pid, (err, stat) => {
pusage.stat(process.pid, function (err, stat) {

if (err) {
return cb(err);
Expand All @@ -53,12 +53,12 @@ const dashboardAgent = () => {

};

const resetEventMetrics = () => {
var resetEventMetrics = function () {
eventLoop.delay = 0;
};

const _emitStats = () => {
_getStats((err, newMetrics) => {
var _emitStats = function () {
_getStats(function (err, newMetrics) {
if (err) {
console.error("Failed to load metrics: ", err); //eslint-disable-line
socket.emit("error", JSON.stringify(err));
Expand All @@ -70,22 +70,22 @@ const dashboardAgent = () => {
});
};

const startPump = () => {
var startPump = function () {
options.intervalId = setInterval(_emitStats, options.refreshInterval);
};

const destroy = () => {
var destroy = function () {
socket.close();
clearInterval(options.intervalId);
};

startPump();

return {
_delayed,
_getStats,
_emitStats,
destroy
_delayed: _delayed,
_getStats: _getStats,
_emitStats: _emitStats,
destroy: destroy
};
};

Expand Down
105 changes: 50 additions & 55 deletions lib/dashboard.js
@@ -1,68 +1,63 @@
"use strict";

const _ = require("lodash");
const blessed = require("blessed");

const StreamView = require("./views/stream-view");
const EventLoopView = require("./views/eventloop-view");
const MemoryView = require("./views/memory-view");
const CpuView = require("./views/cpu-view");
const EventEmitter = require("events");

class Dashboard {

constructor(options) {
this.screen = blessed.screen({
smartCSR: true,
title: `${options.appName}`
});

this.screen.key(["escape", "q", "C-c"], () => {
process.exit(0); // eslint-disable-line no-process-exit
});

this.eventPump = new EventEmitter();
this._createView();

}

onEvent(event) {
this.eventPump.emit(event.type, event.data);
this.screen.render();
}
var _ = require("lodash");
var blessed = require("blessed");

var StreamView = require("./views/stream-view");
var EventLoopView = require("./views/eventloop-view");
var MemoryView = require("./views/memory-view");
var CpuView = require("./views/cpu-view");
var EventEmitter = require("events");

function Dashboard(options) {
this.screen = blessed.screen({
smartCSR: true,
title: options.appName
});

this.screen.key(["escape", "q", "C-c"], function () {
process.exit(0); // eslint-disable-line no-process-exit
});

this.eventPump = new EventEmitter();
this._createView();
}

_createView() {
// fixes weird scrolling issue
const container = blessed.box({});
this.screen.append(container);
Dashboard.prototype.onEvent = function (event) {
this.eventPump.emit(event.type, event.data);
this.screen.render();
};

const stdoutView = new StreamView({
parent: container,
label: "stdout",
color: "green"
});
Dashboard.prototype._createView = function () {
// fixes weird scrolling issue
var container = blessed.box({});
this.screen.append(container);

this.eventPump.addListener("stdout", stdoutView.onEvent.bind(stdoutView));
var stdoutView = new StreamView({
parent: container,
label: "stdout",
color: "green"
});

const stderrView = new StreamView({
parent: container,
label: "stderr",
color: "red",
top: "50%"
});
this.eventPump.addListener("stdout", stdoutView.onEvent.bind(stdoutView));

this.eventPump.addListener("stderr", stderrView.onEvent.bind(stderrView));
var stderrView = new StreamView({
parent: container,
label: "stderr",
color: "red",
top: "50%"
});

const metrics = [MemoryView, CpuView, EventLoopView];
this.eventPump.addListener("stderr", stderrView.onEvent.bind(stderrView));

_.each(metrics, (Metric) => {
const view = new Metric({ parent: this.screen });
this.eventPump.addListener("metrics", view.onEvent.bind(view));
});
var metrics = [MemoryView, CpuView, EventLoopView];

this.screen.render();
}
_.each(metrics, function (Metric) {
var view = new Metric({ parent: this.screen });
this.eventPump.addListener("metrics", view.onEvent.bind(view));
}.bind(this));

}
this.screen.render();
};

module.exports = Dashboard;

0 comments on commit 248e2d1

Please sign in to comment.