242 lines
8.9 KiB
Go
242 lines
8.9 KiB
Go
package main
|
|
|
|
// The keeper's working model of a condition and its events, over the seat mesh-conditions' contract types (novox/hq
|
|
// ADR 0564 clause 3, ADR 0634 §1, §4). The seat's types keep an optional time as a pointer; the keeper's rules read
|
|
// better over plain times, so the keeper works on its own Condition and Event and converts at the edge: what it writes
|
|
// to `conditions` and `history`, and what it says on the seat, is the seat's form, and what it reads is converted back.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
|
meshhealers "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhealers"
|
|
|
|
cc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
)
|
|
|
|
// Silence is a person saying they know: no messages until it ends. The condition stays open.
|
|
type Silence struct {
|
|
Until time.Time
|
|
By string
|
|
Why string
|
|
Since time.Time
|
|
}
|
|
|
|
// Gap is a stretch in which a fault was cleared, between its clearing and its reopening.
|
|
type Gap struct{ Cleared, Reopened time.Time }
|
|
|
|
// Tried is one healer's try at a condition, as the condition carries it in `tried`.
|
|
type Tried struct {
|
|
At time.Time
|
|
What string
|
|
Outcome string
|
|
By string
|
|
}
|
|
|
|
// Condition is one open condition as the keeper works on it.
|
|
type Condition struct {
|
|
Key string
|
|
Kind string
|
|
Subject seat.Subject
|
|
Severity string
|
|
Summary string
|
|
Headline string
|
|
Explanation string
|
|
Resolved string
|
|
Needs string
|
|
Actions []seat.Action
|
|
Evidence []seat.Evidence
|
|
Source string
|
|
Raised time.Time
|
|
LastObserved time.Time
|
|
// First and Gaps are the fault's earlier life: a reopening within ReopenWithin is the same fault.
|
|
First time.Time
|
|
Gaps []Gap
|
|
Observations int
|
|
Count int
|
|
Tried []Tried
|
|
Resolver string
|
|
// Silenced is nil when no silence is in force.
|
|
Silenced *Silence
|
|
Epoch int64
|
|
LooksUnread string
|
|
}
|
|
|
|
// Escalated says a healer tried and its budget is spent: the operator resolves it now.
|
|
func (c Condition) Escalated() bool { return c.Resolver == cc.ResolverOperator && len(c.Tried) > 0 }
|
|
|
|
// Began is when this fault began as the mesh knows it: its first raising, a reopening within ReopenWithin being the
|
|
// same fault again.
|
|
func (c Condition) Began() time.Time {
|
|
if !c.First.IsZero() && c.First.Before(c.Raised) {
|
|
return c.First
|
|
}
|
|
return c.Raised
|
|
}
|
|
|
|
// Event is one transition: the condition itself with what happened to it beside.
|
|
type Event struct {
|
|
Condition
|
|
Event string
|
|
At time.Time
|
|
Change string
|
|
Was string
|
|
Why string
|
|
Cleared *time.Time
|
|
Show string
|
|
}
|
|
|
|
func timeP(t time.Time) *time.Time {
|
|
if t.IsZero() {
|
|
return nil
|
|
}
|
|
return &t
|
|
}
|
|
|
|
func timeOf(t *time.Time) time.Time {
|
|
if t == nil {
|
|
return time.Time{}
|
|
}
|
|
return *t
|
|
}
|
|
|
|
// Stored is the condition as the seat's state `conditions` keeps it.
|
|
func (c Condition) Stored() seat.ConditionsByKeyValue {
|
|
v := seat.ConditionsByKeyValue{Key: c.Key, Kind: c.Kind, Subject: c.Subject, Severity: c.Severity, Summary: c.Summary,
|
|
Headline: c.Headline, Explanation: c.Explanation, Resolved: c.Resolved, Needs: c.Needs, Actions: c.Actions,
|
|
Evidence: c.Evidence, Source: c.Source, Raised: c.Raised, LastObserved: c.LastObserved, First: timeP(c.First),
|
|
Observations: int64(c.Observations), Count: int64(c.Count), Resolver: c.Resolver, Epoch: c.Epoch,
|
|
LooksUnread: c.LooksUnread}
|
|
for _, g := range c.Gaps {
|
|
v.Gaps = append(v.Gaps, seat.ConditionsByKeyValueGapsItem{Cleared: timeP(g.Cleared), Reopened: timeP(g.Reopened)})
|
|
}
|
|
for _, t := range c.Tried {
|
|
v.Tried = append(v.Tried, seat.ConditionsByKeyValueTriedItem{At: timeP(t.At), What: t.What, Outcome: t.Outcome, By: t.By})
|
|
}
|
|
if c.Silenced != nil {
|
|
v.Silenced = &seat.ConditionsByKeyValueSilenced{Until: timeP(c.Silenced.Until), By: c.Silenced.By,
|
|
Why: c.Silenced.Why, Since: timeP(c.Silenced.Since)}
|
|
}
|
|
return v
|
|
}
|
|
|
|
// conditionOf is the working condition of a stored one.
|
|
func conditionOf(v seat.ConditionsByKeyValue) Condition {
|
|
c := Condition{Key: v.Key, Kind: v.Kind, Subject: v.Subject, Severity: v.Severity, Summary: v.Summary,
|
|
Headline: v.Headline, Explanation: v.Explanation, Resolved: v.Resolved, Needs: v.Needs, Actions: v.Actions,
|
|
Evidence: v.Evidence, Source: v.Source, Raised: v.Raised, LastObserved: v.LastObserved, First: timeOf(v.First),
|
|
Observations: int(v.Observations), Count: int(v.Count), Resolver: v.Resolver, Epoch: v.Epoch,
|
|
LooksUnread: v.LooksUnread}
|
|
for _, g := range v.Gaps {
|
|
c.Gaps = append(c.Gaps, Gap{Cleared: timeOf(g.Cleared), Reopened: timeOf(g.Reopened)})
|
|
}
|
|
for _, t := range v.Tried {
|
|
c.Tried = append(c.Tried, Tried{At: timeOf(t.At), What: t.What, Outcome: t.Outcome, By: t.By})
|
|
}
|
|
if v.Silenced != nil {
|
|
c.Silenced = &Silence{Until: timeOf(v.Silenced.Until), By: v.Silenced.By, Why: v.Silenced.Why,
|
|
Since: timeOf(v.Silenced.Since)}
|
|
}
|
|
return c
|
|
}
|
|
|
|
// decodeCondition reads a condition of the state `conditions`; the seat's layout is read as it is written, one field
|
|
// the keeper does not know being the seat's change, not a fault.
|
|
func decodeCondition(raw []byte) (Condition, error) {
|
|
var v seat.ConditionsByKeyValue
|
|
if err := json.Unmarshal(raw, &v); err != nil {
|
|
return Condition{}, err
|
|
}
|
|
return conditionOf(v), nil
|
|
}
|
|
|
|
// Said is the event as the seat says it and `history` keeps it.
|
|
func (e Event) Said() seat.ConditionEvent {
|
|
s := e.Condition.Stored()
|
|
v := seat.ConditionEvent{Key: s.Key, Kind: s.Kind, Subject: &s.Subject, Severity: s.Severity, Summary: s.Summary,
|
|
Headline: s.Headline, Explanation: s.Explanation, Resolved: s.Resolved, Needs: s.Needs, Actions: s.Actions,
|
|
Evidence: s.Evidence, Source: s.Source, Raised: timeP(s.Raised), LastObserved: timeP(s.LastObserved), First: s.First,
|
|
Observations: s.Observations, Count: s.Count, Resolver: s.Resolver, Epoch: s.Epoch, LooksUnread: s.LooksUnread,
|
|
Event: e.Event, At: e.At, Change: e.Change, Was: e.Was, Why: e.Why, Cleared: e.Cleared, Show: e.Show}
|
|
for _, g := range s.Gaps {
|
|
v.Gaps = append(v.Gaps, seat.ConditionEventGapsItem{Cleared: g.Cleared, Reopened: g.Reopened})
|
|
}
|
|
for _, t := range s.Tried {
|
|
v.Tried = append(v.Tried, seat.ConditionEventTriedItem{At: t.At, What: t.What, Outcome: t.Outcome, By: t.By})
|
|
}
|
|
if s.Silenced != nil {
|
|
v.Silenced = &seat.ConditionEventSilenced{Until: s.Silenced.Until, By: s.Silenced.By, Why: s.Silenced.Why,
|
|
Since: s.Silenced.Since}
|
|
}
|
|
return v
|
|
}
|
|
|
|
// eventOf is the working event of one the seat says.
|
|
func eventOf(v seat.ConditionEvent) Event {
|
|
s := seat.ConditionsByKeyValue{Key: v.Key, Kind: v.Kind, Severity: v.Severity, Summary: v.Summary,
|
|
Headline: v.Headline, Explanation: v.Explanation, Resolved: v.Resolved, Needs: v.Needs, Actions: v.Actions,
|
|
Evidence: v.Evidence, Source: v.Source, Raised: timeOf(v.Raised), LastObserved: timeOf(v.LastObserved),
|
|
First: v.First, Observations: v.Observations, Count: v.Count, Resolver: v.Resolver, Epoch: v.Epoch,
|
|
LooksUnread: v.LooksUnread}
|
|
if v.Subject != nil {
|
|
s.Subject = *v.Subject
|
|
}
|
|
for _, g := range v.Gaps {
|
|
s.Gaps = append(s.Gaps, seat.ConditionsByKeyValueGapsItem{Cleared: g.Cleared, Reopened: g.Reopened})
|
|
}
|
|
for _, t := range v.Tried {
|
|
s.Tried = append(s.Tried, seat.ConditionsByKeyValueTriedItem{At: t.At, What: t.What, Outcome: t.Outcome, By: t.By})
|
|
}
|
|
if v.Silenced != nil {
|
|
s.Silenced = &seat.ConditionsByKeyValueSilenced{Until: v.Silenced.Until, By: v.Silenced.By, Why: v.Silenced.Why,
|
|
Since: v.Silenced.Since}
|
|
}
|
|
return Event{Condition: conditionOf(s), Event: v.Event, At: v.At, Change: v.Change, Was: v.Was, Why: v.Why,
|
|
Cleared: v.Cleared, Show: v.Show}
|
|
}
|
|
|
|
// order sorts conditions as `conditions` says them.
|
|
func order(list []Condition) {
|
|
sort.SliceStable(list, func(i, j int) bool {
|
|
if list[i].Severity != list[j].Severity {
|
|
return list[i].Severity == cc.Urgent
|
|
}
|
|
if !list[i].Raised.Equal(list[j].Raised) {
|
|
return list[i].Raised.Before(list[j].Raised)
|
|
}
|
|
return list[i].Key < list[j].Key
|
|
})
|
|
}
|
|
|
|
// decodeFinding reads an observer's finding of the seat's state `findings`, refusing one the keeper could not make a
|
|
// condition of. A finding written before `ref` was named has it as `token`.
|
|
func decodeFinding(data []byte) (seat.FindingsByKeyValue, error) {
|
|
var f seat.FindingsByKeyValue
|
|
if err := json.Unmarshal(data, &f); err != nil {
|
|
return f, fmt.Errorf("a finding is not readable: %w", err)
|
|
}
|
|
if f.Ref == "" {
|
|
var old struct {
|
|
Token string `json:"token"`
|
|
}
|
|
_ = json.Unmarshal(data, &old)
|
|
f.Ref = old.Token
|
|
}
|
|
if f.V != cc.FindingVersion {
|
|
return f, fmt.Errorf("a finding of version %d, and this reads %d", f.V, cc.FindingVersion)
|
|
}
|
|
return f, cc.CheckFinding(f)
|
|
}
|
|
|
|
// triedOf is the attempt as the condition carries it in `tried`.
|
|
func triedOf(a meshhealers.AttemptsByAttemptValue) Tried {
|
|
outcome := a.Outcome
|
|
if a.Said != "" {
|
|
outcome += ": " + a.Said
|
|
}
|
|
return Tried{At: a.Begun, What: a.Act, Outcome: outcome, By: "healer " + a.Healer}
|
|
}
|