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
- Variables
- func OrganizationETagVersion(organizationVersion int64) int64
- func RowETagVersion(rowVersion int64) int64
- func Validate(in SetInput, prefix string, c Check) (Set, *FieldError)
- func WithCall(org Set, gatewayID string) (Set, *FieldError)
- type Action
- type ActionInput
- type ActionView
- type Call
- type Check
- type Config
- type DBTX
- type Due
- type FieldError
- type Gateway
- type GatewayRef
- type Gateways
- type Pool
- type Queries
- type Requester
- type Rules
- type Runs
- func (r *Runs) Advance(ctx context.Context, tx DBTX, id int64, deadline time.Time, firing bool) (*Due, error)
- func (r *Runs) Group(ctx context.Context, tx DBTX, id int64) (int64, bool, error)
- func (r *Runs) Start(ctx context.Context, tx DBTX, s Start) (Started, error)
- func (r *Runs) StopForGroup(ctx context.Context, tx DBTX, groupID int64, at time.Time) error
- type RunsConfig
- type Service
- func (s *Service) Effective(ctx context.Context, userID int64) (Set, error)
- func (s *Service) Get(ctx context.Context, userID int64) (Rules, error)
- func (s *Service) GetOfUser(ctx context.Context, userPublicID string) (Rules, error)
- func (s *Service) RemoveUserRules(ctx context.Context, tx usersdb.DBTX, userID int64) (func(context.Context), error)
- func (s *Service) Reset(ctx context.Context, r Requester, userID int64) (Rules, error)
- func (s *Service) Runs(cfg RunsConfig) *Runs
- func (s *Service) Update(ctx context.Context, r Requester, userID int64, version *int64, in Update) (Rules, error)
- func (s *Service) UpdateOfUser(ctx context.Context, r Requester, userPublicID string, version *int64, ...) (Rules, error)
- type Set
- type SetInput
- type SetView
- type Source
- type Start
- type Started
- type Store
- type Update
Constants ¶
const ( KindMention = "mention" KindCall = "call" KindWait = "wait" )
The kinds of an action.
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.
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).
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.
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).
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.
const ( RunRunning = "running" RunDone = "done" RunStopped = "stopped" )
The states of a rule run.
const ImportanceImportant = "important"
ImportanceImportant is the importance of a Page by the important rule; any other Pages by the normal one.
const TimerKind = "notification_rule_action"
TimerKind is the kind of the timer of a rule run.
Variables ¶
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 ¶
OrganizationETagVersion is the version of If-Match of a User in the state organization; see RowETagVersion.
func RowETagVersion ¶
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 ¶
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 ¶
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 ¶
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 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 ¶
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 ¶
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 ¶
GatewayRef names a gateway in a read.
type Queries ¶
type Queries interface {
LockRulesUser(ctx context.Context, arg dbgen.LockRulesUserParams) (dbgen.LockRulesUserRow, error)
GetRulesUser(ctx context.Context, arg dbgen.GetRulesUserParams) (dbgen.GetRulesUserRow, error)
GetRulesUserByID(ctx context.Context, arg dbgen.GetRulesUserByIDParams) (dbgen.GetRulesUserByIDRow, error)
GetOrganizationRules(ctx context.Context, orgID int64) (dbgen.GetOrganizationRulesRow, error)
GetRules(ctx context.Context, arg dbgen.GetRulesParams) (dbgen.GetRulesRow, error)
ListRuleGateways(ctx context.Context, arg dbgen.ListRuleGatewaysParams) ([]dbgen.ListRuleGatewaysRow, error)
UpsertRules(ctx context.Context, arg dbgen.UpsertRulesParams) (int64, error)
DeleteRules(ctx context.Context, arg dbgen.DeleteRulesParams) (int64, error)
audit.Store
}
Queries are the queries of the package, with the insert of the Audit log.
type Requester ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 (*Service) Effective ¶
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) GetOfUser ¶
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 ¶
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.
type Set ¶
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 WithoutCalls ¶
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).
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.
type Source ¶
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 ¶
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 ¶
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.