All files / apps/web/src/utils usage.js

93.93% Statements 31/33
94.28% Branches 33/35
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40      3x     88x 59x 59x       247x 219x 219x   219x 130x 130x 130x 42x         89x 87x 85x 84x 5x         87x 60x 43x 41x    
// Helpers d'affichage de la consommation (partagés par `QuotaBar`).
// L'unité monétaire historique de `UsageTracking` est le 1/10 000 €.
 
const UNLIMITED = '∞'
 
export function usagePercent(used, limit) {
  if (limit == null || !Number.isFinite(limit) || limit <= 0) return null
  const value = Number.isFinite(used) ? used : 0
  return Math.round((value / limit) * 100)
}
 
export function formatUsageValue(value, type = 'count', reference = null) {
  if (value == null) return UNLIMITED
  const n = Number(value)
  Iif (!Number.isFinite(n)) return UNLIMITED
 
  if (type === 'cost') {
    const base = reference ?? n
    Iif (base == null) return `${(n / 10000).toFixed(2)}¢`
    if (base >= 1000000) return `${(n / 1000000).toFixed(2)}€`
    return `${(n / 10000).toFixed(2)}¢`
  }
 
  // Paliers : 10³, 10⁶, 10⁹ (milliard), 10¹² (téra). Sans le palier « Md »/« T », un quota
  // sur mesure à 10¹² s'affichait « 1000000M ».
  if (n >= 1000000000000) return `${(n / 1000000000000).toFixed(1)}T`
  if (n >= 1000000000) return `${(n / 1000000000).toFixed(1)}Md`
  if (n >= 1000000) return `${(n / 1000000).toFixed(1)}M`
  if (n >= 1000) return `${(n / 1000).toFixed(0)}k`
  return String(n)
}
 
// Ton de la consommation : neutre sans quota, puis vert / orange / rouge.
export function usageTone(percent) {
  if (percent == null) return 'neutral'
  if (percent >= 90) return 'red'
  if (percent >= 60) return 'orange'
  return 'green'
}