feat(judgement,approvals,governance,ui): a class earns a raise after N approvals and drops on denial or failure; a freeze holds changing actions (#2004)
This commit is contained in:
17 files changed
+1185
-58
No files matched your search
@@ -0,0 +1,247 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// ErrNoRaise means no raise has that id.
|
||||
var ErrNoRaise = errors.New("no such raise")
|
||||
|
||||
// Raise is a proposal to loosen a class by one level.
|
||||
type Raise struct {
|
||||
ID uuid.UUID `db:"id"`
|
||||
OrgID string `db:"org_id"`
|
||||
ClassID uuid.UUID `db:"class_id"`
|
||||
FromLevel string `db:"from_level"`
|
||||
ToLevel string `db:"to_level"`
|
||||
Evidence []byte `db:"evidence"`
|
||||
State string `db:"state"`
|
||||
Deadline time.Time `db:"deadline"`
|
||||
ProposedAt time.Time `db:"proposed_at"`
|
||||
AnsweredBy sql.NullString `db:"answered_by"`
|
||||
AnsweredAt sql.NullTime `db:"answered_at"`
|
||||
}
|
||||
|
||||
const raiseColumns = `id, org_id, class_id, from_level, to_level, evidence, state, deadline, proposed_at, answered_by, answered_at`
|
||||
|
||||
// ClassOfDecision returns the class a decision belongs to.
|
||||
func (s *Store) ClassOfDecision(ctx context.Context, orgID string, decisionID uuid.UUID) (Class, error) {
|
||||
var out Class
|
||||
err := s.db.GetContext(ctx, &out, `
|
||||
SELECT c.id, c.org_id, c.connector, c.action, c.target_kind, c.environment, c.read_only, c.level,
|
||||
c.created_at, c.reset_at, c.tightened_at, c.tightened_because
|
||||
FROM judgement.decisions d
|
||||
JOIN judgement.classes c ON c.org_id = d.org_id AND c.connector = d.connector AND c.action = d.action
|
||||
AND c.target_kind = d.target_kind AND c.environment = d.environment
|
||||
WHERE d.org_id = $1 AND d.id = $2`, orgID, decisionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Class{}, ErrNoClass
|
||||
}
|
||||
if err != nil {
|
||||
return Class{}, fmt.Errorf("read class of decision: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ClassByKey returns the class named by its four names.
|
||||
func (s *Store) ClassByKey(ctx context.Context, c Class) (Class, error) {
|
||||
var out Class
|
||||
err := s.db.GetContext(ctx, &out, `
|
||||
SELECT `+classColumns+` FROM judgement.classes
|
||||
WHERE org_id = $1 AND connector = $2 AND action = $3 AND target_kind = $4 AND environment = $5`,
|
||||
c.OrgID, c.Connector, c.Action, c.TargetKind, c.Environment)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Class{}, ErrNoClass
|
||||
}
|
||||
if err != nil {
|
||||
return Class{}, fmt.Errorf("read class: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// RaiseByID returns one raise.
|
||||
func (s *Store) RaiseByID(ctx context.Context, orgID string, id uuid.UUID) (Raise, error) {
|
||||
var out Raise
|
||||
err := s.db.GetContext(ctx, &out, `SELECT `+raiseColumns+` FROM judgement.class_raises WHERE org_id = $1 AND id = $2`, orgID, id)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Raise{}, ErrNoRaise
|
||||
}
|
||||
if err != nil {
|
||||
return Raise{}, fmt.Errorf("read raise: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ClassRaises returns a class's raises, newest first.
|
||||
func (s *Store) ClassRaises(ctx context.Context, id uuid.UUID) ([]Raise, error) {
|
||||
var out []Raise
|
||||
if err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT `+raiseColumns+` FROM judgement.class_raises WHERE class_id = $1 ORDER BY proposed_at DESC, id`, id); err != nil {
|
||||
return nil, fmt.Errorf("list class raises: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ClassCount returns how many approvals a class has counted, for the class
|
||||
// view.
|
||||
func (s *Store) ClassCount(ctx context.Context, id uuid.UUID) (int, error) {
|
||||
var n int
|
||||
if err := s.db.GetContext(ctx, &n, `SELECT count(*) FROM judgement.class_approvals WHERE class_id = $1 AND counted`, id); err != nil {
|
||||
return 0, fmt.Errorf("count approvals: %w", err)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// ClassTx is one class held for update. Everything done through it commits
|
||||
// together, so two answers arriving at once see each other's count.
|
||||
type ClassTx struct {
|
||||
tx *sqlx.Tx
|
||||
now time.Time
|
||||
Class Class
|
||||
}
|
||||
|
||||
// InClass runs fn with the class locked. A raise nobody answered by its
|
||||
// deadline is expired first, and its count restarts, so a class that wasn't
|
||||
// raised is asked again only after earning it again.
|
||||
func (s *Store) InClass(ctx context.Context, orgID string, id uuid.UUID, now time.Time, fn func(*ClassTx) error) error {
|
||||
tx, err := s.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("class: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
t := &ClassTx{tx: tx, now: now}
|
||||
err = tx.GetContext(ctx, &t.Class, `SELECT `+classColumns+` FROM judgement.classes WHERE org_id = $1 AND id = $2 FOR UPDATE`, orgID, id)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrNoClass
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("lock class: %w", err)
|
||||
}
|
||||
res, err := tx.ExecContext(ctx, `
|
||||
UPDATE judgement.class_raises SET state = 'expired', answered_at = $2
|
||||
WHERE class_id = $1 AND state = 'pending' AND deadline <= $2`, id, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("expire raise: %w", err)
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n > 0 {
|
||||
if err := t.ClearCount(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := fn(t); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("class: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Count returns how many approvals count toward a raise, and the raise waiting
|
||||
// for an answer, if one is.
|
||||
func (t *ClassTx) Count(ctx context.Context) (int, *Raise, error) {
|
||||
var n int
|
||||
if err := t.tx.GetContext(ctx, &n, `SELECT count(*) FROM judgement.class_approvals WHERE class_id = $1 AND counted`, t.Class.ID); err != nil {
|
||||
return 0, nil, fmt.Errorf("count approvals: %w", err)
|
||||
}
|
||||
var r Raise
|
||||
err := t.tx.GetContext(ctx, &r, `SELECT `+raiseColumns+` FROM judgement.class_raises WHERE class_id = $1 AND state = 'pending'`, t.Class.ID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return n, nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, nil, fmt.Errorf("read pending raise: %w", err)
|
||||
}
|
||||
return n, &r, nil
|
||||
}
|
||||
|
||||
// AddApproval counts one approved decision. A decision counts once.
|
||||
func (t *ClassTx) AddApproval(ctx context.Context, decisionID uuid.UUID, by string) error {
|
||||
if _, err := t.tx.ExecContext(ctx, `
|
||||
INSERT INTO judgement.class_approvals (id, org_id, class_id, decision_id, by, at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (decision_id) DO NOTHING`,
|
||||
uuid.New(), t.Class.OrgID, t.Class.ID, decisionID, by, t.now); err != nil {
|
||||
return fmt.Errorf("record approval: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Approval is one counted approval, the evidence for a raise.
|
||||
type Approval struct {
|
||||
DecisionID uuid.UUID `db:"decision_id" json:"decision_id"`
|
||||
By string `db:"by" json:"by"`
|
||||
At time.Time `db:"at" json:"at"`
|
||||
}
|
||||
|
||||
// Approvals returns the approvals that count, oldest first.
|
||||
func (t *ClassTx) Approvals(ctx context.Context) ([]Approval, error) {
|
||||
var out []Approval
|
||||
if err := t.tx.SelectContext(ctx, &out, `
|
||||
SELECT decision_id, by, at FROM judgement.class_approvals WHERE class_id = $1 AND counted ORDER BY at, id`, t.Class.ID); err != nil {
|
||||
return nil, fmt.Errorf("list approvals: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ClearCount stops every approval counting.
|
||||
func (t *ClassTx) ClearCount(ctx context.Context) error {
|
||||
if _, err := t.tx.ExecContext(ctx, `UPDATE judgement.class_approvals SET counted = false WHERE class_id = $1 AND counted`, t.Class.ID); err != nil {
|
||||
return fmt.Errorf("clear approvals: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetLevel sets the class's level.
|
||||
func (t *ClassTx) SetLevel(ctx context.Context, level string) error {
|
||||
if _, err := t.tx.ExecContext(ctx, `UPDATE judgement.classes SET level = $2 WHERE id = $1`, t.Class.ID, level); err != nil {
|
||||
return fmt.Errorf("set level: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Tighten records that the class lost autonomy, and why.
|
||||
func (t *ClassTx) Tighten(ctx context.Context, level, because string) error {
|
||||
if _, err := t.tx.ExecContext(ctx, `
|
||||
UPDATE judgement.classes SET level = $2, tightened_at = $3, tightened_because = $4 WHERE id = $1`,
|
||||
t.Class.ID, level, t.now, because); err != nil {
|
||||
return fmt.Errorf("tighten class: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseRaise settles the class's pending raise.
|
||||
func (t *ClassTx) CloseRaise(ctx context.Context, state, by string) error {
|
||||
if _, err := t.tx.ExecContext(ctx, `
|
||||
UPDATE judgement.class_raises SET state = $2, answered_by = nullif($3, ''), answered_at = $4
|
||||
WHERE class_id = $1 AND state = 'pending'`, t.Class.ID, state, by, t.now); err != nil {
|
||||
return fmt.Errorf("close raise: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InsertRaise records a proposal.
|
||||
func (t *ClassTx) InsertRaise(ctx context.Context, r Raise) error {
|
||||
if _, err := t.tx.ExecContext(ctx, `
|
||||
INSERT INTO judgement.class_raises (id, org_id, class_id, from_level, to_level, evidence, state, deadline, proposed_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, 'pending', $7, $8)`,
|
||||
r.ID, t.Class.OrgID, t.Class.ID, r.FromLevel, r.ToLevel, r.Evidence, r.Deadline, t.now); err != nil {
|
||||
return fmt.Errorf("record raise: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CancelRaise withdraws a pending raise whose request couldn't be asked.
|
||||
func (s *Store) CancelRaise(ctx context.Context, orgID string, id uuid.UUID) error {
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
UPDATE judgement.class_raises SET state = 'superseded' WHERE org_id = $1 AND id = $2 AND state = 'pending'`, orgID, id); err != nil {
|
||||
return fmt.Errorf("cancel raise: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -25,6 +25,10 @@ type Class struct {
|
||||
Level string `db:"level"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
ResetAt sql.NullTime `db:"reset_at"`
|
||||
// TightenedAt and TightenedBecause say when and why the class last lost
|
||||
// autonomy; the count of approvals restarts there.
|
||||
TightenedAt sql.NullTime `db:"tightened_at"`
|
||||
TightenedBecause sql.NullString `db:"tightened_because"`
|
||||
}
|
||||
|
||||
// ClassSummary is a class with how many decisions it holds.
|
||||
@@ -32,6 +36,7 @@ type ClassSummary struct {
|
||||
Class
|
||||
Decisions int `db:"decisions"`
|
||||
LastAt sql.NullTime `db:"last_at"`
|
||||
Streak int `db:"streak"`
|
||||
}
|
||||
|
||||
// Reset is one return of a class to its default.
|
||||
@@ -43,7 +48,7 @@ type Reset struct {
|
||||
At time.Time `db:"at"`
|
||||
}
|
||||
|
||||
const classColumns = `id, org_id, connector, action, target_kind, environment, read_only, level, created_at, reset_at`
|
||||
const classColumns = `id, org_id, connector, action, target_kind, environment, read_only, level, created_at, reset_at, tightened_at, tightened_because`
|
||||
|
||||
// EnsureClass returns the class c names, creating it at c.Level when it is
|
||||
// new. An existing class keeps its level.
|
||||
@@ -67,7 +72,9 @@ func (s *Store) Classes(ctx context.Context, orgID string) ([]ClassSummary, erro
|
||||
var out []ClassSummary
|
||||
err := s.db.SelectContext(ctx, &out, `
|
||||
SELECT c.id, c.org_id, c.connector, c.action, c.target_kind, c.environment, c.read_only, c.level,
|
||||
c.created_at, c.reset_at, count(d.id) AS decisions, max(d.created_at) AS last_at
|
||||
c.created_at, c.reset_at, c.tightened_at, c.tightened_because,
|
||||
count(d.id) AS decisions, max(d.created_at) AS last_at,
|
||||
(SELECT count(*) FROM judgement.class_approvals a WHERE a.class_id = c.id AND a.counted) AS streak
|
||||
FROM judgement.classes c
|
||||
LEFT JOIN judgement.decisions d ON d.org_id = c.org_id AND d.connector = c.connector
|
||||
AND d.action = c.action AND d.target_kind = c.target_kind AND d.environment = c.environment
|
||||
@@ -140,6 +147,13 @@ func (s *Store) ResetClass(ctx context.Context, orgID string, id uuid.UUID, leve
|
||||
UPDATE judgement.classes SET level = $2, reset_at = $3 WHERE id = $1 RETURNING `+classColumns, id, level, at); err != nil {
|
||||
return Class{}, fmt.Errorf("reset class: %w", err)
|
||||
}
|
||||
// A reset starts the count again and withdraws a raise that was waiting.
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE judgement.class_approvals SET counted = false WHERE class_id = $1 AND counted`, id); err != nil {
|
||||
return Class{}, fmt.Errorf("reset class: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `UPDATE judgement.class_raises SET state = 'superseded' WHERE class_id = $1 AND state = 'pending'`, id); err != nil {
|
||||
return Class{}, fmt.Errorf("reset class: %w", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO judgement.class_resets (id, org_id, class_id, from_level, to_level, by, at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)`, uuid.New(), orgID, id, before.Level, level, by, at); err != nil {
|
||||
|
||||
Reference in new issue
Block a user