157 lines
6.6 KiB
Go
157 lines
6.6 KiB
Go
package assign
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"mesh-delivery-assigner/internal/assignment"
|
|
"strings"
|
|
"time"
|
|
|
|
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
mcseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
|
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
|
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
// The assigner's said conditions become findings (novox/hq ADR 0564 §2, issue 1952 WM6, note 16): every condition it says
|
|
// under its own name (ADR 0437 §9) — a hold and its lift, a node failing alone, a module on neither list, a backup, a pair
|
|
// placed by one source, the seat waiting for its states — is also a finding in its own bucket `findings`, written at its
|
|
// raise and deleted at its clear, which the condition keeper raises and clears. Until the operator's take-over the router
|
|
// hears it straight from the assigner, as before; from the take-over the keeper's condition is the one said, so the
|
|
// assigner no longer says it straight (never both).
|
|
|
|
// FindingsBucket is the assigner's findings.
|
|
// FindingsState is the assigner's state of its findings, and FindingsBucket its bucket.
|
|
const FindingsState = "findings"
|
|
|
|
var FindingsBucket = assignment.Module + "_" + FindingsState
|
|
|
|
// KeyID is an id as the parts of a key: each dot-separated part made safe for a bus key, as the key of a condition is
|
|
// (mcc.Key), so a finding's key keeps the shape its condition had.
|
|
func KeyID(id string) string {
|
|
key := mcc.Key("x", id, "x")
|
|
return strings.TrimSuffix(strings.TrimPrefix(key, "x."), ".x")
|
|
}
|
|
|
|
// Observed is what the assigner observes wrong, before it is the seat mesh-conditions' generated finding.
|
|
type Observed struct {
|
|
Scope, ID, Kind, Machine string
|
|
Also []string
|
|
Severity string
|
|
Summary, Said string
|
|
Headline, Explanation string
|
|
Needs, Source string
|
|
Actions []mcseat.Action
|
|
}
|
|
|
|
// Finding is o as the seat's generated finding at at (design 47 G §686, A-22): its id made safe as a key's part, the resolved
|
|
// words not a field of it, its observer the assigner; the writer completes its `since`.
|
|
func (o Observed) Finding(at time.Time) mcseat.Finding {
|
|
at = at.UTC()
|
|
id := KeyID(o.ID)
|
|
said := o.Said
|
|
if said == "" {
|
|
said = o.Summary
|
|
}
|
|
sev := o.Severity
|
|
if sev == "" {
|
|
sev = mcseat.FindingSeverityWarning
|
|
}
|
|
return mcseat.Finding{Scope: o.Scope, ID: id, Kind: o.Kind, Severity: sev, Summary: o.Summary, Headline: o.Headline,
|
|
Explanation: o.Explanation, Needs: o.Needs, Actions: o.Actions, Source: o.Source, Observer: assignment.Module, At: at,
|
|
Evidence: []mcseat.Evidence{{At: at, Said: said}},
|
|
Subject: mcseat.Subject{Scope: o.Scope, ID: id, Machine: o.Machine, Also: o.Also}}
|
|
}
|
|
|
|
// CallUnanswered is the observation of a kept call to callee (a seat verb's or a tool's key) about id (what the assigner waits
|
|
// on: a node, a key), unanswered since since, its last fault said. Its key is `call.<id>.call-unanswered`.
|
|
func CallUnanswered(id, callee, machine, fault, source string, since time.Time) Observed {
|
|
return Observed{Scope: mcc.ScopeCall, ID: id, Kind: mcc.KindCallUnanswered, Machine: machine, Severity: mcseat.FindingSeverityWarning,
|
|
Summary: fmt.Sprintf("the call %s about %s is unanswered since %s", callee, id, since.UTC().Format(time.RFC3339)),
|
|
Said: fault, Headline: "A call whose answer is kept is not answered",
|
|
Explanation: fmt.Sprintf("The call %s is made again on every transport fault and on the callee's event; it has "+
|
|
"not been answered since %s. Its last fault: %s.", callee, since.UTC().Format(time.RFC3339), fault),
|
|
Source: source}
|
|
}
|
|
|
|
// FindingOf is a said condition as a finding: from the condition's own key `mesh-delivery-assigner.<kind>.<id…>` (the
|
|
// subject's id when the key names none), its words unchanged but the resolved ones. raised is false for a clear, whose
|
|
// finding names only its scope, id and kind.
|
|
func FindingOf(c oc.Condition, at time.Time) (f mcseat.Finding, raised bool, err error) {
|
|
rest, ok := strings.CutPrefix(c.Key, assignment.Module+".")
|
|
if !ok || rest == "" {
|
|
return f, false, fmt.Errorf("the condition %q is not the assigner's", c.Key)
|
|
}
|
|
kind, id, _ := strings.Cut(rest, ".")
|
|
if id == "" {
|
|
id = c.Subject.ID
|
|
}
|
|
scope := c.Subject.Scope
|
|
if scope == "" {
|
|
scope = mcc.ScopeModule
|
|
}
|
|
if c.State == occ.StateCleared {
|
|
return mcseat.Finding{Scope: scope, ID: KeyID(id), Kind: kind}, false, nil
|
|
}
|
|
if c.State != occ.StateRaised {
|
|
return f, false, fmt.Errorf("the condition %s is neither raised nor cleared", c.Key)
|
|
}
|
|
summary := c.Summary
|
|
if summary == "" {
|
|
summary = c.Headline
|
|
}
|
|
var actions []mcseat.Action
|
|
for _, a := range c.Actions {
|
|
actions = append(actions, mcseat.Action{Label: a.Label, Verb: a.Verb, Machine: a.Machine, Arguments: a.Arguments, Level: string(a.Level)})
|
|
}
|
|
return Observed{Scope: scope, ID: id, Kind: kind, Machine: c.Subject.Machine, Severity: string(c.Severity), Summary: summary,
|
|
Headline: c.Headline, Explanation: c.Explanation, Needs: c.Needs, Actions: actions, Source: "assigner." + kind}.Finding(at), true, nil
|
|
}
|
|
|
|
// Findings is where the assigner's findings go: its finding writer's write and clear (design 47 G §689, A-29), by the
|
|
// finding's key `<scope>.<id>.<kind>`.
|
|
type Findings struct {
|
|
Put func(f mcseat.Finding) error
|
|
Delete func(key string) error
|
|
Keys func() ([]string, error)
|
|
Now func() time.Time
|
|
}
|
|
|
|
// Say is say with each condition written as a finding (or its finding deleted), which the condition keeper carries, the
|
|
// one source since the controller's end (novox/hq ADR 0590 §3); said straight to the router only where there is no
|
|
// findings bucket at all. A finding that cannot be written is the error.
|
|
func (f Findings) Say(say func(oc.Condition) error) func(oc.Condition) error {
|
|
return func(c oc.Condition) error {
|
|
var errs []error
|
|
if f.Put != nil {
|
|
now := time.Now
|
|
if f.Now != nil {
|
|
now = f.Now
|
|
}
|
|
finding, raised, err := FindingOf(c, now())
|
|
key := views.FindingKey(finding.Scope, finding.ID, finding.Kind)
|
|
switch {
|
|
case err != nil:
|
|
errs = append(errs, err)
|
|
case raised:
|
|
if err := f.Put(finding); err != nil {
|
|
errs = append(errs, fmt.Errorf("the finding %s cannot be written: %w", key, err))
|
|
}
|
|
default:
|
|
if err := f.Delete(key); err != nil {
|
|
errs = append(errs, fmt.Errorf("the finding %s cannot be deleted: %w", key, err))
|
|
}
|
|
}
|
|
}
|
|
if f.Put == nil {
|
|
if say != nil {
|
|
if err := say(c); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|
|
}
|