diff --git a/internal/server/reports.go b/internal/server/reports.go index 8c47c2d9..16f69962 100644 --- a/internal/server/reports.go +++ b/internal/server/reports.go @@ -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") diff --git a/internal/server/stats.go b/internal/server/stats.go index c7b0377f..d379ccf6 100644 --- a/internal/server/stats.go +++ b/internal/server/stats.go @@ -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") @@ -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 @@ -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") diff --git a/internal/store/matches.go b/internal/store/matches.go index f2150a08..86d55524 100644 --- a/internal/store/matches.go +++ b/internal/store/matches.go @@ -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) @@ -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 +} diff --git a/internal/store/reports.go b/internal/store/reports.go index 72e6cae2..943f3b72 100644 --- a/internal/store/reports.go +++ b/internal/store/reports.go @@ -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