Files

110 lines
3.7 KiB
Go

package postgres
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// systemResolver is recorded as the resolver of an alert the store clears
// itself because the task moved on.
const systemResolver = "system"
// recordTransition writes a transition's audit row and the alerts that
// transition raises or clears, in the same transaction. Every task status
// move goes through it, so the alerts follow the state without a poller.
func recordTransition(ctx context.Context, q *postgresdb.Queries, p postgresdb.InsertTransitionParams) error {
if err := q.InsertTransition(ctx, p); err != nil {
return err
}
return syncTaskAlerts(ctx, q, p)
}
// needsDecision reports whether a task in status, parked with class, is waiting
// on an operator's decision. A park that names a blocker waits on the operator
// whatever its class, because only a fix to that setting lets a retry succeed.
func needsDecision(status, class, blocker string) bool {
return status == workflow.StatusWaitingApproval ||
(status == workflow.StatusParked && (class == taskstate.ParkNeedsDecision || blocker != ""))
}
// failed reports whether a task in status, parked with class, will not
// succeed without an operator starting it over.
func failed(status, class string) bool {
return status == workflow.StatusDead || (status == workflow.StatusParked && class == taskstate.ParkTerminal)
}
func syncTaskAlerts(ctx context.Context, q *postgresdb.Queries, p postgresdb.InsertTransitionParams) error {
leaving := p.FromStatus == workflow.StatusWaitingApproval || p.FromStatus == workflow.StatusParked
if !leaving && p.ToStatus != workflow.StatusWaitingApproval && p.ToStatus != workflow.StatusParked && p.ToStatus != workflow.StatusDead {
return nil
}
info, err := q.TaskAlertInfo(ctx, p.TaskID)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
if err != nil {
return err
}
subject := fmt.Sprintf("task:%d", p.TaskID)
link := fmt.Sprintf("/tasks/%d", p.TaskID)
name := fmt.Sprintf("Task #%d", p.TaskID)
title := name
if info.Title != "" {
title = truncate(info.Title, 80)
}
key := func(kind string) alert.Key {
return alert.Key{Org: org.OrgID(info.OrgID), Kind: kind, Subject: subject}
}
if needsDecision(p.ToStatus, info.ParkClass, info.ParkBlocker) {
_, err := raiseAlert(ctx, q, alert.Input{
Key: key(alert.KindTaskNeedsDecision), Severity: alert.SeverityAction, Condition: true,
Title: title, Body: decisionBody(name, p.ToStatus, info.ParkBlocker), Link: link,
})
if err != nil {
return err
}
} else if leaving {
if err := resolveAlert(ctx, q, key(alert.KindTaskNeedsDecision), systemResolver); err != nil {
return err
}
}
if failed(p.ToStatus, info.ParkClass) {
_, err := raiseAlert(ctx, q, alert.Input{
Key: key(alert.KindTaskFailed), Severity: alert.SeverityWarning,
Title: title, Body: name + " failed.", Link: link,
})
return err
}
return nil
}
// decisionBody says why a task waits on the operator.
func decisionBody(name, status, blocker string) string {
switch {
case status != workflow.StatusParked:
return name + " is waiting for approval or review."
case blocker != "":
return name + " is blocked by a missing setting."
}
return name + " is parked and needs a decision."
}
// truncate shortens s to at most n runes, marking the cut with an ellipsis.
func truncate(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return s
}
return string(r[:n-1]) + "…"
}