notificationrules

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package notificationrules holds the Notification rules of C-31: every User's normal and important rule, each an ordered list of actions — a Mention in the Thread, a call through a Notification gateway, a wait — within notification_rule.limits, and the four states of a User's rules. A User without rules of their own follows the Organization's current rules (organization.notification_rules), the state organization; "Call" and "Don't call" keep the Organization's rules as they were when the User chose them, with or without a call (journal D569); custom rules are sequences set through the API. Every change is recorded in the Audit log as notification_rules.updated with a before/after diff. A call action names its gateway by its public_id; one whose gateway was deleted, or at which the User has no confirmed contact any more, stays in the rule and reads unavailable.

Index

Constants

View Source
const (
	KindMention = "mention"
	KindCall    = "call"
	KindWait    = "wait"
)

The kinds of an action.

View Source
const (
	MaxActions     = 10
	MinWaitSeconds = 60
	MaxWaitSeconds = 3600
)

The limits of a rule, notification_rule.limits (P-85): at most MaxActions actions, each wait a whole number of minutes from MinWaitSeconds to MaxWaitSeconds.

View Source
const (
	StateOrganization = "organization"
	StateCall         = "call"
	StateNoCall       = "no_call"
	StateCustom       = "custom"
)

The states of a User's rules: as in the Organization, "Call", "Don't call" and custom (C-31.FR-2).

View Source
const (
	ReasonGatewayDeleted     = "gateway_deleted"
	ReasonNoConfirmedContact = "no_confirmed_contact"
)

Why a call action is unavailable: its gateway was deleted or is no Notification gateway any more, or the User has no confirmed contact at it.

View Source
const (
	CodeRequired        = "required"
	CodeInvalidFormat   = "invalid_format"
	CodeOutOfRange      = "out_of_range"
	CodeUnknownID       = "unknown_id"
	CodeGatewayRequired = "gateway_required"
	CodeContactRequired = "contact_required"
)

The codes of field errors, from the catalogue of the API (x-problem-codes).

View Source
const (
	ActionUpdated = "notification_rules.updated"
	ResourceType  = "notification_rules"
)

The Audit log action and resource type of a User's rules; the resource is named by the User.

View Source
const (
	RunRunning = "running"
	RunDone    = "done"
	RunStopped = "stopped"
)

The states of a rule run.

View Source
const ImportanceImportant = "important"

ImportanceImportant is the importance of a Page by the important rule; any other Pages by the normal one.

View Source
const TimerKind = "notification_rule_action"

TimerKind is the kind of the timer of a rule run.

Variables

View Source
var (
	// ErrVersionMismatch is an If-Match that names another version of the rules.
	ErrVersionMismatch = errors.New("the notification rules changed since they were read")
	// ErrUserNotFound is a User that does not exist or is deleted.
	ErrUserNotFound = errors.New("no such user")
)

Functions

func OrganizationETagVersion

func OrganizationETagVersion(organizationVersion int64) int64

OrganizationETagVersion is the version of If-Match of a User in the state organization; see RowETagVersion.

func RowETagVersion

func RowETagVersion(rowVersion int64) int64

RowETagVersion and OrganizationETagVersion are the version of If-Match, the number the ETag carries, of a User's rules: twice the version of their stored row, which comes from notification_rules_version_seq, or in the state organization, without a row, twice the version of the organization resource plus one. Even and odd never collide; the ETag of a stored row never repeats, and that of the state organization changes with every change of the Organization, whose rules the User follows.

func Validate

func Validate(in SetInput, prefix string, c Check) (Set, *FieldError)

Validate validates both rules of in, at the pointers prefix+"/normal" and prefix+"/important", and returns them as stored: a call without gateway_id names the only Notification gateway, and fields an action's kind does not take are dropped. A missing rule is required; more than MaxActions actions are out_of_range at the rule; a wait outside notification_rule.limits, or not a whole number of minutes, is out_of_range at its wait_seconds; a rule without a Mention or a call is required at the rule. A call while no Notification gateway exists is gateway_required at the action, without gateway_id while several exist required at its gateway_id, through a Connection that is no Notification gateway unknown_id there, and with Contacts through a gateway at which the User has no confirmed contact contact_required at the action.

func WithCall

func WithCall(org Set, gatewayID string) (Set, *FieldError)

WithCall is the Set of the state call (C-31.FR-2, FR-5): the Organization's rules org with a call through the gateway gatewayID at the start of the important rule, together with its first action, so that both happen at the same moment — unless that rule already calls through that gateway, which keeps it as it is. The normal rule stays as it is. An important rule that would pass MaxActions is out_of_range at /state.

Types

type Action

type Action struct {
	Kind        string `json:"kind"`
	GatewayID   string `json:"gateway_id,omitempty"`
	WaitSeconds int    `json:"wait_seconds,omitempty"`
}

Action is one action of a rule as stored: a Mention, a call through the gateway GatewayID (a public_id), or a wait of WaitSeconds.

type ActionInput

type ActionInput struct {
	Kind        string
	GatewayID   *string
	WaitSeconds *int
}

ActionInput is an action of a request body; fields its kind does not take are ignored.

type ActionView

type ActionView struct {
	Action
	// Gateway is the gateway of a call; nil for a Connection that no longer exists, and for the other kinds.
	Gateway *GatewayRef
	// Available is false for a call whose gateway was deleted or is no Notification gateway any more
	// (ReasonGatewayDeleted), or at which the User has no confirmed contact (ReasonNoConfirmedContact).
	Available bool
	Reason    string
}

ActionView is an action as a read shows it: a call with its gateway, and whether it can run now and why not.

type Call

type Call struct {
	User    int64
	RuleRun int64
	Gateway string
}

Call is a call action of a rule run that came due: the person, the rule run, and its Notification gateway by public_id (C-31.FR-3, C-30.FR-4).

type Check

type Check struct {
	Gateways Gateways
	Contacts bool
	Kept     Set
}

Check is what a validation checks a call action against: the gateways; with Contacts, that the User has a confirmed contact at the gateway, which the Organization's rules do not need (C-31.FR-4, C-20.FR-1). Kept are the stored rules of the same owner — the User's own row, never the Organization's rules they follow, or the Organization's own rules: as many calls through a gateway as Kept has, across both rules, pass even when their gateway was deleted or their contact removed, so that such an action stays in a rule saved again; a further call through it is checked.

type Config

type Config struct {
	OrgID int64
	Store Store
	Audit *audit.Writer
	// Business is the business clock: the time of a change.
	Business clock.Clock
}

Config is what the Service needs.

type DBTX

type DBTX = dbgen.DBTX

DBTX is the pool or transaction the queries run in.

type Due

type Due struct {
	Mention       bool
	Calls         []string
	Again         bool
	RuleRun       int64
	User          int64
	Run           int64
	Chain         int64
	ChainPublicID string
	ChainName     string
	Sources       []Source
	OwningTeam    int64
	Step          int
	Importance    string
	Schedule      int64
}

Due is what a rule run made after a wait (Advance): the person, the run of the chain with its chain, sources and owning Team, the step with its importance and Schedule; Mention when it made a Mention, and whether the rule run Mentioned the person before, which makes it a later Mention, "again"; and the gateways of the call actions it made, by public_id, each a Page of its own.

type FieldError

type FieldError struct {
	Pointer string
	Code    string
	Detail  string
}

FieldError is a field of an update that is not valid, at a JSON pointer of the request body.

func (*FieldError) Error

func (e *FieldError) Error() string

type Gateway

type Gateway struct {
	ID       int64
	PublicID string
	Name     string
	Live     bool
	Contact  bool
}

Gateway is a Bridge Connection that a call action may name, deleted ones included: Live while it is a Notification gateway — not deleted, its info declaring the role gateway — and Contact while the User has a confirmed contact there.

type GatewayRef

type GatewayRef struct {
	PublicID string
	Name     string
}

GatewayRef names a gateway in a read.

type Gateways

type Gateways []Gateway

Gateways are the Bridge Connections a call may name.

type Pool

type Pool interface {
	DBTX
	db.Beginner
}

Pool is the main pool: the queries, and the transactions of a change.

type Queries

Queries are the queries of the package, with the insert of the Audit log.

type Requester

type Requester struct {
	Actor     audit.Actor
	Transport audit.Transport
	Address   netip.Addr
}

Requester is who changes the rules and how: the actor and the Transport of the Audit log entry, and the client address.

type Rules

type Rules struct {
	UserPublicID string
	State        string
	Gateway      *GatewayRef
	Rules        SetView
	Version      int64
}

Rules are a User's rules as read: their state, the gateway of the state call, the sequences in effect and the version of If-Match (see ETagVersion).

type Runs

type Runs struct {
	// contains filtered or unexported fields
}

Runs holds the runs of the Notification rules of the Organization's Users.

func (*Runs) Advance

func (r *Runs) Advance(ctx context.Context, tx DBTX, id int64, deadline time.Time, firing bool) (*Due, error)

Advance makes the actions of the rule run id that came due at the deadline its timer was claimed with, in the transaction tx after the dispatcher locked its Alert Group (C-31.FR-3); firing is whether the Alert Group is firing. It locks the rule run and does nothing when it no longer runs or its timer was moved or removed since it was claimed, so that a second replica firing the same action changes nothing. A rule run whose Alert Group is no longer firing, or whose chain's run stopped, as a replica of an earlier version may leave it, is stopped then. Otherwise it makes the actions due — the overdue Mentions after an outage as one — sets the timer at the next wait, or ends the rule run after its last action, and returns the Mention and the calls to Page, each call overdue by then included, nil when it made none.

func (*Runs) Group

func (r *Runs) Group(ctx context.Context, tx DBTX, id int64) (int64, bool, error)

Group is the Alert Group of the rule run id, which the action of its timer locks first; false once the rule run is gone.

func (*Runs) Start

func (r *Runs) Start(ctx context.Context, tx DBTX, s Start) (Started, error)

Start starts the run of each person's rule of the step's importance, in the dispatcher's transaction tx after the lock of the Alert Group (C-31.FR-3): the rule as it is now (Effective), its actions due at once made — the Mentions returned, for one Page of the step naming everyone they Mention, and the calls returned, each with its rule run — and its timer at the end of its first wait; a rule that ran out is done.

func (*Runs) StopForGroup

func (r *Runs) StopForGroup(ctx context.Context, tx DBTX, groupID int64, at time.Time) error

StopForGroup stops the rule runs of the Alert Group groupID that still run, and deletes their timers, in the dispatcher's transaction tx (C-31.FR-3): its chains stopped — acknowledged, snoozed, resolved — or started over, those that ran out included, whose rules may still repeat.

type RunsConfig

type RunsConfig struct {
	Business      clock.Clock
	TimersChannel string
}

RunsConfig is what the runs need: the business clock and the channel that wakes the timer workers.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service reads and changes the Notification rules of the Users of an Organization.

func New

func New(cfg Config) *Service

New returns the Service of cfg.

func (*Service) Effective

func (s *Service) Effective(ctx context.Context, userID int64) (Set, error)

Effective is the sequences in effect for the User userID, which the runs of the rules follow (C-31.FR-3): the Organization's current rules in the state organization, the stored ones otherwise.

func (*Service) Get

func (s *Service) Get(ctx context.Context, userID int64) (Rules, error)

Get reads the rules of the User userID, the signed-in one (C-31.FR-4).

func (*Service) GetOfUser

func (s *Service) GetOfUser(ctx context.Context, userPublicID string) (Rules, error)

GetOfUser reads the rules of the User an Admin names; a deleted User is ErrUserNotFound.

func (*Service) RemoveUserRules

func (s *Service) RemoveUserRules(ctx context.Context, tx usersdb.DBTX, userID int64) (func(context.Context), error)

RemoveUserRules deletes the rules of the User userID in the transaction tx of their deletion (C-31.FR-5, C-03.FR-13). It records nothing: the deletion of the User is in the Audit log. It returns nothing to run after the commit.

func (*Service) Reset

func (s *Service) Reset(ctx context.Context, r Requester, userID int64) (Rules, error)

Reset returns the User userID to the state organization, "Reset to the Organization's rules" (C-31.FR-2).

func (*Service) Runs

func (s *Service) Runs(cfg RunsConfig) *Runs

Runs returns the runs of the rules of s.

func (*Service) Update

func (s *Service) Update(ctx context.Context, r Requester, userID int64, version *int64, in Update) (Rules, error)

Update changes the rules of the User userID, the signed-in one, from their web session (C-31.FR-4, FR-5).

func (*Service) UpdateOfUser

func (s *Service) UpdateOfUser(ctx context.Context, r Requester, userPublicID string, version *int64,
	in Update) (Rules, error)

UpdateOfUser changes the rules of the User an Admin names (C-31.FR-4).

type Set

type Set struct {
	Normal    []Action `json:"normal"`
	Important []Action `json:"important"`
}

Set is a normal and an important rule.

func Defaults

func Defaults() Set

Defaults are the default rules of organization.notification_rules (C-31.FR-2): normal, a Mention; important, a Mention, a wait of 5 minutes and a Mention. Migration 0037 writes the same value as the column's default.

func ParseSet

func ParseSet(raw []byte) (Set, error)

ParseSet reads a Set as stored.

func WithoutCalls

func WithoutCalls(org Set) Set

WithoutCalls is the Set of the state no_call (C-31.FR-2): the Organization's rules org without any call. A rule left without a Mention gets one in place of its first call and keeps its waits, so that every person can opt out of calls and every rule keeps an action that Pages (Open question 1 of S-152, settled by the maintainer).

func (Set) JSON

func (s Set) JSON() []byte

JSON is the Set as stored.

type SetInput

type SetInput struct {
	Normal    *[]ActionInput
	Important *[]ActionInput
}

SetInput is the two rules of a request body; nil is a missing rule.

type SetView

type SetView struct {
	Normal    []ActionView
	Important []ActionView
}

SetView is a Set as a read shows it.

func Describe

func Describe(s Set, gws Gateways, contacts bool) SetView

Describe is the Set s as a read shows it, against the gateways gws; with contacts, a call through a gateway at which the User has no confirmed contact reads unavailable, which the Organization's rules never do.

type Source

type Source struct {
	Kind   string `json:"kind"`
	TeamID int64  `json:"team_id,omitempty"`
}

Source is a source of the run of a chain as escalation_runs.sources stores it: its Route, or a Team by id.

type Start

type Start struct {
	Group      int64
	Run        int64
	Step       int
	Importance string
	Users      []int64
	RunStepSeq int
}

Start is a step that Pages people by their rules on an Alert Group: the Alert Group, the run of its chain, the step, its importance and the people, and RunStepSeq, the seq of the run step that makes it, which the later Mentions of the rule runs belong to (C-32.FR-12), 0 for none.

type Started

type Started struct {
	Mentioned    []int64
	AgainSeconds int
	Calls        []Call
}

Started is what the start of the rule runs made at once: the people whose rule Mentioned them at once, in their order, and the wait until their next Mentions when every one of them has a rule whose next Mention follows after the same wait, 0 otherwise: the one Page that names them all says "again in N min" only when it holds for each; and the call actions that came due at once, each a Page of its own.

type Store

type Store interface {
	Queries
	InTx(ctx context.Context, f func(Queries) error) error
}

Store runs the queries alone or in one transaction.

func NewStore

func NewStore(p Pool) Store

NewStore is the Store over the main pool.

type Update

type Update struct {
	State     string
	GatewayID *string
	Rules     SetInput
}

Update is a change of a User's rules: the state, with the gateway of the state call, nil for the only one at which the User has a confirmed contact, or with the custom sequences. Fields the state does not take are ignored.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL