arngin

package module
v0.0.0-...-47c2dae Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 3 Imported by: 0

README

play-arngin

Addon rule engine playground.

Setup

First install Go v1.13 or above. We use Go modules for building the binary without relying on GOPATH. And errors are wrapped using the new %w verb(just cause).

The dependencies can be installed by running go get but this is optional since go build is module aware and will fetch dependencies if required.

Build

$ go build ./cmd/arngin

Usage

$ ./arngin
usage: arngin [<flags>] <command> [<args> ...]

Addon rule engine playground.

Flags:
  -h, --help                     Show context-sensitive help (also try --help-long and --help-man).
  -e, --engines=elastic ...      Addon rule engines to run command against. Accepted values - opa, arango, elastic, all.
      --arango-url="http://localhost:8529"
                                 Endpoint for ArangoDB instance.
      --arango-db-name="arngin"  Database name for ArangoDB.
      --arango-username=ARANGO-USERNAME
                                 Username for ArangoDB instance.
      --arango-password=ARANGO-PASSWORD
                                 Password for ArangoDB instance.
      --elastic-url="http://localhost:9200"
                                 Elasticsearch server URL.

Commands:
  help [<command>...]
    Show help.

  load [<flags>]
    Load addon rules into the rule engine(s). Addon rules are generated randomly.

  query [<flags>]
    Run queries against the rule engine(s). Queries are generated randomly.

Engines

The playground has multiple implementations of the Engine interface with the objective of comparing the performance of loading rules and running queries. with the following:

  • engine/opa: Open Policy Agent is an open source, general-purpose policy engine that unifies policy enforcement across the stack. The rules and match criteria are expressed in Rego, OPA’s policy language.
  • engine/arango: ArangoDB is a multi-model, open-source database with flexible data models for documents, graphs, and key-values. Rules are stored in ArangoDB and AQL is used to match the rules against the query.
  • engine/elasticsearch: Elasticsearch is a distributed, RESTful search and analytics engine. Rules are stored as documents and the Elasticsearch query DSL and aggregations are used to match the rules against the query.
Examples
# Load 1000 rules into all engines, use default endpoints
$ ./arngin load --engines all --count 10000 --arango-username testuser --arango-password testpass --arango-db-name arnginTest
Loaded 1000 rules into opa [took: 9.034712154s]
Loaded 1000 rules into arango [took: 109.521924ms]
Loaded 1000 rules into elastic [took: 848.229276ms]

# Run 100 queries with a concurrency of 4 against opa
$ ./arngin query --engines opa --count 100 --concurrency 4
Ran 100 queries with concurrency of 4 against opa [avg: 362.49385ms]

# Run 1000 queries with a concurrency of 10 against arango and elastic
$ ./arngin query -e arango -e elastic --count 1000 --concurrency 10 --arango-username testuser --arango-password testpass --arango-db-name arnginTest
Ran 1000 queries with concurrency of 10 against arango [avg: 82.168104ms]
Ran 1000 queries with concurrency of 10 against elastic [avg: 7.499732ms]

Documentation

Index

Constants

View Source
const (
	Opa     = "opa"
	Arango  = "arango"
	Elastic = "elastic"
)

Engine names

Variables

This section is empty.

Functions

This section is empty.

Types

type AddonRule

type AddonRule struct {
	ID           uuid.UUID   `json:"id"`
	Sources      []int       `json:"sources,omitempty"`
	Destinations []int       `json:"destinations,omitempty"`
	BoardingPts  []int       `json:"boardingPts,omitempty"`
	DroppingPts  []int       `json:"droppingPts,omitempty"`
	BoardingTime *FloatRange `json:"boardingTime,omitempty"`
	DroppingTime *FloatRange `json:"droppingTime,omitempty"`
	SeatCount    *NumRule    `json:"seatCount,omitempty"`
	BusOperators []string    `json:"busOperators,omitempty"`
	Duration     *NumRule    `json:"duration,omitempty"`
	Appversion   *NumRule    `json:"appversion,omitempty"`
	Channels     []string    `json:"channels,omitempty"`
	Addons       []string    `json:"addons"`
}

AddonRule is composed of optional rule attributes and the list of addon IDs.

type AddonsQ

type AddonsQ struct {
	Source       int     `json:"source"`
	Destination  int     `json:"destination"`
	BoardingPt   int     `json:"boardingPt"`
	DroppingPt   int     `json:"droppingPt"`
	BoardingTime float64 `json:"boardingTime"`
	DroppingTime float64 `json:"droppingTime"`
	Seats        int     `json:"seats"`
	BusOperator  string  `json:"busOperator"`
	Duration     int     `json:"duration"`
	Appversion   int     `json:"appversion"`
	Channel      string  `json:"channel"` // One of WEB_DIRECT, MOBILE_APP, MOBILE_WEB
}

AddonsQ is the input which needs to be matched against addon rules.

type CompareOp

type CompareOp string

CompareOp is one of equal to, greater than equal to and less than or equal to.

const (
	EqOp  CompareOp = "eq"
	GTEOp CompareOp = "gte"
	LTEOp CompareOp = "lte"
)

Compare operators

type Engine

type Engine interface {
	// LoadRules loads the rules into the engine in preparation for
	// running the queries.
	LoadRules(ctx context.Context, rules []AddonRule) error

	// RunQuery matches the query against the addon rules loaded
	// into the rule engine.
	RunQuery(ctx context.Context, q AddonsQ) ([]string, error)
}

Engine describes the functionality of an addon rule engine.

type Errors

type Errors []error

Errors is an alias for slice of errors which satisfies the error interface.

func (Errors) Error

func (errs Errors) Error() string

type FloatRange

type FloatRange struct {
	GTE float64 `json:"gte"`
	LTE float64 `json:"lte"`
}

FloatRange is a range of floating point numbers that the input is expected to match against.

type NumRule

type NumRule struct {
	Op    CompareOp `json:"op" validate:"required,oneof=eq lte gte"`
	Value int       `json:"value" validate:"required,gte=0"`
}

NumRule defines the relation and value to compare against for the input.

Directories

Path Synopsis
cmd
engine
opa

Jump to

Keyboard shortcuts

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