rules

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

Rules

Rules service provides a rule engine that evaluates conditions on incoming messages and triggers actions such as alarms, SMTP notifications, or SMPP notifications.

Configuration

The service is configured using the environment variables presented in the following table. Note that any unset variables will be replaced with their default values.

Variable Description Default
MF_RULES_LOG_LEVEL Log level for the Rules service (debug, info, warn, error) error
MF_BROKER_URL Message broker instance URL nats://localhost:4222
MF_RULES_HTTP_PORT Rules service HTTP port 9027
MF_JAEGER_URL Jaeger server URL
MF_RULES_DB_HOST Database host address localhost
MF_RULES_DB_PORT Database host port 5432
MF_RULES_DB_USER Database user mainflux
MF_RULES_DB_PASS Database password mainflux
MF_RULES_DB Name of the database used by the service rules
MF_RULES_DB_SSL_MODE Database connection SSL mode (disable, require, verify-ca, verify-full) disable
MF_RULES_DB_SSL_CERT Path to the PEM encoded certificate file
MF_RULES_DB_SSL_KEY Path to the PEM encoded key file
MF_RULES_DB_SSL_ROOT_CERT Path to the PEM encoded root certificate file
MF_RULES_CLIENT_TLS Flag that indicates if TLS should be turned on false
MF_RULES_CA_CERTS Path to trusted CAs in PEM format
MF_RULES_SERVER_CERT Path to server certificate in PEM format
MF_RULES_SERVER_KEY Path to server key in PEM format
MF_THINGS_AUTH_GRPC_URL Things service Auth gRPC URL localhost:8183
MF_THINGS_AUTH_GRPC_TIMEOUT Things service Auth gRPC request timeout in seconds 1s
MF_RULES_ES_URL Event store URL redis://localhost:6379/0
MF_RULES_EVENT_CONSUMER Event store consumer name rules

Deployment

The service itself is distributed as Docker container. Check the rules service section in docker-compose to see how service is deployed.

To start the service, execute the following shell script:

# download the latest version of the service
git clone https://github.com/MainfluxLabs/mainflux

cd mainflux

# compile the rules service
make rules

# copy binary to bin
make install

# Set the environment variables and run the service
MF_RULES_LOG_LEVEL=[Rules log level] \
MF_BROKER_URL=[Message broker instance URL] \
MF_RULES_HTTP_PORT=[Rules service HTTP port] \
MF_RULES_DB_HOST=[Database host address] \
MF_RULES_DB_PORT=[Database host port] \
MF_RULES_DB_USER=[Database user] \
MF_RULES_DB_PASS=[Database password] \
MF_RULES_DB=[Rules database name] \
MF_THINGS_AUTH_GRPC_URL=[Things service Auth gRPC URL] \
MF_THINGS_AUTH_GRPC_TIMEOUT=[Things service Auth gRPC request timeout] \
$GOBIN/mainfluxlabs-rules

Usage

Starting the service will begin consuming messages from the message broker. Each incoming message is evaluated against the rules assigned to the publishing thing. When all conditions of a rule are met (using AND or OR logic), the service publishes an action message to the appropriate subject — triggering alarms, SMTP notifications, or SMPP notifications. For more information about service capabilities and its usage, please check out the API documentation.

Documentation

Index

Constants

View Source
const (
	ActionTypeSMTP  = "smtp"
	ActionTypeSMPP  = "smpp"
	ActionTypeAlarm = "alarm"

	OperatorAND = "AND"
	OperatorOR  = "OR"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	ID   string `json:"id"`
	Type string `json:"type"`
}

type Condition

type Condition struct {
	Field      string   `json:"field"`
	Comparator string   `json:"comparator"`
	Threshold  *float64 `json:"threshold"`
}

type Rule

type Rule struct {
	ID          string
	GroupID     string
	Name        string
	Description string
	Conditions  []Condition
	Operator    string
	Actions     []Action
}

type RuleRepository

type RuleRepository interface {
	// Save persists multiple rules. Rules are saved using a transaction.
	// If one rule fails then none will be saved.
	// Successful operation is indicated by a non-nil error response.
	Save(ctx context.Context, rules ...Rule) ([]Rule, error)

	// RetrieveByID retrieves a rule having the provided ID.
	RetrieveByID(ctx context.Context, id string) (Rule, error)

	// RetrieveByThing retrieves rules assigned to a certain thing,
	// identified by a given thing ID.
	RetrieveByThing(ctx context.Context, thingID string, pm apiutil.PageMetadata) (RulesPage, error)

	// RetrieveByGroup retrieves rules related to a certain group,
	// identified by a given group ID.
	RetrieveByGroup(ctx context.Context, groupID string, pm apiutil.PageMetadata) (RulesPage, error)

	// RetrieveThingIDsByRule retrieves all thing IDs that have the given rule assigned.
	RetrieveThingIDsByRule(ctx context.Context, ruleID string) ([]string, error)

	// Update performs an update to the existing rule.
	// A non-nil error is returned to indicate operation failure.
	Update(ctx context.Context, r Rule) error

	// Remove removes rules having the provided IDs.
	Remove(ctx context.Context, ids ...string) error

	// RemoveByGroup removes rules related to a certain group,
	// identified by a given group ID.
	RemoveByGroup(ctx context.Context, groupID string) error

	// Assign assigns rules to the specified thing.
	Assign(ctx context.Context, thingID string, ruleIDs ...string) error

	// Unassign removes specific rule assignments from a given thing.
	Unassign(ctx context.Context, thingID string, ruleIDs ...string) error

	// UnassignByThing removes all rule assignments for a certain thing,
	// identified by a given thing ID.
	UnassignByThing(ctx context.Context, thingID string) error
}

type RulesPage

type RulesPage struct {
	Total uint64
	Rules []Rule
}

type Service

type Service interface {
	// CreateRules creates rules.
	CreateRules(ctx context.Context, token, groupID string, rules ...Rule) ([]Rule, error)

	// ListRulesByThing retrieves a paginated list of rules by thing.
	ListRulesByThing(ctx context.Context, token, thingID string, pm apiutil.PageMetadata) (RulesPage, error)

	// ListRulesByGroup retrieves a paginated list of rules by group.
	ListRulesByGroup(ctx context.Context, token, groupID string, pm apiutil.PageMetadata) (RulesPage, error)

	// ListThingIDsByRule retrieves a list of thing IDs attached to the given rule ID.
	ListThingIDsByRule(ctx context.Context, token, ruleID string) ([]string, error)

	// ViewRule retrieves a specific rule by its ID.
	ViewRule(ctx context.Context, token, id string) (Rule, error)

	// UpdateRule updates the rule identified by the provided ID.
	UpdateRule(ctx context.Context, token string, rule Rule) error

	// RemoveRules removes the rules identified with the provided IDs.
	RemoveRules(ctx context.Context, token string, ids ...string) error

	// RemoveRulesByGroup removes the rules identified with the provided IDs.
	RemoveRulesByGroup(ctx context.Context, groupID string) error

	// AssignRules assigns rules to a specific thing.
	AssignRules(ctx context.Context, token, thingID string, ruleIDs ...string) error

	// UnassignRules removes rule assignments from a specific thing.
	UnassignRules(ctx context.Context, token, thingID string, ruleIDs ...string) error

	// UnassignRulesByThing removes all rule assignments from a specific thing.
	UnassignRulesByThing(ctx context.Context, thingID string) error

	consumers.Consumer
}

Service specifies an API that must be fullfiled by the domain service implementation, and all of its decorators (e.g. logging & metrics). All methods that accept a token parameter use it to identify and authorize the user performing the operation.

func New

New instantiates the rules service implementation.

Directories

Path Synopsis
api

Jump to

Keyboard shortcuts

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