models

package
v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 22, 2018 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const SegmentInsect = "Database/Insect"

SegmentInsect represents a segment

View Source
const SegmentInsert = "Database/Insert"

SegmentInsert represents a segment

View Source
const SegmentModel = "Model"

SegmentModel represents a segment

View Source
const SegmentPostgres = "PostgreSQL"

SegmentPostgres represents a segment

View Source
const SegmentSelect = "Database/Select"

SegmentSelect represents a segment

View Source
const SegmentSerialization = "Serialization"

SegmentSerialization represents a segment

View Source
const SegmentUpdate = "Database/Update"

SegmentUpdate represents a segment

View Source
const SegmentUpsert = "Database/Upsert"

SegmentUpsert represents a segment

Variables

This section is empty.

Functions

func ClaimOffer

func ClaimOffer(
	ctx context.Context,
	db runner.Connection,
	gameID, offerInstanceID, playerID, productID, transactionID string,
	timestamp int64,
	t time.Time,
	mr *MixedMetricsReporter,
) (dat.JSON, bool, int64, error)

ClaimOffer claims the offer

func ClaimOfferPlayer

func ClaimOfferPlayer(ctx context.Context, db runner.Connection, offerPlayer *OfferPlayer, t time.Time, mr *MixedMetricsReporter) error

ClaimOfferPlayer increments the claim counter and updates the timestamp

func CreateOfferPlayer

func CreateOfferPlayer(ctx context.Context, db runner.Connection, offerPlayer *OfferPlayer, mr *MixedMetricsReporter) error

CreateOfferPlayer creates an offer player

func GetAvailableOffers

func GetAvailableOffers(
	ctx context.Context,
	db runner.Connection,
	offersCache *cache.Cache,
	gameID, playerID string,
	t time.Time,
	expireDuration time.Duration,
	filterAttrs map[string]string,
	allowInefficientQueries bool,
	mr *MixedMetricsReporter,
) (map[string][]*OfferToReturn, error)

GetAvailableOffers returns the offers that match the criteria of enabled offer templates

func GetDB

func GetDB(
	host string, user string, port int, sslmode string,
	dbName string, password string,
	maxIdleConns, maxOpenConns int,
	connectionTimeoutMS int,
) (runner.Connection, error)

GetDB Connection using the given properties

func GetEnabledOffersKey

func GetEnabledOffersKey(gameID string) string

GetEnabledOffersKey returns the key of the current enabled offers

func IsForeignKeyViolationError

func IsForeignKeyViolationError(err error) (*pq.Error, bool)

IsForeignKeyViolationError returns true if the error is a pq error stating a foreign key has been violated

func IsNoRowsInResultSetError

func IsNoRowsInResultSetError(err error) bool

IsNoRowsInResultSetError returns true if the error is a sqlx error stating that now rows were found

func SetEnabledOffer

func SetEnabledOffer(ctx context.Context, db runner.Connection, gameID, id string, enabled bool, offersCache *cache.Cache, mr *MixedMetricsReporter) error

SetEnabledOffer can enable or disable an offer template

func ShouldPing

func ShouldPing(db *sql.DB, timeout time.Duration) error

ShouldPing the database

func UpsertGame

func UpsertGame(ctx context.Context, db runner.Connection, game *Game, t time.Time, mr *MixedMetricsReporter) error

UpsertGame updates a game with new meta or insert with the new UUID

func ValidateString

func ValidateString(s string) bool

ValidateString validates the string contains only valid characters for filters

func ViewOffer

func ViewOffer(
	ctx context.Context,
	db runner.Connection,
	gameID, offerInstanceID, playerID, impressionID string,
	t time.Time,
	mr *MixedMetricsReporter,
) (bool, int64, error)

ViewOffer views the offer

func ViewOfferPlayer

func ViewOfferPlayer(ctx context.Context, db runner.Connection, offerPlayer *OfferPlayer, t time.Time, mr *MixedMetricsReporter) error

ViewOfferPlayer increments the view counter and updates the timestamp

Types

type ClaimOfferPayload

type ClaimOfferPayload struct {
	GameID          string `json:"gameId" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255),required"`
	PlayerID        string `json:"playerId" valid:"ascii,stringlength(1|1000),required"`
	ProductID       string `json:"productId" valid:"ascii,stringlength(1|255)"`
	Timestamp       int64  `json:"timestamp" valid:"int64,required"`
	TransactionID   string `json:"transactionId" valid:"ascii,stringlength(1|1000),required"`
	OfferInstanceID string `json:"id" valid:"uuidv4,optional"`
}

ClaimOfferPayload has required fields for claiming an offer

type Clock

type Clock interface {
	GetTime() time.Time
}

Clock returns the time

type DefaultTrigger

type DefaultTrigger struct{}

DefaultTrigger implements interface Trigger

func (DefaultTrigger) IsTriggered

func (dt DefaultTrigger) IsTriggered(times interface{}, user interface{}) bool

IsTriggered returns the current time

type FrequencyOrPeriod

type FrequencyOrPeriod struct {
	Every string
	Max   int
}

FrequencyOrPeriod is the struct for basic Frequency and Period types

type Game

type Game struct {
	ID       string   `db:"id" json:"id" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255)"`
	Name     string   `db:"name" json:"name" valid:"ascii,stringlength(1|255),required"`
	Metadata dat.JSON `db:"metadata" json:"metadata" valid:"JSONObject"`

	//TODO: Validate dates
	CreatedAt dat.NullTime `db:"created_at" json:"createdAt" valid:""`
	UpdatedAt dat.NullTime `db:"updated_at" json:"updatedAt" valid:""`
}

Game represents a tenant in offers API

func GetGameByID

func GetGameByID(ctx context.Context, db runner.Connection, id string, mr *MixedMetricsReporter) (*Game, error)

GetGameByID returns a game by it's pk

func ListGames

func ListGames(ctx context.Context, db runner.Connection, mr *MixedMetricsReporter) ([]*Game, error)

ListGames returns a the full list of games

func (*Game) GetMetadata

func (g *Game) GetMetadata() (map[string]interface{}, error)

GetMetadata for game

type MetricsReporter

type MetricsReporter interface {
	StartSegment(string) map[string]interface{}
	EndSegment(map[string]interface{}, string)

	StartDatastoreSegment(datastore, collection, operation string) map[string]interface{}
	EndDatastoreSegment(map[string]interface{})

	StartExternalSegment(string) map[string]interface{}
	EndExternalSegment(map[string]interface{})
}

MetricsReporter is a contract for reporters of metrics

type MixedMetricsReporter

type MixedMetricsReporter struct {
	MetricsReporters []MetricsReporter
	Func             func(name string, f func() error) error
}

MixedMetricsReporter calls other metrics reporters

func NewMixedMetricsReporter

func NewMixedMetricsReporter() *MixedMetricsReporter

NewMixedMetricsReporter ctor

func (*MixedMetricsReporter) AddReporter

func (m *MixedMetricsReporter) AddReporter(mr MetricsReporter)

AddReporter to metrics reporter

func (*MixedMetricsReporter) WithDatastoreSegment

func (m *MixedMetricsReporter) WithDatastoreSegment(table, operation string, f func() error) error

WithDatastoreSegment that calls all the other metrics reporters

func (*MixedMetricsReporter) WithExternalSegment

func (m *MixedMetricsReporter) WithExternalSegment(url string, f func() error) error

WithExternalSegment that calls all the other metrics reporters

func (*MixedMetricsReporter) WithSegment

func (m *MixedMetricsReporter) WithSegment(name string, f func() error) error

WithSegment that calls all the other metrics reporters

type Offer

type Offer struct {
	ID        string    `db:"id" json:"id" valid:"uuidv4"`
	GameID    string    `db:"game_id" json:"gameId" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255),required"`
	Name      string    `db:"name" json:"name" valid:"ascii,stringlength(1|255),required"`
	Period    dat.JSON  `db:"period" json:"period" valid:"RequiredJSONObject"`
	Frequency dat.JSON  `db:"frequency" json:"frequency" valid:"RequiredJSONObject"`
	Trigger   dat.JSON  `db:"trigger" json:"trigger" valid:"RequiredJSONObject"`
	Placement string    `db:"placement" json:"placement" valid:"ascii,stringlength(1|255),required"`
	Metadata  dat.JSON  `db:"metadata" json:"metadata" valid:"JSONObject"`
	ProductID string    `db:"product_id" json:"productId,omitempty" valid:"ascii,stringlength(1|255)"`
	Contents  dat.JSON  `db:"contents" json:"contents" valid:"RequiredJSONObject"`
	Enabled   bool      `db:"enabled" json:"enabled" valid:"matches(^(true|false)$),optional"`
	Version   int       `db:"version" json:"version" valid:"int,optional"`
	CreatedAt time.Time `db:"created_at" json:"createdAt" valid:"optional"`
	Filters   dat.JSON  `db:"filters" json:"filters" valid:"FilterJSONObject"`
	Cost      dat.JSON  `db:"cost" json:"cost,omitempty" valid:"JSONObject"`
}

Offer contains the parameters of an offer

func GetEnabledOffers

func GetEnabledOffers(ctx context.Context, db runner.Connection, gameID string, offersCache *cache.Cache, expireDuration time.Duration, currentTime time.Time, filterAttrs map[string]string, allowInefficientQueries bool, mr *MixedMetricsReporter) ([]*Offer, error)

GetEnabledOffers returns all the enabled offers and matching offers

func GetOfferByID

func GetOfferByID(ctx context.Context, db runner.Connection, gameID, id string, mr *MixedMetricsReporter) (*Offer, error)

GetOfferByID returns Offer by ID

func InsertOffer

func InsertOffer(ctx context.Context, db runner.Connection, offer *Offer, offersCache *cache.Cache, mr *MixedMetricsReporter) (*Offer, error)

InsertOffer inserts a new offer template into DB

func ListOffers

func ListOffers(
	ctx context.Context,
	db runner.Connection,
	gameID string,
	limit, offset uint64,
	mr *MixedMetricsReporter,
) ([]*Offer, int, error)

ListOffers returns all the offer templates for a given game return the number of pages using the number of offers and given the limit for each page

func UpdateOffer

func UpdateOffer(ctx context.Context, db runner.Connection, offer *Offer, offersCache *cache.Cache, mr *MixedMetricsReporter) (*Offer, error)

UpdateOffer updates a given offer

type OfferImpressionPayload

type OfferImpressionPayload struct {
	GameID       string `json:"gameId" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255),required"`
	PlayerID     string `json:"playerId" valid:"ascii,stringlength(1|1000),required"`
	ImpressionID string `json:"impressionId" valid:"uuidv4,required"`
}

OfferImpressionPayload has required fields for an offer impression

type OfferInstance

type OfferInstance struct {
	ID           string       `db:"id" json:"id" valid:"uuidv4,required"`
	GameID       string       `db:"game_id" json:"gameId" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255),required"`
	PlayerID     string       `db:"player_id" json:"playerId" valid:"ascii,stringlength(1|1000),required"`
	OfferID      string       `db:"offer_id" json:"offerId" valid:"uuidv4,required"`
	OfferVersion int          `db:"offer_version" json:"offerVersion" valid:"int,required"`
	Contents     dat.JSON     `db:"contents" json:"contents" valid:"RequiredJSONObject"`
	ProductID    string       `db:"product_id" json:"productId" valid:"ascii,stringlength(1|255)"`
	Cost         dat.JSON     `db:"cost" json:"cost" valid:"JSONObject"`
	CreatedAt    dat.NullTime `db:"created_at" json:"createdAt" valid:""`
}

OfferInstance represents a tenant in offers API it cannot be updated, only inserted

type OfferInstanceOffer

type OfferInstanceOffer struct {
	ID       string   `db:"id" json:"id" valid:"uuidv4,required"`
	GameID   string   `db:"game_id" json:"gameId" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255),required"`
	OfferID  string   `db:"offer_id" json:"offerId" valid:"uuidv4,required"`
	Contents dat.JSON `db:"contents" json:"contents" valid:"RequiredJSONObject"`
	Enabled  bool     `db:"enabled" json:"enabled"`
}

OfferInstanceOffer is a join of OfferInstance with offer

type OfferPlayer

type OfferPlayer struct {
	ID             string       `db:"id" json:"id" valid:"uuidv4,required"`
	GameID         string       `db:"game_id" json:"gameId" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255),required"`
	PlayerID       string       `db:"player_id" json:"playerId" valid:"ascii,stringlength(1|1000),required"`
	OfferID        string       `db:"offer_id" json:"offerId" valid:"uuidv4,required"`
	ClaimCounter   int          `db:"claim_counter" json:"claimCounter" valid:"int"`
	ClaimTimestamp dat.NullTime `db:"claim_timestamp" json:"claimTimestamp" valid:""`
	ViewCounter    int          `db:"view_counter" json:"viewCounter" valid:"int"`
	ViewTimestamp  dat.NullTime `db:"view_timestamp" json:"viewTimestamp" valid:""`
	Transactions   dat.JSON     `db:"transactions" json:"transactions" valid:""`
	Impressions    dat.JSON     `db:"impressions" json:"impressions" valid:""`
}

OfferPlayer represents an offer seen by a player

func GetOfferPlayer

func GetOfferPlayer(ctx context.Context, db runner.Connection, gameID, playerID, offerID string, mr *MixedMetricsReporter) (*OfferPlayer, error)

GetOfferPlayer returns an offer player

func GetOffersByPlayer

func GetOffersByPlayer(ctx context.Context, db runner.Connection, gameID, playerID string, mr *MixedMetricsReporter) ([]*OfferPlayer, error)

GetOffersByPlayer returns all offers by player

type OfferToReturn

type OfferToReturn struct {
	ID        string   `db:"id" json:"id"`
	ProductID string   `db:"product_id" json:"productId,omitempty"`
	Cost      dat.JSON `db:"cost" json:"cost,omitempty" valid:"JSONObject"`
	Contents  dat.JSON `db:"contents" json:"contents"`
	Metadata  dat.JSON `db:"metadata" json:"metadata"`
	ExpireAt  int64    `db:"expire_at" json:"expireAt"`
}

OfferToReturn has the fields for the returned offer

func GetOfferInfo

func GetOfferInfo(
	ctx context.Context,
	db runner.Connection,
	gameID, offerInstanceID string,
	expireDuration time.Duration,
	mr *MixedMetricsReporter,
) (*OfferToReturn, error)

GetOfferInfo returns the offers that match the criteria of enabled offer templates

type OfferVersion

type OfferVersion struct {
	ID           string       `db:"id" json:"id" valid:"uuidv4,required"`
	GameID       string       `db:"game_id" json:"gameId" valid:"matches(^[^-][a-zA-Z0-9-_]*$),stringlength(1|255),required"`
	OfferID      string       `db:"offer_id" json:"offerId" valid:"uuidv4,required"`
	OfferVersion int          `db:"offer_version" json:"offerVersion" valid:"int,required"`
	Contents     dat.JSON     `db:"contents" json:"contents" valid:"RequiredJSONObject"`
	ProductID    string       `db:"product_id" json:"productId" valid:"ascii,stringlength(1|255)"`
	Cost         dat.JSON     `db:"cost" json:"cost" valid:"JSONObject"`
	CreatedAt    dat.NullTime `db:"created_at" json:"createdAt" valid:""`
}

OfferVersion represents a tenant in offers API it cannot be updated, only inserted

type RealClock

type RealClock struct{}

RealClock returns the actual time from the OS

func (RealClock) GetTime

func (r RealClock) GetTime() time.Time

GetTime returns the current time

type TimeTrigger

type TimeTrigger struct{}

TimeTrigger implements interface Trigger

func (TimeTrigger) IsTriggered

func (tt TimeTrigger) IsTriggered(times interface{}, now interface{}) bool

IsTriggered returns the current time

type Times

type Times struct {
	From int64 `json:"from"`
	To   int64 `json:"to"`
}

Times holds from and to in UnixTimestamp

type Trigger

type Trigger interface {
	IsTriggered(interface{}, interface{}) bool
}

Trigger return true if offer is triggered

Jump to

Keyboard shortcuts

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