v0.309.0: the fill warning reaches the household in its own language; the root-filesystem disk event pinned end to end (R-521 F7)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
2026-10-12 17:11:53 +02:00
parent 402fa41e45
commit 7626a28de3
14 changed files with 290 additions and 53 deletions
+61 -27
View File
@@ -27,6 +27,8 @@ import (
"sort"
"strings"
"sync"
"gitea.dooplex.hu/admin/felhom-controller/internal/i18n"
)
// ── Thresholds ───────────────────────────────────────────────────────────────────────────────────
@@ -120,6 +122,11 @@ type Usage struct {
type Target struct {
Path string
Label string
// LabelKey, when set, names the target in the household's language from the bundle (the two
// built-in areas: „Alkalmazások területe", „Rendszer- és mentési terület"). Label stays the
// Hungarian name for the log and the event details. A household's own drive label has no key —
// the household named it, in whatever language it wished.
LabelKey string
}
// Event is one crossing, handed to the notify seam.
@@ -127,7 +134,10 @@ type Event struct {
Target Target
Band Band
Usage Usage
Message string // the Hungarian customer text, already rendered
Message string // the Hungarian customer text, already rendered (the wire's `message`)
// MessageHousehold is the same sentence in the household's language (the wire's
// `message_customer`, R-558); "" for a Hungarian household, which sends nothing extra.
MessageHousehold string
}
// Watcher holds the persisted per-filesystem band and emits on escalation only.
@@ -142,9 +152,12 @@ type Watcher struct {
usage func(path string) *Usage
// notify pushes the customer event. Nil-safe.
notify func(Event)
// extra appends a sentence to the warning for one target — the kept folders on it the household
// can delete (`09` §3 decision 36). Nil-safe; "" appends nothing.
extra func(Target) string
// extra appends a sentence to the warning for one target, in a language — the kept folders on it
// the household can delete (`09` §3 decision 36). Nil-safe; "" appends nothing.
extra func(t Target, lang string) string
// lang returns the household's language (R-521 F7: the warning reaches an English household in
// English). Nil-safe: nil means Hungarian only.
lang func() string
mu sync.Mutex
bands map[string]Band
@@ -168,7 +181,10 @@ func New(statePath string, logger *log.Logger, targets func() []Target, usage fu
func (w *Watcher) SetNotify(fn func(Event)) { w.notify = fn }
// SetExtra wires the kept-data sentence (main.go). INIT-ONLY.
func (w *Watcher) SetExtra(fn func(Target) string) { w.extra = fn }
func (w *Watcher) SetExtra(fn func(t Target, lang string) string) { w.extra = fn }
// SetLang wires the household's language (main.go: settings.GetLanguage). INIT-ONLY.
func (w *Watcher) SetLang(fn func() string) { w.lang = fn }
// classify decides the band from a reading AND the previous band, which is what makes the hysteresis
// work: between the clear and warn thresholds the previous band is HELD rather than recomputed.
@@ -236,17 +252,18 @@ func (w *Watcher) Check() error {
continue
}
msg := Message(t, *u, next)
if w.extra != nil {
if x := w.extra(t); x != "" {
msg += " " + x
msg := w.messageWithExtra(i18n.Default, t, *u, next)
household := ""
if w.lang != nil {
if lang := i18n.Normalize(w.lang()); lang != i18n.Default {
household = w.messageWithExtra(lang, t, *u, next)
}
}
w.logger.Printf("[WARN] [fillwatch] %s (%q): %s → %s — %.0f%% used, %.1f GB free of %.1f GB; notifying the customer",
t.Path, t.Label, prev, next, u.UsedPercent, u.AvailGB, u.TotalGB)
emitted++
if w.notify != nil {
w.notify(Event{Target: t, Band: next, Usage: *u, Message: msg})
w.notify(Event{Target: t, Band: next, Usage: *u, Message: msg, MessageHousehold: household})
}
}
@@ -273,6 +290,17 @@ func (w *Watcher) Check() error {
return w.saveLocked()
}
// messageWithExtra is the warning in one language, with the kept-data sentence when there is one.
func (w *Watcher) messageWithExtra(lang string, t Target, u Usage, band Band) string {
msg := MessageLang(lang, t, u, band)
if w.extra != nil {
if x := w.extra(t, lang); x != "" {
msg += " " + x
}
}
return msg
}
// bandSummaryLocked renders the current per-path bands for the run summary, deterministically. Caller
// holds w.mu.
func (w *Watcher) bandSummaryLocked() string {
@@ -376,35 +404,41 @@ func (w *Watcher) saveLocked() error {
// these two types: `FormatCustomerEmail` PREFERS the entry over the message, so a static template
// would discard the label and the figures — exactly why `offbox_enlarge_blocked` and
// `disk_health_degraded` have none either.
func Message(t Target, u Usage, band Band) string {
func Message(t Target, u Usage, band Band) string { return MessageLang(i18n.Default, t, u, band) }
// MessageLang is Message in a language, from the bundle keys `event.fill.critical` /
// `event.fill.warning` (R-521 F7). The Hungarian is the literal these keys replaced minus its formal
// „Kérjük," opening (the R-516 te-form gate); TestMessageHungarianWireText pins the exact bytes.
func MessageLang(lang string, t Target, u Usage, band Band) string {
b, err := i18n.Shared()
if err != nil {
// The bundle is embedded: this is a broken build. Say it in the log and send the figures bare
// rather than nothing — an event with a visible fault reaches the operator and gets fixed.
return fmt.Sprintf("%s: %s, %s (%v)", t.Path, gbIn(lang, u.AvailGB), percent(u.UsedPercent), err)
}
name := t.Label
if t.LabelKey != "" && b.Has(i18n.Normalize(lang), t.LabelKey) {
name = b.Msg(lang, t.LabelKey)
}
if name == "" {
name = t.Path
}
if band == BandCritical {
return fmt.Sprintf(
"A(z) „%s” tároló kritikusan megtelt: %s szabad hely maradt (%s foglalt). "+
"A biztonsági mentések és az alkalmazások írásai bármikor meghiúsulhatnak. "+
"Kérjük, mielőbb szabadíts fel helyet: törölj felesleges fájlokat, vagy csatlakoztass új meghajtót.",
name, hunGB(u.AvailGB), hunPercent(u.UsedPercent))
return b.Msgf(lang, "event.fill.critical", name, gbIn(lang, u.AvailGB), percent(u.UsedPercent))
}
return fmt.Sprintf(
"A(z) „%s” tároló %s foglalt — %s szabad hely maradt. "+
"Kérjük, szabadíts fel helyet, mielőtt megtelik: törölj felesleges fájlokat, vagy csatlakoztass új meghajtót. "+
"Ha megtelik, a biztonsági mentések meghiúsulnak.",
name, hunPercent(u.UsedPercent), hunGB(u.AvailGB))
return b.Msgf(lang, "event.fill.warning", name, percent(u.UsedPercent), gbIn(lang, u.AvailGB))
}
// hunGB formats a GB figure the Hungarian way — decimal COMMA, one decimal place. A "4.2 GB" in a
// Hungarian sentence reads as a typo to the customer.
func hunGB(gb float64) string {
// gbIn formats a GB figure with lang's decimal separator, one decimal place — a "4.2 GB" in a
// Hungarian sentence reads as a typo to the customer, and "4,2 GB" in an English one.
func gbIn(lang string, gb float64) string {
if gb >= 1024 {
return strings.Replace(fmt.Sprintf("%.1f TB", gb/1024), ".", ",", 1)
return i18n.FormatDecimal(lang, gb/1024, 1) + " TB"
}
return strings.Replace(fmt.Sprintf("%.1f GB", gb), ".", ",", 1)
return i18n.FormatDecimal(lang, gb, 1) + " GB"
}
func hunPercent(p float64) string { return fmt.Sprintf("%.0f%%", p) }
func percent(p float64) string { return fmt.Sprintf("%.0f%%", p) }
// SortTargets gives Check a deterministic order, so a multi-filesystem crossing produces its events
// in a stable sequence (tests, and a readable log).