overleaf/libraries/access-token-encryptor/scripts/helpers/format-usage-stats.js
Jakob Ackermann 246d62b578 Merge pull request #11582 from overleaf/jpa-count-tokens-empty-result
[access-token-encryptor] nicer log message for zero access token found

GitOrigin-RevId: 2a28fdc71a1e063f091f55bc247d9dd26bbd0ee3
2023-02-02 09:03:02 +00:00

27 lines
774 B
JavaScript

function formatTokenUsageStats(STATS) {
const prettyStats = []
const sortedStats = Object.entries(STATS).sort((a, b) =>
a[0] > b[0] ? 1 : -1
)
const totalByName = {}
for (const [key, n] of sortedStats) {
const [name, version, collectionName, path, label] = key.split(':')
totalByName[name] = (totalByName[name] || 0) + n
prettyStats.push({ name, version, collectionName, path, label, n })
}
for (const row of prettyStats) {
row.percentage = ((100 * row.n) / totalByName[row.name])
.toFixed(2)
.padStart(6)
}
if (prettyStats.length === 0) {
console.warn('---')
console.warn('Found 0 access tokens.')
console.warn('---')
} else {
console.table(prettyStats)
}
}
module.exports = { formatTokenUsageStats }