From 7b45f8d6eacd65e323d15c1f50d7c19b21d80ac1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 16 Sep 2016 23:56:25 -0400 Subject: [PATCH] basic monitor --- lib/blockchain.js | 2 +- lib/deploy.js | 16 +-- lib/index.js | 20 ++- lib/logger.js | 42 ++++--- lib/monitor.js | 309 ++++++++++++++++++++++++++++++++++++++++++++++ lib/pipeline.js | 6 +- lib/server.js | 3 +- lib/watch.js | 14 +-- package.json | 1 + 9 files changed, 375 insertions(+), 38 deletions(-) create mode 100644 lib/monitor.js diff --git a/lib/blockchain.js b/lib/blockchain.js index 2d7867ef6..6080bfd7a 100644 --- a/lib/blockchain.js +++ b/lib/blockchain.js @@ -105,7 +105,7 @@ Blockchain.prototype.generate_basic_command = function() { cmd += "--rpcport " + this.blockchainConfig.rpcPort + " "; cmd += "--rpcaddr " + this.blockchainConfig.rpcHost + " "; cmd += "--networkid " + "12301" + " "; - cmd += "--rpccorsdomain=\"" + "*" + "\" "; + cmd += "--rpccorsdomain=\"" + "localhost" + "\" "; //cmd += "--port " + config.port + " "; //cmd += "--rpc "; diff --git a/lib/deploy.js b/lib/deploy.js index 5b5cf2fe5..cfb0e31d8 100644 --- a/lib/deploy.js +++ b/lib/deploy.js @@ -1,12 +1,14 @@ var async = require('async'); var Compiler = require('./compiler.js'); -var Deploy = function(web3, contractsManager) { +var Deploy = function(web3, contractsManager, logger) { this.web3 = web3; this.contractsManager = contractsManager; + this.logger = logger; }; Deploy.prototype.deployContract = function(contract, params, callback) { + var self = this; var contractObject = this.web3.eth.contract(contract.abiDefinition); var contractParams = params || contract.args; @@ -20,10 +22,10 @@ Deploy.prototype.deployContract = function(contract, params, callback) { contractParams.push(function(err, transaction) { if (err) { - console.log("error"); + self.logger.info("error"); callback(new Error(err)); } else if (transaction.address !== undefined) { - console.log("address contract: " + transaction.address); + self.logger.info("address contract: " + transaction.address); contract.deployedAddress = transaction.address; callback(null, transaction.address); } @@ -34,16 +36,16 @@ Deploy.prototype.deployContract = function(contract, params, callback) { Deploy.prototype.deployAll = function(done) { var self = this; - console.log("deployAll"); + this.logger.info("deployAll"); async.eachOfSeries(this.contractsManager.listContracts(), function(contract, key, callback) { - console.log(arguments); + self.logger.info(arguments); self.deployContract(contract, null, callback); }, function(err, results) { - console.log("finished"); - console.log(arguments); + self.logger.info("finished"); + self.logger.info(arguments); done(); } ); diff --git a/lib/index.js b/lib/index.js index c4e697fb2..b4952ac05 100644 --- a/lib/index.js +++ b/lib/index.js @@ -19,6 +19,7 @@ var Pipeline = require('./pipeline.js'); var Test = require('./test.js'); var Logger = require('./logger.js'); var Config = require('./config.js'); +var Monitor = require('./monitor.js'); var Embark = { @@ -35,6 +36,7 @@ var Embark = { initConfig: function(env, options) { this.config = new Config(env); this.config.loadConfigFiles(options); + this.logger = new Logger({}); //this.contractsManager = new ContractsManager(configDir, files, env); //this.contractsManager.init(); @@ -44,6 +46,11 @@ var Embark = { run: function(env) { var self = this; async.waterfall([ + function startMonitor(callback) { + Embark.monitor = new Monitor({env: env}); + self.logger.logFunction = Embark.monitor.setData; + callback(); + }, function deployAndGenerateABI(callback) { Embark.deploy(function(abi) { callback(null, abi); @@ -53,22 +60,23 @@ var Embark = { var pipeline = new Pipeline({ buildDir: self.config.buildDir, contractsFiles: self.config.contractsFiles, - assetFiles: self.config.assetFiles + assetFiles: self.config.assetFiles, + logger: self.logger }); pipeline.build(abi); callback(); }, function startAssetServer(callback) { - var server = new Server({}); + var server = new Server({logger: self.logger}); server.start(callback); }, function watchFilesForChanges(callback) { - var watch = new Watch(); + var watch = new Watch({logger: self.logger}); watch.start(); callback(); } ], function(err, result) { - console.log("finished".underline); + self.logger.trace("finished".underline); }); }, @@ -85,7 +93,7 @@ var Embark = { callback(); } ], function(err, result) { - console.log("finished".underline); + self.logger.trace("finished".underline); }); }, @@ -114,7 +122,7 @@ var Embark = { var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - var deploy = new Deploy(web3, contractsManager); + var deploy = new Deploy(web3, contractsManager, Embark.logger); deploy.deployAll(function() { callback(null, contractsManager); }); diff --git a/lib/logger.js b/lib/logger.js index 3486dc62d..b069b03ac 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,24 +1,38 @@ var colors = require('colors'); -var Logger = { - logLevel: 'info', +var Logger = function(options) { + this.logLevels = ['error', 'warn', 'info', 'debug', 'trace']; + this.logLevel = options.logLevel || 'info'; + this.logFunction = options.logFunction || console.log; +}; - info: function(txt) { - console.log(txt.blue); - }, +Logger.prototype.error = function(txt) { + if (!(this.shouldLog('error'))) { return; } + this.logFunction(txt.red); +}; - log: function(txt) { - console.log(txt); - }, +Logger.prototype.warn = function(txt) { + if (!(this.shouldLog('warn'))) { return; } + this.logFunction(txt.yellow); +}; - warn: function(txt) { - console.log(txt.yellow); - }, +Logger.prototype.info = function(txt) { + if (!(this.shouldLog('info'))) { return; } + this.logFunction(txt.green); +}; - error: function(txt) { - console.log(txt.red); - } +Logger.prototype.debug = function(txt) { + if (!(this.shouldLog('debug'))) { return; } + this.logFunction(txt); +}; +Logger.prototype.trace = function(txt) { + if (!(this.shouldLog('trace'))) { return; } + this.logFunction(txt); +}; + +Logger.prototype.shouldLog = function(level) { + return (this.logLevels.indexOf(level) <= this.logLevels.indexOf(this.logLevel)); }; module.exports = Logger; diff --git a/lib/monitor.js b/lib/monitor.js new file mode 100644 index 000000000..f2e338441 --- /dev/null +++ b/lib/monitor.js @@ -0,0 +1,309 @@ +/* eslint-disable */ +"use strict"; + +var blessed = require("blessed"); + +//var formatOutput = require("../utils/format-output.js"); +//var formatModules = require("../utils/format-modules.js"); +//var formatAssets = require("../utils/format-assets.js"); + +function Dashboard(options) { + var title = options && options.title || "Embark 2.0"; + this.env = options.env; + + this.color = options && options.color || "green"; + this.minimal = options && options.minimal || false; + this.setData = this.setData.bind(this); + + this.screen = blessed.screen({ + smartCSR: true, + title: title, + dockBorders: false, + fullUnicode: true, + autoPadding: true + }); + + this.layoutLog.call(this); + this.layoutStatus.call(this); + this.layoutModules.call(this); + this.layoutCmd.call(this); + + this.screen.key(["C-c"], function() { + process.exit(0); + }); + + this.status.setContent(this.env); + this.moduleTable.setData([ + ["Contract Name", "Address", "Status"], + ["SimpleStorage", "0x123", "Deployed".green] + ]); + + this.screen.render(); + + this.input.focus(); +} + +Dashboard.prototype.setData = function(dataArr) { + var self = this; + + self.logText.log(dataArr); + + this.screen.render(); +}; + +Dashboard.prototype.layoutLog = function() { + this.log = blessed.box({ + label: "Logs", + padding: 1, + width: "100%", + height: "55%", + left: "0%", + top: "42%", + border: { + type: "line", + }, + style: { + fg: -1, + border: { + fg: this.color, + }, + }, + }); + + 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); +}; + +Dashboard.prototype.layoutModules = function() { + this.modules = blessed.box({ + label: "Contracts", + tags: true, + padding: 1, + width: "75%", + height: "42%", + left: "0%", + top: "0", + border: { + type: "line", + }, + style: { + fg: -1, + border: { + fg: this.color, + }, + }, + }); + + this.moduleTable = blessed.table({ + parent: this.modules, + height: "100%", + width: "100%-5", + align: "left", + pad: 1, + 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); +}; + +Dashboard.prototype.layoutAssets = function() { + this.assets = blessed.box({ + label: "Asset Pipeline", + tags: true, + padding: 1, + width: "50%", + height: "55%", + left: "50%", + top: "42%", + border: { + type: "line", + }, + style: { + fg: -1, + border: { + fg: this.color, + }, + }, + }); + + 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); +}; + +Dashboard.prototype.layoutStatus = function() { + + this.wrapper = blessed.layout({ + width: "25%", + height: "42%", + top: "0%", + left: "75%", + layout: "grid" + }); + + this.status = blessed.box({ + parent: this.wrapper, + label: "Environment", + tags: true, + padding: { + left: 1, + }, + width: "100%", + height: "25%", + valign: "middle", + border: { + type: "line", + }, + style: { + fg: -1, + border: { + fg: this.color, + }, + }, + }); + + this.operations = blessed.box({ + parent: this.wrapper, + label: "Status", + tags: true, + padding: { + left: 1, + }, + width: "100%", + height: "25%", + valign: "middle", + border: { + type: "line", + }, + style: { + fg: -1, + border: { + fg: this.color, + }, + }, + }); + + this.progress = blessed.box({ + parent: this.wrapper, + label: "Available Services", + tags: true, + padding: this.minimal ? { + left: 1, + } : 1, + width: "100%", + height: "58%", + valign: "middle", + border: { + type: "line", + }, + style: { + fg: -1, + border: { + fg: this.color, + }, + }, + }); + + this.screen.append(this.wrapper); +}; + + +Dashboard.prototype.layoutCmd = function() { + this.consoleBox = blessed.box({ + label: 'Console', + tags: true, + padding: 0, + width: '100%', + height: '6%', + left: '0%', + top: '95%', + border: { + type: 'line', + }, + style: { + fg: 'black', + border: { + fg: this.color, + }, + }, + }); + + 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', + bg: 'black', + focus: { + bg: 'black', + fg: 'green' + } + } + }); + + var self = this; + + this.input.on('submit', function(data) { + if (data !== '') { + self.logText.log('console> ' + data); + } + if (data === 'quit') { + exit(); + }; + self.input.clearValue(); + self.input.focus(); + }); + + this.screen.append(this.consoleBox); +}; + +module.exports = Dashboard; diff --git a/lib/pipeline.js b/lib/pipeline.js index 1303afcd1..e86c07e63 100644 --- a/lib/pipeline.js +++ b/lib/pipeline.js @@ -6,13 +6,15 @@ var Pipeline = function(options) { this.buildDir = options.buildDir; this.contractsFiles = options.contractsFiles; this.assetFiles = options.assetFiles; + this.logger = options.logger; }; Pipeline.prototype.build = function(abi) { + var self = this; for(var targetFile in this.assetFiles) { var content = this.assetFiles[targetFile].map(function(file) { - console.log("reading " + file.filename); + self.logger.info("reading " + file.filename); if (file.filename === 'embark.js') { return file.content + "\n" + abi; } else { @@ -21,7 +23,7 @@ Pipeline.prototype.build = function(abi) { }).join("\n"); var dir = targetFile.split('/').slice(0, -1).join('/'); - console.log("creating dir " + this.buildDir + dir); + self.logger.info("creating dir " + this.buildDir + dir); mkdirp.sync(this.buildDir + dir); fs.writeFileSync(this.buildDir + targetFile, content); diff --git a/lib/server.js b/lib/server.js index 095599afe..4af23bb65 100644 --- a/lib/server.js +++ b/lib/server.js @@ -5,6 +5,7 @@ var serveStatic = require('serve-static'); var Server = function(options) { this.dist = options.dist || 'dist/'; this.port = options.port || 8000; + this.logger = options.logger; }; Server.prototype.start = function(callback) { @@ -14,7 +15,7 @@ Server.prototype.start = function(callback) { serve(req, res, finalhandler(req, res)); }); - console.log(("listening on port " + this.port).underline.green); + this.logger.info(("listening on port " + this.port).underline.green); server.listen(this.port) ; callback(); }; diff --git a/lib/watch.js b/lib/watch.js index 764c83fdb..f8b6dbd44 100644 --- a/lib/watch.js +++ b/lib/watch.js @@ -3,7 +3,7 @@ var fs = require('fs'); var chokidar = require('chokidar'); var Watch = function(options) { - this.options = options; + this.logger = options.logger; }; Watch.prototype.start = function() { @@ -17,7 +17,7 @@ Watch.prototype.start = function() { } // TODO: add callback to ready - console.log(filesToWatch); + this.logger.trace(filesToWatch); var watcher = chokidar.watch(filesToWatch, { ignored: /[\/\\]\./, persistent: true, @@ -25,11 +25,11 @@ Watch.prototype.start = function() { followSymlinks: true }); watcher - .on('add', path => console.log(`File ${path} has been added`)) - .on('change', path => console.log(`File ${path} has been changed`)) - .on('unlink', path => console.log(`File ${path} has been removed`)) - .on('ready', () => console.log('ready to watch changes')); - console.log("done!"); + .on('add', path => this.logger.info(`File ${path} has been added`)) + .on('change', path => this.logger.info(`File ${path} has been changed`)) + .on('unlink', path => this.logger.info(`File ${path} has been removed`)) + .on('ready', () => this.logger.info('ready to watch changes')); + this.logger.info("done!"); }; module.exports = Watch; diff --git a/package.json b/package.json index 6f38424f1..ae25b31d6 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "dependencies": { "async": "^2.0.1", "bignumber.js": "debris/bignumber.js#master", + "blessed": "^0.1.81", "bluebird": "^3.4.1", "chokidar": "^1.6.0", "colors": "^1.1.2",