graph

package
v0.13.4 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2023 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const UpdaterLoggerTag = "GRAPH_UPDATER"

Variables

This section is empty.

Functions

func Walk

func Walk(fn func(Node), nodes ...Node)

Walk walks through the graph recursively and calls the given function for each node.

Types

type DevCircuitBreakerNode

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

DevCircuitBreakerNode is a circuit breaker that tips if the value deviation between two nodes is greater than the breaker value.

It must have three nodes. First node is the value, second is the reference value and third is the threshold value.

The value deviation is calculated as: abs(1.0 - (reference / value))

All nodes must return a value that implements the data.NumericValue interface.

func NewDevCircuitBreakerNode

func NewDevCircuitBreakerNode() *DevCircuitBreakerNode

NewDevCircuitBreakerNode creates a new DevCircuitBreakerNode instance.

func (*DevCircuitBreakerNode) AddNodes

func (n *DevCircuitBreakerNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

If more than three nodes are added, an error is returned.

func (*DevCircuitBreakerNode) DataPoint

func (n *DevCircuitBreakerNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*DevCircuitBreakerNode) Meta

func (n *DevCircuitBreakerNode) Meta() map[string]any

Meta implements the Node interface.

func (*DevCircuitBreakerNode) Nodes

func (n *DevCircuitBreakerNode) Nodes() []Node

Nodes implements the Node interface.

type ErrModelNotFound

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

func (ErrModelNotFound) Error

func (e ErrModelNotFound) Error() string

type MapMeta

type MapMeta map[string]any

MapMeta is a map that contains meta information as a key-value pairs.

func (MapMeta) Map

func (m MapMeta) Map() map[string]any

Map implements the data.Meta interface.

type Node

type Node interface {
	// Nodes returns a list of nodes that are connected to the current node.
	Nodes() []Node

	// AddNodes adds nodes to the current node.
	AddNodes(...Node) error

	// DataPoint returns a data point for the current node. Leaf nodes return
	// a data point from a data source, while other nodes return a data point
	// calculated from the data points of connected nodes.
	DataPoint() datapoint.Point

	// Meta returns a map that contains meta information about the node used
	// to debug price models. It should not contain data that is accessible
	// from node's methods.
	Meta() map[string]any
}

Node represents a node in a graph.

A node can use data from connected nodes to calculate its own data or it can return data from a data source.

func DetectCycle

func DetectCycle(node Node) []Node

DetectCycle returns a cycle path in the given graph if a cycle is detected, otherwise returns an empty slice.

type OriginNode

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

OriginNode is a node that provides a data point for a specific origin.

func NewOriginNode

func NewOriginNode(
	origin string,
	query any,
	freshnessThreshold time.Duration,
	expiryThreshold time.Duration,
) *OriginNode

NewOriginNode creates a new OriginNode instance.

The query argument is used to fetch a data point from the origin.

The freshnessThreshold and expiryThreshold arguments are used to determine whether a data point is fresh or expired.

A data point is considered fresh if it was updated within the freshnessThreshold duration. In this case, the data point update is not required.

A data point is considered expired if it was updated more than the expiryThreshold duration ago. In this case, the data point is considered invalid and an update is required.

There must be a gap between the freshnessThreshold and expiryThreshold so that a data point will be updated before it is considered expired.

func (*OriginNode) AddNodes

func (n *OriginNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

func (*OriginNode) DataPoint

func (n *OriginNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*OriginNode) IsExpired

func (n *OriginNode) IsExpired() bool

IsExpired returns true if the price is considered expired.

func (*OriginNode) IsFresh

func (n *OriginNode) IsFresh() bool

IsFresh returns true if the price is considered fresh, that is, the price update is not required.

Note, that the price that is not fresh is not necessarily expired.

func (*OriginNode) Meta

func (n *OriginNode) Meta() map[string]any

Meta implements the Node interface.

func (*OriginNode) Nodes

func (n *OriginNode) Nodes() []Node

Nodes implements the Node interface.

func (*OriginNode) Origin

func (n *OriginNode) Origin() string

Origin returns the origin name.

func (*OriginNode) Query

func (n *OriginNode) Query() any

Query returns the query used to fetch the data point.

func (*OriginNode) SetDataPoint

func (n *OriginNode) SetDataPoint(point datapoint.Point) error

SetDataPoint sets the data point.

If the provided data point is older than the current data point, it will be ignored and an error will be returned.

If the provided data point is invalid, it will be ignored as long as the current data point is still valid. This is done to prevent from updating valid data points with invalid ones. In this case, the error will be returned.

type Provider

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

Provider is a data provider which uses a graph structure to provide data points.

func NewProvider

func NewProvider(models map[string]Node, updater *Updater) Provider

NewProvider creates a new price data.

Models are map of data models graphs keyed by their data model name.

Updater is an optional updater which will be used to update the data models before returning the data point.

func (Provider) DataPoint

func (p Provider) DataPoint(ctx context.Context, model string) (datapoint.Point, error)

DataPoint implements the data.Provider interface.

func (Provider) DataPoints

func (p Provider) DataPoints(ctx context.Context, models ...string) (map[string]datapoint.Point, error)

DataPoints implements the data.Provider interface.

func (Provider) Model

func (p Provider) Model(_ context.Context, model string) (datapoint.Model, error)

Model implements the data.Provider interface.

func (Provider) ModelNames

func (p Provider) ModelNames(_ context.Context) []string

ModelNames implements the data.Provider interface.

func (Provider) Models

func (p Provider) Models(_ context.Context, models ...string) (map[string]datapoint.Model, error)

Models implements the data.Provider interface.

type ReferenceNode

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

ReferenceNode is a node that references another node.

func NewReferenceNode

func NewReferenceNode() *ReferenceNode

NewReferenceNode creates a new ReferenceNode instance.

func (*ReferenceNode) AddNodes

func (n *ReferenceNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

func (*ReferenceNode) DataPoint

func (n *ReferenceNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*ReferenceNode) Meta

func (n *ReferenceNode) Meta() map[string]any

Meta implements the Node interface.

func (*ReferenceNode) Nodes

func (n *ReferenceNode) Nodes() []Node

Nodes implements the Node interface.

type TickAliasNode

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

TickAliasNode is a node that aliases another tick's asset pair.

It expects one node that returns a data point with an value.Tick value.

func NewTickAliasNode

func NewTickAliasNode(alias value.Pair) *TickAliasNode

NewTickAliasNode creates a new TickAliasNode instance.

func (*TickAliasNode) AddNodes

func (n *TickAliasNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

Only one node is allowed. If more than one node is added, an error is returned.

func (*TickAliasNode) DataPoint

func (n *TickAliasNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*TickAliasNode) Meta

func (n *TickAliasNode) Meta() map[string]any

Meta implements the Node interface.

func (*TickAliasNode) Nodes

func (n *TickAliasNode) Nodes() []Node

Nodes implements the Node interface.

type TickIndirectNode

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

TickIndirectNode is a node that calculates cross rate from the list of price ticks from its nodes.

It expects that all nodes return data points with value.Tick values.

The order of nodes is important because prices are calculated from first to last. Adjacent nodes must have one common asset.

func NewTickIndirectNode

func NewTickIndirectNode() *TickIndirectNode

NewTickIndirectNode creates a new TickIndirectNode instance.

func (*TickIndirectNode) AddNodes

func (n *TickIndirectNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

func (*TickIndirectNode) DataPoint

func (n *TickIndirectNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*TickIndirectNode) Meta

func (n *TickIndirectNode) Meta() map[string]any

Meta implements the Node interface.

func (*TickIndirectNode) Nodes

func (n *TickIndirectNode) Nodes() []Node

Nodes implements the Node interface.

type TickInvertNode

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

TickInvertNode is a node that inverts a price. E.g. if the asset query is BTC/USD and the price is 1000, then the asset pair will be USD/BTC and the price will be 0.001.

It expects one node that returns a data point with an value.Tick value.

func NewTickInvertNode

func NewTickInvertNode() *TickInvertNode

NewTickInvertNode creates a new TickInvertNode instance.

func (*TickInvertNode) AddNodes

func (n *TickInvertNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

Only one node is allowed. If more than one node is added, an error is returned.

func (*TickInvertNode) DataPoint

func (n *TickInvertNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*TickInvertNode) Meta

func (n *TickInvertNode) Meta() map[string]any

Meta implements the Node interface.

func (*TickInvertNode) Nodes

func (n *TickInvertNode) Nodes() []Node

Nodes implements the Node interface.

type TickMedianNode

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

TickMedianNode is a node that calculates median value from its nodes.

It expects that all nodes return data points with value.Tick values.

func NewTickMedianNode

func NewTickMedianNode(min int) *TickMedianNode

NewTickMedianNode creates a new TickMedianNode instance.

The min argument is a minimum number of valid prices obtained from nodes required to calculate median price.

func (*TickMedianNode) AddNodes

func (n *TickMedianNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

func (*TickMedianNode) DataPoint

func (n *TickMedianNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*TickMedianNode) Meta

func (n *TickMedianNode) Meta() map[string]any

Meta implements the Node interface.

func (*TickMedianNode) Nodes

func (n *TickMedianNode) Nodes() []Node

Nodes implements the Node interface.

type Updater

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

Updater updates the origin nodes using points from the origins.

func NewUpdater

func NewUpdater(origins map[string]origin.Origin, logger log.Logger) *Updater

NewUpdater returns a new Updater instance.

func (*Updater) Update

func (u *Updater) Update(ctx context.Context, graphs []Node)

Update updates the origin nodes in the given graphs.

Only origin nodes that are not fresh will be updated.

type WrapperNode

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

WrapperNode is a node that wraps another node with a custom meta. It is useful for adding additional information to the node to better describe a price model.

func NewWrapperNode

func NewWrapperNode(node Node, meta map[string]any) *WrapperNode

NewWrapperNode creates a new WrapperNode instance.

func (*WrapperNode) AddNodes

func (n *WrapperNode) AddNodes(nodes ...Node) error

AddNodes implements the Node interface.

Only one node is allowed. If more than one node is added, an error is returned.

func (*WrapperNode) DataPoint

func (n *WrapperNode) DataPoint() datapoint.Point

DataPoint implements the Node interface.

func (*WrapperNode) Meta

func (n *WrapperNode) Meta() map[string]any

Meta implements the Node interface.

func (*WrapperNode) Nodes

func (n *WrapperNode) Nodes() []Node

Nodes implements the Node interface.

Jump to

Keyboard shortcuts

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