feat(memory,runs,governance): remember each target's incidents, cite them in a run, and propose a root-cause fix after K with one cause (archie #1993)
This commit is contained in:
14 files changed
+774
-26
No files matched your search
@@ -0,0 +1,109 @@
|
||||
// Package store holds the memory module's SQL: incidents and the proposals
|
||||
// raised from them.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// Incident is one investigation of one target as stored.
|
||||
type Incident struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
RunID uuid.UUID `db:"run_id"`
|
||||
TargetKind string `db:"target_kind"`
|
||||
TargetID string `db:"target_id"`
|
||||
Cause string `db:"cause"`
|
||||
Summary string `db:"summary"`
|
||||
Event []byte `db:"event"`
|
||||
Actions []byte `db:"actions"`
|
||||
At time.Time `db:"at"`
|
||||
}
|
||||
|
||||
// Proposal is a root-cause fix asked for on a target.
|
||||
type Proposal struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
TargetKind string `db:"target_kind"`
|
||||
TargetID string `db:"target_id"`
|
||||
Cause string `db:"cause"`
|
||||
Incidents int `db:"incidents"`
|
||||
Body string `db:"body"`
|
||||
}
|
||||
|
||||
// Store is the memory module's Postgres store.
|
||||
type Store struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// New returns a store on db.
|
||||
func New(db *sqlx.DB) *Store { return &Store{db: db} }
|
||||
|
||||
// Upsert records the incident of a run, replacing what an earlier attempt of the
|
||||
// same run recorded.
|
||||
func (s *Store) Upsert(ctx context.Context, orgID string, in Incident) error {
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO memory.incidents (id, org_id, run_id, target_kind, target_id, cause, summary, event)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (org_id, run_id) DO UPDATE
|
||||
SET target_kind = EXCLUDED.target_kind, target_id = EXCLUDED.target_id,
|
||||
cause = EXCLUDED.cause, summary = EXCLUDED.summary`,
|
||||
in.ID, orgID, in.RunID, in.TargetKind, in.TargetID, in.Cause, in.Summary, in.Event); err != nil {
|
||||
return fmt.Errorf("record incident: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetActions records what was done in a run's incident.
|
||||
func (s *Store) SetActions(ctx context.Context, orgID string, runID uuid.UUID, actions []byte) error {
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
UPDATE memory.incidents SET actions = $3 WHERE org_id = $1 AND run_id = $2`,
|
||||
orgID, runID, actions); err != nil {
|
||||
return fmt.Errorf("record incident actions: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Incident returns a run's incident; ok is false when it has none.
|
||||
func (s *Store) Incident(ctx context.Context, orgID string, runID uuid.UUID) (Incident, bool, error) {
|
||||
var out []Incident
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT id, run_id, target_kind, target_id, cause, summary, event, actions, at
|
||||
FROM memory.incidents WHERE org_id = $1 AND run_id = $2`, orgID, runID); err != nil {
|
||||
return Incident{}, false, fmt.Errorf("read incident: %w", err)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return Incident{}, false, nil
|
||||
}
|
||||
return out[0], true, nil
|
||||
}
|
||||
|
||||
// ByTarget returns a target's incidents, newest first, up to limit.
|
||||
func (s *Store) ByTarget(ctx context.Context, orgID, kind, id string, limit int) ([]Incident, error) {
|
||||
var out []Incident
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT id, run_id, target_kind, target_id, cause, summary, event, actions, at
|
||||
FROM memory.incidents WHERE org_id = $1 AND target_kind = $2 AND target_id = $3
|
||||
ORDER BY at DESC LIMIT $4`, orgID, kind, id, limit); err != nil {
|
||||
return nil, fmt.Errorf("list incidents: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Propose records p unless the target and cause already have one, and returns
|
||||
// the one that stands.
|
||||
func (s *Store) Propose(ctx context.Context, orgID string, p Proposal) (Proposal, error) {
|
||||
var out Proposal
|
||||
if err := s.db.GetContext(ctx, &out, `
|
||||
INSERT INTO memory.proposals (id, org_id, target_kind, target_id, cause, incidents, body)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (org_id, target_kind, target_id, cause) DO UPDATE SET incidents = EXCLUDED.incidents
|
||||
RETURNING id, target_kind, target_id, cause, incidents, body`,
|
||||
p.ID, orgID, p.TargetKind, p.TargetID, p.Cause, p.Incidents, p.Body); err != nil {
|
||||
return Proposal{}, fmt.Errorf("record proposal: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in new issue
Block a user