situation

package
v4.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildSituationsFromFile

func BuildSituationsFromFile(path string, file string) (map[int64]*Situation, []error)

BuildSituationsFromFile reads an ensemble of situations from the provided file and returns them Deprecated: Situation loading from YAML file has been replaced by standard REST API

func GetHistoryMetadata

func GetHistoryMetadata(situationID int64, t time.Time, templateInstanceID int64) ([]models.MetaData, error)

GetHistoryMetadata returns metadatas for a specific history entry (based on situation ID and timestamp)

func GetLastHistoryMetadata

func GetLastHistoryMetadata(situationID int64, templateInstanceID int64) ([]models.MetaData, error)

GetLastHistoryMetadata returns the metadatas of the last evaluation of a situation

func Persist

func Persist(record HistoryRecord, evaluated bool) error

Persist persists a situation history record in postgresql

func ReplaceGlobals

func ReplaceGlobals(repository Repository) func()

ReplaceGlobals affect a new repository to the global repository singleton

func SetAsEvaluated

func SetAsEvaluated(situationID int64, t time.Time, templateInstanceID int64) error

SetAsEvaluated sets as evaluated the situation instance history

func UpdateExpressionFacts

func UpdateExpressionFacts(record HistoryRecord) error

UpdateExpressionFacts update the expression facts result on a situation history record

func UpdateHistoryMetadata

func UpdateHistoryMetadata(situationID int64, t time.Time, templateInstanceID int64, metaDatas []models.MetaData) error

UpdateHistoryMetadata updates a situation entry in postgresql and update its metadatas

Types

type ExpressionFact

type ExpressionFact struct {
	Name       string `json:"name"`
	Expression string `json:"expression"`
}

ExpressionFact represent a custom calculated fact based on gval expression

type HistoryRecord

type HistoryRecord struct {
	ID                       int64
	TS                       time.Time
	TemplateInstanceID       int64
	FactsIDS                 map[int64]*time.Time
	Parameters               map[string]string
	EvaluatedExpressionFacts map[string]interface{}
}

HistoryRecord represents a single and unique situation history entry

func GetFromHistory

func GetFromHistory(situationID int64, t time.Time, templateInstanceID int64, closest bool) (*HistoryRecord, error)

GetFromHistory returns a single situation entry from postgresql It can be based on an exact timestamp, or return the closest result

func (HistoryRecord) OverrideParameters

func (hr HistoryRecord) OverrideParameters(p map[string]string)

OverrideParameters overrides the parameters of the History Record

type PostgresRepository

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

PostgresRepository is a repository containing the situation definition based on a PSQL database and implementing the repository interface

func (*PostgresRepository) AddRule

func (r *PostgresRepository) AddRule(tx *sqlx.Tx, id int64, ruleID int64) error

AddRule adds a rule ad the end of the situation rule list

func (*PostgresRepository) Create

func (r *PostgresRepository) Create(situation Situation) (int64, error)

Create creates a new situation in the database using the given situation object

func (*PostgresRepository) CreateTemplateInstance

func (r *PostgresRepository) CreateTemplateInstance(situationID int64, instance TemplateInstance) (int64, error)

CreateTemplateInstance creates a situation template instance

func (*PostgresRepository) Delete

func (r *PostgresRepository) Delete(id int64) error

Delete deletes an entity from the repository by its name

func (*PostgresRepository) DeleteTemplateInstance

func (r *PostgresRepository) DeleteTemplateInstance(instanceID int64) error

DeleteTemplateInstance deletes a situation template instance

func (*PostgresRepository) Get

func (r *PostgresRepository) Get(id int64) (Situation, bool, error)

Get retrieve the specified situation definition

func (*PostgresRepository) GetAll

func (r *PostgresRepository) GetAll() (map[int64]Situation, error)

GetAll returns all entities in the repository

func (*PostgresRepository) GetAllByIDs added in v4.2.0

func (r *PostgresRepository) GetAllByIDs(ids []int64) (map[int64]Situation, error)

GetAllByIDs returns all entities filtered by IDs in the repository

func (*PostgresRepository) GetAllByRuleID

func (r *PostgresRepository) GetAllByRuleID(ruleID int64) (map[int64]Situation, error)

GetAllByRuleID returns all entities in the repository based on a rule ID

func (*PostgresRepository) GetAllTemplateInstances

func (r *PostgresRepository) GetAllTemplateInstances(situationID int64) (map[int64]TemplateInstance, error)

GetAllTemplateInstances returns the list of template instances of the situation

func (*PostgresRepository) GetByName

func (r *PostgresRepository) GetByName(name string) (Situation, bool, error)

GetByName retrieve the specified situation definition by it's name

func (*PostgresRepository) GetFacts

func (r *PostgresRepository) GetFacts(id int64) ([]int64, error)

GetFacts returns the list of facts

func (*PostgresRepository) GetRules

func (r *PostgresRepository) GetRules(id int64) ([]int64, error)

GetRules returns the list of rules used in to evaluate the situation

func (*PostgresRepository) GetSituationsByFactID

func (r *PostgresRepository) GetSituationsByFactID(factID int64, ignoreIsObject bool) ([]Situation, error)

GetSituationsByFactID returns the situations in which the fact is required

func (*PostgresRepository) GetTemplateInstance

func (r *PostgresRepository) GetTemplateInstance(instanceID int64) (TemplateInstance, bool, error)

GetTemplateInstance returns the situation template instance

func (*PostgresRepository) RemoveRule

func (r *PostgresRepository) RemoveRule(tx *sqlx.Tx, id int64, ruleID int64) error

RemoveRule removes a rule ad the end of the situation rule list

func (*PostgresRepository) SetRules

func (r *PostgresRepository) SetRules(id int64, rules []int64) error

SetRules sets the list of rules for the situation evaluation

func (*PostgresRepository) Update

func (r *PostgresRepository) Update(id int64, situation Situation) error

Update updates an entity in the repository by its name

func (*PostgresRepository) UpdateTemplateInstance

func (r *PostgresRepository) UpdateTemplateInstance(instanceID int64, instance TemplateInstance) error

UpdateTemplateInstance updates a situation template instance

type Repository

type Repository interface {
	Get(id int64) (Situation, bool, error)
	GetByName(name string) (Situation, bool, error)
	Create(situation Situation) (int64, error)
	Update(id int64, situation Situation) error
	Delete(id int64) error
	GetAll() (map[int64]Situation, error)
	GetAllByIDs(ids []int64) (map[int64]Situation, error)
	GetAllByRuleID(ruleID int64) (map[int64]Situation, error)

	GetRules(id int64) ([]int64, error)
	SetRules(id int64, rules []int64) error
	AddRule(tx *sqlx.Tx, id int64, ruleID int64) error
	RemoveRule(tx *sqlx.Tx, id int64, ruleID int64) error
	GetSituationsByFactID(factID int64, ignoreIsObject bool) ([]Situation, error)
	GetFacts(id int64) ([]int64, error)

	CreateTemplateInstance(situationID int64, instance TemplateInstance) (int64, error)
	UpdateTemplateInstance(instanceID int64, instance TemplateInstance) error
	DeleteTemplateInstance(instanceID int64) error
	GetTemplateInstance(instanceID int64) (TemplateInstance, bool, error)
	GetAllTemplateInstances(situationID int64) (map[int64]TemplateInstance, error)
}

Repository is a storage interface which can be implemented by multiple backend (in-memory map, sql database, in-memory cache, file system, ...) It allows standard CRUD operation on situations

func NewPostgresRepository

func NewPostgresRepository(dbClient *sqlx.DB) Repository

NewPostgresRepository returns a new instance of PostgresRepository

func R

func R() Repository

R is used to access the global repository singleton

type Situation

type Situation struct {
	ID              int64             `json:"id,omitempty"`
	Name            string            `json:"name"`
	Facts           []int64           `json:"facts"`
	CalendarID      int64             `json:"calendarId"`
	Parameters      map[string]string `json:"parameters"`
	ExpressionFacts []ExpressionFact  `json:"expressionFacts"`
	IsTemplate      bool              `json:"isTemplate"`
	IsObject        bool              `json:"isObject"`
}

Situation is a struct used to represent a situation (or an ensemble of fact)

func (Situation) IsValid

func (s Situation) IsValid() (bool, error)

IsValid checks if an internal schedule definition is valid and has no missing mandatory fields

func (Situation) MarshalJSON

func (s Situation) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Situation as a json object

type TemplateInstance

type TemplateInstance struct {
	ID          int64             `json:"id"`
	Name        string            `json:"name"`
	SituationID int64             `json:"situationId"`
	Parameters  map[string]string `json:"parameters"`
	CalendarID  int64             `json:"calendarId"`
}

TemplateInstance is a struct used to represent a situation template instance

Jump to

Keyboard shortcuts

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