Newer
Older
hard-panel / panel / frontend / src / format.js
// Форматтеры панели

export function fmtBytes(value) {
  if (!value && value !== 0) return '—'
  const units = ['Б', 'КБ', 'МБ', 'ГБ', 'ТБ']
  let v = value
  let i = 0
  while (v >= 1024 && i < units.length - 1) {
    v /= 1024
    i++
  }
  const digits = v >= 100 ? 0 : v >= 10 ? 1 : 2
  return `${v.toFixed(digits)} ${units[i]}`
}

export function fmtGiB(bytes) {
  if (!bytes && bytes !== 0) return '—'
  return (bytes / 2 ** 30).toFixed(1) + ' GiB'
}

export function fmtPercent(value) {
  if (value === null || value === undefined) return '—'
  return `${Math.round(value * 10) / 10}%`
}

export function fmtMbs(value) {
  if (value === null || value === undefined) return '—'
  if (value < 0.01) return '0 МБ/с'
  if (value < 1) return `${Math.round(value * 100) / 100} МБ/с`
  return `${Math.round(value * 10) / 10} МБ/с`
}

export function fmtUptime(seconds) {
  if (!seconds && seconds !== 0) return '—'
  const days = Math.floor(seconds / 86400)
  const hours = Math.floor((seconds % 86400) / 3600)
  if (days > 0) return `${days}д ${hours}ч`
  const minutes = Math.floor((seconds % 3600) / 60)
  if (hours > 0) return `${hours}ч ${minutes}м`
  return `${minutes}м ${seconds % 60}с`
}

export function fmtAgo(iso) {
  if (!iso) return 'никогда'
  const seconds = Math.floor((Date.now() - new Date(iso).getTime()) / 1000)
  if (seconds < 5) return 'только что'
  if (seconds < 60) return `${seconds} с назад`
  if (seconds < 3600) return `${Math.floor(seconds / 60)} мин назад`
  if (seconds < 86400) return `${Math.floor(seconds / 3600)} ч назад`
  return `${Math.floor(seconds / 86400)} дн назад`
}

export function ramPercent(metrics) {
  if (!metrics || !metrics.ram || !metrics.ram.total) return null
  return (metrics.ram.used / metrics.ram.total) * 100
}