userdb

package
v0.0.0-...-43b2f59 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithUserRecord

func WithUserRecord(ctx context.Context, u User) context.Context

Types

type Amount

type Amount struct {
	Units int64
	Nanos int32
}

func ToAmount

func ToAmount(i decimal.Decimal) Amount

func (Amount) F

func (a Amount) F() decimal.Decimal

func (Amount) IsNegative

func (a Amount) IsNegative() bool

func (Amount) IsZero

func (a Amount) IsZero() bool

func (Amount) Less

func (a Amount) Less(b Amount) bool

func (Amount) V

func (a Amount) V() *grpcoin.Amount

type MockProfileCache

type MockProfileCache struct{}

func (MockProfileCache) GetTrades

func (m MockProfileCache) GetTrades(_ context.Context, _ string) ([]TradeRecord, bool, error)

func (MockProfileCache) GetValuation

func (m MockProfileCache) GetValuation(_ context.Context, _ string, _ time.Time) ([]ValuationHistory, bool, error)

func (MockProfileCache) InvalidateTrades

func (m MockProfileCache) InvalidateTrades(_ context.Context, _ string) error

func (MockProfileCache) SaveTrades

func (m MockProfileCache) SaveTrades(_ context.Context, _ string, v []TradeRecord) error

func (MockProfileCache) SaveValuation

func (m MockProfileCache) SaveValuation(_ context.Context, _ string, _ time.Time, v []ValuationHistory) error

type Portfolio

type Portfolio struct {
	CashUSD   Amount
	Positions map[string]Amount
}

type ProfileCache

type ProfileCache interface {
	GetTrades(ctx context.Context, uid string) ([]TradeRecord, bool, error)
	SaveTrades(ctx context.Context, uid string, v []TradeRecord) error
	InvalidateTrades(ctx context.Context, uid string) error

	GetValuation(ctx context.Context, uid string, now time.Time) ([]ValuationHistory, bool, error)
	SaveValuation(ctx context.Context, uid string, now time.Time, v []ValuationHistory) error
}

type TradeRecord

type TradeRecord struct {
	Date   time.Time           `firestore:"date"`
	Ticker string              `firestore:"ticker"`
	Action grpcoin.TradeAction `firestore:"action"`
	Size   Amount              `firestore:"size"`
	Price  Amount              `firestore:"price"`
}

TradeRecord represents a trade user has made in the past.

type User

type User struct {
	ID          string
	DisplayName string
	ProfileURL  string
	CreatedAt   time.Time
	Portfolio   Portfolio
	TradeStats  struct {
		LastTrade  time.Time
		TradeCount int
	}
}

func UserRecordFromContext

func UserRecordFromContext(ctx context.Context) (User, bool)

type UserDB

type UserDB struct {
	DB           *firestore.Client
	Cache        ProfileCache
	TradeCounter *tradecounters.TradeCounter
	T            trace.Tracer
}

func (*UserDB) Create

func (u *UserDB) Create(ctx context.Context, au auth.AuthenticatedUser) error

func (*UserDB) EnsureAccountExists

func (u *UserDB) EnsureAccountExists(ctx context.Context, au auth.AuthenticatedUser) (User, error)

func (*UserDB) EnsureAccountExistsInterceptor

func (u *UserDB) EnsureAccountExistsInterceptor() grpc_auth.AuthFunc

EnsureAccountExistsInterceptor creates an account for the authenticated client (or retrieves it) and augments the ctx with the user's db record.

func (*UserDB) Get

func (u *UserDB) Get(ctx context.Context, userID string) (User, bool, error)

func (*UserDB) GetAll

func (u *UserDB) GetAll(ctx context.Context) ([]User, error)

func (*UserDB) RotateTradeHistory

func (u *UserDB) RotateTradeHistory(ctx context.Context, uid string, maxHist int) error

func (*UserDB) RotateUserValuationHistory

func (u *UserDB) RotateUserValuationHistory(ctx context.Context, uid string, deleteBefore time.Time) error

func (*UserDB) SetUserValuationHistory

func (u *UserDB) SetUserValuationHistory(ctx context.Context, uid string, v ValuationHistory) error

func (*UserDB) Trade

func (u *UserDB) Trade(ctx context.Context, uid string, ticker string, action grpcoin.TradeAction,
	quote, quantity *grpcoin.Amount) (Portfolio, error)

func (*UserDB) UserTrades

func (u *UserDB) UserTrades(ctx context.Context, uid string) ([]TradeRecord, error)

func (*UserDB) UserValuationHistory

func (u *UserDB) UserValuationHistory(ctx context.Context, uid string) ([]ValuationHistory, error)

type UserDBCache

type UserDBCache struct {
	R *redis.Client
}

func (UserDBCache) GetTrades

func (u UserDBCache) GetTrades(ctx context.Context, uid string) ([]TradeRecord, bool, error)

func (UserDBCache) GetValuation

func (u UserDBCache) GetValuation(ctx context.Context, uid string, now time.Time) ([]ValuationHistory, bool, error)

func (UserDBCache) InvalidateTrades

func (u UserDBCache) InvalidateTrades(ctx context.Context, uid string) error

func (UserDBCache) SaveTrades

func (u UserDBCache) SaveTrades(ctx context.Context, uid string, v []TradeRecord) error

func (UserDBCache) SaveValuation

func (u UserDBCache) SaveValuation(ctx context.Context, uid string, now time.Time, v []ValuationHistory) error

type ValuationHistory

type ValuationHistory struct {
	Date  time.Time `firestore:"date"`
	Value Amount    `firestore:"value"`
}

ValuationHistory represents user's portfolio value at a particular time.

Jump to

Keyboard shortcuts

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