Files

155 lines
4.0 KiB
Go

package governance
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"time"
)
// Timing is when the organisation lets actions that change something run
// without asking. Outside business hours, or inside a blackout, they wait for
// approval. Reading is never held.
type Timing struct {
// Zone is the IANA zone business hours are read in; empty means UTC.
Zone string `json:"zone"`
// Start and End bound business hours as "15:04". Both empty means every
// hour is a business hour. End before Start spans midnight.
Start string `json:"start"`
End string `json:"end"`
// Days are the business days, as "mon" to "sun". Empty means every day.
Days []string `json:"days"`
Blackouts []Blackout `json:"blackouts"`
}
// Blackout is a window in which nothing that changes something runs unasked.
type Blackout struct {
From time.Time `json:"from"`
To time.Time `json:"to"`
Reason string `json:"reason"`
}
const clock = "15:04"
var weekdays = []string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"}
// validate checks that the zone, hours, days and blackouts mean something.
func (t Timing) validate() error {
if _, err := t.location(); err != nil {
return err
}
if (t.Start == "") != (t.End == "") {
return errors.New("business hours need both a start and an end")
}
for _, c := range []string{t.Start, t.End} {
if c == "" {
continue
}
if _, err := time.Parse(clock, c); err != nil {
return fmt.Errorf("business hours are 24-hour times such as 09:00, not %q", c)
}
}
if t.Start != "" && t.Start == t.End {
return errors.New("business hours start and end at the same time")
}
for _, d := range t.Days {
if !slices.Contains(weekdays, d) {
return fmt.Errorf("business days are mon to sun, not %q", d)
}
}
for _, b := range t.Blackouts {
if b.From.IsZero() || !b.To.After(b.From) {
return errors.New("a blackout needs a start and a later end")
}
}
return nil
}
func (t Timing) location() (*time.Location, error) {
if t.Zone == "" {
return time.UTC, nil
}
loc, err := time.LoadLocation(t.Zone)
if err != nil {
return nil, fmt.Errorf("unknown time zone %q", t.Zone)
}
return loc, nil
}
// Hold says whether an action that changes something must wait at now, and why.
func (t Timing) Hold(now time.Time) (string, bool) {
for _, b := range t.Blackouts {
if !now.Before(b.From) && now.Before(b.To) {
if b.Reason == "" {
return "a blackout is in force", true
}
return "a blackout is in force: " + b.Reason, true
}
}
if t.Start == "" {
return "", false
}
loc, err := t.location()
if err != nil {
loc = time.UTC
}
local := now.In(loc)
if len(t.Days) > 0 && !slices.Contains(t.Days, weekdays[local.Weekday()]) {
return "outside business days", true
}
at := local.Format(clock)
inside := at >= t.Start && at < t.End
if t.Start > t.End {
inside = at >= t.Start || at < t.End
}
if !inside {
return "outside business hours", true
}
return "", false
}
// normalised lowercases the days so they compare, and gives a nil list a value
// that encodes as an array.
func (t Timing) normalised() Timing {
days := make([]string, len(t.Days))
for i, d := range t.Days {
days[i] = strings.ToLower(d)
}
t.Days = days
if t.Blackouts == nil {
t.Blackouts = []Blackout{}
}
return t
}
// Timing returns when the organisation lets changing actions run unasked.
func (m *Module) Timing(ctx context.Context) (Timing, error) {
set, err := m.Settings(ctx)
if err != nil {
return Timing{}, err
}
return set.Timing, nil
}
// Hold says whether an action that changes something must wait now, and why.
func (m *Module) Hold(ctx context.Context, now time.Time) (string, bool, error) {
t, err := m.Timing(ctx)
if err != nil {
return "", false, err
}
reason, held := t.Hold(now)
return reason, held, nil
}
// ActionBudgets returns how many actions one run, and the organisation in a
// day, may take.
func (m *Module) ActionBudgets(ctx context.Context) (perRun, perDay int, err error) {
set, err := m.Settings(ctx)
if err != nil {
return 0, 0, err
}
return set.ActionsPerRun, set.ActionsPerDay, nil
}