101 lines
3.3 KiB
Go
101 lines
3.3 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)
|
|
}
|
|
|
|
// needsHuman reports whether a task in status, parked with class, is waiting
|
|
// on an operator's decision.
|
|
func needsHuman(status, class string) bool {
|
|
return status == workflow.StatusWaitingHuman || (status == workflow.StatusParked && class == taskstate.ParkNeedsHuman)
|
|
}
|
|
|
|
// 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.StatusWaitingHuman || p.FromStatus == workflow.StatusParked
|
|
if !leaving && p.ToStatus != workflow.StatusWaitingHuman && 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 needsHuman(p.ToStatus, info.ParkClass) {
|
|
body := name + " is waiting for approval or review."
|
|
if p.ToStatus == workflow.StatusParked {
|
|
body = name + " is parked and needs a decision."
|
|
}
|
|
_, err := raiseAlert(ctx, q, alert.Input{
|
|
Key: key(alert.KindTaskNeedsHuman), Severity: alert.SeverityAction, Condition: true,
|
|
Title: title, Body: body, Link: link,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if leaving {
|
|
if err := resolveAlert(ctx, q, key(alert.KindTaskNeedsHuman), 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
|
|
}
|
|
|
|
// 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]) + "…"
|
|
}
|