Documentation
¶
Overview ¶
Package events derives event descriptions at deploy time and runs the event worker role, which evaluates device and import data and correlates matching events (EVT-01 to EVT-08).
Index ¶
- Constants
- Variables
- func Columns(service models.Service) []string
- func DecodeLocalEvent(payload []byte, service models.Service) (map[string]any, error)
- func LocalEventTopic(localDeviceId string, localServiceId string) string
- func MessageOf(values map[string]any) map[string]any
- func ServiceOf(d model.EventDescription) (service models.Service, err error)
- type CachedDeviceRepo
- func (this *CachedDeviceRepo) GetAspectNode(ctx context.Context, id string) (models.AspectNode, error)
- func (this *CachedDeviceRepo) GetAspectNodes(ctx context.Context, ids []string) ([]models.AspectNode, error)
- func (this *CachedDeviceRepo) GetCharacteristic(id string) (models.Characteristic, error)
- func (this *CachedDeviceRepo) GetConcept(id string) (models.Concept, error)
- func (this *CachedDeviceRepo) GetConceptIdOfFunction(id string) string
- func (this *CachedDeviceRepo) GetFunction(id string) (models.Function, error)
- type Consumer
- type Correlator
- type Describer
- type DeviceMessage
- type DeviceRepo
- type Evaluation
- type Evaluator
- type Imports
- type LocalConverter
- type Marshaller
- type Message
- type MessageDesc
- type OperatorMessage
- type Pipeline
- type PipelineOperator
- type Pipelines
- type Row
- type Series
- type Timescale
- type Unmarshaller
- type Worker
- type WorkerConfig
Constants ¶
const LocalDataSegment = "data"
LocalDataSegment is the segment the payload of a local event is.
const LocalEventPrefix = "event/"
LocalEventPrefix is the topic prefix of device events on the broker of a gateway.
Variables ¶
ErrUnavailable marks an evaluation that failed because device-repository could not answer; the message is read again instead of notifying the owner, as the event-worker did.
var ErrUnexpectedType = errors.New("unexpected type")
var LocalProtocol = models.Protocol{ Id: "urn:infai:ses:protocol:f3a63aeb-187e-4dd9-9ef5-d97a6eb6292b", Name: "standard-connector", Handler: "connector", ProtocolSegments: []models.ProtocolSegment{ {Id: "urn:infai:ses:protocol-segment:9956d8b5-46fa-4381-a227-c1df69808997", Name: "metadata"}, {Id: "urn:infai:ses:protocol-segment:0d211842-cef8-41ec-ab6b-9dbc31bc3a65", Name: "data"}, }, }
LocalProtocol is the protocol of the device connectors of a gateway.
Functions ¶
func Columns ¶
Columns lists the columns timescale-tableworker creates for the outputs of a service: the names of the content variables joined by dots, lists of variable length as one column.
func DecodeLocalEvent ¶
DecodeLocalEvent turns the payload of a local event into the message of the service.
func LocalEventTopic ¶
LocalEventTopic is the topic of a service of a device on the broker of a gateway.
Types ¶
type CachedDeviceRepo ¶
type CachedDeviceRepo struct {
// contains filtered or unexported fields
}
CachedDeviceRepo gives the marshaller library characteristics, concepts, functions and aspect nodes, read through devicerepository.WithCache with ttl. The library asks for aspect nodes with a context; characteristics, concepts and functions it asks for without one, so these reads carry no trace.
func NewCachedDeviceRepo ¶
func NewCachedDeviceRepo(client devicerepository.Interface, ttl time.Duration) *CachedDeviceRepo
NewCachedDeviceRepo reads through client, which brings the token if one is needed.
func (*CachedDeviceRepo) GetAspectNode ¶
func (this *CachedDeviceRepo) GetAspectNode(ctx context.Context, id string) (models.AspectNode, error)
func (*CachedDeviceRepo) GetAspectNodes ¶
func (this *CachedDeviceRepo) GetAspectNodes(ctx context.Context, ids []string) ([]models.AspectNode, error)
GetAspectNodes reads the aspect nodes of several ids in one request, each id once; every id has to resolve, because criteria with an unknown aspect cannot be answered
func (*CachedDeviceRepo) GetCharacteristic ¶
func (this *CachedDeviceRepo) GetCharacteristic(id string) (models.Characteristic, error)
func (*CachedDeviceRepo) GetConcept ¶
func (this *CachedDeviceRepo) GetConcept(id string) (models.Concept, error)
GetConcept reads the concept with characteristic ids and conversions, which is what the marshaller needs, not with the characteristics themselves
func (*CachedDeviceRepo) GetConceptIdOfFunction ¶
func (this *CachedDeviceRepo) GetConceptIdOfFunction(id string) string
GetConceptIdOfFunction is empty if the function cannot be read; the marshaller then reports the missing conversion
func (*CachedDeviceRepo) GetFunction ¶
func (this *CachedDeviceRepo) GetFunction(id string) (models.Function, error)
type Consumer ¶
Consumer reads topics until ctx is done; handle returning an error means the message is not acknowledged and read again.
func LocalConsumer ¶
LocalConsumer reads the device events of the broker of a gateway (FOG-06). It subscribes to all events once, like the event-worker in fog mode; messages without a description are dropped by the worker. A restart of the consumer only replaces the handler, so that no unsubscribe of the previous consumer can race with the subscription of the next.
MQTT has no offsets: a message gets a unique number, so that message ids stay apart (EVT-05). A message that cannot be processed is logged and acknowledged anyway, because an unacknowledged message would block the delivery of later ones.
type Correlator ¶
type Describer ¶
type Describer struct {
// contains filtered or unexported fields
}
Describer derives the event descriptions of a deployment version (DEP-07), following event-deployment v0.2.0: a device group becomes device × matching service, a device without service all its matching services, an import one description with an artificial service.
func NewDescriber ¶
func NewDescriber(iot devicerepository.Interface, cacheTimeout time.Duration, tokens auth.UserTokenSource, imports Imports) *Describer
NewDescriber reads devices, device types, device groups and aspect nodes through iot, cached per user for cacheTimeout, with the tokens of the owners.
func (*Describer) Describe ¶
func (this *Describer) Describe(ctx context.Context, deployment model.Deployment, version int, process *bpmn.Process, selections map[string]model.Selection) (result []model.EventDescription, groups []string, err error)
Describe returns the descriptions and the device groups they depend on.
func (*Describer) Local ¶
Local makes the describer describe events for a gateway: the topic is event/{local device id}/{local service id}, and imports are not available.
func (*Describer) WithPipelines ¶ added in v0.35.0
WithPipelines lets deployments wait for the output of analytics operators (SNRGY-5113).
type DeviceMessage ¶
type DeviceMessage struct {
DeviceId string `json:"device_id"`
ServiceId string `json:"service_id"`
Value map[string]any `json:"value"`
}
DeviceMessage is a message of platform-connector on a device data topic: the value of a service, by protocol segment, as the service describes it.
type DeviceRepo ¶
type DeviceRepo interface {
GetCharacteristic(id string) (characteristic models.Characteristic, err error)
GetConcept(id string) (concept models.Concept, err error)
GetConceptIdOfFunction(id string) string
// GetAspectNode is used by the aspect distance sorting of the marshaller, which walks the
// tree one node at a time
GetAspectNode(ctx context.Context, id string) (models.AspectNode, error)
GetAspectNodes(ctx context.Context, ids []string) ([]models.AspectNode, error)
}
DeviceRepo is the read access the unmarshalling needs to device-repository.
type Evaluation ¶
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator applies the evaluation of the event-worker: unmarshalling, then the condition script with the value and the variables. The debug reconstruction uses the same code (EVT-10).
func NewEvaluator ¶
func NewEvaluator(unmarshaller Unmarshaller) *Evaluator
func (*Evaluator) Evaluate ¶
func (this *Evaluator) Evaluate(d model.EventDescription, message map[string]any) (result Evaluation, err error)
func (*Evaluator) WithBlocklist ¶ added in v0.31.0
WithBlocklist refuses to evaluate conditions that ended the engine (EXPR-03).
func (*Evaluator) WithCrashBrake ¶ added in v0.30.0
func (this *Evaluator) WithCrashBrake(brake *crashbrake.Brake) *Evaluator
WithCrashBrake records each condition as running in the brake, so that one that ends the process is suspended at the next start instead of running again (EXPR-03).
type Imports ¶
type Imports interface {
Instance(token string, id string) (models.Import, error)
Type(token string, id string) (models.ImportType, error)
}
Imports reads import instances and types with the token of the deployment owner.
func NewImports ¶
NewImports reads from import-deploy and import-repository.
type LocalConverter ¶ added in v0.24.0
type LocalConverter struct {
// contains filtered or unexported fields
}
LocalConverter converts in the process, where the marshaller service calls the converter service; it sends no request, so it has no use for the context of the marshaller library.
func NewLocalConverter ¶ added in v0.24.0
func NewLocalConverter() (LocalConverter, error)
func (LocalConverter) CastWithExtension ¶ added in v0.24.0
func (this LocalConverter) CastWithExtension(_ context.Context, in any, from string, to string, extensions []models.ConverterExtension) (any, error)
type Marshaller ¶
type Marshaller struct {
// contains filtered or unexported fields
}
Marshaller converts device and import messages into the characteristic of an event as the event-worker did (EVT-04).
func NewMarshaller ¶
func NewMarshaller(repo DeviceRepo) (*Marshaller, error)
func (*Marshaller) Unmarshal ¶
func (this *Marshaller) Unmarshal(desc MessageDesc) (value any, err error)
type MessageDesc ¶
type MessageDesc struct {
Service models.Service
CharacteristicId string
FunctionId string
AspectIds []string
// a path set at deploy time wins over the search by function and aspects
Path string
Message map[string]any
}
MessageDesc is what the unmarshalling needs of an event description and the message it reads.
type OperatorMessage ¶ added in v0.35.0
type OperatorMessage struct {
PipelineId string `json:"pipeline_id" description:"id of the pipeline in analytics-pipeline"`
OperatorId string `json:"operator_id" description:"id of the operator in the pipeline"`
Analytics map[string]any `json:"analytics" description:"outputs of the operator; the local variable event of the correlation"`
}
OperatorMessage is a message of an analytics operator on its output topic: the outputs of the operator under "analytics", with the pipeline and the operator that wrote them (SNRGY-5113).
type Pipeline ¶ added in v0.35.0
type Pipeline struct {
Id string `json:"id"`
UserId string `json:"userId"`
Operators []PipelineOperator `json:"operators"`
}
Pipeline is the part of an analytics-pipeline pipeline the describer needs.
type PipelineOperator ¶ added in v0.35.0
type PipelineOperator struct {
// the id the operator writes as operator_id into its messages
Id string `json:"id"`
Name string `json:"name"`
// cloud or local; the output of a local operator reaches Kafka only if it is forwarded
DeploymentType string `json:"deploymentType"`
// analytics-<operator name> in the cloud, shared by every pipeline with this operator
OutputTopic string `json:"outputTopic"`
}
type Pipelines ¶ added in v0.35.0
Pipelines reads analytics pipelines with the token of the deployment owner (SNRGY-5113).
func NewPipelines ¶ added in v0.35.0
NewPipelines reads from analytics-pipeline, e.g. http://pipeline.analytics:8000.
type Series ¶
type Series interface {
// Range returns rows after from and before to in ascending time, at most limit
Range(ctx context.Context, token string, deviceId string, serviceId string, columns []string, from time.Time, to time.Time, limit int) ([]Row, error)
// Last returns the newest row; nil if there is none
Last(ctx context.Context, token string, deviceId string, serviceId string, columns []string) (*Row, error)
}
Series reads stored device messages, the source of the reconstruction of event evaluations (EVT-10) and of the current value of a waiting event (DBG-18).
type Timescale ¶
type Timescale struct {
// contains filtered or unexported fields
}
Timescale reads rows through timescale-wrapper /queries with the token of the owner.
func NewTimescale ¶
type Unmarshaller ¶
type Unmarshaller interface {
Unmarshal(desc MessageDesc) (value any, err error)
}
Unmarshaller converts a device or import message into the characteristic of the event; the Marshaller does this as the event-worker did (EVT-04).
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker is the event worker role: it holds the descriptions in memory, subscribes to exactly the topics they name, evaluates every message against the descriptions of its device and service or import, and correlates triggered events in the process (EVT-01 to EVT-08).
func NewWorker ¶
func NewWorker(config WorkerConfig, s *store.Store, evaluator *Evaluator, correlator Correlator, consume Consumer, logger *slog.Logger) *Worker
func (*Worker) Handle ¶
Handle evaluates a message against its descriptions; qos 1 descriptions are evaluated and correlated before the message is acknowledged, qos 0 ones afterwards (EVT-05).
type WorkerConfig ¶
type WorkerConfig struct {
// messages older than this are dropped (EVT-06)
MaxMessageAge time.Duration
// how often changed descriptions are read, and how often all of them
SyncInterval time.Duration
FullReloadInterval time.Duration
// a topic no description names any more stays subscribed this long, so that deployments
// created and deleted on the same topic do not replace the consumer every time: each
// replacement is a rebalance of the whole consumer group
TopicGrace time.Duration
// at most one notification per description in this window (EVT-07)
ErrorNotificationWindow time.Duration
// concurrent evaluations of qos 0 descriptions
AsyncWorkers int
// deployments whose qos 1 descriptions of a message are handled at the same time; each holds a
// database connection while it correlates. 1 handles them one after the other.
CorrelationWorkers int
// on a gateway: descriptions carry the topic event/{local device}/{local service}, and
// messages are the raw payload of the device (FOG-06)
Local bool
}
func DefaultWorkerConfig ¶
func DefaultWorkerConfig() WorkerConfig