store

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2019 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment

type Comment struct {
	ID         int64  `json:"id" db:"id"`
	EventKey   string `json:"-" db:"event_key"`
	MatchKey   string `json:"matchKey" db:"match_key"`
	TeamKey    string `json:"-" db:"team_key"`
	ReporterID *int64 `json:"reporterId" db:"reporter_id"`
	RealmID    *int64 `json:"-" db:"realm_id"`
	Comment    string `json:"comment" db:"comment"`
}

Comment defines a comment on a robots performance during a match. It is the qualitative equivalent of a report.

type ErrExists

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

ErrExists is returned if a unique record already exists.

type ErrFKeyViolation

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

ErrFKeyViolation is returned if inserting a record causes a foreign key violation.

type ErrNoResults

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

ErrNoResults indicates that no data matching the query was found.

type Event

type Event struct {
	Key          string         `json:"key" db:"key"`
	RealmID      *int64         `json:"realmId,omitempty" db:"realm_id"`
	SchemaID     *int64         `json:"schemaId,omitempty" db:"schema_id"`
	Name         string         `json:"name" db:"name"`
	District     *string        `json:"district,omitempty" db:"district"`
	FullDistrict *string        `json:"fullDistrict,omitempty" db:"full_district"`
	Week         *int           `json:"week,omitempty" db:"week"`
	StartDate    UnixTime       `json:"startDate" db:"start_date"`
	EndDate      UnixTime       `json:"endDate" db:"end_date"`
	Webcasts     pq.StringArray `json:"webcasts" db:"webcasts"`
	LocationName string         `json:"locationName" db:"location_name"`
	Lat          float64        `json:"lat" db:"lat"`
	Lon          float64        `json:"lon" db:"lon"`
	TBADeleted   bool           `json:"tbaDeleted" db:"tba_deleted"`
}

Event holds information about an FRC event such as webcast associated with it, the location, its start date, and more.

type Leaderboard

type Leaderboard []struct {
	ReporterID int64 `json:"reporterId" db:"reporter_id"`
	Reports    int64 `json:"reports" db:"num_reports"`
}

Leaderboard holds information about how many reports each reporter submitted.

type Match

type Match struct {
	Key           string         `json:"key" db:"key"`
	EventKey      string         `json:"eventKey" db:"event_key"`
	PredictedTime *UnixTime      `json:"predictedTime" db:"predicted_time"`
	ActualTime    *UnixTime      `json:"actualTime" db:"actual_time"`
	ScheduledTime *UnixTime      `json:"scheduledTime" db:"scheduled_time"`
	RedScore      *int           `json:"redScore" db:"red_score"`
	BlueScore     *int           `json:"blueScore" db:"blue_score"`
	RedAlliance   pq.StringArray `json:"redAlliance" db:"red_alliance"`
	BlueAlliance  pq.StringArray `json:"blueAlliance" db:"blue_alliance"`
	TBADeleted    bool           `json:"tba_deleted" db:"tba_deleted"`
}

Match holds information about an FRC match at a specific event

func (*Match) GetTime

func (m *Match) GetTime() *UnixTime

GetTime returns the actual match time if available, and if not, predicted time

type PatchSchema

type PatchSchema struct {
	ID     int64            `json:"id" db:"id"`
	Year   *int64           `json:"year,omitempty" db:"year"`
	Auto   StatDescriptions `json:"auto" db:"auto"`
	Teleop StatDescriptions `json:"teleop" db:"teleop"`
}

PatchSchema is a nullable schema for patching.

type PatchUser

type PatchUser struct {
	ID              int64          `json:"id" db:"id"`
	Username        *string        `json:"username" db:"username"`
	HashedPassword  *string        `json:"-" db:"hashed_password"`
	PasswordChanged *UnixTime      `json:"-" db:"password_changed"`
	FirstName       *string        `json:"firstName" db:"first_name"`
	LastName        *string        `json:"lastName" db:"last_name"`
	Roles           *Roles         `json:"roles" db:"roles"`
	Stars           pq.StringArray `json:"stars"`
}

PatchUser is like User but with all nullable fields (besides id and realmID) for patching.

type Realm

type Realm struct {
	ID           int64  `json:"id" db:"id"`
	Name         string `json:"name" db:"name" validate:"omitempty,gte=1,lte=32"`
	ShareReports bool   `json:"shareReports" db:"share_reports"`
}

Realm holds the name of a realm, and whether to share the realms reports.

type Report

type Report struct {
	ID         int64      `json:"-" db:"id"`
	MatchKey   string     `json:"-" db:"match_key"`
	TeamKey    string     `json:"-" db:"team_key"`
	ReporterID *int64     `json:"reporterId" db:"reporter_id"`
	RealmID    *int64     `json:"-" db:"realm_id"`
	AutoName   string     `json:"autoName" db:"auto_name"`
	Data       ReportData `json:"data" db:"data"`
}

Report is data about how an FRC team performed in a specific match.

type ReportData

type ReportData struct {
	Auto   []Stat `json:"auto"`
	Teleop []Stat `json:"teleop"`
}

ReportData holds all the data in a report

func (*ReportData) Scan

func (rd *ReportData) Scan(src interface{}) error

Scan implements sql.Scanner to scan JSON from the DB into ReportData.

func (ReportData) Value

func (rd ReportData) Value() (driver.Value, error)

Value implements driver.Valuer to return JSON for the DB from ReportData.

type Roles

type Roles struct {
	IsSuperAdmin bool `json:"isSuperAdmin" yaml:"isSuperAdmin"`
	IsAdmin      bool `json:"isAdmin" yaml:"isAdmin"`
	IsVerified   bool `json:"isVerified" yaml:"isVerified"`
}

Roles holds information about a users roles and permissions such as whether they are an administrator.

func (*Roles) Scan

func (r *Roles) Scan(src interface{}) error

Scan is provided for scanning data from PostgreSQL's JSONB type into Roles.

func (Roles) Value

func (r Roles) Value() (driver.Value, error)

Value is provided for returning the value of Roles as marshalled JSON for PostgreSQL's JSONB type.

type Schema

type Schema struct {
	ID      int64            `json:"id" db:"id"`
	Year    *int64           `json:"year,omitempty" db:"year"`
	RealmID *int64           `json:"realmId,omitempty" db:"realm_id"`
	Auto    StatDescriptions `json:"auto" db:"auto"`
	Teleop  StatDescriptions `json:"teleop" db:"teleop"`
}

Schema describes the statistics that reports should include

type Service

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

Service provides methods for storing data in a PostgreSQL database.

func New

func New(ctx context.Context, dsn string, logger *logrus.Logger) (Service, error)

New creates a new store service from a dataSourceName. The logger is used to log errors that would not otherwise be returned such as issues rolling back transactions. The context passed is used for pinging the database.

func (*Service) AlliancesUpsert

func (s *Service) AlliancesUpsert(ctx context.Context, matchKey string, blueAlliance []string, redAlliance []string, tx *sqlx.Tx) error

AlliancesUpsert upserts the red and blue alliances for a specific match. matchKey is the key of the match. Upsert done within transaction.

func (*Service) CheckMatchKeyExists

func (s *Service) CheckMatchKeyExists(matchKey string) (bool, error)

CheckMatchKeyExists returns whether the match key exists in the database.

func (*Service) CheckSimilarUsernameExists

func (s *Service) CheckSimilarUsernameExists(ctx context.Context, username string, id *int64) error

CheckSimilarUsernameExists checks whether a user with (case insensitive) the same username exists. It returns an ErrExists if a similar user exists. If an id is given, it will ignore the user with that id.

func (*Service) CheckTBAEventKeyExists

func (s *Service) CheckTBAEventKeyExists(ctx context.Context, eventKey string) (bool, error)

CheckTBAEventKeyExists checks whether a specific event key exists and is from TBA rather than manually added. Returns ErrNoResults if event does not exist.

func (*Service) Close

func (s *Service) Close() error

Close closes the underlying postgresql database.

func (*Service) CreateSchema

func (s *Service) CreateSchema(ctx context.Context, schema Schema) error

CreateSchema creates a new schema

func (*Service) CreateUser

func (s *Service) CreateUser(ctx context.Context, u User) error

CreateUser creates a given user.

func (*Service) DeleteRealm

func (s *Service) DeleteRealm(ctx context.Context, id int64) error

DeleteRealm deletes a realm from the database.

func (*Service) DeleteUser

func (s *Service) DeleteUser(ctx context.Context, id int64) error

DeleteUser deletes a specific user from the database.

func (*Service) EventsUpsert

func (s *Service) EventsUpsert(ctx context.Context, events []Event) error

EventsUpsert upserts multiple events into the database. It will set tba_deleted to false for all updated events.

func (*Service) GetEvent

func (s *Service) GetEvent(ctx context.Context, eventKey string) (Event, error)

GetEvent retrieves a specific event.

func (*Service) GetEventComments

func (s *Service) GetEventComments(ctx context.Context, eventKey, teamKey string) (comments []Comment, err error)

GetEventComments gets all comments for a given team in an event.

func (*Service) GetEventReports

func (s *Service) GetEventReports(ctx context.Context, eventKey string, realmID *int64) ([]Report, error)

GetEventReports retrieves all reports for an event from the db. If a realmID is specified, only reports from that realm will be included.

func (*Service) GetEvents

func (s *Service) GetEvents(ctx context.Context, tbaDeleted bool) ([]Event, error)

GetEvents returns all events from the database. event.Webcasts and schemaID will be nil for every event. If tbaDeleted is true, events that have been deleted from TBA will be returned in addition to events that have not been deleted. Otherwise, only events that have not been deleted will be returned.

func (*Service) GetEventsFromRealm

func (s *Service) GetEventsFromRealm(ctx context.Context, realm *int64, tbaDeleted bool) ([]Event, error)

GetEventsFromRealm returns all events from a specific realm. Additionally all TBA events will be retrieved. If no realm is specified (nil) then just the TBA events will be retrieved. event.Webcasts and schemaID will be nil for every event. If tbaDeleted is true, events that have been deleted from TBA will be returned in addition to events that have not been deleted. Otherwise, only events that have not been deleted will be returned.

func (*Service) GetLeaderboard

func (s *Service) GetLeaderboard(ctx context.Context) (Leaderboard, error)

GetLeaderboard retrieves leaderboard information from the reports and users table.

func (*Service) GetMatch

func (s *Service) GetMatch(ctx context.Context, matchKey string) (Match, error)

GetMatch returns a specific match.

func (*Service) GetMatchTeamComments

func (s *Service) GetMatchTeamComments(ctx context.Context, matchKey, teamKey string) (comments []Comment, err error)

GetMatchTeamComments gets all comments for a given team in a match.

func (*Service) GetMatches

func (s *Service) GetMatches(ctx context.Context, eventKey string, teamKeys []string, tbaDeleted bool) ([]Match, error)

GetMatches returns all matches from a specific event that include the given teams. If teams is nil or empty a list of all the matches for that event are returned. // If tbaDeleted is true, matches that have been deleted from TBA will be returned in addition to matches that have not been deleted. Otherwise, only matches that have not been deleted will be returned.

func (*Service) GetPublicRealms

func (s *Service) GetPublicRealms(ctx context.Context) ([]Realm, error)

GetPublicRealms returns all public realms in the database.

func (*Service) GetRealm

func (s *Service) GetRealm(ctx context.Context, id int64) (Realm, error)

GetRealm retrieves a specific realm.

func (*Service) GetRealms

func (s *Service) GetRealms(ctx context.Context) ([]Realm, error)

GetRealms returns all realms in the database.

func (*Service) GetReportsBySchemaID

func (s *Service) GetReportsBySchemaID(ctx context.Context, schemaID int64) ([]Report, error)

GetReportsBySchemaID retrieves all reports with a specific schema.

func (*Service) GetSchemaByID

func (s *Service) GetSchemaByID(ctx context.Context, id int64) (Schema, error)

GetSchemaByID retrieves a schema given its ID

func (*Service) GetSchemaByYear

func (s *Service) GetSchemaByYear(ctx context.Context, year int) (Schema, error)

GetSchemaByYear retrieves the schema for a given year

func (*Service) GetSchemas

func (s *Service) GetSchemas(ctx context.Context) ([]Schema, error)

GetSchemas retrieves all schemas from the database.

func (*Service) GetTeam

func (s *Service) GetTeam(ctx context.Context, teamKey string, eventKey string) (Team, error)

GetTeam retrieves a team specified by teamKey from a event specified by eventKey.

func (*Service) GetTeamEventReports

func (s *Service) GetTeamEventReports(ctx context.Context, eventKey string, teamKey string, realmID *int64) ([]Report, error)

GetTeamEventReports retrieves all reports for a specific team and event from the db. If a realmID is specified, only reports from that realm will be included.

func (*Service) GetTeamKeys

func (s *Service) GetTeamKeys(ctx context.Context, eventKey string) ([]string, error)

GetTeamKeys retrieves all team keys from an event specified by eventKey.

func (*Service) GetTeamMatchReports

func (s *Service) GetTeamMatchReports(ctx context.Context, matchKey string, teamKey string) ([]Report, error)

GetTeamMatchReports retrieves all reports for a specific team and match from the db.

func (*Service) GetUserByID

func (s *Service) GetUserByID(ctx context.Context, id int64) (User, error)

GetUserByID retrieves a user from the database by id.

func (*Service) GetUserByUsername

func (s *Service) GetUserByUsername(ctx context.Context, username string) (User, error)

GetUserByUsername retrieves a user from the database by username. It does not retrieve the users stars.

func (*Service) GetUsers

func (s *Service) GetUsers(ctx context.Context) ([]User, error)

GetUsers retrieves all users.

func (*Service) GetUsersByRealm

func (s *Service) GetUsersByRealm(ctx context.Context, realmID int64) ([]User, error)

GetUsersByRealm retrieves all users in a specific realm.

func (*Service) GetVisibleSchemas

func (s *Service) GetVisibleSchemas(ctx context.Context, realmID *int64) ([]Schema, error)

GetVisibleSchemas retrieves schemas from the database frm a specific realm, from realms with public events, and standard FRC schemas. If the realm ID is nil, no private realms' schemas will be retrieved.

func (*Service) InsertRealm

func (s *Service) InsertRealm(ctx context.Context, realm Realm) (int64, error)

InsertRealm inserts a realm into the database.

func (*Service) MarkEventsDeleted

func (s *Service) MarkEventsDeleted(ctx context.Context, events []Event) error

MarkEventsDeleted will set tba_deleted to true on all events that were *not* included in the events slice and are not custom events (have a NULL realm_id).

func (*Service) MarkMatchesDeleted

func (s *Service) MarkMatchesDeleted(ctx context.Context, eventKey string, matches []Match) error

MarkMatchesDeleted will set tba_deleted to true on all matches for an event that were *not* included in the passed matches slice.

func (*Service) PatchUser

func (s *Service) PatchUser(ctx context.Context, pu PatchUser) error

PatchUser updates a user by their ID.

func (*Service) Ping

func (s *Service) Ping(ctx context.Context) error

Ping pings the underlying postgresql database. You would think we would call db.Ping() here, but that doesn't actually Ping the database because reasons.

func (*Service) TeamKeysUpsert

func (s *Service) TeamKeysUpsert(ctx context.Context, eventKey string, keys []string) error

TeamKeysUpsert upserts multiple team keys from a single event into the database.

func (*Service) TeamsUpsert

func (s *Service) TeamsUpsert(ctx context.Context, teams []Team) error

TeamsUpsert upserts multiple teams into the database.

func (*Service) UpdateRealm

func (s *Service) UpdateRealm(ctx context.Context, realm Realm) error

UpdateRealm updates a realm.

func (*Service) UpdateTBAMatches

func (s *Service) UpdateTBAMatches(ctx context.Context, eventKey string, matches []Match) error

UpdateTBAMatches puts a set of multiple matches and their alliances from TBA into the database. New matches are added, existing matches will be updated, and matches deleted from TBA will be deleted from the database. User-created matches will be unaffected. If eventKey is specified, only matches from that event will be affected. It will set tba_deleted to false for all updated matches.

func (*Service) UpsertEvent

func (s *Service) UpsertEvent(ctx context.Context, event Event) (created bool, err error)

UpsertEvent upserts a single event into the database and returns whether the event was created or updated.

func (*Service) UpsertMatch

func (s *Service) UpsertMatch(ctx context.Context, match Match) error

UpsertMatch upserts a match and its alliances into the database.

func (*Service) UpsertMatchTeamComment

func (s *Service) UpsertMatchTeamComment(ctx context.Context, c Comment) (created bool, err error)

UpsertMatchTeamComment will upsert a comment for a team in a match. There can only be one comment per reporter per team per match per event.

func (*Service) UpsertReport

func (s *Service) UpsertReport(ctx context.Context, r Report) (created bool, err error)

UpsertReport creates a new report in the db, or replaces the existing one if the same reporter already has a report in the db for that team and match. It returns a boolean that is true when the report was created, and false when it was updated.

type Stat

type Stat struct {
	Attempts  *int   `json:"attempts,omitempty"`
	Successes *int   `json:"successes,omitempty"`
	Name      string `json:"name"`
}

A Stat holds a single statistic from a single match, and could be either a boolean or numeric statistic

type StatDescription

type StatDescription struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

StatDescription describes a single statistic in a schema

type StatDescriptions

type StatDescriptions []StatDescription

StatDescriptions holds multiple StatDescriptions for storing in one DB column

func (*StatDescriptions) Scan

func (sd *StatDescriptions) Scan(src interface{}) error

Scan implements sql.Scanner to scan JSON from the DB into StatDescriptions.

func (StatDescriptions) Value

func (sd StatDescriptions) Value() (driver.Value, error)

Value implements driver.Valuer to return JSON for the DB from StatDescription.

type Team

type Team struct {
	Key          string   `db:"key"`
	EventKey     string   `db:"event_key"`
	Rank         *int     `db:"rank"`
	RankingScore *float64 `db:"ranking_score"`
}

Team holds data about a single FRC team at a specific event.

type UnixTime

type UnixTime struct {
	Unix int64
}

UnixTime exists so that we can have times that look like time.Time's to database drivers and JSON marshallers/unmarshallers but are internally represented as unix timestamps for easier comparison.

func NewUnixFromInt

func NewUnixFromInt(time int64) UnixTime

NewUnixFromInt creates a new UnixTime timestamp from an int64.

func NewUnixFromTime

func NewUnixFromTime(time time.Time) UnixTime

NewUnixFromTime creates a new UnixTime timestamp from a time.Time.

func (*UnixTime) MarshalJSON

func (ut *UnixTime) MarshalJSON() ([]byte, error)

MarshalJSON returns a []byte that represents this UnixTime in RFC 3339 format.

func (*UnixTime) Scan

func (ut *UnixTime) Scan(src interface{}) error

Scan accepts either a time.Time or an int64 for scanning from a database into a unix timestamp.

func (*UnixTime) UnmarshalJSON

func (ut *UnixTime) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts a []byte representing a time.Time value, and unmarshals it into a unix timestamp.

func (UnixTime) Value

func (ut UnixTime) Value() (driver.Value, error)

Value returns a driver.Value that is always a time.Time that represents the internally stored unix time.

type User

type User struct {
	ID              int64          `json:"id" db:"id"`
	Username        string         `json:"username" db:"username"`
	HashedPassword  string         `json:"-" db:"hashed_password"`
	PasswordChanged UnixTime       `json:"-" db:"password_changed"`
	RealmID         int64          `json:"realmId" db:"realm_id"`
	FirstName       string         `json:"firstName" db:"first_name"`
	LastName        string         `json:"lastName" db:"last_name"`
	Roles           Roles          `json:"roles" db:"roles"`
	Stars           pq.StringArray `json:"stars" db:"stars"`
}

User holds information about a user such as their id, username, and hashed password.

Jump to

Keyboard shortcuts

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