format-usage-stats.js 774 B

123456789101112131415161718192021222324252627
  1. function formatTokenUsageStats(STATS) {
  2. const prettyStats = []
  3. const sortedStats = Object.entries(STATS).sort((a, b) =>
  4. a[0] > b[0] ? 1 : -1
  5. )
  6. const totalByName = {}
  7. for (const [key, n] of sortedStats) {
  8. const [name, version, collectionName, path, label] = key.split(':')
  9. totalByName[name] = (totalByName[name] || 0) + n
  10. prettyStats.push({ name, version, collectionName, path, label, n })
  11. }
  12. for (const row of prettyStats) {
  13. row.percentage = ((100 * row.n) / totalByName[row.name])
  14. .toFixed(2)
  15. .padStart(6)
  16. }
  17. if (prettyStats.length === 0) {
  18. console.warn('---')
  19. console.warn('Found 0 access tokens.')
  20. console.warn('---')
  21. } else {
  22. console.table(prettyStats)
  23. }
  24. }
  25. module.exports = { formatTokenUsageStats }