embark-area-51/lib/dashboard/monitor.js

382 lines
7.3 KiB
JavaScript
Raw Normal View History

2018-06-13 15:17:55 +00:00
let blessed = require("neo-blessed");
2017-03-29 17:50:05 +00:00
let CommandHistory = require('./command_history.js');
2016-09-17 03:56:25 +00:00
2017-03-30 11:12:39 +00:00
class Dashboard {
constructor(_options) {
let options = _options || {};
2017-03-30 11:12:39 +00:00
this.env = options.env;
this.console = options.console;
this.history = new CommandHistory();
2018-02-27 22:04:41 +00:00
this.events = options.events;
2017-03-30 11:12:39 +00:00
this.color = options.color || "green";
this.minimal = options.minimal || false;
2017-03-30 11:12:39 +00:00
this.screen = blessed.screen({
smartCSR: true,
title: options.title || ("Embark " + options.version),
2017-03-30 11:12:39 +00:00
dockBorders: false,
fullUnicode: true,
autoPadding: true
});
this.layoutLog();
this.layoutStatus();
this.layoutModules();
this.layoutCmd();
this.screen.key(["C-c"], function () {
process.exit(0);
});
this.logEntry = this.logEntry.bind(this);
this.setContracts = this.setContracts.bind(this);
this.availableServices = this.availableServices.bind(this);
this.status.setContent(this.env.green);
this.screen.render();
this.input.focus();
}
availableServices(_services) {
2017-12-16 15:54:17 +00:00
let stateColors = {
'on': 'green',
'off': 'red',
'warn': 'grey'
};
let services = Object.keys(_services).map((service) => {
let checkObj = _services[service];
if (checkObj.status in stateColors) {
let color = stateColors[checkObj.status];
return checkObj.name[color];
}
2017-12-16 15:54:17 +00:00
return checkObj.name;
});
2017-03-30 11:12:39 +00:00
this.progress.setContent(services.join('\n'));
this.screen.render();
}
setStatus(status) {
this.operations.setContent(status);
this.screen.render();
}
setContracts(contracts) {
let data = [];
2016-09-17 03:56:25 +00:00
2018-05-08 21:49:46 +00:00
data.push([__("Contract Name"), __("Address"), __("Status")]);
2017-03-30 11:12:39 +00:00
contracts.forEach(function (row) {
data.push(row);
});
this.moduleTable.setData(data);
this.screen.render();
}
2017-03-30 11:12:39 +00:00
logEntry(text) {
this.logText.log(text);
this.screen.render();
}
layoutLog() {
this.log = blessed.box({
2018-05-08 21:49:46 +00:00
label: __("Logs"),
2017-03-30 11:12:39 +00:00
padding: 1,
width: "100%",
height: "55%",
left: "0%",
top: "42%",
2016-09-17 03:56:25 +00:00
border: {
2017-03-30 11:12:39 +00:00
type: "line"
},
style: {
fg: -1,
border: {
fg: this.color
}
2016-10-23 00:10:25 +00:00
}
2017-03-30 11:12:39 +00:00
});
this.logText = blessed.log({
parent: this.log,
tags: true,
width: "100%-5",
//height: '90%',
scrollable: true,
input: false,
alwaysScroll: true,
scrollbar: {
ch: " ",
inverse: true
},
keys: false,
vi: false,
mouse: true
});
this.screen.append(this.log);
}
layoutModules() {
this.modules = blessed.box({
2018-05-08 21:49:46 +00:00
label: __("Contracts"),
2017-03-30 11:12:39 +00:00
tags: true,
padding: 1,
width: "75%",
height: "42%",
left: "0%",
top: "0",
2016-09-17 03:56:25 +00:00
border: {
2017-03-30 11:12:39 +00:00
type: "line"
},
style: {
fg: -1,
border: {
fg: this.color
}
2016-10-23 00:10:25 +00:00
}
2017-03-30 11:12:39 +00:00
});
this.moduleTable = blessed.table({
parent: this.modules,
height: "100%",
width: "100%-5",
align: "left",
pad: 1,
margin: "auto",
shrink: true,
scrollable: true,
alwaysScroll: true,
scrollbar: {
ch: " ",
inverse: true
},
keys: false,
vi: false,
mouse: true,
data: [["ContractName", "Address", "Status"]]
});
this.screen.append(this.modules);
}
layoutAssets() {
this.assets = blessed.box({
2018-05-08 21:49:46 +00:00
label: __("Asset Pipeline"),
2017-03-30 11:12:39 +00:00
tags: true,
padding: 1,
width: "50%",
height: "55%",
left: "50%",
top: "42%",
2016-09-17 03:56:25 +00:00
border: {
2017-03-30 11:12:39 +00:00
type: "line"
},
style: {
fg: -1,
border: {
fg: this.color
}
2016-10-23 00:16:00 +00:00
}
2017-03-30 11:12:39 +00:00
});
this.assetTable = blessed.table({
parent: this.assets,
height: "100%",
width: "100%-5",
align: "left",
pad: 1,
scrollable: true,
alwaysScroll: true,
scrollbar: {
ch: " ",
inverse: true
},
keys: false,
vi: false,
mouse: true,
data: [["Name", "Size"]]
});
this.screen.append(this.assets);
}
layoutStatus() {
this.wrapper = blessed.layout({
width: "25%",
height: "42%",
top: "0%",
left: "75%",
layout: "grid"
});
this.status = blessed.box({
parent: this.wrapper,
2018-05-08 21:49:46 +00:00
label: __("Environment"),
2017-03-30 11:12:39 +00:00
tags: true,
padding: {
left: 1
},
width: "100%",
height: "20%",
valign: "middle",
2016-09-17 03:56:25 +00:00
border: {
2017-03-30 11:12:39 +00:00
type: "line"
},
style: {
fg: -1,
border: {
fg: this.color
}
2016-10-23 00:16:00 +00:00
}
2017-03-30 11:12:39 +00:00
});
this.operations = blessed.box({
parent: this.wrapper,
2018-05-08 21:49:46 +00:00
label: __("Status"),
2017-03-30 11:12:39 +00:00
tags: true,
padding: {
left: 1
},
width: "100%",
height: "20%",
valign: "middle",
2016-09-17 03:56:25 +00:00
border: {
2017-03-30 11:12:39 +00:00
type: "line"
},
style: {
fg: -1,
border: {
fg: this.color
}
2016-10-23 00:16:00 +00:00
}
2017-03-30 11:12:39 +00:00
});
this.progress = blessed.box({
parent: this.wrapper,
2018-05-08 21:49:46 +00:00
label: __("Available Services"),
2017-03-30 11:12:39 +00:00
tags: true,
padding: this.minimal ? {
left: 1
} : 1,
width: "100%",
height: "60%",
valign: "top",
2016-09-17 03:56:25 +00:00
border: {
2017-03-30 11:12:39 +00:00
type: "line"
},
scrollable: true,
alwaysScroll: true,
scrollbar: {
ch: " ",
inverse: true
},
2017-03-30 11:12:39 +00:00
style: {
fg: -1,
border: {
fg: this.color
}
2016-10-23 00:16:00 +00:00
}
2017-03-30 11:12:39 +00:00
});
this.screen.append(this.wrapper);
}
layoutCmd() {
this.consoleBox = blessed.box({
2018-05-08 21:49:46 +00:00
label: __('Console'),
2017-03-30 11:12:39 +00:00
tags: true,
padding: 0,
width: '100%',
height: '6%',
left: '0%',
top: '95%',
2016-09-17 03:56:25 +00:00
border: {
2017-03-30 11:12:39 +00:00
type: 'line'
},
style: {
fg: 'black',
border: {
fg: this.color
}
2016-10-23 00:16:00 +00:00
}
2017-03-30 11:12:39 +00:00
});
this.input = blessed.textbox({
parent: this.consoleBox,
name: 'input',
input: true,
keys: false,
top: 0,
left: 1,
height: '50%',
width: '100%-2',
inputOnFocus: true,
style: {
fg: 'green',
2016-09-17 03:56:25 +00:00
bg: 'black',
2017-03-30 11:12:39 +00:00
focus: {
bg: 'black',
fg: 'green'
}
2016-09-17 03:56:25 +00:00
}
2017-03-30 11:12:39 +00:00
});
let self = this;
this.input.key(["C-c"], function () {
2018-05-30 14:52:15 +00:00
self.events.emit('exit');
2017-03-30 11:12:39 +00:00
process.exit(0);
});
this.input.key(["C-w"], function () {
self.input.clearValue();
self.input.focus();
});
this.input.key(["up"], function () {
let cmd = self.history.getPreviousCommand();
self.input.setValue(cmd);
self.input.focus();
});
this.input.key(["down"], function () {
let cmd = self.history.getNextCommand();
self.input.setValue(cmd);
self.input.focus();
});
2018-02-28 00:17:56 +00:00
this.input.on('submit', this.submitCmd.bind(this));
2018-02-27 22:50:43 +00:00
this.screen.append(this.consoleBox);
}
2017-03-30 11:12:39 +00:00
2018-02-28 00:17:56 +00:00
submitCmd(cmd) {
2018-02-27 22:50:43 +00:00
if (cmd !== '') {
2018-02-28 00:17:56 +00:00
this.history.addCommand(cmd);
this.executeCmd(cmd);
2018-02-27 22:50:43 +00:00
}
2018-02-28 00:17:56 +00:00
this.input.clearValue();
this.input.focus();
}
executeCmd(cmd, cb) {
2018-02-28 00:17:56 +00:00
const self = this;
self.logText.log('console> '.bold.green + cmd);
self.console.executeCmd(cmd, function (result) {
self.logText.log(result);
if (cb) {
cb(result);
}
2018-02-28 00:17:56 +00:00
});
2017-03-30 11:12:39 +00:00
}
2016-09-17 03:56:25 +00:00
2017-03-30 11:12:39 +00:00
}
2016-09-17 03:56:25 +00:00
module.exports = Dashboard;