Files
refusol-datalogging/frontend/src/lib/format.js
T
bonaminandClaude Opus 5.5 0b74842560 feat(frontend): settings page, Greek translation, comparison and chart options
The end user is Greek and the owner wants to confirm at a glance that
every inverter is working without one cloud-shaded reading raising doubt.

- Settings page (gear in the header), stored on the server and shared
  by every viewer; saving asks for the API key once per device.
- Output comparison: latest reading or average of the last 3/5 (evens out
  a passing cloud); bar scale auto (round ceiling above the best) /
  highest = 100% / custom kW; percentage vs fleet median, best inverter
  or the custom kW.
- Power chart: 30 min to last month plus "last production run", a
  per-visit range picker on the card, optional light/strong smoothing.
  Series reporting slower than the chart bucket (INV5, every 5 min) are
  now interpolated across short gaps — on 1-min buckets their line used
  to vanish entirely; night gaps still break the line.
- Greek/English: per-device switch in the header, shared default in
  settings, Greek number/date formats; issue texts translated from
  code + params.
- AC voltage shown as phase (L-N) or line (L-L, x√3, matching the
  inverter screens); lifetime total says how many inverters it covers;
  the inverter's own status text appears on its card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:15:38 +03:00

105 lines
3.3 KiB
JavaScript

// Set by the language provider so numbers and dates follow the chosen language
let lang = 'en'
const LOCALES = { en: 'en-GB', el: 'el-GR' }
export function setFormatLanguage(l) {
lang = LOCALES[l] ? l : 'en'
}
const locale = () => LOCALES[lang]
const nfCache = new Map()
const nf = (digits) => {
const key = `${lang}:${digits}`
if (!nfCache.has(key)) {
nfCache.set(key, new Intl.NumberFormat(locale(), { minimumFractionDigits: digits, maximumFractionDigits: digits }))
}
return nfCache.get(key)
}
export function num(v, digits = 0) {
return v === null || v === undefined || Number.isNaN(v) ? '—' : nf(digits).format(v)
}
/** Power with an auto unit: 950 W, 12.4 kW, 1.20 MW. Returns [value, unit]. */
export function powerParts(w) {
if (w === null || w === undefined) return ['—', 'W']
const a = Math.abs(w)
if (a >= 1e6) return [num(w / 1e6, 2), 'MW']
if (a >= 1000) return [num(w / 1000, a >= 1e5 ? 0 : 1), 'kW']
return [num(w, 0), 'W']
}
export function power(w) {
const [v, u] = powerParts(w)
return `${v} ${u}`
}
/** Energy with an auto unit: 850 Wh, 42.3 kWh, 123.4 MWh. Returns [value, unit]. */
export function energyParts(wh) {
if (wh === null || wh === undefined) return ['—', 'kWh']
const a = Math.abs(wh)
if (a >= 1e6) return [num(wh / 1e6, a >= 1e8 ? 0 : 1), 'MWh']
if (a >= 1000) return [num(wh / 1000, a >= 1e5 ? 0 : 1), 'kWh']
return [num(wh, 0), 'Wh']
}
export function energy(wh) {
const [v, u] = energyParts(wh)
return `${v} ${u}`
}
const UNITS = {
en: { s: 's', min: 'min', h: 'h', d: 'd', days: 'days', ago: (x) => `${x} ago`, never: 'never' },
el: { s: 'δευτ.', min: 'λεπ.', h: 'ώρ.', d: 'ημ.', days: 'ημ.', ago: (x) => `πριν ${x}`, never: 'ποτέ' },
}
export function ago(iso) {
const u = UNITS[lang]
if (!iso) return u.never
const s = Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 1000))
if (s < 60) return u.ago(`${s} ${u.s}`)
if (s < 3600) return u.ago(`${Math.floor(s / 60)} ${u.min}`)
if (s < 86400) return u.ago(`${Math.floor(s / 3600)} ${u.h}`)
return u.ago(`${Math.floor(s / 86400)} ${u.d}`)
}
/** Length of a silence: 12 min, 3.5 h, 2.0 days. */
export function duration(seconds) {
const u = UNITS[lang]
if (seconds < 3600) return `${Math.floor(seconds / 60)} ${u.min}`
if (seconds < 86400) return `${num(seconds / 3600, 1)} ${u.h}`
return `${num(seconds / 86400, 1)} ${u.days}`
}
export function dateTime(iso) {
return new Date(iso).toLocaleString(locale())
}
export function pct(ratio) {
return ratio === null || ratio === undefined ? '—' : `${Math.round(ratio * 100)}%`
}
export function timeLabel(unix, spanSeconds) {
const d = new Date(unix * 1000)
if (spanSeconds <= 2 * 86400) return d.toLocaleTimeString(locale(), { hour: '2-digit', minute: '2-digit' })
return d.toLocaleDateString(locale(), { weekday: 'short', day: 'numeric' })
}
export function dateTimeLabel(unix) {
return new Date(unix * 1000).toLocaleString(locale(), {
weekday: 'short',
day: 'numeric',
month: 'short',
hour: '2-digit',
minute: '2-digit',
})
}
export function dayLabel(isoDay, long = false) {
const d = new Date(`${isoDay}T12:00:00`)
return long
? d.toLocaleDateString(locale(), { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' })
: d.toLocaleDateString(locale(), { day: 'numeric', month: 'short' })
}