Skip to content

Commit

Permalink
Refactor statistics script for importability (#6848)
Browse files Browse the repository at this point in the history
  • Loading branch information
queengooborg authored Oct 15, 2020
1 parent e26fc2b commit 5bb65b5
Showing 1 changed file with 72 additions and 57 deletions.
129 changes: 72 additions & 57 deletions scripts/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const { argv } = require('yargs').command(
default: '',
})
.option('all', {
alias: 'a',
describe: 'Show statistics for all browsers within BCD',
type: 'flags',
type: 'boolean',
nargs: 0,
});
},
Expand All @@ -36,27 +37,6 @@ const { argv } = require('yargs').command(
* @property {number} real The total number of real values for the browser.
*/

/**
* @constant {string[]}
*/
const browsers = argv.all
? Object.keys(bcd.browsers)
: [
'chrome',
'chrome_android',
'edge',
'firefox',
'ie',
'safari',
'safari_ios',
'webview_android',
];
/** @type {object.<string, VersionStats>} */
let stats = { total: { all: 0, true: 0, null: 0, range: 0, real: 0 } };
browsers.forEach(browser => {
stats[browser] = { all: 0, true: 0, null: 0, range: 0, real: 0 };
});

/**
* Check whether a support statement is a specified type
*
Expand Down Expand Up @@ -86,9 +66,11 @@ const checkSupport = (supportData, type) => {
* Iterate through all of the browsers and count the number of true, null, real, and ranged values for each browser
*
* @param {Identifier} data The data to process and count stats for
* @param {string[]} browsers The browsers to test
* @param {object.<string, VersionStats>} stats The stats object to update
* @returns {void}
*/
const processData = data => {
const processData = (data, browsers, stats) => {
if (data.support) {
browsers.forEach(browser => {
stats[browser].all++;
Expand Down Expand Up @@ -117,74 +99,107 @@ const processData = data => {
* Iterate through all of the data and process statistics
*
* @param {Identifier} data The compat data to iterate
* @param {string[]} browsers The browsers to test
* @param {object.<string, VersionStats>} stats The stats object to update
* @returns {void}
*/
const iterateData = data => {
const iterateData = (data, browsers, stats) => {
for (const key in data) {
if (key === '__compat') {
processData(data[key]);
processData(data[key], browsers, stats);
} else {
iterateData(data[key]);
iterateData(data[key], browsers, stats);
}
}
};

/**
* Print a Markdown-formatted table of the statistics
* Get all of the stats
*
* @returns {void}
* @param {string} folder The folder to show statistics for (or all folders if blank)
* @param {boolean} allBrowsers If true, get stats for all browsers, not just main eight
* @returns {object.<string, VersionStats>?}
*/
const printTable = () => {
let table = `| browser | real values | ranged values | \`true\` values | \`null\` values |
| --- | --- | --- | --- | --- |
`;
const getStats = (folder, allBrowsers) => {
/**
* @constant {string[]}
*/
const browsers = allBrowsers
? Object.keys(bcd.browsers)
: [
'chrome',
'chrome_android',
'edge',
'firefox',
'ie',
'safari',
'safari_ios',
'webview_android',
];

Object.keys(stats).forEach(entry => {
table += `| ${entry.replace('_', ' ')} | `;
table += `${((stats[entry].real / stats[entry].all) * 100).toFixed(2)}% | `;
table += `${((stats[entry].range / stats[entry].all) * 100).toFixed(
2,
)}% | `;
table += `${((stats[entry].true / stats[entry].all) * 100).toFixed(2)}% | `;
table += `${((stats[entry].null / stats[entry].all) * 100).toFixed(2)}% |
`;
/** @type {object.<string, VersionStats>} */
let stats = { total: { all: 0, true: 0, null: 0, range: 0, real: 0 } };
browsers.forEach(browser => {
stats[browser] = { all: 0, true: 0, null: 0, range: 0, real: 0 };
});

console.log(table);
};

/**
* Print statistics of BCD
*
* @param {string} folder The folder to show statistics for (or all folders if blank)
* @returns {boolean} False if the folder specified wasn't found
*/
const printStats = folder => {
if (folder) {
if (bcd[folder]) {
iterateData(bcd[folder]);
iterateData(bcd[folder], browsers, stats);
} else {
console.error(chalk`{red.bold Folder "${folder}/" doesn't exist!}`);
return false;
return null;
}
} else {
for (const data in bcd) {
if (!(data === 'browsers' || data === 'webextensions')) {
iterateData(bcd[data]);
iterateData(bcd[data], browsers, stats);
}
}
}

return stats;
};

/**
* Print statistics of BCD
*
* @param {object.<string, VersionStats>} stats The stats object to print from
* @param {string} folder The folder to show statistics for (or all folders if blank)
* @returns {void}
*/
const printStats = (stats, folder) => {
if (!stats) {
console.error(`No stats${folder ? ` for folder ${folder}` : ''}!`);
return;
}

console.log(
chalk`{bold Status as of version 1.0.xx (released on 2020-MM-DD) for ${
folder ? `${folder}/ directory` : 'web platform features'
}}: \n`,
);
printTable();

return true;
let table = `| browser | real values | ranged values | \`true\` values | \`null\` values |
| --- | --- | --- | --- | --- |
`;

Object.keys(stats).forEach(entry => {
table += `| ${entry.replace('_', ' ')} | `;
table += `${((stats[entry].real / stats[entry].all) * 100).toFixed(2)}% | `;
table += `${((stats[entry].range / stats[entry].all) * 100).toFixed(
2,
)}% | `;
table += `${((stats[entry].true / stats[entry].all) * 100).toFixed(2)}% | `;
table += `${((stats[entry].null / stats[entry].all) * 100).toFixed(2)}% |
`;
});

console.log(table);
};

if (require.main === module) {
printStats(argv.folder);
printStats(getStats(argv.folder, argv.all), argv.folder);
}

module.exports = getStats;

0 comments on commit 5bb65b5

Please sign in to comment.