Skip to content

Commit

Permalink
Merge pull request #228 from Pigmice2733/fix-team-stats
Browse files Browse the repository at this point in the history
Fix the match team stats endpoint
  • Loading branch information
fharding1 committed Oct 5, 2019
2 parents 8cf5026 + 59dfe57 commit 3083802
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion internal/server/reports.go
Expand Up @@ -28,7 +28,7 @@ func (s *Server) getReports() http.HandlerFunc {
realmID = &userRealmID
}

reports, err := s.Store.GetMatchTeamReportsForRealm(r.Context(), matchKey, teamKey, realmID)
reports, err := s.Store.GetMatchTeamReportsForRealm(r.Context(), eventKey, matchKey, teamKey, realmID)
if err != nil {
ihttp.Error(w, http.StatusInternalServerError)
s.Logger.WithError(err).Error("getting reports")
Expand Down
6 changes: 3 additions & 3 deletions internal/server/stats.go
Expand Up @@ -55,7 +55,7 @@ func (s *Server) eventStats() http.HandlerFunc {
return
}

storeMatches, err := s.Store.GetAnalysisInfoForRealm(r.Context(), eventKey, realmID)
storeMatches, err := s.Store.GetEventAnalysisInfoForRealm(r.Context(), eventKey, realmID)
if err != nil {
ihttp.Error(w, http.StatusInternalServerError)
s.Logger.WithError(err).Error("retrieving match analysis info")
Expand Down Expand Up @@ -112,7 +112,7 @@ func (s *Server) matchTeamStats() http.HandlerFunc {
// Add eventKey as prefix to matchKey so that matchKey is globally
// unique and consistent with TBA match keys.
matchKey := fmt.Sprintf("%s_%s", eventKey, partialMatchKey)
match, err := s.Store.GetMatchForRealm(r.Context(), matchKey, realmID)
match, err := s.Store.GetMatchAnalysisInfoForRealm(r.Context(), eventKey, matchKey, realmID)
if errors.Is(err, store.ErrNoResults{}) {
ihttp.Error(w, http.StatusNotFound)
return
Expand All @@ -122,7 +122,7 @@ func (s *Server) matchTeamStats() http.HandlerFunc {
return
}

reports, err := s.Store.GetMatchTeamReportsForRealm(r.Context(), eventKey, teamKey, realmID)
reports, err := s.Store.GetMatchTeamReportsForRealm(r.Context(), eventKey, matchKey, teamKey, realmID)
if err != nil {
ihttp.Error(w, http.StatusInternalServerError)
s.Logger.WithError(err).Error("retrieving reports")
Expand Down
72 changes: 44 additions & 28 deletions internal/store/matches.go
Expand Up @@ -138,7 +138,7 @@ func (s *Service) GetEventRealmIDByMatchKeyTx(ctx context.Context, tx *sqlx.Tx,

// GetMatchForRealm returns a specific match by key in the given realm.
func (s *Service) GetMatchForRealm(ctx context.Context, matchKey string, realmID *int64) (Match, error) {
query := matchesQuery + " AND matches.key = $2"
const query = matchesQuery + " AND matches.key = $2"

var m Match
err := s.db.GetContext(ctx, &m, query, realmID, matchKey)
Expand Down Expand Up @@ -273,39 +273,55 @@ func (s *Service) UpdateTBAMatches(ctx context.Context, eventKey string, matches
})
}

// GetAnalysisInfoForRealm returns match information that's pertinent to doing analysis by getting
const analysisInfoQuery = `
SELECT
matches.key,
r.team_keys AS red_alliance,
b.team_keys AS blue_alliance,
matches.red_score_breakdown,
matches.blue_score_breakdown
FROM
matches
INNER JOIN
alliances r
ON
matches.key = r.match_key AND r.is_blue = false
INNER JOIN
alliances b
ON
matches.key = b.match_key AND b.is_blue = true
INNER JOIN
events
ON
matches.event_key = events.key
WHERE
(events.realm_id = $1 OR events.realm_id IS NULL) AND
matches.event_key = $2`

// GetEventAnalysisInfoForRealm returns match information that's pertinent to doing analysis by getting
// all the matches with the given event key and either null or matching realm IDs.
func (s *Service) GetAnalysisInfoForRealm(ctx context.Context, eventKey string, realmID *int64) ([]Match, error) {
func (s *Service) GetEventAnalysisInfoForRealm(ctx context.Context, eventKey string, realmID *int64) ([]Match, error) {
matches := make([]Match, 0)

err := s.db.SelectContext(ctx, &matches, `
SELECT
matches.key,
r.team_keys AS red_alliance,
b.team_keys AS blue_alliance,
matches.red_score_breakdown,
matches.blue_score_breakdown
FROM
matches
INNER JOIN
alliances r
ON
matches.key = r.match_key AND r.is_blue = false
INNER JOIN
alliances b
ON
matches.key = b.match_key AND b.is_blue = true
INNER JOIN
events
ON
matches.event_key = events.key
WHERE
(events.realm_id = $1 OR events.realm_id IS NULL) AND
matches.event_key = $2
`, realmID, eventKey)
err := s.db.SelectContext(ctx, &matches, analysisInfoQuery, realmID, eventKey)
if err != nil {
return matches, fmt.Errorf("unable to get analysis info: %w", err)
}

return matches, nil
}

// GetMatchAnalysisInfoForRealm returns match information that's pertinent to doing analysis by getting
// all the matches with the given event key and either null or matching realm IDs.
func (s *Service) GetMatchAnalysisInfoForRealm(ctx context.Context, eventKey, matchKey string, realmID *int64) (match Match, err error) {
const query = analysisInfoQuery + "AND matches.key = $3"

err = s.db.GetContext(ctx, &match, query, realmID, eventKey, matchKey)
if err == sql.ErrNoRows {
return match, ErrNoResults{fmt.Errorf("unable to find match: %w", err)}
} else if err != nil {
return match, fmt.Errorf("unable to get analysis info: %w", err)
}

return match, nil
}
11 changes: 6 additions & 5 deletions internal/store/reports.go
Expand Up @@ -124,19 +124,20 @@ func (s *Service) GetEventTeamReportsForRealm(ctx context.Context, eventKey stri

// GetMatchTeamReportsForRealm retrieves all reports for a specific team and event, filtering to only retrieve reports for realms
// that are sharing reports or have a matching realm ID.
func (s *Service) GetMatchTeamReportsForRealm(ctx context.Context, matchKey string, teamKey string, realmID *int64) (reports []Report, err error) {
func (s *Service) GetMatchTeamReportsForRealm(ctx context.Context, eventKey, matchKey string, teamKey string, realmID *int64) (reports []Report, err error) {
const query = `
SELECT reports.*
FROM reports
INNER JOIN realms
ON realms.id = reports.realm_id
WHERE
reports.match_key = $1 AND
reports.team_key = $2 AND
(realms.share_reports = true OR realms.id = $3)`
reports.event_key = $1 AND
reports.match_key = $2 AND
reports.team_key = $3 AND
(realms.share_reports = true OR realms.id = $4)`

reports = make([]Report, 0)
return reports, s.db.SelectContext(ctx, &reports, query, matchKey, teamKey, realmID)
return reports, s.db.SelectContext(ctx, &reports, query, eventKey, matchKey, teamKey, realmID)
}

// GetLeaderboardForRealm retrieves leaderboard information from the reports and users table for users
Expand Down

0 comments on commit 3083802

Please sign in to comment.