mesh/delivery merged: source.main moved to novox/mesh-catalog@6d000647de2d
mesh/check being carried to cff1005422c1 on 6d000647de2d | from f9dcbc295723
The one bundle that did everything becomes the manager bundle, which runs long and answers the seat's ten verbs and the three acting tools, and a tools bundle of its own that answers the four reading tools and writes nothing, so they answer while the manager fails to load or stops. Both import the module's one package for the layouts of the kept data and the log entries, the key form, the entries' names and the settings with their validity; the settings file and the account with its directory stay as they are. Builds: design 39 parts §1, §2, §3, §4, §5, §6, §7, §8
157 lines
6.1 KiB
Go
157 lines
6.1 KiB
Go
package licences
|
|
|
|
// The module's settings (design 39 §8, design 39 bus-and-switch §33, B37; design 39 parts §5): today's five and the six
|
|
// of the switch, each declared in the manifest's `config` with its default and bound, set through mesh-config and
|
|
// written into the settings file; with their defaults, bounds and validity, read by both bundles.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// Settings are the manager's own, declared with defaults.
|
|
type Settings struct {
|
|
Cadence time.Duration // rotate a grant once older than this
|
|
Floor time.Duration // refresh in any case with less than this left
|
|
FailuresToNotify int
|
|
Cooldown time.Duration // at most one notification per licence in this window
|
|
RefreshWarn time.Duration // warn this long before a refresh token itself expires
|
|
// SwitchAuto is whether a reading at its threshold moves the licence's consumers (§26).
|
|
SwitchAuto bool
|
|
// SwitchAt, RoomBelow and WarnAt are percentages: where a licence's consumers move, what a target's windows must
|
|
// both be below, and where a licence with consumers is warned about.
|
|
SwitchAt, RoomBelow, WarnAt float64
|
|
// UsageEvery is how often each subscription's usage is read.
|
|
UsageEvery time.Duration
|
|
// UsageStale is how old a licence's latest successful reading may be for its room to be known (§25).
|
|
UsageStale time.Duration
|
|
// OutOfBound is every setting the file sets outside the bound the manifest declares, kept at its default and said
|
|
// as settings.<setting>.invalid until it is fixed (B37).
|
|
OutOfBound []OutOfBound
|
|
}
|
|
|
|
// OutOfBound is one setting set outside its bound: the value set, the bound and the default kept in its place.
|
|
type OutOfBound struct {
|
|
Setting string
|
|
Value float64
|
|
Min, Max float64
|
|
Default float64
|
|
HasMax bool
|
|
}
|
|
|
|
// Defaults are the settings a fresh mesh runs with.
|
|
var Defaults = Settings{Cadence: 4 * time.Hour, Floor: time.Hour, FailuresToNotify: 3, Cooldown: 24 * time.Hour,
|
|
RefreshWarn: 72 * time.Hour, SwitchAuto: true, SwitchAt: 99, RoomBelow: 90, WarnAt: 90, UsageEvery: 5 * time.Minute,
|
|
UsageStale: 15 * time.Minute}
|
|
|
|
// SettingsFrom reads the settings file. Today's five keep a default for anything absent or not positive; the six of
|
|
// the switch keep a default for anything absent or outside the bound the manifest declares (§33).
|
|
func SettingsFrom(path string) Settings {
|
|
s := Defaults
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return s
|
|
}
|
|
var f map[string]any
|
|
if json.Unmarshal(raw, &f) != nil {
|
|
return s
|
|
}
|
|
num := func(k string) (float64, bool) { v, ok := f[k].(float64); return v, ok }
|
|
// below is a value under the manifest's minimum of 0 for today's five: kept at its default and said (B37).
|
|
below := func(k string, v float64, def float64) {
|
|
if v < 0 {
|
|
s.OutOfBound = append(s.OutOfBound, OutOfBound{Setting: k, Value: v, Default: def})
|
|
}
|
|
}
|
|
set := func(k string, unit time.Duration, into *time.Duration) {
|
|
v, ok := num(k)
|
|
if ok && v > 0 {
|
|
*into = time.Duration(v * float64(unit))
|
|
} else if ok {
|
|
below(k, v, float64(*into)/float64(unit))
|
|
}
|
|
}
|
|
set("cadence_minutes", time.Minute, &s.Cadence)
|
|
set("floor_minutes", time.Minute, &s.Floor)
|
|
set("cooldown_hours", time.Hour, &s.Cooldown)
|
|
set("refresh_warn_days", 24*time.Hour, &s.RefreshWarn)
|
|
if v, ok := num("failures_to_notify"); ok && v > 0 {
|
|
s.FailuresToNotify = int(v)
|
|
} else if ok {
|
|
below("failures_to_notify", v, float64(s.FailuresToNotify))
|
|
}
|
|
if v, ok := f["switch_auto"].(bool); ok {
|
|
s.SwitchAuto = v
|
|
}
|
|
bounded := func(k string, lo, hi float64) (float64, bool) {
|
|
v, ok := num(k)
|
|
if ok && (v < lo || v > hi) {
|
|
def, _ := Defaults.Values()[k].(float64)
|
|
s.OutOfBound = append(s.OutOfBound, OutOfBound{Setting: k, Value: v, Min: lo, Max: hi, Default: def, HasMax: true})
|
|
}
|
|
return v, ok && v >= lo && v <= hi
|
|
}
|
|
for _, p := range []struct {
|
|
key string
|
|
into *float64
|
|
}{{"switch_at_pct", &s.SwitchAt}, {"room_below_pct", &s.RoomBelow}, {"warn_at_pct", &s.WarnAt}} {
|
|
if v, ok := bounded(p.key, 1, 100); ok {
|
|
*p.into = v
|
|
}
|
|
}
|
|
if v, ok := bounded("usage_minutes", 1, 60); ok {
|
|
s.UsageEvery = time.Duration(v * float64(time.Minute))
|
|
}
|
|
if v, ok := bounded("usage_stale_minutes", 5, 180); ok {
|
|
s.UsageStale = time.Duration(v * float64(time.Minute))
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Values are the settings by their names in the manifest's `config`, in its units.
|
|
func (s Settings) Values() map[string]any {
|
|
return map[string]any{"cadence_minutes": s.Cadence.Minutes(), "floor_minutes": s.Floor.Minutes(),
|
|
"failures_to_notify": float64(s.FailuresToNotify), "cooldown_hours": s.Cooldown.Hours(),
|
|
"refresh_warn_days": s.RefreshWarn.Hours() / 24, "switch_auto": s.SwitchAuto, "switch_at_pct": s.SwitchAt,
|
|
"room_below_pct": s.RoomBelow, "warn_at_pct": s.WarnAt, "usage_minutes": s.UsageEvery.Minutes(),
|
|
"usage_stale_minutes": s.UsageStale.Minutes()}
|
|
}
|
|
|
|
// Invalid is a setting above switch_at_pct, which moves no consumer until it is fixed (§33): the setting and both
|
|
// values.
|
|
type Invalid struct {
|
|
Setting string
|
|
Value float64
|
|
SwitchAt float64
|
|
}
|
|
|
|
// Invalid is every setting of the pairs §33 names that is above switch_at_pct: room_below_pct, then warn_at_pct.
|
|
func (s Settings) Invalid() []Invalid {
|
|
var out []Invalid
|
|
if s.RoomBelow > s.SwitchAt {
|
|
out = append(out, Invalid{Setting: "room_below_pct", Value: s.RoomBelow, SwitchAt: s.SwitchAt})
|
|
}
|
|
if s.WarnAt > s.SwitchAt {
|
|
out = append(out, Invalid{Setting: "warn_at_pct", Value: s.WarnAt, SwitchAt: s.SwitchAt})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Reasons are why each invalid setting is invalid, by setting, in the words its condition's summary says (§33, B37): a
|
|
// setting outside its bound first, then a setting of §33's pairs above switch_at_pct.
|
|
func (s Settings) Reasons() map[string]string {
|
|
out := map[string]string{}
|
|
for _, o := range s.OutOfBound {
|
|
out[o.Setting] = fmt.Sprintf("%s is %g, outside its bound; its default %g is used", o.Setting, o.Value, o.Default)
|
|
}
|
|
for _, p := range s.Invalid() {
|
|
if _, ok := out[p.Setting]; !ok {
|
|
out[p.Setting] = fmt.Sprintf("%s is %g, above switch_at_pct %g; no consumer is moved", p.Setting, p.Value,
|
|
p.SwitchAt)
|
|
}
|
|
}
|
|
return out
|
|
}
|