pbehavior

package
v0.0.0-...-9b5cd94 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TypeActive      = "active"
	TypeMaintenance = "maintenance"
	TypePause       = "pause"
	TypeInactive    = "inactive"
)
View Source
const DefaultPoolSize = 100
View Source
const (
	ReasonCollectionName = mongo.PbehaviorReasonMongoCollection
)

Variables

View Source
var ErrCacheNotLoaded = errors.New("cache is not loaded")
View Source
var ErrNoComputed = errors.New("pbehavior intervals not computed")
View Source
var ErrRecomputeNeed = errors.New("provided time is out of computed date, probably need recompute data")

Functions

func GetRruleEnd

func GetRruleEnd(
	start datetime.CpsTime,
	rrule string,
	loc *time.Location,
) (*datetime.CpsTime, error)

func GetTimeSpans

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

GetTimeSpans returns all time spans of event within view.

func NewPBehaviorInfo

func NewPBehaviorInfo(time datetime.CpsTime, result ResolveResult) types.PbehaviorInfo

func ResolveDefaultTypes

func ResolveDefaultTypes(typesByID map[string]Type) (map[string]string, error)

ResolveDefaultTypes finds default types which uses : - active default type if there aren't any behaviors - inactive default type if there is behavior with active type

Types

type Adapter

type Adapter interface {
	UpdateLastAlarmDate(ctx context.Context, id string, time datetime.CpsTime) error
}

func NewAdapter

func NewAdapter(client mongo.DbClient) Adapter

type Cleaner

type Cleaner interface {
	Clean(ctx context.Context, before datetime.CpsTime, limit int64) (int64, error)
}

func NewCleaner

func NewCleaner(client mongo.DbClient, bulkSize int, logger zerolog.Logger) Cleaner

type Comment

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

type Comments

type Comments []*Comment

type ComputeResult

type ComputeResult struct {
	ComputedPbehaviors map[string]ComputedPbehavior
	TypesByID          map[string]Type
	DefaultActiveType  string
}

ComputeResult represents computed data.

type ComputedEntityGetter

type ComputedEntityGetter interface {
	Compute(ctx context.Context, filters []bson.M) error
	GetComputedEntityIDs() ([]string, error)
}

ComputedEntityGetter checks if there are entities which are matched to filters. It saves matched entity ids to local cache.

func NewComputedEntityGetter

func NewComputedEntityGetter(dbClient mongo.DbClient) ComputedEntityGetter

type ComputedEntityTypeResolver

type ComputedEntityTypeResolver interface {
	Resolve(
		ctx context.Context,
		entity types.Entity,
		t time.Time,
	) (ResolveResult, error)
	GetComputedEntityIDs() ([]string, error)
	GetPbehaviorsCount(ctx context.Context, t time.Time) (int, error)
}

ComputedEntityTypeResolver uses data in memory to resolve type for an entity.

func NewComputedEntityTypeResolver

func NewComputedEntityTypeResolver(
	getter ComputedEntityGetter,
	resolver TypeResolver,
) ComputedEntityTypeResolver

type ComputedPbehavior

type ComputedPbehavior struct {
	Name       string         `json:"n"`
	ReasonName string         `json:"rn"`
	ReasonID   string         `json:"r"`
	Filter     string         `json:"f"`
	Types      []ComputedType `json:"t"`
	Created    int64          `json:"c"`
	Color      string         `json:"-"`

	EntityPattern pattern.Entity `json:"p,omitempty"`
}

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

func (ComputedPbehavior) MarshalEasyJSON

func (v ComputedPbehavior) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*ComputedPbehavior) UnmarshalEasyJSON

func (v *ComputedPbehavior) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

type ComputedType

type ComputedType struct {
	ID    string        `json:"t"`
	Span  timespan.Span `json:"s"`
	Color string        `json:"-"`
}

ComputedType represents type for determined time span.

type EntityTypeResolver

type EntityTypeResolver interface {
	Resolve(
		ctx context.Context,
		entity types.Entity,
		t time.Time,
	) (ResolveResult, error)
	GetPbehaviors(ctx context.Context, pbhIDs []string, t time.Time) (map[string]ResolveResult, error)
}

func NewEntityTypeResolver

func NewEntityTypeResolver(
	store Store,
	logger zerolog.Logger,
) EntityTypeResolver

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 EventComputer

type EventComputer interface {
	Compute(params PbhEventParams, span timespan.Span) ([]ComputedType, error)
}

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

func NewEventComputer

func NewEventComputer(typesByID map[string]Type, defaultTypes map[string]string) EventComputer

type EventManager

type EventManager interface {
	GetEvent(ResolveResult, types.Alarm, time.Time) types.Event
	GetEventType(resolveResult ResolveResult, curPbehaviorInfo types.PbehaviorInfo) (eventType string, output string)
}

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     *datetime.CpsTime `bson:"created,omitempty" json:"created"`
}

type Exdate

type Exdate struct {
	types.Exdate `bson:"inline"`
	Type         string `bson:"type" json:"type"`
}

type ModelProvider

type ModelProvider interface {
	// GetTypes returns types by id.
	GetTypes(ctx context.Context) (map[string]Type, error)
	// GetEnabledPbehaviors returns pbehaviors.
	GetEnabledPbehaviors(ctx context.Context, span timespan.Span) (map[string]PBehavior, error)
	// GetEnabledPbehaviorsByIds returns pbehaviors.
	GetEnabledPbehaviorsByIds(ctx context.Context, ids []string, span timespan.Span) (map[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"`
	Name          string            `bson:"name"`
	Reason        string            `bson:"reason"`
	Type          string            `bson:"type_"`
	Exdates       []Exdate          `bson:"exdates"`
	Exceptions    []string          `bson:"exceptions"`
	Color         string            `bson:"color"`
	Created       *datetime.CpsTime `bson:"created,omitempty"`
	Updated       *datetime.CpsTime `bson:"updated,omitempty"`
	LastAlarmDate *datetime.CpsTime `bson:"last_alarm_date,omitempty"`

	Start    *datetime.CpsTime `bson:"tstart"`
	Stop     *datetime.CpsTime `bson:"tstop,omitempty"`
	RRule    string            `bson:"rrule"`
	RRuleEnd *datetime.CpsTime `bson:"rrule_end,omitempty"`
	// RRuleComputedStart is an auxiliary start date to compute rrule faster.
	RRuleComputedStart *datetime.CpsTime `bson:"rrule_cstart,omitempty"`

	// Origin is used if a pbehavior is created for certain entities.
	// Origin can contain some feature name or external service name.
	Origin string `bson:"origin,omitempty"`
	// Entity is used if a pbehavior is created for one certain entity.
	Entity string `bson:"entity,omitempty"`
	// Entities is used if a pbehavior is created for multiple certain entities.
	Entities []string `bson:"entities,omitempty"`

	savedpattern.EntityPatternFields `bson:",inline"`
}

PBehavior represents a canopsis periodical behavior.

type PbhEventParams

type PbhEventParams struct {
	ID      string
	Start   datetime.CpsTime
	End     datetime.CpsTime
	RRule   string
	Type    string
	Exdates []Exdate
}

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     datetime.CpsTime `bson:"created,omitempty" json:"created" swaggertype:"integer"`

	// Hidden is used in API to hide documents from the list response
	Hidden *bool `bson:"hidden,omitempty" json:"hidden,omitempty"`
}

type ResolveResult

type ResolveResult struct {
	ResolvedType          Type
	ResolvedPbhID         string
	ResolvedPbhName       string
	ResolvedPbhReasonID   string
	ResolvedPbhReasonName string
	ResolvedCreated       int64
}

ResolveResult represents current state of entity.

type Service

type Service interface {
	Compute(ctx context.Context, span timespan.Span) (ComputedEntityTypeResolver, int, error)
	Recompute(ctx context.Context) (ComputedEntityTypeResolver, error)
	RecomputeByIds(ctx context.Context, pbehaviorIds []string) (ComputedEntityTypeResolver, error)
}

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

func NewService

func NewService(
	dbClient mongo.DbClient,
	computer TypeComputer,
	store Store,
	lockClient redis.LockClient,
	logger zerolog.Logger,
) Service

NewService creates new service.

type Store

type Store interface {
	SetSpan(ctx context.Context, span timespan.Span) error
	GetSpan(ctx context.Context) (timespan.Span, error)
	SetComputed(ctx context.Context, computed ComputeResult) error
	GetComputed(ctx context.Context) (ComputeResult, error)
	SetComputedPbehavior(ctx context.Context, pbhID string, computed ComputedPbehavior) error
	DelComputedPbehavior(ctx context.Context, pbhID string) error
	GetComputedByIDs(ctx context.Context, pbehaviorIDs []string) (ComputeResult, error)
}

func NewStore

func NewStore(
	client redis.Cmdable,
	encoder encoding.Encoder,
	decoder encoding.Decoder,
) Store

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" json:"color"`
}

type TypeComputer

type TypeComputer interface {
	// Compute calculates types for provided timespan.
	Compute(ctx context.Context, span timespan.Span) (ComputeResult, error)
	ComputeByIds(ctx context.Context, span timespan.Span, pbehaviorIds []string) (ComputeResult, error)
}

TypeComputer is used to compute all periodical behaviors' timespans for provided interval.

func NewTypeComputer

func NewTypeComputer(
	modelProvider ModelProvider,
	decoder encoding.Decoder,
) 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.
	// An entity is matched to a pbehavior by an entity pattern or by cachedMatchedPbehaviorIds for old pbehaviors' queries.
	Resolve(ctx context.Context, t time.Time, entity types.Entity) (ResolveResult, error)
	GetPbehaviors(ctx context.Context, t time.Time, pbehaviorIDs []string) ([]ResolveResult, error)
	GetPbehaviorsCount(ctx context.Context, t time.Time) (int, error)
}

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

func NewTypeResolver

func NewTypeResolver(
	span timespan.Span,
	computedPbehaviors map[string]ComputedPbehavior,
	typesByID map[string]Type,
	defaultActiveTypeID string,
	logger zerolog.Logger,
) TypeResolver

NewTypeResolver creates new type resolver.

type Types

type Types struct {
	T map[string]Type
}

func (Types) MarshalEasyJSON

func (v Types) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (*Types) UnmarshalEasyJSON

func (v *Types) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

Jump to

Keyboard shortcuts

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