rules

package
v0.2.20 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 12 Imported by: 0

README

Rules

Function signatures:

func action(ctx *rules.Context, args...) (interface{}, error) {}
func query(ctx *rules.Context, args...) (interface{}, error) {}

Documentation

Index

Constants

View Source
const (
	CONNECTOR_AND string = "&&"
	CONNECTOR_OR  string = "||"

	COMPARER_EQUAL            string = "=="
	COMPARER_NOT_EQUAL        string = "!="
	COMPARER_GREATER          string = ">"
	COMPARER_GREATER_OR_EQUAL string = ">="
	COMPARER_LESS             string = "<"
	COMPARER_LESS_OR_EQUAL    string = "<="
	COMPARER_BETWEEN          string = "<>"
	COMPARER_EXISTS           string = "!!"
	COMPARER_NOT_SET          string = "null"
	COMPARER_IN               string = "in"
	COMPARER_NOT_IN           string = "nin"
	COMPARER_MATCH            string = "match"
	COMPARER_MATCH_NOT        string = "dontmatch"
	COMPARER_ANY              string = "any"
	COMPARER_ALL              string = "all"
	COMPARER_NONE             string = "none"

	PARAMTYPE_STRING   string = "string"
	PARAMTYPE_NUMBER   string = "number"
	PARAMTYPE_FLOAT    string = "float"
	PARAMTYPE_FRACTION string = "fraction"
	PARAMTYPE_BOOL     string = "bool"
	PARAMTYPE_DATE     string = "date"
	PARAMTYPE_DATETIME string = "datetime"
	PARAMTYPE_EVENT    string = "eventType"
	PARAMTYPE_DURATION string = "duration"
	PARAMTYPE_FIELDS   string = "eventFields"

	RETURNTYPE_ARRAY string = "array"

	AGGREGATE_AVARAGE string = "avg"
	AGGREGATE_TOTAL   string = "sum"
	AGGREGATE_MINIMUM string = "min"
	AGGREGATE_MAXIMUM string = "max"
)
View Source
const (
	EventPrefix = "rules."
	Status      = EventPrefix + "status"
	CmdReload   = EventPrefix + "reload"
	CmdStop     = EventPrefix + "stop"
	CmdResume   = EventPrefix + "resume"
)
View Source
const (
	DRY_RUN_KEY        = "dryRun"
	ON_FAIL_DEACTIVATE = 0
	ON_FAIL_KEEPACTIVE = 1
	ON_UNMET_NOOP      = 0
	ON_UNMET_LOG       = 1
	ON_MET_NOOP        = 0
	ON_MET_LOG         = 1
)

Variables

View Source
var (
	// using an empty struct{} here has the advantage that it doesn't require any additional space
	// and go's internal map type is optimized for that kind of values
	// therefore map[string]struct{} is a popular choice for sets in the go world
	CommandTopics = map[string]struct{}{
		CmdReload: {},
		CmdStop:   {},
		CmdResume: {},
	}
)

Functions

func CompareFloat

func CompareFloat(a, b float64, c string) bool

func ComparerTypeName

func ComparerTypeName(condition *Condition) string

func ConnectCondition

func ConnectCondition(res bool, eval bool, connector string) bool

func GetDynamicParamValue

func GetDynamicParamValue(ctx *Context, value string) string

func RunAction

func RunAction(funcs ValueFuncs, ctx *Context, action *Action, dryRun bool) error

Types

type Action

type Action struct {
	Method     string       `json:"method"`
	Parameters []*Parameter `json:"parameters"`
	Alias      string       `json:"alias"`
}

type ActionTrigger

type ActionTrigger struct {
	Action Action                 `json:"action"`
	DryRun bool                   `json:"dryRun"`
	Event  map[string]interface{} `json:"event"`
}

type Condition

type Condition struct {
	Field     string       `json:"field"`
	Comparer  string       `json:"comparer"`
	Connector string       `json:"connector"`
	Value     string       `json:"value"`
	Type      string       `json:"type"`
	Items     []*Condition `json:"items"`
	Method    string       `json:"method"`
	Alias     string       `json:"alias"`
	IsDynamic bool         `json:"isDynamic"`
}

func EvaluateConditions

func EvaluateConditions(ctx *Context, ds interface{}, conditions []*Condition) (bool, *Condition)

type Context

type Context struct {
	Facts     Facts  `json:"facts"`
	Spans     Spans  `json:"spans,omitempty"`
	RuleID    string `json:"ruleId"`
	RuleTitle string `json:"ruleTitle"`
}

func NewContext

func NewContext(eventData interface{}) *Context

func (*Context) GetFact

func (ctx *Context) GetFact(path string) gjson.Result

func (*Context) NewSpan

func (ctx *Context) NewSpan(name string, args []*Parameter) *Span

func (*Context) SetFact

func (ctx *Context) SetFact(path string, value interface{}) (err error)

type Criteria

type Criteria struct {
	DataSource   *DataSource  `json:"datasource"`
	Parameters   []*Parameter `json:"parameters"`
	Returns      string       `json:"returns"`
	Connector    string       `json:"connector"`
	Items        []*Criteria  `json:"items"`
	Conditions   []*Condition `json:"conditions"`
	Filters      []*Condition `json:"filters"`
	Aggregations []*Condition `json:"aggregations"`
}

type DataSource

type DataSource struct {
	ID    string `json:"id"`
	Key   string `json:"key"`
	Alias string `json:"alias"`
}

type DefaultEvaluator

type DefaultEvaluator struct{}

wrapper to fulfill evaluator interface

func (*DefaultEvaluator) EvaluateRule

func (e *DefaultEvaluator) EvaluateRule(funcs ValueFuncs, ctx *Context, rule *Rule, event *Event) *EvalResult

type EvalResult

type EvalResult struct {
	Rule        *Rule         `json:"rule"`
	Event       *Event        `json:"event"`
	Context     *Context      `json:"context"`
	Met         bool          `json:"met"`
	Unmet       bool          `json:"unmet"`
	MetReason   string        `json:"-"`
	UnmetReason string        `json:"-"`
	Error       error         `json:"-"`
	Duration    time.Duration `json:"-"`
}

func (*EvalResult) MarshalJSON

func (e *EvalResult) MarshalJSON() ([]byte, error)

type Evaluator

type Evaluator interface {
	EvaluateRule(funcs ValueFuncs, ctx *Context, rule *Rule, event *Event) *EvalResult
}

evaluator interface to support swappable implementations

type Event

type Event struct {
	Received  time.Time `json:"received"`
	Processed time.Time `json:"processed"`
	DryRun    bool      `json:"dryRun"`
	Topic     string    `json:"topic"`
	Data      []byte    `json:"data"`
}

type Facts

type Facts []byte

func (Facts) Get

func (f Facts) Get(path string) gjson.Result

func (Facts) Map

func (f Facts) Map() map[string]interface{}

func (Facts) MarshalJSON

func (f Facts) MarshalJSON() ([]byte, error)

func (*Facts) Set

func (f *Facts) Set(path string, value interface{}) (err error)

func (Facts) String

func (f Facts) String() string

func (*Facts) UnmarshalJSON

func (f *Facts) UnmarshalJSON(data []byte) error

type FuncMap

type FuncMap map[string]interface{}

type Parameter

type Parameter struct {
	Type      string `json:"type"`
	Value     string `json:"value"`
	IsDynamic bool   `json:"isDynamic"`
}

type Rule

type Rule struct {
	ID           string                 `json:"id"`
	Archive      bool                   `json:"archive"`
	Active       bool                   `json:"active"`
	OnUnmet      int                    `json:"onUnmet"`
	OnMet        int                    `json:"onMet"`
	OnFail       int                    `json:"onFail"`
	Title        string                 `json:"title"`
	Desc         string                 `json:"desc"`
	Tags         string                 `json:"tags"`
	Created      time.Time              `json:"created"`
	Updated      time.Time              `json:"updated"`
	Event        string                 `json:"event"`
	Criterias    []*Criteria            `json:"criterias"`
	NoCriterias  bool                   `json:"noCriterias"`
	Chainable    bool                   `json:"chainable"`
	Actions      []*Action              `json:"actions"`
	UnmetActions []*Action              `json:"unmetActions"`
	Scheduled    bool                   `json:"scheduled"`
	RunAt        string                 `json:"runat"`
	Cron         string                 `json:"cron"`
	CreatedBy    string                 `json:"createdBy"`
	UpdatedBy    string                 `json:"updatedBy"`
	ValidFrom    time.Time              `json:"validFrom"`
	ValidTo      time.Time              `json:"validTo"`
	Changes      string                 `json:"changes"`
	Metadata     map[string]interface{} `json:"metadata"`
}

type Span

type Span struct {
	Name     string       `json:"-"`
	Args     []*Parameter `json:"-"`
	Called   string       `json:"called"`
	Duration string       `json:"duration"`
	// contains filtered or unexported fields
}

func (*Span) Finish

func (s *Span) Finish()

type Spans

type Spans []*Span

type ValueFuncs

type ValueFuncs map[string]reflect.Value

func MakeValueFuncs

func MakeValueFuncs(funcs FuncMap) ValueFuncs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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