The part's state `findings`, its finding stream `mesh-grants-findings` and the consume of `mesh-conditions.findings-asked` are declared in module.json; the process makes mesh-sdk's held writer (Asked from the contract) and starts it once its state is bound. Its other conditions stay said to the operator channel. gofmt of bus.go and grants_test.go. Builds: design 47 G §388 (G-8), §529 (G-12), A-51 (G-20) Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
73 lines
3.2 KiB
Go
73 lines
3.2 KiB
Go
package grants
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
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/keys"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
)
|
|
|
|
// **The grants part's findings** (design 47 G §388, §529, A-51). The four kinds the part says of what it waits for (`view-waits`,
|
|
// `first-view-unanswered`, `first-view-empty`, `binding-unlisted`) are written through mesh-sdk's held finding writer of a process,
|
|
// in the part's own state `findings` and on its finding stream `mesh-grants-findings`; every other condition of the part stays one
|
|
// said to the operator channel (Part.Say).
|
|
|
|
// StateFindings is the part's state of findings (design 47 G §529, a foundation part's old `state` section).
|
|
const StateFindings = "findings"
|
|
|
|
// FindingKinds are the finding kinds the part writes beside the four of a views reader (views.ViewKinds), which its writer always
|
|
// owns: first-view-empty and binding-unlisted.
|
|
var FindingKinds = []string{kindOf(FirstViewEmpty), kindOf(BindingUnlisted)}
|
|
|
|
// FindingWriter is the part's held finding writer (views.HeldWriter): what the part writes and clears its findings through.
|
|
type FindingWriter interface {
|
|
Write(ctx context.Context, f mcseat.Finding) error
|
|
Clear(ctx context.Context, scope, id, kind, why string) error
|
|
}
|
|
|
|
// kindOf is the finding kind of one of the part's condition keys: the key `mesh-grants.<kind>` without its module.
|
|
func kindOf(key string) string { return strings.TrimPrefix(key, Module+".") }
|
|
|
|
// SayFinding makes the part's say of the conditions that are findings: a condition whose key is `mesh-grants.<kind>.<id>` is the
|
|
// finding of that kind and id (the id escaped as a key's name is), written while the condition is raised and cleared when it is
|
|
// cleared. node is the machine the part runs on. A condition of any other key is refused, so none goes through by mistake.
|
|
func SayFinding(w FindingWriter, node string, now func() time.Time) func(oc.Condition) error {
|
|
if now == nil {
|
|
now = time.Now
|
|
}
|
|
return func(c oc.Condition) error {
|
|
rest, ok := strings.CutPrefix(c.Key, Module+".")
|
|
kind, id, hasID := strings.Cut(rest, ".")
|
|
if !ok || !hasID || !isFindingKind(kind) {
|
|
return fmt.Errorf("%q is not one of the part's findings", c.Key)
|
|
}
|
|
id = keys.EscapeName(id)
|
|
ctx := context.Background()
|
|
if c.State != occ.StateRaised {
|
|
return w.Clear(ctx, views.FindingScope, id, kind, c.Resolved)
|
|
}
|
|
sev := c.Severity
|
|
if sev == "" {
|
|
sev = mcseat.FindingSeverityWarning
|
|
}
|
|
return w.Write(ctx, mcseat.Finding{Scope: views.FindingScope, ID: id, Kind: kind, Severity: sev, Source: "mesh-grants",
|
|
Summary: c.Summary, Headline: c.Headline, Needs: c.Needs, Explanation: c.Explanation, Observer: Module, At: now().UTC(),
|
|
Subject: mcseat.Subject{Scope: views.FindingScope, ID: Module, Machine: node}})
|
|
}
|
|
}
|
|
|
|
func isFindingKind(kind string) bool {
|
|
for _, k := range append([]string{views.ViewWaitsKind, views.UnansweredKind}, FindingKinds...) {
|
|
if k == kind {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|