escalation

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package escalation keeps the OTRS ticket escalation index (ticket.escalation_* columns) up to date and raises the OTRS escalation events.

Index

Constants

View Source
const (
	FirstResponse = "FirstResponse"
	Update        = "Update"
	Solution      = "Solution"
)

Escalation kinds, in the order OTRS evaluates them.

View Source
const DefaultDecayTime = 24 * time.Hour

DefaultDecayTime is OTRS's OTRSEscalationEvents::DecayTime default: an escalation event is raised again for the same ticket only after this long.

Variables

Kinds lists the three escalation kinds.

Functions

func DecayTime added in v0.10.0

func DecayTime(db *sql.DB) (time.Duration, error)

DecayTime returns the OTRS setting OTRSEscalationEvents::DecayTime (minutes; 0 repeats events on every check), DefaultDecayTime when unset.

Types

type Calendar added in v0.10.0

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

Calendar is one OTRS business calendar: the working hours of each weekday, the recurring and one-time vacation days, and the time zone in which they are read. It mirrors Kernel::System::DateTime's AsWorkingTime/ForWorkingTime arithmetic.

func ParseCalendar added in v0.10.0

func ParseCalendar(loc *time.Location, workingHours, vacationDays, vacationDaysOneTime string) (*Calendar, error)

ParseCalendar builds a calendar from the OTRS setting values: TimeWorkingHours ({Mon: ['8', '9', ...], ...}), TimeVacationDays ({'12': {'25': 'Christmas'}}) and TimeVacationDaysOneTime ({'2026': {'1': {'2': 'Bridge day'}}}). Empty strings mean "not set". A weekday missing from the working hours is not a working day.

func (*Calendar) AddWorkingTime added in v0.10.0

func (c *Calendar) AddWorkingTime(start time.Time, seconds int64) (time.Time, error)

AddWorkingTime returns the moment at which seconds of working time have passed after start (OTRS DateTime->Add(AsWorkingTime => 1)). Without any configured working hour, or for seconds <= 0, start is returned unchanged, as in OTRS.

func (*Calendar) WorkingTime added in v0.10.0

func (c *Calendar) WorkingTime(start, stop time.Time) (int64, error)

WorkingTime returns the seconds of working time between start and stop (OTRS DateTime->Delta(ForWorkingTime => 1)); 0 when stop is not after start or no working hour is configured.

type Calendars added in v0.10.0

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

Calendars holds the default calendar and the named calendars "1".."9".

func LoadCalendars added in v0.10.0

func LoadCalendars(ctx context.Context, db *sql.DB, loc *time.Location) (*Calendars, error)

LoadCalendars reads the OTRS calendar settings: TimeWorkingHours, TimeVacationDays and TimeVacationDaysOneTime for the default calendar, and their ::CalendarN variants with TimeZone::CalendarN for calendars 1-9. A numbered calendar is used when its working hours are set and its TimeZone::CalendarNName is not blanked out; otherwise its tickets use the default calendar. loc is the time zone of the default calendar and of numbered calendars without their own TimeZone::CalendarN.

func (*Calendars) Get added in v0.10.0

func (cs *Calendars) Get(name string) *Calendar

Get returns the calendar an SLA or queue calendar_name refers to ("" or "1".."9", optionally "Calendar1"); unknown or inactive names get the default calendar, as in OTRS.

type Change added in v0.10.0

type Change struct {
	UserID int
	At     time.Time
}

Change describes the ticket change a rebuild follows: who made it and when. An escalation counts as stopped by the change when it had started by then and no longer has.

type Event added in v0.10.0

type Event struct {
	TicketID int
	Name     string // ticket_history_type name, e.g. EscalationResponseTimeStart
}

Event is one escalation event raised by CheckEscalations.

type Index added in v0.10.0

type Index struct {
	Escalation int64 // earliest of the three
	Response   int64
	Update     int64
	Solution   int64
}

Index is a ticket's escalation index: OTRS destination times in unix seconds, 0 where the kind does not escalate.

func (Index) Of added in v0.10.0

func (ix Index) Of(kind string) int64

Of returns the destination time of one kind.

type Indexer added in v0.10.0

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

Indexer keeps the escalation index current: OTRS rebuilds it from the ticket event handler (Ticket::EventModulePost###9990-EscalationIndex); GoatFlow rebuilds it from RunOnce, which the scheduler calls every few seconds. The first run, and every run after an SLA, queue or calendar setting changed, rebuilds all tickets that can escalate.

func NewIndexer added in v0.10.0

func NewIndexer(svc *Service) *Indexer

NewIndexer returns an indexer that rebuilds through svc.

func (*Indexer) RunOnce added in v0.10.0

func (ix *Indexer) RunOnce(ctx context.Context) (int, error)

RunOnce rebuilds the index of the tickets changed since the previous run and returns how many it rebuilt. A run that starts while another is still going returns immediately.

type Option added in v0.10.0

type Option func(*Service)

Option configures a Service.

func WithClock added in v0.10.0

func WithClock(now func() time.Time) Option

WithClock replaces time.Now.

func WithLocation added in v0.10.0

func WithLocation(loc *time.Location) Option

WithLocation sets the time zone of the default calendar (and of numbered calendars without TimeZone::CalendarN). Default UTC.

func WithLogger added in v0.10.0

func WithLogger(l *log.Logger) Option

WithLogger sets the logger.

type Preferences added in v0.10.0

type Preferences struct {
	FirstResponseTime   int
	FirstResponseNotify int
	UpdateTime          int
	UpdateNotify        int
	SolutionTime        int
	SolutionNotify      int
	Calendar            string
}

Preferences are the escalation settings of a ticket: its SLA's when it has one, else its queue's (OTRS TicketEscalationPreferences). Times are in minutes, notify values in percent.

type Service

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

Service computes the OTRS escalation index of tickets (Kernel::System::Ticket::TicketEscalationIndexBuild) and raises the escalation events (Maint::Ticket::EscalationCheck and Ticket::Event::TriggerEscalationStopEvents).

func NewService

func NewService(db *sql.DB, opts ...Option) *Service

NewService returns a service on db. Calendars are loaded on first use and by LoadCalendars.

func (*Service) CheckEscalations added in v0.10.0

func (s *Service) CheckEscalations(ctx context.Context, decay time.Duration) ([]Event, error)

CheckEscalations is OTRS Maint::Ticket::EscalationCheck: for up to 1000 tickets escalating within five days it raises Escalation*TimeStart for every started escalation and Escalation*TimeNotifyBefore for every one past its notify percentage, by writing the ticket_history row of that type (webhooks and notification events read those). Tickets whose calendar had no working time in the last ten minutes are skipped, and an event is not repeated for a ticket within decay (0 = every run).

func (*Service) LoadCalendars added in v0.10.0

func (s *Service) LoadCalendars(ctx context.Context) error

LoadCalendars (re)reads the calendar settings.

func (*Service) RebuildAll added in v0.10.0

func (s *Service) RebuildAll(ctx context.Context, userID int) (int, error)

RebuildAll rebuilds the escalation index of every ticket that can escalate or still has an index (OTRS Maint::Ticket::EscalationIndexRebuild). It returns how many tickets it rebuilt; a failing ticket is logged and skipped.

func (*Service) TicketEscalationIndexBuild

func (s *Service) TicketEscalationIndexBuild(ctx context.Context, ticketID, userID int) error

TicketEscalationIndexBuild recomputes a ticket's escalation index and stores it in the ticket's escalation_* columns. When the change ends an escalation that had started, it records the OTRS Escalation*TimeStop history event on behalf of userID. Concurrent rebuilds of the same ticket are safe: only the one that moves the stored index records stop events.

type State added in v0.10.0

type State struct {
	Kind        string
	Destination int64 // unix seconds
	Escalated   bool  // the destination time has passed
	Notify      bool  // the notify-before percentage of the working time is used up
	WorkingTime int64 // working seconds left (negative: overdue)
}

State is a ticket's escalation state at one moment (OTRS TicketEscalationDateCalculation) for one kind.

Jump to

Keyboard shortcuts

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