Skip to content

Commit

Permalink
Initial report module (hyperledger-archives#3264)
Browse files Browse the repository at this point in the history
Signed-off-by: James Taylor <[email protected]>
Signed-off-by: Sam Smith <[email protected]>
  • Loading branch information
jt-nti authored and samjsmith committed Jan 30, 2018
1 parent cbde5f9 commit 3c04c00
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 109 deletions.
63 changes: 8 additions & 55 deletions packages/composer-cli/lib/cmds/report/lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,9 @@
*/

'use strict';
const fs = require('fs');
const os = require('os');
const { sep } = require('path');
const cmdUtil = require('../../utils/cmdutils');
const report = require('composer-report');
const chalk = require('chalk');
const moment = require('moment');
const nodereport = require('node-report');
const tar = require('tar');

/**
* Composer "capture" command
Expand All @@ -33,61 +28,19 @@ class Report {
* @return {Promise} promise when command complete
*/
static handler(args) {
return this.report(args.file);
return Promise.resolve(this.createReport(args.file));
}

/**
* Get the current environment data
* @return {Promise} resolved/rejected promise when the command is complete
*/
static report() {
cmdUtil.log(chalk.blue.bold('Creating Composer report\n'));
let tmpDirectory = this._setupReportDir();
this._createNodeReport(tmpDirectory);
return this._archiveReportDir(tmpDirectory);
}

/**
* Sets up the temp directory for the report
* @return {String} the Path to the temporary directory
*/
static _setupReportDir() {
const tmpDir = os.tmpdir();
return fs.mkdtempSync(`${tmpDir}${sep}`);
}

/**
* Trigger node-report to write report in the temp directory
* @param {String} tmpDirectory the temporary directory for collecting report output
*/
static _createNodeReport(tmpDirectory) {
static createReport() {
cmdUtil.log(chalk.bold.blue('Creating Composer report'));
let tmpDirectory = report.setupReportDir();
cmdUtil.log(chalk.blue('Triggering node report...'));
nodereport.setDirectory(tmpDirectory);
nodereport.triggerReport();
}

/**
* Creates an archive of the temp directory for the report
* @param {String} tmpDirectory the temporary directory for collecting report output
* @param {String} outputFilename the name of the file that was optionally passed in on the command line
* @return {Promise} resolved/rejected promise when the archive has been created
*/
static _archiveReportDir(tmpDirectory) {
let timestamp = moment().format('YYYYMMDD[T]HHmmss');
let prefix = 'composer-report-' + timestamp;
let filename = prefix + '.tgz';
return tar.c(
{
cwd: tmpDirectory+'/',
prefix: prefix,
gzip: true,
file: filename
},
['.']
).then(() => {
cmdUtil.log(chalk.blue.bold('\nSuccessfully created Composer report file to '));
cmdUtil.log(chalk.blue('\tOutput file: ')+filename);
});
report.createNodeReport(tmpDirectory);
let outputFile = report.archiveReport(tmpDirectory);
cmdUtil.log(chalk.bold.blue('Created archive file: '+outputFile));
}
}
module.exports = Report;
1 change: 1 addition & 0 deletions packages/composer-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"composer-admin": "0.17.3",
"composer-client": "0.17.3",
"composer-common": "0.17.3",
"composer-report": "0.17.3",
"homedir": "0.6.0",
"js-yaml": "3.10.0",
"mkdirp": "0.5.1",
Expand Down
73 changes: 20 additions & 53 deletions packages/composer-cli/test/report/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,78 +14,45 @@

'use strict';

const Report = require('../../lib/cmds/report.js');
const composerReport = require('composer-report');

const Report = require('../../lib/cmds/report.js');
const ReportCmd = require('../../lib/cmds/report/reportCommand.js');

const chai = require('chai');
const sinon = require('sinon');
chai.should();
chai.use(require('chai-as-promised'));
const fs = require('fs');
const os = require('os');
const nodereport = require('node-report');
const tar = require('tar');
const assert = sinon.assert;
const sinonChai = require('sinon-chai');
chai.use(sinonChai);

describe('composer report CLI', function() {
const sandbox = sinon.sandbox.create();
let consoleLogSpy;
let mkdtempSyncStub;
let triggerReportStub;
let setDirectoryStub;
let cStub;
let setupStub;
let reportStub;
let archiveStub;

beforeEach(function() {
consoleLogSpy = sandbox.spy(console, 'log');
sandbox.stub(process, 'exit');
mkdtempSyncStub = sandbox.stub(fs,'mkdtempSync').returns('COMPOSER_REPORT_TEMPDIR');
sandbox.stub(os, 'tmpdir').returns('OS_TEMPDIR');
triggerReportStub = sandbox.stub(nodereport, 'triggerReport');
setDirectoryStub = sandbox.stub(nodereport, 'setDirectory');
cStub = sandbox.stub(tar, 'c').returns(Promise.resolve());
setupStub = sandbox.stub(composerReport, 'setupReportDir').returns('DIR');
reportStub = sandbox.stub(composerReport, 'createNodeReport');
archiveStub = sandbox.stub(composerReport, 'archiveReport').returns('ARCHIVE');
});

afterEach(function() {
sandbox.restore();
});

it('should successfully run the composer report command with no arguments specified', function() {
const args = {};
return ReportCmd.handler(args).then(() => {
sinon.assert.calledWith(consoleLogSpy, sinon.match('Creating Composer report'));
});
});

it('should create a temporary directory to store files to create the report archive from', function() {
it('should successfully run the composer report command', function() {
const args = {};
return ReportCmd.handler(args).then(() => {
sinon.assert.calledOnce(mkdtempSyncStub);
sinon.assert.calledWith(mkdtempSyncStub, 'OS_TEMPDIR/');
});
});

it('should successfully write a node-report report to the temporary directory', function() {
const args = { };
return ReportCmd.handler(args).then(() => {
sinon.assert.calledOnce(setDirectoryStub);
sinon.assert.calledWith(setDirectoryStub, 'COMPOSER_REPORT_TEMPDIR');
sinon.assert.calledOnce(triggerReportStub);
});
});

it('should successfully create a zipped tar archive of the COMPOSER_REPORT_TEMPDIR in the current directory and log the output filename in the console', function() {
const args = { };
return ReportCmd.handler(args).then(() => {
sinon.assert.calledOnce(cStub);
sinon.assert.calledWith(cStub, {
cwd: 'COMPOSER_REPORT_TEMPDIR/',
prefix: sinon.match(/^composer-report-\d{8}T\d{6}$/),
gzip: true,
file: sinon.match(/^composer-report-\d{8}T\d{6}\.tgz$/)
}, ['.']);
sinon.assert.called(consoleLogSpy);
sinon.assert.calledWith(consoleLogSpy, sinon.match('Triggering node report...'));
sinon.assert.calledWith(consoleLogSpy, sinon.match('Successfully created Composer report file to'));
sinon.assert.calledWith(consoleLogSpy, sinon.match(/Output file: .*composer-report-\d{8}T\d{6}\.tgz$/));
assert.calledThrice(consoleLogSpy);
assert.calledWith(consoleLogSpy, sinon.match('Creating Composer report'));
assert.calledWith(consoleLogSpy, sinon.match('Triggering node report...'));
assert.calledWith(consoleLogSpy, sinon.match('Created archive file: ARCHIVE'));
assert.calledOnce(setupStub);
assert.calledWith(reportStub, 'DIR');
assert.calledWith(archiveStub, 'DIR');
});
});

Expand Down
1 change: 0 additions & 1 deletion packages/composer-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
"exclude": [
"coverage/**",
"lib/codegen/**",

"lib/tools/plantumltoimage.js",
"lib/introspect/parser.js",
"lib/acl/parser.js",
Expand Down
12 changes: 12 additions & 0 deletions packages/composer-report/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions packages/composer-report/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
node_modules
out
47 changes: 47 additions & 0 deletions packages/composer-report/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
env:
es6: true
node: true
mocha: true
extends: 'eslint:recommended'
parserOptions:
sourceType:
- script
rules:
indent:
- error
- 4
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always
no-unused-vars:
- error
- args: none
no-console: warn
curly: error
eqeqeq: error
no-throw-literal: error
strict: error
no-var: error
dot-notation: error
no-tabs: error
no-trailing-spaces: error
# no-use-before-define: error
no-useless-call: error
no-with: error
operator-linebreak: error
require-jsdoc:
- error
- require:
ClassDeclaration: true
MethodDefinition: true
FunctionDeclaration: true
valid-jsdoc:
- error
- requireReturn: false
yoda: error
11 changes: 11 additions & 0 deletions packages/composer-report/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
20 changes: 20 additions & 0 deletions packages/composer-report/bin/cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const report = require('../lib/report.js');

report.report();
13 changes: 13 additions & 0 deletions packages/composer-report/header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
78 changes: 78 additions & 0 deletions packages/composer-report/lib/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const fs = require('fs');
const os = require('os');
const { sep } = require('path');
const moment = require('moment');
const nodereport = require('node-report');
const tar = require('tar');

/**
* Main API called from cmd and composer report.
* @return {String} the name of the archive file that was created.
*/
function report() {
let tmpDirectory = setupReportDir();

// TODO write readme file inc. version of the composer-report module
// Plus versions of the other composer modules?

createNodeReport(tmpDirectory);
return archiveReport(tmpDirectory);
}

/**
* Sets up the temp directory for the report
* @return {String} the Path to the temporary directory
*/
function setupReportDir() {
const tmpDir = os.tmpdir();
return fs.mkdtempSync(`${tmpDir}${sep}`);
}

/**
* Trigger node-report to write report in the temp directory
* @param {String} tmpDirectory the temporary directory for collecting report output
*/
function createNodeReport(tmpDirectory) {
nodereport.setDirectory(tmpDirectory);
nodereport.triggerReport();
}

/**
* Creates an archive of the temp directory for the report
* @param {String} tmpDirectory the temporary directory for collecting report output
* @return {String} the name of the archive file that has been created.
*/
function archiveReport(tmpDirectory) {
let timestamp = moment().utc().format('YYYYMMDD[T]HHmmss');
let prefix = 'composer-report-' + timestamp;
let filename = prefix + '.tgz';
tar.c(
{
cwd: tmpDirectory+'/',
prefix: prefix,
gzip: true,
file: filename,
sync: true
},
['.']
);
return filename;
}

module.exports = { report, setupReportDir, createNodeReport, archiveReport } ;
Loading

0 comments on commit 3c04c00

Please sign in to comment.