graph

package
v0.11.0-dev.2 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2023 License: AGPL-3.0 Imports: 12 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 AliasNode

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

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

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

func NewAliasNode

func NewAliasNode(alias origin.Pair) *AliasNode

NewAliasNode creates a new AliasNode instance.

func (*AliasNode) AddNodes

func (n *AliasNode) 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 (*AliasNode) DataPoint

func (n *AliasNode) DataPoint() data.Point

DataPoint implements the Node interface.

func (*AliasNode) Meta

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

Meta implements the Node interface.

func (*AliasNode) Nodes

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

Nodes implements the Node interface.

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() data.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 IndirectNode

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

IndirectNode 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 origin.Tick values.

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

func NewIndirectNode

func NewIndirectNode() *IndirectNode

NewIndirectNode creates a new IndirectNode instance.

func (*IndirectNode) AddNodes

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

AddNodes implements the Node interface.

func (*IndirectNode) DataPoint

func (n *IndirectNode) DataPoint() data.Point

DataPoint implements the Node interface.

func (*IndirectNode) Meta

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

Meta implements the Node interface.

func (*IndirectNode) Nodes

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

Nodes implements the Node interface.

type InvertNode

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

InvertNode 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 origin.Tick value.

func NewInvertNode

func NewInvertNode() *InvertNode

NewInvertNode creates a new InvertNode instance.

func (*InvertNode) AddNodes

func (n *InvertNode) 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 (*InvertNode) DataPoint

func (n *InvertNode) DataPoint() data.Point

DataPoint implements the Node interface.

func (*InvertNode) Meta

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

Meta implements the Node interface.

func (*InvertNode) Nodes

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

Nodes implements the Node interface.

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 MedianNode

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

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

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

func NewMedianNode

func NewMedianNode(min int) *MedianNode

NewMedianNode creates a new MedianNode instance.

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

func (*MedianNode) AddNodes

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

AddNodes implements the Node interface.

func (*MedianNode) DataPoint

func (n *MedianNode) DataPoint() data.Point

DataPoint implements the Node interface.

func (*MedianNode) Meta

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

Meta implements the Node interface.

func (*MedianNode) Nodes

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

Nodes implements the Node 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() data.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 tick for a given asset pair from 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 the tick from the origin.

The freshnessThreshold and expiryThreshold arguments are used to determine whether the price is fresh or expired.

The tick is considered fresh if it was updated within the freshnessThreshold duration. In this case, the price update is not required.

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

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

Note that price that is considered not fresh may not be considered expired.

func (*OriginNode) AddNodes

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

AddNodes implements the Node interface.

func (*OriginNode) DataPoint

func (n *OriginNode) DataPoint() data.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 data.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. In this case, the error will be returned.

type Provider

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

Provider is a price provider which uses a graph to calculate prices.

func NewProvider

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

NewProvider creates a new price data.

func (Provider) DataPoint

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

DataPoint implements the data.Provider interface.

func (Provider) DataPoints

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

DataPoints implements the data.Provider interface.

func (Provider) Model

func (p Provider) Model(_ context.Context, model string) (data.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]data.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() data.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 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) error

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() data.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