entityservice

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: 29 Imported by: 0

Documentation

Index

Constants

View Source
const BulkMaxSize = 10000

Variables

This section is empty.

Functions

func GetAlarmCountersFromEvent

func GetAlarmCountersFromEvent(event types.Event) (*AlarmCounters, *AlarmCounters, bool)

GetAlarmCountersFromEvent returns counters for old alarm state and for new alarm state base on alarm change type.

func GetServiceIDsFromEvent

func GetServiceIDsFromEvent(event types.Event, serviceIDs []string) ([]string, []string, []string)

GetServiceIDsFromEvent return services which entity is added to, services which entity is removed from, services which entity is kept in.

func GetServiceOutput

func GetServiceOutput(outputTemplate string, counters AlarmCounters) (string, error)

GetServiceOutput returns the output of the service.

func GetServiceState

func GetServiceState(counters AlarmCounters) int

GetServiceState returns the state of the service.

Types

type Adapter

type Adapter interface {
	GetAll() ([]EntityService, error)

	GetEnabled() ([]EntityService, error)

	GetValid() ([]EntityService, error)

	GetByID(id string) (*EntityService, error)

	AddDepends(id string, depends []string) (bool, error)

	RemoveDepends(id string, depends []string) (bool, error)

	RemoveDependByQuery(query interface{}, depend string) ([]string, error)

	// UpdateCounters saves service counters to storage.
	UpdateCounters(string, AlarmCounters) error

	// UpdateBulk bulk update
	UpdateBulk(ctx context.Context, writeModels []mongodriver.WriteModel) error

	// GetCounters calculates service counters base on dependencies alarms state.
	GetCounters(ctx context.Context, serviceID string) (mongo.Cursor, error)
}

Adapter is an interface that provides methods for database queries regarding services and their dependencies.

func NewAdapter

func NewAdapter(client mongo.DbClient) Adapter

type AlarmCounters

type AlarmCounters struct {
	// All is count of unresolved
	All int64 `bson:"all"`
	// Alarms is count of unresolved and active (by pbehavior)
	Alarms int64         `bson:"active"`
	State  StateCounters `bson:"state"`
	// Acknowledged is count of unresolved and acked and active (by pbehavior)
	Acknowledged int64 `bson:"acked"`
	// NotAcknowledged is count of unresolved and unacked and active (by pbehavior)
	NotAcknowledged int64 `bson:"unacked"`
	// PbehaviorCounters contains counters for each pbehavior type.
	PbehaviorCounters map[string]int64 `bson:"pbehavior"`
}

AlarmCounters is a struct containing various counters that are used to determine a service's state and output.

func GetAlarmCountersFromAlarm

func GetAlarmCountersFromAlarm(alarm types.Alarm) AlarmCounters

GetAlarmCountersFromAlarm returns alarm counters based on alarm.

func (AlarmCounters) Add

Add returns a new AlarmCounters containing the sums of two AlarmCounters.

func (AlarmCounters) IsZero

func (c AlarmCounters) IsZero() bool

IsZero returns true if all the counters of the AlarmCounters are equal to 0.

func (AlarmCounters) Negate

func (c AlarmCounters) Negate() AlarmCounters

Negate returns a new AlarmCounters, with all the counters negated.

type ChangeEntityMessage

type ChangeEntityMessage struct {
	ID string
	// IsToggled is true if entity is disabled or enabled.
	IsToggled bool
	IsService bool
	// IsPatternChanged defines should service's context graph and state be recomputed.
	IsServicePatternChanged bool
	// ServiceAlarm is required on entity service delete because alarm is removed from
	// storage but alarm state is required by engine-service.
	ServiceAlarm *types.Alarm
}

type CountersCache

type CountersCache interface {
	Update(context.Context, map[string]AlarmCounters) (map[string]AlarmCounters, error)

	Replace(context.Context, string, AlarmCounters) error

	Remove(context.Context, string) error

	RemoveAndGet(context.Context, string) (*AlarmCounters, error)

	ClearAll(context.Context) error

	KeepOnly(ctx context.Context, ids []string) error
}

CountersCache is a type that handles the counting of the alarms and entities that impact services.

func NewCountersCache

func NewCountersCache(client redis.UniversalClient, logger zerolog.Logger) CountersCache

NewCountersCache creates a new CountersCache

type EntityService

type EntityService struct {
	types.Entity   `bson:",inline"`
	EntityPatterns pattern.EntityPatternList `bson:"entity_patterns" json:"entity_patterns"`
	OutputTemplate string                    `bson:"output_template" json:"output_template"`
}

type EventPublisher

type EventPublisher interface {
	Publish(ctx context.Context, ch <-chan ChangeEntityMessage)
}

EventPublisher is used to send event to engines' event flow to notify about entity changes.

func NewEventPublisher

func NewEventPublisher(
	alarmAdapter libalarm.Adapter,
	publisher libamqp.Publisher,
	encoder encoding.Encoder,
	contentType string,
	exchange, routingKey string,
	logger zerolog.Logger,
) EventPublisher

type Manager

type Manager interface {
	// LoadServices loads services from storage.
	LoadServices(ctx context.Context) error
	// UpdateServices adds or removes entities.
	UpdateServices(ctx context.Context, entities []types.Entity) (addedTo map[string][]string, removedFrom map[string][]string, err error)
	// UpdateService adds service to matched entities impacts and updates its depends.
	UpdateService(ctx context.Context, serviceID string) (isUpdated bool, removedFrom []string, err error)
	// ReloadService loads service from storage to cache.
	ReloadService(ctx context.Context, serviceID string) error
	// HasEntityServiceByComponentInfos returns true of at least one entity service patterns
	// contains component infos condition.
	HasEntityServiceByComponentInfos(ctx context.Context) (bool, error)
}

Manager is used to implement context graph modifier for entity service.

func NewManager

func NewManager(
	adapter Adapter,
	entityAdapter entity.Adapter,
	storage Storage,
	logger zerolog.Logger,
) Manager

NewManager creates new manager.

type Service

type Service interface {
	// Process updates the services impacted by the event and alarmChange
	// This method should be called on each event published by the axe engine
	// and context graph update by the che engine,
	// so that the services' states are updated in real time.
	Process(ctx context.Context, event types.Event) error

	ProcessRpc(ctx context.Context, event types.Event) error

	// UpdateService updates the state of a service given its ID.
	UpdateService(ctx context.Context, event types.Event) error

	// ReloadService loads service from storage to cache.
	ReloadService(ctx context.Context, serviceID string) error

	// ComputeAllServices updates the states of all services.
	ComputeAllServices(ctx context.Context) error

	RecomputeIdleSince(parentCtx context.Context) error

	// ClearCache clears all counters of entity services from cache.
	ClearCache(ctx context.Context) error
}

func NewService

func NewService(
	pubChannel amqp.Publisher,
	pubExchangeName, pubQueueName string,
	encoder encoding.Encoder,
	adapter Adapter,
	entityAdapter entity.Adapter,
	countersCache CountersCache,
	storage Storage,
	lockClient libredis.LockClient,
	redisClient redis.Cmdable,
	logger zerolog.Logger,
) Service

NewService gives the correct service adapter.

type ServiceData

type ServiceData struct {
	ID             string                    `json:"_id"`
	OutputTemplate string                    `json:"output_template,omitempty"`
	EntityPatterns pattern.EntityPatternList `json:"entity_patterns"`
	Impacts        []string                  `json:"impacts"`
}

type ServicesIdleSinceMap

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

func NewServicesIdleSinceMap

func NewServicesIdleSinceMap() ServicesIdleSinceMap

func (*ServicesIdleSinceMap) Mark

func (m *ServicesIdleSinceMap) Mark(id string, newIdleSince int64) bool

type StateCounters

type StateCounters struct {
	Critical int64 `bson:"critical"`
	Major    int64 `bson:"major"`
	Minor    int64 `bson:"minor"`
	Info     int64 `bson:"info"`
}

StateCounters is a struct containing the number of alarms in each state that impact a service.

func NewStateCounters

func NewStateCounters(state types.CpsNumber) StateCounters

NewStateCounters create state counters.

func (StateCounters) Add

Add returns a new StateCounters containing the sums of two StateCounters.

func (StateCounters) IsZero

func (c StateCounters) IsZero() bool

IsZero returns true if all the counters of the StateCounters are equal to 0.

func (StateCounters) Negate

func (c StateCounters) Negate() StateCounters

Negate returns a new StateCounters, with all the counters negated.

type Storage

type Storage interface {
	SaveAll(ctx context.Context, data []ServiceData) error
	Save(ctx context.Context, data ServiceData) error
	Load(ctx context.Context) ([]ServiceData, error)
	Get(ctx context.Context, id string) (*ServiceData, error)
	Delete(ctx context.Context, id string) error
}

func NewStorage

func NewStorage(
	client redis.Cmdable,
	encoder encoding.Encoder,
	decoder encoding.Decoder,
	logger zerolog.Logger,
) Storage

Jump to

Keyboard shortcuts

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