pbehavior

package
v0.0.0-...-d841f61 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2021 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OperationCreate = iota
	OperationUpdate
	OperationDelete
)
View Source
const (
	CalculateAll  = ""
	CalculateNone = "none"
)
View Source
const (
	PBehaviorCollectionName = mongo.PbehaviorMongoCollection
	TypeCollectionName      = mongo.PbehaviorTypeMongoCollection
)
View Source
const (
	TypeActive      = "active"
	TypeMaintenance = "maintenance"
	TypePause       = "pause"
	TypeInactive    = "inactive"
)
View Source
const DefaultPoolSize = 10
View Source
const (
	ExceptionCollectionName = mongo.PbehaviorExceptionMongoCollection
)
View Source
const (
	ReasonCollectionName = mongo.PbehaviorReasonMongoCollection
)

Variables

View Source
var ErrRecomputeNeed = errors.New("provided time is out of computed date, probably need recompute data")

Functions

func GetDateSpans

func GetDateSpans(event Event, view timespan.Span, exdates ...[]timespan.Span) ([]timespan.Span, error)

GetDateSpans returns all date spans of event within view.

func GetTimeSpans

func GetTimeSpans(event Event, view timespan.Span, exdates ...[]timespan.Span) ([]timespan.Span, error)

GetTimeSpans returns all time spans of event within view.

func NewPBehaviorInfo

func NewPBehaviorInfo(result ResolveResult) types.PbehaviorInfo

func NewTypeResolver

func NewTypeResolver(
	matcher EntityMatcher,
	span timespan.Span,
	computedPbehaviors map[string]ComputedPbehavior,
	typesByID map[string]*Type,
	defaultActiveTypeID string,
	logger zerolog.Logger,
	workerPoolSize ...int,
) *typeResolver

NewTypeResolver creates new type resolver.

Types

type Comment

type Comment struct {
	ID        string         `bson:"_id" json:"_id"`
	Author    string         `bson:"author" json:"author"`
	Timestamp *types.CpsTime `bson:"ts" json:"ts"`
	Message   string         `bson:"message" json:"message"`
}

type Comments

type Comments []*Comment

type ComputeResult

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

ComputeResult represents computed data.

type ComputeTask

type ComputeTask struct {
	// PbehaviorID defines for which pbehavior intervals should be recomputed.
	// If empty all intervals are recomputed.
	PbehaviorID string
	// UpdateAlarms prescribes to update alarms by pbehavior. PbehaviorID shouldn't be empty.
	OperationType int
}

type ComputedPbehavior

type ComputedPbehavior struct {
	Name    string         `json:"n"`
	Reason  string         `json:"r"`
	Filter  string         `json:"f"`
	Types   []computedType `json:"t"`
	Created int64          `json:"c"`
}

ComputedPbehavior represents all computed types for periodical behavior. Computed types are sorted: - time spans which are defined by exdate - time spans which are defined by rrule - time spans which are defined by default inactive interval of active pbehavior For example, for active daily periodical behavior at 10:00-12:00 and date 2020-06-01: [2020-06-01T10:00, 2020-06-01T12:00] ActiveTypeID [2020-06-01T00:00, 2020-06-02T00:00] InactiveTypeID

type Computer

type Computer interface {
	// Compute recomputes all pbehaviors on empty signal
	// or recomputes only one pbehavior on signal which conatins pbehavior id.
	Compute(ctx context.Context, ch <-chan ComputeTask)
}

Computer is used to implement pbehavior intervals recompute on singnal from channel.

func NewCancelableComputer

func NewCancelableComputer(
	lockClient redis.LockClient,
	redisStore redis.Store,
	service Service,
	dbClient mongo.DbClient,
	publisher libamqp.Publisher,
	eventManager EventManager,
	encoder encoding.Encoder,
	queue string,
	logger zerolog.Logger,
) Computer

NewCancelableComputer creates new computer.

type EntityMatcher

type EntityMatcher interface {
	Match(ctx context.Context, entityID string, filter string) (bool, error)
	MatchAll(ctx context.Context, entityID string, filters map[string]string) (map[string]bool, error)
}

EntityMatcher checks if an entity is matched to filter.

func NewEntityMatcher

func NewEntityMatcher(dbClient mongo.DbClient, aggregationStep ...int) EntityMatcher

NewEntityMatcher creates new matcher.

type Event

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

Event represents a recurrent calendar event.

func NewEvent

func NewEvent(startAt, endAt time.Time) Event

NewEvent creates a new event with the given start and end times.

func NewRecEvent

func NewRecEvent(startAt, endAt time.Time, rOption *rrule.ROption) Event

NewRecEvent creates a new recurrent event with the given start and end times and recurrent rule.

type EventManager

type EventManager interface {
	GetEvent(ResolveResult, types.Alarm, time.Time) types.Event
}

func NewEventManager

func NewEventManager() EventManager

type Exception

type Exception struct {
	ID          string         `bson:"_id,omitempty" json:"_id"`
	Name        string         `bson:"name" json:"name"`
	Description string         `bson:"description" json:"description"`
	Exdates     []Exdate       `bson:"exdates" json:"exdates"`
	Created     *types.CpsTime `bson:"created,omitempty" json:"created"`
}

type Exdate

type Exdate struct {
	Begin types.CpsTime `bson:"begin" json:"begin"`
	End   types.CpsTime `bson:"end" json:"end"`
	Type  string        `bson:"type" json:"type"`
}

type ModelProvider

type ModelProvider interface {
	// GetTypes returns types by id.
	GetTypes(ctx context.Context) (map[string]*Type, error)
	// GetPbehaviors returns pbehaviors by id.
	GetEnabledPbehaviors(ctx context.Context) (map[string]*PBehavior, error)
	// GetPbehavior returns pbehavior.
	GetEnabledPbehavior(ctx context.Context, id string) (*PBehavior, error)
	// GetExceptions returns exceptions by id.
	GetExceptions(ctx context.Context) (map[string]*Exception, error)
	// GetReasons returns reasons by id.
	GetReasons(ctx context.Context) (map[string]*Reason, error)
}

ModelProvider is used to implement fetching models from storage.

func NewModelProvider

func NewModelProvider(dbClient mongo.DbClient) ModelProvider

NewModelProvider creates new model provider.

type PBehavior

type PBehavior struct {
	ID         string         `bson:"_id,omitempty"`
	Author     string         `bson:"author"`
	Comments   Comments       `bson:"comments,omitempty"`
	Enabled    bool           `bson:"enabled"`
	Filter     string         `bson:"filter"`
	Name       string         `bson:"name"`
	Reason     string         `bson:"reason"`
	RRule      string         `bson:"rrule"`
	Start      *types.CpsTime `bson:"tstart"`
	Stop       *types.CpsTime `bson:"tstop,omitempty"`
	Type       string         `bson:"type_"`
	Exdates    []Exdate       `bson:"exdates"`
	Exceptions []string       `bson:"exceptions"`
	Created    types.CpsTime  `bson:"created,omitempty"`
	Updated    types.CpsTime  `bson:"updated,omitempty"`
}

PBehavior represents a canopsis periodical behavior.

func (PBehavior) IsActive

func (pb PBehavior) IsActive(date time.Time) (bool, error)

IsActive checks if a pbehavior is active at the given time.

type Reason

type Reason struct {
	ID          string        `bson:"_id,omitempty" json:"_id"`
	Name        string        `bson:"name" json:"name" binding:"required"`
	Description string        `bson:"description" json:"description" binding:"required"`
	Created     types.CpsTime `bson:"created,omitempty" json:"created"`
}

type ResolveResult

type ResolveResult struct {
	ResolvedType      *Type
	ResolvedPbhID     string
	ResolvedPbhName   string
	ResolvedPbhReason string
	ResolvedCreated   int64
}

ResolveResult represents current state of entity.

type Service

type Service interface {
	Resolve(ctx context.Context, entity *libtypes.Entity, t time.Time) (ResolveResult, error)
	Compute(ctx context.Context, span timespan.Span) error
	Recompute(ctx context.Context, pbehaviorID string) error
	GetSpan() timespan.Span
	GetPbehaviorStatus(ctx context.Context, pbehaviorIDs []string, t time.Time) (map[string]bool, error)
}

Service computes pbehavior timespans and figures out state of provided entity by computed data.

func NewService

func NewService(
	modelProvider ModelProvider,
	matcher EntityMatcher,
	logger zerolog.Logger,
	workerPoolSize ...int,
) Service

NewService creates new service.

type Type

type Type struct {
	ID          string `bson:"_id,omitempty" json:"_id,omitempty"`
	Name        string `bson:"name" json:"name"`
	Description string `bson:"description" json:"description"`
	Type        string `bson:"type" json:"type"`
	Priority    int    `bson:"priority" json:"priority"`
	IconName    string `bson:"icon_name" json:"icon_name"`
	Color       string `bson:"color,omitempty" json:"color,omitempty"`
}

type TypeComputer

type TypeComputer interface {
	// Compute calculates types for provided timespan.
	Compute(ctx context.Context, span timespan.Span) (*ComputeResult, error)
	Recompute(ctx context.Context, span timespan.Span, pbehaviorID string) (*ComputedPbehavior, error)
}

TypeComputer is used to compute periodical behavior timespans for provided interval.

func NewTypeComputer

func NewTypeComputer(
	modelProvider ModelProvider,
	logger zerolog.Logger,
	workerPoolSize ...int,
) TypeComputer

NewTypeComputer creates new type resolver.

type TypeResolver

type TypeResolver interface {
	// Resolve returns current type for entity if there is corresponding periodical behavior.
	// Otherwise it returns default active type.
	Resolve(context.Context, *libtypes.Entity, time.Time) (ResolveResult, error)
	GetSpan() timespan.Span
}

TypeResolver figures out in which state provided entity at the moment is.

Jump to

Keyboard shortcuts

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