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
@@ -31,6 +31,13 @@ const roleAgent = "agent"
|
||||
// when the organisation hasn't said.
|
||||
const DefaultApprovalDeadline = 24 * time.Hour
|
||||
|
||||
// DefaultRaiseAfter is how many approvals in a row earn a proposal to raise an
|
||||
// action class when the organisation hasn't said.
|
||||
const DefaultRaiseAfter = 3
|
||||
|
||||
// maxRaiseAfter bounds the count: past it a class could never earn a raise.
|
||||
const maxRaiseAfter = 100
|
||||
|
||||
// The bounds on an approval deadline. Below a minute nobody could answer; past
|
||||
// a month the request outlives the incident it was raised for.
|
||||
const (
|
||||
@@ -43,6 +50,12 @@ type Settings struct {
|
||||
// ApprovalDeadline is how long an approval request waits for an answer
|
||||
// before the run gives up on it.
|
||||
ApprovalDeadline time.Duration
|
||||
// RaiseAfter is how many approvals in a row, since an action class was last
|
||||
// tightened, earn a proposal to raise it one level.
|
||||
RaiseAfter int
|
||||
// Frozen holds every action that changes something at propose and wait,
|
||||
// without changing any class's level.
|
||||
Frozen bool
|
||||
}
|
||||
|
||||
// settingsRequest is the settings as the API shows and sets them. A duration
|
||||
@@ -50,6 +63,12 @@ type Settings struct {
|
||||
// in the config takes.
|
||||
type settingsRequest struct {
|
||||
ApprovalDeadline string `json:"approval_deadline"`
|
||||
RaiseAfter int `json:"raise_after"`
|
||||
Frozen bool `json:"frozen"`
|
||||
}
|
||||
|
||||
func (s Settings) request() settingsRequest {
|
||||
return settingsRequest{ApprovalDeadline: s.ApprovalDeadline.String(), RaiseAfter: s.RaiseAfter, Frozen: s.Frozen}
|
||||
}
|
||||
|
||||
// Model is the model chosen for a role.
|
||||
@@ -110,9 +129,31 @@ func (m *Module) Settings(ctx context.Context) (Settings, error) {
|
||||
return Settings{}, err
|
||||
}
|
||||
if !ok {
|
||||
return Settings{ApprovalDeadline: DefaultApprovalDeadline}, nil
|
||||
return Settings{ApprovalDeadline: DefaultApprovalDeadline, RaiseAfter: DefaultRaiseAfter}, nil
|
||||
}
|
||||
return Settings{ApprovalDeadline: time.Duration(row.ApprovalDeadlineMS) * time.Millisecond}, nil
|
||||
return Settings{
|
||||
ApprovalDeadline: time.Duration(row.ApprovalDeadlineMS) * time.Millisecond,
|
||||
RaiseAfter: row.RaiseAfter, Frozen: row.Frozen,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RaiseAfter is how many approvals in a row earn a proposal to raise an action
|
||||
// class.
|
||||
func (m *Module) RaiseAfter(ctx context.Context) (int, error) {
|
||||
set, err := m.Settings(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return set.RaiseAfter, nil
|
||||
}
|
||||
|
||||
// Frozen reports whether the organisation has frozen its actions.
|
||||
func (m *Module) Frozen(ctx context.Context) (bool, error) {
|
||||
set, err := m.Settings(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return set.Frozen, nil
|
||||
}
|
||||
|
||||
// ApprovalDeadline is how long an approval request waits for an answer.
|
||||
@@ -139,7 +180,7 @@ func (m *Module) getSettings(w http.ResponseWriter, r *http.Request) {
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't read the settings")
|
||||
return
|
||||
}
|
||||
httpjson.Write(w, http.StatusOK, settingsRequest{ApprovalDeadline: set.ApprovalDeadline.String()})
|
||||
httpjson.Write(w, http.StatusOK, set.request())
|
||||
}
|
||||
|
||||
func (m *Module) putSettings(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -153,26 +194,31 @@ func (m *Module) putSettings(w http.ResponseWriter, r *http.Request) {
|
||||
httpjson.Error(w, http.StatusBadRequest, "approval deadline is a duration, such as 24h")
|
||||
return
|
||||
}
|
||||
set := Settings{ApprovalDeadline: deadline}
|
||||
set := Settings{ApprovalDeadline: deadline, RaiseAfter: req.RaiseAfter, Frozen: req.Frozen}
|
||||
if err := set.validate(); err != nil {
|
||||
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if err := m.store.PutSettings(r.Context(), store.Settings{
|
||||
OrgID: m.orgID, ApprovalDeadlineMS: set.ApprovalDeadline.Milliseconds(),
|
||||
RaiseAfter: set.RaiseAfter, Frozen: set.Frozen,
|
||||
}); err != nil {
|
||||
m.log.ErrorContext(r.Context(), "put settings", "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't save the settings")
|
||||
return
|
||||
}
|
||||
httpjson.Write(w, http.StatusOK, settingsRequest{ApprovalDeadline: set.ApprovalDeadline.String()})
|
||||
httpjson.Write(w, http.StatusOK, set.request())
|
||||
}
|
||||
|
||||
// validate keeps a deadline inside the range an answer can be given in.
|
||||
// validate keeps a deadline inside the range an answer can be given in, and
|
||||
// the count of approvals a raise needs inside the range a class can earn.
|
||||
func (s Settings) validate() error {
|
||||
if s.ApprovalDeadline < minApprovalDeadline || s.ApprovalDeadline > maxApprovalDeadline {
|
||||
return fmt.Errorf("approval deadline must be between %s and %s", minApprovalDeadline, maxApprovalDeadline)
|
||||
}
|
||||
if s.RaiseAfter < 1 || s.RaiseAfter > maxRaiseAfter {
|
||||
return fmt.Errorf("raise after must be between 1 and %d approvals", maxRaiseAfter)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -62,6 +62,11 @@ type Settings struct {
|
||||
OrgID string `db:"org_id"`
|
||||
// ApprovalDeadlineMS is how long an approval request waits for an answer.
|
||||
ApprovalDeadlineMS int64 `db:"approval_deadline_ms"`
|
||||
// RaiseAfter is how many approvals in a row earn a proposal to raise an
|
||||
// action class.
|
||||
RaiseAfter int `db:"raise_after"`
|
||||
// Frozen holds every action that changes something at propose and wait.
|
||||
Frozen bool `db:"frozen"`
|
||||
}
|
||||
|
||||
// Settings returns the organisation's settings; ok is false when none were
|
||||
@@ -69,7 +74,7 @@ type Settings struct {
|
||||
func (s *Store) Settings(ctx context.Context, orgID string) (Settings, bool, error) {
|
||||
var out Settings
|
||||
err := s.db.GetContext(ctx, &out, `
|
||||
SELECT org_id, approval_deadline_ms FROM governance.settings WHERE org_id = $1`, orgID)
|
||||
SELECT org_id, approval_deadline_ms, raise_after, frozen FROM governance.settings WHERE org_id = $1`, orgID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Settings{}, false, nil
|
||||
}
|
||||
@@ -82,10 +87,11 @@ func (s *Store) Settings(ctx context.Context, orgID string) (Settings, bool, err
|
||||
// PutSettings records the organisation's settings.
|
||||
func (s *Store) PutSettings(ctx context.Context, set Settings) error {
|
||||
if _, err := s.db.NamedExecContext(ctx, `
|
||||
INSERT INTO governance.settings (org_id, approval_deadline_ms)
|
||||
VALUES (:org_id, :approval_deadline_ms)
|
||||
INSERT INTO governance.settings (org_id, approval_deadline_ms, raise_after, frozen)
|
||||
VALUES (:org_id, :approval_deadline_ms, :raise_after, :frozen)
|
||||
ON CONFLICT (org_id) DO UPDATE
|
||||
SET approval_deadline_ms = EXCLUDED.approval_deadline_ms, updated_at = now()`, set); err != nil {
|
||||
SET approval_deadline_ms = EXCLUDED.approval_deadline_ms, raise_after = EXCLUDED.raise_after,
|
||||
frozen = EXCLUDED.frozen, updated_at = now()`, set); err != nil {
|
||||
return fmt.Errorf("put settings: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user