Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON report format #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions cli-core/reportJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const fs = require('fs');
const path = require('path');
const ProgressBar = require('progress');

//create json report for all the analysed pages and recap
async function create_JSON_report(reportObject, options){
//Path of the output file
let OUTPUT_FILE = path.resolve(options.report_output_file);
if (OUTPUT_FILE.toLowerCase().endsWith('results.xlsx')) {
OUTPUT_FILE = OUTPUT_FILE.substring(0, OUTPUT_FILE.length-'.xlsx'.length) + '.json'
}
if (!OUTPUT_FILE.toLowerCase().endsWith('.json')) {
throw ` report_output_file : File "${OUTPUT_FILE}" does not end with the ".json" extension.`
}

const fileList = reportObject.reports;
const globalReport = reportObject.globalReport;

//initialise progress bar
let progressBar;
if (!options.ci){
progressBar = new ProgressBar(' Create Json report [:bar] :percent Remaining: :etas Time: :elapseds', {
complete: '=',
incomplete: ' ',
width: 40,
total: fileList.length+2
});
progressBar.tick()
} else {
console.log('Creating JSON report ...');
}

let global = JSON.parse(fs.readFileSync(globalReport.path).toString());
let pages = {};
if (progressBar) progressBar.tick()

fileList.forEach((file)=>{
pages[file.name] = JSON.parse(fs.readFileSync(file.path).toString());
if (progressBar) progressBar.tick()
})

try {
fs.writeFileSync(OUTPUT_FILE, JSON.stringify({global, pages}));
} catch (error) {
throw ` report_output_file : Path "${OUTPUT_FILE}" cannot be reached.`
}
}

module.exports = {
create_JSON_report
}
10 changes: 6 additions & 4 deletions commands/analyse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const login = require('../cli-core/analysis.js').login;
const create_global_report = require('../cli-core/reportGlobal.js').create_global_report;
const create_XLSX_report = require('../cli-core/reportExcel.js').create_XLSX_report;
const create_html_report = require('../cli-core/reportHtml.js').create_html_report;
const create_JSON_report = require("../cli-core/reportJson").create_JSON_report;
const writeToInflux = require("../cli-core/influxdb").write;

//launch core
Expand Down Expand Up @@ -83,8 +84,9 @@ async function analyse_core(options) {
await create_html_report(reportObj, options);
} else if (reportFormat === 'influxdb') {
await writeToInflux(reports, options);
}
else {
} else if (reportFormat === 'json') {
await create_JSON_report(reportObj, options);
} else {
await create_XLSX_report(reportObj, options);
}

Expand Down Expand Up @@ -117,7 +119,7 @@ function readHeaders(headersFile) {

function getReportFormat(format, filename) {
// Check if format is defined
const formats = ['xlsx', 'html', 'influxdb'];
const formats = ['xlsx', 'html', 'influxdb', 'json'];
if (format && formats.includes(format.toLowerCase())) {
return format.toLowerCase();
}
Expand All @@ -136,4 +138,4 @@ function analyse(options) {
analyse_core(options).catch(e=>console.error("ERROR : \n", e))
}

module.exports = analyse;
module.exports = analyse;