def _render(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) return medias_future = media_query.EventTeamsPreferredMediasQuery(event_key).fetch_async() next_match = MatchHelper.upcomingMatches(event.matches, num=1) next_match = next_match[0] if next_match else None team_and_medias = [] if next_match: # Organize medias by team teams = ndb.get_multi([ndb.Key(Team, team_key) for team_key in next_match.alliances['red']['teams'] + next_match.alliances['blue']['teams']]) image_medias = MediaHelper.get_images([media for media in medias_future.get_result()]) team_medias = defaultdict(list) for image_media in image_medias: for reference in image_media.references: team_medias[reference].append(image_media) stations = ['Red 1', 'Red 2', 'Red 3', 'Blue 1', 'Blue 2', 'Blue 3'] for i, team in enumerate(teams): team_and_medias.append((team, stations[i], team_medias.get(team.key, []))) self.template_values.update({ 'event': event, 'next_match': next_match, 'teams_and_media': team_and_medias, }) return jinja2_engine.render('nextmatch.html', self.template_values)
def send_upcoming_matches(cls, live_events): from helpers.match_helper import MatchHelper # PJL: Hacky :P # Causes circular import, otherwise # https://github.com/the-blue-alliance/the-blue-alliance/pull/1098#discussion_r25128966 now = datetime.datetime.utcnow() for event in live_events: matches = event.matches if not matches: continue last_matches = MatchHelper.recentMatches(matches, num=1) next_matches = MatchHelper.upcomingMatches(matches, num=2) # First, compare the difference between scheduled times of next/last match # Send an upcoming notification if it's <10 minutes, to account for events ahead of schedule if last_matches != []: last_match = last_matches[0] for i, next_match in enumerate(next_matches): if not next_match.push_sent and last_match.time and next_match.time: diff = next_match.time - last_match.time if diff < datetime.timedelta(minutes=10 * (i + 1)): cls.send_upcoming_match_notification(next_match, event) for match in next_matches: if match and not match.push_sent: # Only continue sending for the next match if a push hasn't already been sent for it if match.time is None or match.time + datetime.timedelta(minutes=-7) <= now: # Only send notifications for matches no more than 7 minutes (average-ish match cycle time) before it's scheduled to start # Unless, the match has no time info. Then #yolo and send it cls.send_upcoming_match_notification(match, event)
def generate_team_at_event_status(cls, team_key, event, matches=None): """ Generate a dict containing team@event status information :param team_key: Key name of the team to focus on :param event: Event object :param matches: Organized matches (via MatchHelper.organizeMatches) from the event, optional """ event_details = event.details if not matches: matches = event.matches team_matches = [m for m in matches if team_key in m.team_key_names] next_match = MatchHelper.upcomingMatches(team_matches, num=1) last_match = MatchHelper.recentMatches(team_matches, num=1) matches = MatchHelper.organizeMatches(matches) return copy.deepcopy( { 'qual': cls._build_qual_info(team_key, event_details, matches, event.year), 'alliance': cls._build_alliance_info(team_key, event_details, matches), 'playoff': cls._build_playoff_info(team_key, event_details, matches, event.year, event.playoff_type), 'last_match_key': last_match[0].key_name if last_match else None, 'next_match_key': next_match[0].key_name if next_match else None, } ) # TODO: Results are getting mixed unless copied. 2017-02-03 -fangeugene
def _render(self, team_number): team = Team.get_by_id("frc" + team_number) if not team: return self.redirect("/error/404") event_team_keys_future = EventTeam.query(EventTeam.team == team.key).fetch_async(1000, keys_only=True) award_keys_future = Award.query(Award.team == team.key).fetch_async(1000, keys_only=True) event_teams_futures = ndb.get_multi_async(event_team_keys_future.get_result()) awards_futures = ndb.get_multi_async(award_keys_future.get_result()) event_keys = [event_team_future.get_result().event for event_team_future in event_teams_futures] events_futures = ndb.get_multi_async(event_keys) awards_by_event = {} for award_future in awards_futures: award = award_future.get_result() if award.event.id() not in awards_by_event: awards_by_event[award.event.id()] = [award] else: awards_by_event[award.event.id()].append(award) event_awards = [] current_event = None matches_upcoming = None short_cache = False for event_future in events_futures: event = event_future.get_result() if event.now: current_event = event team_matches_future = Match.query(Match.event == event.key, Match.team_key_names == team.key_name)\ .fetch_async(500, keys_only=True) matches = ndb.get_multi(team_matches_future.get_result()) matches_upcoming = MatchHelper.upcomingMatches(matches) if event.within_a_day: short_cache = True if event.key_name in awards_by_event: sorted_awards = AwardHelper.organizeAwards(awards_by_event[event.key_name])['list'] else: sorted_awards = [] event_awards.append((event, sorted_awards)) event_awards = sorted(event_awards, key=lambda (e, _): e.start_date if e.start_date else datetime.datetime(e.year, 12, 31)) years = sorted(set([et.get_result().year for et in event_teams_futures if et.get_result().year != None])) template_values = {'team': team, 'event_awards': event_awards, 'years': years, 'current_event': current_event, 'matches_upcoming': matches_upcoming} if short_cache: self._cache_expiration = self.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_history.html') return template.render(path, template_values)
def __init__(self, event): from helpers.match_helper import MatchHelper # recursive import issues self.event = event self._event_feed = event.key_name self._district_feed = event.event_district_abbrev upcoming = MatchHelper.upcomingMatches(event.matches, 1) self.next_match = upcoming[0] if upcoming[0] else None
def next_match(self): from helpers.match_helper import MatchHelper upcoming_matches = MatchHelper.upcomingMatches(self.matches, 1) if upcoming_matches: return upcoming_matches[0] else: return None
def post(self): self._require_admin() event_key = self.request.get('event_key') matches_csv = self.request.get('matches_csv') matches = OffseasonMatchesParser.parse(matches_csv) event = Event.get_by_id(event_key) matches = [Match( id=Match.renderKeyName( event.key.id(), match.get("comp_level", None), match.get("set_number", 0), match.get("match_number", 0)), event=event.key, game=Match.FRC_GAMES_BY_YEAR.get(event.year, "frc_unknown"), set_number=match.get("set_number", 0), match_number=match.get("match_number", 0), comp_level=match.get("comp_level", None), team_key_names=match.get("team_key_names", None), alliances_json=match.get("alliances_json", None) ) for match in matches] new_matches = MatchManipulator.createOrUpdate(matches) try: last_matches = MatchHelper.recentMatches(new_matches, 1) upcoming_matches = MatchHelper.upcomingMatches(new_matches, 8) except: logging.warning("Computing last/upcoming matches for Firebase failed!") try: FirebasePusher.updateEvent(event, last_matches, upcoming_matches) except: logging.warning("Enqueuing Firebase push failed!") self.redirect('/admin/event/{}'.format(event_key))
def _render(self, event_key): event = Event.get_by_id(event_key) if not event: return self.redirect("/error/404") event.prepAwards() event.prepMatches() event.prepTeams() awards = AwardHelper.organizeAwards(event.awards) matches = MatchHelper.organizeMatches(event.matches) teams = TeamHelper.sortTeams(event.teams) num_teams = len(teams) middle_value = num_teams/2 if num_teams%2 != 0: middle_value += 1 teams_a, teams_b = teams[:middle_value], teams[middle_value:] oprs = sorted(zip(event.oprs,event.opr_teams), reverse=True) # sort by OPR oprs = oprs[:14] # get the top 15 OPRs if event.within_a_day: matches_recent = MatchHelper.recentMatches(event.matches) matches_upcoming = MatchHelper.upcomingMatches(event.matches) else: matches_recent = None matches_upcoming = None bracket_table = {} qf_matches = matches['qf'] sf_matches = matches['sf'] f_matches = matches['f'] if qf_matches: bracket_table['qf'] = MatchHelper.generateBracket(qf_matches) if sf_matches: bracket_table['sf'] = MatchHelper.generateBracket(sf_matches) if f_matches: bracket_table['f'] = MatchHelper.generateBracket(f_matches) template_values = { "event": event, "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, } if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/event_details.html') return template.render(path, template_values)
def get(self): self._require_registration() current_events = filter(lambda e: e.now, EventHelper.getEventsWithinADay()) popular_teams_events = TeamHelper.getPopularTeamsEvents(current_events) popular_team_keys = set() for team, _ in popular_teams_events: popular_team_keys.add(team.key.id()) for event in current_events: event.prep_details() event.prep_matches() finished_matches = [] current_matches = [] upcoming_matches = [] ranks = {} alliances = {} for event in current_events: if not event.details: continue finished_matches += MatchHelper.recentMatches(event.matches, num=1) for i, match in enumerate(MatchHelper.upcomingMatches(event.matches, num=3)): if not match.time: continue if not event.details.predictions or match.key.id() not in event.details.predictions['match_predictions']['qual' if match.comp_level == 'qm' else 'playoff']: match.prediction = defaultdict(lambda: defaultdict(float)) match.bluezone_score = 0 else: match.prediction = event.details.predictions['match_predictions']['qual' if match.comp_level == 'qm' else 'playoff'][match.key.id()] match.bluezone_score = self.get_qual_bluezone_score(match.prediction) if match.comp_level == 'qm' else self.get_elim_bluezone_score(match.prediction) if i == 0: current_matches.append(match) else: upcoming_matches.append(match) if event.details.rankings2: for rank in event.details.rankings2: ranks[rank['team_key']] = rank['rank'] if event.alliance_selections: for i, alliance in enumerate(event.alliance_selections): for pick in alliance['picks']: alliances[pick] = i + 1 finished_matches = sorted(finished_matches, key=lambda m: m.actual_time if m.actual_time else m.time) current_matches = sorted(current_matches, key=lambda m: m.predicted_time if m.predicted_time else m.time) upcoming_matches = sorted(upcoming_matches, key=lambda m: m.predicted_time if m.predicted_time else m.time) self.template_values.update({ 'finished_matches': finished_matches, 'current_matches': current_matches, 'upcoming_matches': upcoming_matches, 'ranks': ranks, 'alliances': alliances, 'popular_team_keys': popular_team_keys, }) self.response.out.write(jinja2_engine.render('match_suggestion.html', self.template_values))
def render_team_history(cls, handler, team, is_canonical): award_futures = award_query.TeamAwardsQuery(team.key.id()).fetch_async() event_futures = event_query.TeamEventsQuery(team.key.id()).fetch_async() participation_future = team_query.TeamParticipationQuery(team.key.id()).fetch_async() social_media_future = media_query.TeamSocialMediaQuery(team.key.id()).fetch_async() awards_by_event = {} for award in award_futures.get_result(): if award.event.id() not in awards_by_event: awards_by_event[award.event.id()] = [award] else: awards_by_event[award.event.id()].append(award) event_awards = [] current_event = None matches_upcoming = None short_cache = False years = set() for event in event_futures.get_result(): years.add(event.year) if event.now: current_event = event matches = match_query.TeamEventMatchesQuery(team.key.id(), event.key.id()).fetch() matches_upcoming = MatchHelper.upcomingMatches(matches) if event.within_a_day: short_cache = True if event.key_name in awards_by_event: sorted_awards = AwardHelper.organizeAwards(awards_by_event[event.key_name]) else: sorted_awards = [] event_awards.append((event, sorted_awards)) event_awards = sorted(event_awards, key=lambda (e, _): e.start_date if e.start_date else datetime.datetime(e.year, 12, 31)) last_competed = None participation_years = participation_future.get_result() if len(participation_years) > 0: last_competed = max(participation_years) current_year = datetime.date.today().year social_medias = sorted(social_media_future.get_result(), key=MediaHelper.social_media_sorter) handler.template_values.update({ 'is_canonical': is_canonical, 'team': team, 'event_awards': event_awards, 'years': sorted(years), "social_medias": social_medias, 'current_event': current_event, 'matches_upcoming': matches_upcoming, 'last_competed': last_competed, 'current_year': current_year }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION return jinja2_engine.render('team_history.html', handler.template_values)
def _render(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) event.prepAwardsMatchesTeams() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) num_teams = len(teams) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = teams[:middle_value], teams[middle_value:] oprs = [i for i in event.matchstats['oprs'].items()] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.within_a_day: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = {} qf_matches = matches['qf'] sf_matches = matches['sf'] f_matches = matches['f'] if qf_matches: bracket_table['qf'] = MatchHelper.generateBracket(qf_matches) if sf_matches: bracket_table['sf'] = MatchHelper.generateBracket(sf_matches) if f_matches: bracket_table['f'] = MatchHelper.generateBracket(f_matches) template_values = { "event": event, "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, } if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/event_details.html') return template.render(path, template_values)
def __init__(self, event, next_match=None): from helpers.match_helper import MatchHelper # recursive import issues self.event = event if not next_match: upcoming = MatchHelper.upcomingMatches(event.matches, 1) self.next_match = upcoming[0] if upcoming and len(upcoming) > 0 else None else: self.next_match = next_match
def __init__(self, event, next_match=None): from helpers.match_helper import MatchHelper # recursive import issues self.event = event self._event_feed = event.key_name self._district_feed = event.event_district_abbrev if not next_match: upcoming = MatchHelper.upcomingMatches(event.matches, 1) self.next_match = upcoming[0] if upcoming and len(upcoming) > 0 else None else: self.next_match = next_match
def render_team_history(cls, handler, team, is_canonical): award_futures = award_query.TeamAwardsQuery( team.key.id()).fetch_async() event_futures = event_query.TeamEventsQuery( team.key.id()).fetch_async() awards_by_event = {} for award in award_futures.get_result(): if award.event.id() not in awards_by_event: awards_by_event[award.event.id()] = [award] else: awards_by_event[award.event.id()].append(award) event_awards = [] current_event = None matches_upcoming = None short_cache = False years = set() for event in event_futures.get_result(): years.add(event.year) if event.now: current_event = event matches = match_query.TeamEventMatchesQuery( team.key.id(), event.key.id()).fetch() matches_upcoming = MatchHelper.upcomingMatches(matches) if event.within_a_day: short_cache = True if event.key_name in awards_by_event: sorted_awards = AwardHelper.organizeAwards( awards_by_event[event.key_name]) else: sorted_awards = [] event_awards.append((event, sorted_awards)) event_awards = sorted( event_awards, key=lambda (e, _): e.start_date if e.start_date else datetime.datetime(e.year, 12, 31)) handler.template_values.update({ 'is_canonical': is_canonical, 'team': team, 'event_awards': event_awards, 'years': sorted(years), 'current_event': current_event, 'matches_upcoming': matches_upcoming }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_history.html') return template.render(path, handler.template_values)
def _render(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) event.prepAwardsMatchesTeams() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) num_teams = len(teams) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = teams[:middle_value], teams[middle_value:] oprs = [i for i in event.matchstats['oprs'].items()] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.within_a_day: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = MatchHelper.generateBracket(matches, event.alliance_selections) district_points_sorted = None if event.district_points: district_points_sorted = sorted(event.district_points['points'].items(), key=lambda (team, points): -points['total']) self.template_values.update({ "event": event, "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, "district_points_sorted": district_points_sorted, }) if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/event_details.html') return template.render(path, self.template_values)
def __init__(self, event, next_match=None): from helpers.match_helper import MatchHelper # recursive import issues self.event = event self._event_feed = event.key_name self._district_feed = event.event_district_abbrev if not next_match: upcoming = MatchHelper.upcomingMatches(event.matches, 1) self.next_match = upcoming[0] if upcoming and len( upcoming) > 0 else None else: self.next_match = next_match
def test_build(self): expected = {} expected['notification_type'] = NotificationType.type_names[NotificationType.SCHEDULE_UPDATED] expected['message_data'] = {} expected['message_data']['event_key'] = self.event.key_name expected['message_data']['event_name'] = self.event.name upcoming = MatchHelper.upcomingMatches(self.event.matches, 1) expected['message_data']['first_match_time'] = calendar.timegm(upcoming[0].time.utctimetuple()) data = self.notification._build_dict() self.assertEqual(expected, data)
def get(self): self._require_registration() current_events = filter(lambda e: e.now, EventHelper.getEventsWithinADay()) for event in current_events: event.prep_details() event.prep_matches() finished_matches = [] current_matches = [] upcoming_matches = [] ranks = {} alliances = {} for event in current_events: if not event.details: continue finished_matches += MatchHelper.recentMatches(event.matches, num=1) for i, match in enumerate(MatchHelper.upcomingMatches(event.matches, num=3)): if not match.time: continue if not event.details.predictions or match.key.id() not in event.details.predictions['match_predictions']['qual' if match.comp_level == 'qm' else 'playoff']: match.prediction = defaultdict(lambda: defaultdict(float)) match.bluezone_score = 0 else: match.prediction = event.details.predictions['match_predictions']['qual' if match.comp_level == 'qm' else 'playoff'][match.key.id()] match.bluezone_score = self.get_qual_bluezone_score(match.prediction) if match.comp_level == 'qm' else self.get_elim_bluezone_score(match.prediction) if i == 0: current_matches.append(match) else: upcoming_matches.append(match) if event.details.rankings2: for rank in event.details.rankings2: ranks[rank['team_key']] = rank['rank'] if event.alliance_selections: for i, alliance in enumerate(event.alliance_selections): for pick in alliance['picks']: alliances[pick] = i + 1 finished_matches = sorted(finished_matches, key=lambda m: m.actual_time if m.actual_time else m.time) current_matches = sorted(current_matches, key=lambda m: m.predicted_time if m.predicted_time else m.time) upcoming_matches = sorted(upcoming_matches, key=lambda m: m.predicted_time if m.predicted_time else m.time) self.template_values.update({ 'finished_matches': finished_matches, 'current_matches': current_matches, 'upcoming_matches': upcoming_matches, 'ranks': ranks, 'alliances': alliances, }) self.response.out.write(jinja2_engine.render('match_suggestion.html', self.template_values))
def send_upcoming_matches(cls, live_events): from helpers.match_helper import MatchHelper # PJL: Hacky :P # Causes circular import, otherwise # https://github.com/the-blue-alliance/the-blue-alliance/pull/1098#discussion_r25128966 down_events = [] now = datetime.datetime.utcnow() for event in live_events: matches = event.matches if not matches: continue last_matches = MatchHelper.recentMatches(matches, num=1) next_matches = MatchHelper.upcomingMatches(matches, num=2) # First, compare the difference between scheduled times of next/last match # Send an upcoming notification if it's <10 minutes, to account for events ahead of schedule if last_matches != []: last_match = last_matches[0] for i, next_match in enumerate(next_matches): if not next_match.push_sent and last_match.time and next_match.time: diff = next_match.time - last_match.time if diff < datetime.timedelta(minutes=10 * (i + 1)): cls.send_upcoming_match_notification( next_match, event) for match in next_matches: if match and not match.push_sent: # Only continue sending for the next match if a push hasn't already been sent for it if match.time is None or match.time + datetime.timedelta( minutes=-7) <= now: # Only send notifications for matches no more than 7 minutes (average-ish match cycle time) before it's scheduled to start # Unless, the match has no time info. Then #yolo and send it cls.send_upcoming_match_notification(match, event) # Determine if event is down if cls.is_event_down(last_matches[0] if last_matches else None, next_matches[0] if next_matches else None): down_events.append(event.key_name) # Update the status sitevar status_sitevar = Sitevar.get_by_id('apistatus.down_events') if status_sitevar is None: status_sitevar = Sitevar(id="apistatus.down_events", description="A list of down event keys", values_json="[]") old_status = status_sitevar.contents status_sitevar.contents = down_events status_sitevar.put() # Clear API Response cache ApiStatusController.clear_cache_if_needed(old_status, down_events)
def schedule_upcoming_matches(cls, event, user_id=None): # Schedule `match_upcoming` notifications for Match 1 and Match 2 # Match 3 (and onward) will be dispatched after Match 1 (or Match N - 2) has been played if not event.matches: logging.error( 'Unable to schedule `match_upcoming` notification for {} - no matches' .format(event.key_name)) return from helpers.match_helper import MatchHelper next_matches = MatchHelper.upcomingMatches(event.matches, num=2) for match in next_matches: cls.schedule_upcoming_match(match, user_id)
def get(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) matches = event.matches if not matches: return timezone = pytz.timezone(event.timezone_id) played_matches = MatchHelper.recentMatches(matches, num=0) unplayed_matches = MatchHelper.upcomingMatches(matches, num=10) MatchTimePredictionHelper.predict_future_matches(played_matches, unplayed_matches, timezone, event.within_a_day)
def get(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) matches = event.matches if not matches: return timezone = pytz.timezone(event.timezone_id) played_matches = MatchHelper.recentMatches(matches, num=0) unplayed_matches = MatchHelper.upcomingMatches(matches, num=10) MatchTimePredictionHelper.predict_future_matches( played_matches, unplayed_matches, timezone, event.within_a_day)
def test_build(self): expected = {} expected['message_type'] = NotificationType.type_names[ NotificationType.SCHEDULE_UPDATED] expected['message_data'] = {} expected['message_data']['event_key'] = self.event.key_name expected['message_data']['event_name'] = self.event.name upcoming = MatchHelper.upcomingMatches(self.event.matches, 1) expected['message_data']['first_match_time'] = calendar.timegm( upcoming[0].time.utctimetuple()) data = self.notification._build_dict() self.assertEqual(expected, data)
def get(self, event_key): import pytz event = Event.get_by_id(event_key) if not event: self.abort(404) matches = event.matches if not matches or not event.timezone_id: return timezone = pytz.timezone(event.timezone_id) played_matches = MatchHelper.recentMatches(matches, num=0) unplayed_matches = MatchHelper.upcomingMatches(matches, num=len(matches)) MatchTimePredictionHelper.predict_future_matches( event_key, played_matches, unplayed_matches, timezone, event.within_a_day) # Detect whether the event is down # An event NOT down if ANY unplayed match's predicted time is within its scheduled time by a threshold and # the last played match (if it exists) wasn't too long ago. event_down = len(unplayed_matches) > 0 for unplayed_match in unplayed_matches: if ((unplayed_match.predicted_time and unplayed_match.time and unplayed_match.predicted_time < unplayed_match.time + datetime.timedelta(minutes=30)) or (played_matches == [] or played_matches[-1].actual_time is None or played_matches[-1].actual_time > datetime.datetime.now() - datetime.timedelta(minutes=30))): event_down = False break status_sitevar = Sitevar.get_by_id('apistatus.down_events') if status_sitevar is None: status_sitevar = Sitevar(id="apistatus.down_events", description="A list of down event keys", values_json="[]") old_status = set(status_sitevar.contents) new_status = old_status.copy() if event_down: new_status.add(event_key) elif event_key in new_status: new_status.remove(event_key) status_sitevar.contents = list(new_status) status_sitevar.put() # Clear API Response cache ApiStatusController.clear_cache_if_needed(old_status, new_status)
def match_score(cls, match, user_id=None): event = match.event.get() from models.notifications.match_score import MatchScoreNotification # Send to Event subscribers if NotificationType.MATCH_SCORE in NotificationType.enabled_event_notifications: users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_event( event, NotificationType.MATCH_SCORE) if users: cls._send(users, MatchScoreNotification(match)) # Send to Team subscribers if NotificationType.MATCH_SCORE in NotificationType.enabled_team_notifications: for team_key in match.team_keys: users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_team( team_key.get(), NotificationType.MATCH_SCORE) if users: cls._send(users, MatchScoreNotification(match, team_key.get())) # Send to Match subscribers if NotificationType.MATCH_SCORE in NotificationType.enabled_match_notifications: users = [user_id] if user_id else [] if not users: users = Subscription.users_subscribed_to_match( match, NotificationType.MATCH_SCORE) if users: cls._send(users, MatchScoreNotification(match)) # Send UPCOMING_MATCH for the N + 2 match after this one if not event.matches: return from helpers.match_helper import MatchHelper next_matches = MatchHelper.upcomingMatches(event.matches, num=2) # TODO: Think about if we need special-case handling for replayed matches # (I don't think we do because if a match gets replayed at EoD, we'll cancel/reschedule # for that match notification. If a match gets replayed back-to-back (which doesn't happen?) # sending a second notification is probably fine. # If there are not 2 scheduled matches (end of Quals, end of Quarters, etc.) don't send if len(next_matches) < 2: return next_match = next_matches.pop() cls.schedule_upcoming_match(next_match, user_id)
def send_upcoming_matches(cls, live_events): from helpers.match_helper import MatchHelper # PJL: Hacky :P # Causes circular import, otherwise # https://github.com/the-blue-alliance/the-blue-alliance/pull/1098#discussion_r25128966 down_events = [] now = datetime.datetime.utcnow() for event in live_events: matches = event.matches if not matches: continue last_matches = MatchHelper.recentMatches(matches, num=1) next_matches = MatchHelper.upcomingMatches(matches, num=2) # First, compare the difference between scheduled times of next/last match # Send an upcoming notification if it's <10 minutes, to account for events ahead of schedule if last_matches != []: last_match = last_matches[0] for i, next_match in enumerate(next_matches): if not next_match.push_sent and last_match.time and next_match.time: diff = next_match.time - last_match.time if diff < datetime.timedelta(minutes=10 * (i + 1)): cls.send_upcoming_match_notification(next_match, event) for match in next_matches: if match and not match.push_sent: # Only continue sending for the next match if a push hasn't already been sent for it if match.time is None or match.time + datetime.timedelta(minutes=-7) <= now: # Only send notifications for matches no more than 7 minutes (average-ish match cycle time) before it's scheduled to start # Unless, the match has no time info. Then #yolo and send it cls.send_upcoming_match_notification(match, event) # Determine if event is down if cls.is_event_down(last_matches[0] if last_matches else None, next_matches[0] if next_matches else None): down_events.append(event.key_name) # Update the status sitevar status_sitevar = Sitevar.get_by_id('apistatus.down_events') if status_sitevar is None: status_sitevar = Sitevar(id="apistatus.down_events", description="A list of down event keys", values_json="[]") old_status = status_sitevar.contents status_sitevar.contents = down_events status_sitevar.put() # Clear API Response cache ApiStatusController.clear_cache_if_needed(old_status, down_events)
def render_team_history(cls, handler, team, is_canonical): award_futures = award_query.TeamAwardsQuery(team.key.id()).fetch_async() event_futures = event_query.TeamEventsQuery(team.key.id()).fetch_async() awards_by_event = {} for award in award_futures.get_result(): if award.event.id() not in awards_by_event: awards_by_event[award.event.id()] = [award] else: awards_by_event[award.event.id()].append(award) event_awards = [] current_event = None matches_upcoming = None short_cache = False years = set() for event in event_futures.get_result(): years.add(event.year) if event.now: current_event = event matches = match_query.TeamEventMatchesQuery(team.key.id(), event.key.id()).fetch() matches_upcoming = MatchHelper.upcomingMatches(matches) if event.within_a_day: short_cache = True if event.key_name in awards_by_event: sorted_awards = AwardHelper.organizeAwards(awards_by_event[event.key_name]) else: sorted_awards = [] event_awards.append((event, sorted_awards)) event_awards = sorted(event_awards, key=lambda (e, _): e.start_date if e.start_date else datetime.datetime(e.year, 12, 31)) handler.template_values.update({ 'is_canonical': is_canonical, 'team': team, 'event_awards': event_awards, 'years': sorted(years), 'current_event': current_event, 'matches_upcoming': matches_upcoming }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_history.html') return template.render(path, handler.template_values)
def get(self, event_key): import pytz event = Event.get_by_id(event_key) if not event: self.abort(404) matches = event.matches if not matches or not event.timezone_id: return timezone = pytz.timezone(event.timezone_id) played_matches = MatchHelper.recentMatches(matches, num=0) unplayed_matches = MatchHelper.upcomingMatches(matches, num=len(matches)) MatchTimePredictionHelper.predict_future_matches(event_key, played_matches, unplayed_matches, timezone, event.within_a_day) # Detect whether the event is down # An event NOT down if ANY unplayed match's predicted time is within its scheduled time by a threshold and # the last played match (if it exists) wasn't too long ago. event_down = len(unplayed_matches) > 0 for unplayed_match in unplayed_matches: if ((unplayed_match.predicted_time and unplayed_match.time and unplayed_match.predicted_time < unplayed_match.time + datetime.timedelta(minutes=30)) or (played_matches == [] or played_matches[-1].actual_time is None or played_matches[-1].actual_time > datetime.datetime.now() - datetime.timedelta(minutes=30))): event_down = False break status_sitevar = Sitevar.get_by_id('apistatus.down_events') if status_sitevar is None: status_sitevar = Sitevar(id="apistatus.down_events", description="A list of down event keys", values_json="[]") old_status = set(status_sitevar.contents) new_status = old_status.copy() if event_down: new_status.add(event_key) elif event_key in new_status: new_status.remove(event_key) status_sitevar.contents = list(new_status) status_sitevar.put() # Clear API Response cache ApiStatusController.clear_cache_if_needed(old_status, new_status)
def schedule_upcoming_matches(cls, event, user_id=None): # Schedule `match_upcoming` notifications for Match 1 and Match 2 # Match 3 (and onward) will be dispatched after Match 1 (or Match N - 2) has been played if not event.matches: logging.error('Unable to schedule `match_upcoming` notification for {} - no matches'.format(event.key_name)) return from helpers.match_helper import MatchHelper next_matches = MatchHelper.upcomingMatches(event.matches, num=2) if len(next_matches) == 0: return # Only schedule/send upcoming matches for new levels - if a schedule gets updated mid-way through the event, don't # bother sending new notifications (this is to prevent a bug where we send a bunch of duplicate notifications) if not (next_matches[0].set_number == 1 and next_matches[0].match_number == 1): return for match in next_matches: cls.schedule_upcoming_match(match, user_id)
def generate_team_at_event_status(cls, team_key, event, matches=None): """ Generate a dict containing team@event status information :param team_key: Key name of the team to focus on :param event: Event object :param matches: Organized matches (via MatchHelper.organizeMatches) from the event, optional """ event_details = event.details if not matches: matches = event.matches team_matches = [m for m in matches if team_key in m.team_key_names] next_match = MatchHelper.upcomingMatches(team_matches, num=1) last_match = MatchHelper.recentMatches(team_matches, num=1) matches = MatchHelper.organizeMatches(matches) return copy.deepcopy({ 'qual': cls._build_qual_info(team_key, event_details, matches, event.year), 'alliance': cls._build_alliance_info(team_key, event_details, matches), 'playoff': cls._build_playoff_info(team_key, event_details, matches, event.year, event.playoff_type), 'last_match_key': last_match[0].key_name if last_match else None, 'next_match_key': next_match[0].key_name if next_match else None, }) # TODO: Results are getting mixed unless copied. 2017-02-03 -fangeugene
def get(self): self._require_registration() current_events = filter(lambda e: e.now, EventHelper.getEventsWithinADay()) for event in current_events: event.prep_details() event.prep_matches() finished_matches = [] current_matches = [] upcoming_matches = [] ranks = {} for event in current_events: finished_matches += MatchHelper.recentMatches(event.matches, num=1) for i, match in enumerate(MatchHelper.upcomingMatches(event.matches, num=3)): if match.key.id() not in event.details.predictions['match_predictions']['qual' if match.comp_level == 'qm' else 'playoff']: match.prediction = defaultdict(lambda: defaultdict()) match.bluezone_score = 0 continue match.prediction = event.details.predictions['match_predictions']['qual' if match.comp_level == 'qm' else 'playoff'][match.key.id()] match.bluezone_score = self.get_qual_bluezone_score(match.prediction) if match.comp_level == 'qm' else self.get_elim_bluezone_score(match.prediction) if i == 0: current_matches.append(match) else: upcoming_matches.append(match) for rank in event.details.rankings2: ranks[rank['team_key']] = rank['rank'] finished_matches = sorted(finished_matches, key=lambda m: m.actual_time if m.actual_time else m.time) current_matches = sorted(current_matches, key=lambda m: m.predicted_time if m.predicted_time else m.time) upcoming_matches = sorted(upcoming_matches, key=lambda m: m.predicted_time if m.predicted_time else m.time) self.template_values.update({ 'finished_matches': finished_matches, 'current_matches': current_matches, 'upcoming_matches': upcoming_matches, 'ranks': ranks, }) self.response.out.write(jinja2_engine.render('match_suggestion.html', self.template_values))
def get(self, event_key): df = DatafeedUsfirst() event = Event.get_by_id(event_key) new_matches = MatchManipulator.createOrUpdate(df.getMatches(event)) try: last_matches = MatchHelper.recentMatches(new_matches, 1) upcoming_matches = MatchHelper.upcomingMatches(new_matches, 8) except: logging.warning("Computing last/upcoming matches for Firebase failed!") try: FirebasePusher.updateEvent(event, last_matches, upcoming_matches) except: logging.warning("Enqueuing Firebase push failed!") template_values = { 'matches': new_matches, } path = os.path.join(os.path.dirname(__file__), '../templates/datafeeds/usfirst_matches_get.html') self.response.out.write(template.render(path, template_values))
def render_team_details(cls, handler, team, year, is_canonical): hof_award_future = award_query.TeamEventTypeAwardsQuery(team.key.id(), EventType.CMP_FINALS, AwardType.CHAIRMANS).fetch_async() hof_video_future = media_query.TeamTagMediasQuery(team.key.id(), MediaTag.CHAIRMANS_VIDEO).fetch_async() hof_presentation_future = media_query.TeamTagMediasQuery(team.key.id(), MediaTag.CHAIRMANS_PRESENTATION).fetch_async() hof_essay_future = media_query.TeamTagMediasQuery(team.key.id(), MediaTag.CHAIRMANS_ESSAY).fetch_async() media_future = media_query.TeamYearMediaQuery(team.key.id(), year).fetch_async() social_media_future = media_query.TeamSocialMediaQuery(team.key.id()).fetch_async() robot_future = Robot.get_by_id_async("{}_{}".format(team.key.id(), year)) team_districts_future = team_query.TeamDistrictsQuery(team.key.id()).fetch_async() participation_future = team_query.TeamParticipationQuery(team.key.id()).fetch_async() hof_awards = hof_award_future.get_result() hof_video = hof_video_future.get_result() hof_presentation = hof_presentation_future.get_result() hof_essay = hof_essay_future.get_result() hall_of_fame = { "is_hof": len(hof_awards) > 0, "years": [award.year for award in hof_awards], "media": { "video": hof_video[0].youtube_url_link if len(hof_video) > 0 else None, "presentation": hof_presentation[0].youtube_url_link if len(hof_presentation) > 0 else None, "essay": hof_essay[0].external_link if len(hof_essay) > 0 else None, }, } events_sorted, matches_by_event_key, awards_by_event_key, valid_years = TeamDetailsDataFetcher.fetch(team, year, return_valid_years=True) if not events_sorted: return None district_name = None district_abbrev = None team_district_points = None team_districts = team_districts_future.get_result() for district in team_districts: if district and district.year == year: district_abbrev = district.abbreviation district_name = district.display_name if district.rankings: team_district_points = next( iter(filter(lambda r: r['team_key'] == team.key_name, district.rankings)), None) break participation = [] season_wlt_list = [] offseason_wlt_list = [] year_match_avg_list = [] current_event = None matches_upcoming = None short_cache = False for event in events_sorted: event_matches = matches_by_event_key.get(event.key, []) event_awards = AwardHelper.organizeAwards(awards_by_event_key.get(event.key, [])) matches_organized = MatchHelper.organizeMatches(event_matches) if event.now: current_event = event matches_upcoming = MatchHelper.upcomingMatches(event_matches) if event.within_a_day: short_cache = True if year == 2015: display_wlt = None match_avg = EventHelper.calculateTeamAvgScoreFromMatches(team.key_name, event_matches) year_match_avg_list.append(match_avg) qual_avg, elim_avg, _, _ = match_avg else: qual_avg = None elim_avg = None wlt = EventHelper.calculateTeamWLTFromMatches(team.key_name, event_matches) if event.event_type_enum in EventType.SEASON_EVENT_TYPES: season_wlt_list.append(wlt) else: offseason_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if event.details and event.details.rankings2: for ranking in event.details.rankings2: if ranking['team_key'] == team.key.id(): team_rank = ranking['rank'] break video_ids = [] playlist = "" for level in Match.COMP_LEVELS: matches = matches_organized[level] for match in matches: video_ids += [video.split("?")[0] for video in match.youtube_videos] if video_ids: playlist_title = u"{} (Team {})".format(event.name, team.team_number) playlist = u"https://www.youtube.com/watch_videos?video_ids={}&title={}" playlist = playlist.format(u",".join(video_ids), playlist_title) district_points = None if team_district_points: district_points = next( iter( filter(lambda e: e['event_key'] == event.key_name, team_district_points['event_points'])), None) participation.append({ "event": event, "matches": matches_organized, "wlt": display_wlt, "qual_avg": qual_avg, "elim_avg": elim_avg, "rank": team_rank, "awards": event_awards, "playlist": playlist, "district_points": district_points, }) season_wlt = None offseason_wlt = None if year == 2015: year_wlt = None year_qual_scores = [] year_elim_scores = [] for _, _, event_qual_scores, event_elim_scores in year_match_avg_list: year_qual_scores += event_qual_scores year_elim_scores += event_elim_scores year_qual_avg = float(sum(year_qual_scores)) / len(year_qual_scores) if year_qual_scores != [] else None year_elim_avg = float(sum(year_elim_scores)) / len(year_elim_scores) if year_elim_scores != [] else None else: year_qual_avg = None year_elim_avg = None season_wlt = {"win": 0, "loss": 0, "tie": 0} offseason_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in season_wlt_list: season_wlt["win"] += wlt["win"] season_wlt["loss"] += wlt["loss"] season_wlt["tie"] += wlt["tie"] if season_wlt["win"] + season_wlt["loss"] + season_wlt["tie"] == 0: season_wlt = None for wlt in offseason_wlt_list: offseason_wlt["win"] += wlt["win"] offseason_wlt["loss"] += wlt["loss"] offseason_wlt["tie"] += wlt["tie"] if offseason_wlt["win"] + offseason_wlt["loss"] + offseason_wlt["tie"] == 0: offseason_wlt = None medias_by_slugname = MediaHelper.group_by_slugname([media for media in media_future.get_result()]) avatar = MediaHelper.get_avatar(media_future.get_result()) image_medias = MediaHelper.get_images(media_future.get_result()) social_medias = sorted(social_media_future.get_result(), key=MediaHelper.social_media_sorter) preferred_image_medias = filter(lambda x: team.key in x.preferred_references, image_medias) last_competed = None participation_years = participation_future.get_result() if len(participation_years) > 0: last_competed = max(participation_years) current_year = datetime.date.today().year handler.template_values.update({ "is_canonical": is_canonical, "team": team, "participation": participation, "year": year, "years": valid_years, "season_wlt": season_wlt, "offseason_wlt": offseason_wlt, "year_qual_avg": year_qual_avg, "year_elim_avg": year_elim_avg, "current_event": current_event, "matches_upcoming": matches_upcoming, "medias_by_slugname": medias_by_slugname, "avatar": avatar, "social_medias": social_medias, "image_medias": image_medias, "preferred_image_medias": preferred_image_medias, "robot": robot_future.get_result(), "district_name": district_name, "district_abbrev": district_abbrev, "last_competed": last_competed, "current_year": current_year, "max_year": tba_config.MAX_YEAR, "hof": hall_of_fame, "team_district_points": team_district_points, }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION return jinja2_engine.render("team_details.html", handler.template_values)
def render_team_history(cls, handler, team, is_canonical): event_team_keys_future = EventTeam.query( EventTeam.team == team.key).fetch_async(1000, keys_only=True) award_keys_future = Award.query( Award.team_list == team.key).fetch_async(1000, keys_only=True) event_teams_futures = ndb.get_multi_async( event_team_keys_future.get_result()) awards_futures = ndb.get_multi_async(award_keys_future.get_result()) event_keys = [ event_team_future.get_result().event for event_team_future in event_teams_futures ] events_futures = ndb.get_multi_async(event_keys) awards_by_event = {} for award_future in awards_futures: award = award_future.get_result() if award.event.id() not in awards_by_event: awards_by_event[award.event.id()] = [award] else: awards_by_event[award.event.id()].append(award) event_awards = [] current_event = None matches_upcoming = None short_cache = False for event_future in events_futures: event = event_future.get_result() if event.now: current_event = event team_matches_future = Match.query(Match.event == event.key, Match.team_key_names == team.key_name)\ .fetch_async(500, keys_only=True) matches = ndb.get_multi(team_matches_future.get_result()) matches_upcoming = MatchHelper.upcomingMatches(matches) if event.within_a_day: short_cache = True if event.key_name in awards_by_event: sorted_awards = AwardHelper.organizeAwards( awards_by_event[event.key_name]) else: sorted_awards = [] event_awards.append((event, sorted_awards)) event_awards = sorted( event_awards, key=lambda (e, _): e.start_date if e.start_date else datetime.datetime(e.year, 12, 31)) years = sorted( set([ et.get_result().year for et in event_teams_futures if et.get_result().year is not None ])) handler.template_values.update({ 'is_canonical': is_canonical, 'team': team, 'event_awards': event_awards, 'years': years, 'current_event': current_event, 'matches_upcoming': matches_upcoming }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_history.html') return template.render(path, handler.template_values)
def get_upcoming_matches(cls, live_events, n=1): matches = [] for event in live_events: upcoming_matches = MatchHelper.upcomingMatches(event.matches, n) matches.extend(upcoming_matches) return matches
def render_team_history(cls, handler, team, is_canonical): hof_award_future = award_query.TeamEventTypeAwardsQuery(team.key.id(), EventType.CMP_FINALS, AwardType.CHAIRMANS).fetch_async() hof_video_future = media_query.TeamTagMediasQuery(team.key.id(), MediaTag.CHAIRMANS_VIDEO).fetch_async() hof_presentation_future = media_query.TeamTagMediasQuery(team.key.id(), MediaTag.CHAIRMANS_PRESENTATION).fetch_async() hof_essay_future = media_query.TeamTagMediasQuery(team.key.id(), MediaTag.CHAIRMANS_ESSAY).fetch_async() award_futures = award_query.TeamAwardsQuery(team.key.id()).fetch_async() event_futures = event_query.TeamEventsQuery(team.key.id()).fetch_async() participation_future = team_query.TeamParticipationQuery(team.key.id()).fetch_async() social_media_future = media_query.TeamSocialMediaQuery(team.key.id()).fetch_async() hof_awards = hof_award_future.get_result() hof_video = hof_video_future.get_result() hof_presentation = hof_presentation_future.get_result() hof_essay = hof_essay_future.get_result() hall_of_fame = { "is_hof": len(hof_awards) > 0, "years": [award.year for award in hof_awards], "media": { "video": hof_video[0].youtube_url_link if len(hof_video) > 0 else None, "presentation": hof_presentation[0].youtube_url_link if len(hof_presentation) > 0 else None, "essay": hof_essay[0].external_link if len(hof_essay) > 0 else None, }, } awards_by_event = {} for award in award_futures.get_result(): if award.event.id() not in awards_by_event: awards_by_event[award.event.id()] = [award] else: awards_by_event[award.event.id()].append(award) event_awards = [] current_event = None matches_upcoming = None short_cache = False years = set() for event in event_futures.get_result(): years.add(event.year) if event.now: current_event = event matches = match_query.TeamEventMatchesQuery(team.key.id(), event.key.id()).fetch() matches_upcoming = MatchHelper.upcomingMatches(matches) if event.within_a_day: short_cache = True if event.key_name in awards_by_event: sorted_awards = AwardHelper.organizeAwards(awards_by_event[event.key_name]) else: sorted_awards = [] event_awards.append((event, sorted_awards)) event_awards = sorted(event_awards, key=lambda (e, _): e.start_date if e.start_date else datetime.datetime(e.year, 12, 31)) last_competed = None participation_years = participation_future.get_result() if len(participation_years) > 0: last_competed = max(participation_years) current_year = datetime.date.today().year social_medias = sorted(social_media_future.get_result(), key=MediaHelper.social_media_sorter) handler.template_values.update({ "is_canonical": is_canonical, "team": team, "event_awards": event_awards, "years": sorted(years), "social_medias": social_medias, "current_event": current_event, "matches_upcoming": matches_upcoming, "last_competed": last_competed, "current_year": current_year, "max_year": tba_config.MAX_YEAR, "hof": hall_of_fame, }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION return jinja2_engine.render("team_history.html", handler.template_values)
def _render(self, team_number, year=None, explicit_year=False): team = Team.get_by_id("frc" + team_number) if not team: return self.redirect("/error/404") event_teams = EventTeam.query(EventTeam.team == team.key).fetch(1000) event_keys = [event_team.event for event_team in event_teams if event_team.year == year] events = ndb.get_multi(event_keys) for event in events: if not event.start_date: event.start_date = datetime.datetime(year, 12, 31) #unknown goes last events = sorted(events, key=lambda event: event.start_date) years = sorted(set([a.year for a in event_teams if a.year != None])) awards_future = Award.query(Award.year == year, Award.team == team.key).fetch_async(500) for e in events: e.team_matches_future = Match.query(Match.event == e.key, Match.team_key_names == team.key_name).fetch_async(500) # Return an array of event names and a list of matches from that event that the # team was a participant in. participation = list() year_wlt_list = list() current_event = None matches_upcoming = None short_cache = False for e in events: awards = AwardHelper.organizeAwards([award for award in awards_future.get_result() if award.event == e.key]) matches = e.team_matches_future.get_result() matches_organized = MatchHelper.organizeMatches(matches) if e.now: current_event = e matches_upcoming = MatchHelper.upcomingMatches(matches) if event.within_a_day: short_cache = True wlt = EventHelper.calculateTeamWLTFromMatches(team.key_name, matches) year_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if e.rankings: for element in e.rankings: if element[1] == team_number: team_rank = element[0] break participation.append({ 'event' : e, 'matches' : matches_organized, 'wlt': display_wlt, 'rank': team_rank, 'awards': awards }) year_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in year_wlt_list: year_wlt["win"] += wlt["win"] year_wlt["loss"] += wlt["loss"] year_wlt["tie"] += wlt["tie"] if year_wlt["win"] + year_wlt["loss"] + year_wlt["tie"] == 0: year_wlt = None template_values = { "explicit_year": explicit_year, "team": team, "participation": participation, "year": year, "years": years, "year_wlt": year_wlt, "current_event": current_event, "matches_upcoming": matches_upcoming } if short_cache: self._cache_expiration = self.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_details.html') return template.render(path, template_values)
def render_team_details(cls, handler, team, year, is_canonical): media_key_futures = Media.query(Media.references == team.key, Media.year == year).fetch_async( 500, keys_only=True) events_sorted, matches_by_event_key, awards_by_event_key, valid_years = TeamDetailsDataFetcher.fetch( team, year, return_valid_years=True) if not events_sorted: return None media_futures = ndb.get_multi_async(media_key_futures.get_result()) participation = [] year_wlt_list = [] year_match_avg_list = [] current_event = None matches_upcoming = None short_cache = False for event in events_sorted: event_matches = matches_by_event_key.get(event.key, []) event_awards = AwardHelper.organizeAwards( awards_by_event_key.get(event.key, [])) matches_organized = MatchHelper.organizeMatches(event_matches) if event.now: current_event = event matches_upcoming = MatchHelper.upcomingMatches(event_matches) if event.within_a_day: short_cache = True if year == 2015: display_wlt = None match_avg = EventHelper.calculateTeamAvgScoreFromMatches( team.key_name, event_matches) year_match_avg_list.append(match_avg) qual_avg, elim_avg, _, _ = match_avg else: qual_avg = None elim_avg = None wlt = EventHelper.calculateTeamWLTFromMatches( team.key_name, event_matches) year_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if event.rankings: for element in event.rankings: if str(element[1]) == str(team.team_number): team_rank = element[0] break participation.append({ 'event': event, 'matches': matches_organized, 'wlt': display_wlt, 'qual_avg': qual_avg, 'elim_avg': elim_avg, 'rank': team_rank, 'awards': event_awards }) if year == 2015: year_wlt = None year_qual_scores = [] year_elim_scores = [] for _, _, event_qual_scores, event_elim_scores in year_match_avg_list: year_qual_scores += event_qual_scores year_elim_scores += event_elim_scores year_qual_avg = float(sum(year_qual_scores)) / len( year_qual_scores) if year_qual_scores != [] else None year_elim_avg = float(sum(year_elim_scores)) / len( year_elim_scores) if year_elim_scores != [] else None else: year_qual_avg = None year_elim_avg = None year_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in year_wlt_list: year_wlt["win"] += wlt["win"] year_wlt["loss"] += wlt["loss"] year_wlt["tie"] += wlt["tie"] if year_wlt["win"] + year_wlt["loss"] + year_wlt["tie"] == 0: year_wlt = None medias_by_slugname = MediaHelper.group_by_slugname( [media_future.get_result() for media_future in media_futures]) handler.template_values.update({ "is_canonical": is_canonical, "team": team, "participation": participation, "year": year, "years": valid_years, "year_wlt": year_wlt, "year_qual_avg": year_qual_avg, "year_elim_avg": year_elim_avg, "current_event": current_event, "matches_upcoming": matches_upcoming, "medias_by_slugname": medias_by_slugname }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_details.html') return template.render(path, handler.template_values)
def _render(self, event_key): event = EventQuery(event_key).fetch() if not event: self.abort(404) event.prepAwardsMatchesTeams() event.prep_details() medias_future = media_query.EventTeamsPreferredMediasQuery(event_key).fetch_async() district_future = DistrictQuery(event.district_key.id()).fetch_async() if event.district_key else None event_medias_future = media_query.EventMediasQuery(event_key).fetch_async() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) # Organize medias by team image_medias = MediaHelper.get_images([media for media in medias_future.get_result()]) team_medias = defaultdict(list) for image_media in image_medias: for reference in image_media.references: team_medias[reference].append(image_media) team_and_medias = [] for team in teams: team_and_medias.append((team, team_medias.get(team.key, []))) num_teams = len(team_and_medias) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = team_and_medias[:middle_value], team_and_medias[middle_value:] oprs = [i for i in event.matchstats['oprs'].items()] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.now: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = MatchHelper.generateBracket(matches, event.alliance_selections) is_2015_playoff = EventHelper.is_2015_playoff(event_key) if is_2015_playoff: playoff_advancement = MatchHelper.generatePlayoffAdvancement2015(matches, event.alliance_selections) for comp_level in ['qf', 'sf']: if comp_level in bracket_table: del bracket_table[comp_level] else: playoff_advancement = None district_points_sorted = None if event.district_points: district_points_sorted = sorted(event.district_points['points'].items(), key=lambda (team, points): -points['total']) event_insights = event.details.insights if event.details else None event_insights_template = None if event_insights: event_insights_template = 'event_partials/event_insights_{}.html'.format(event.year) district = district_future.get_result() if district_future else None medias_by_slugname = MediaHelper.group_by_slugname([media for media in event_medias_future.get_result()]) self.template_values.update({ "event": event, "district_name": district.display_name if district else None, "district_abbrev": district.abbreviation if district else None, "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, "playoff_advancement": playoff_advancement, "district_points_sorted": district_points_sorted, "is_2015_playoff": is_2015_playoff, "event_insights_qual": event_insights['qual'] if event_insights else None, "event_insights_playoff": event_insights['playoff'] if event_insights else None, "event_insights_template": event_insights_template, "medias_by_slugname": medias_by_slugname, }) if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION return jinja2_engine.render('event_details.html', self.template_values)
def _render(self, event_key): event = EventQuery(event_key).fetch() if not event: self.abort(404) event.prepAwardsMatchesTeams() event.prep_details() medias_future = media_query.EventTeamsPreferredMediasQuery(event_key).fetch_async() district_future = DistrictQuery(event.district_key.id()).fetch_async() if event.district_key else None event_medias_future = media_query.EventMediasQuery(event_key).fetch_async() status_sitevar_future = Sitevar.get_by_id_async('apistatus.down_events') event_divisions_future = None event_codivisions_future = None parent_event_future = None if event.divisions: event_divisions_future = ndb.get_multi_async(event.divisions) elif event.parent_event: parent_event_future = event.parent_event.get_async() event_codivisions_future = EventDivisionsQuery(event.parent_event.id()).fetch_async() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches, event) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) # Organize medias by team image_medias = MediaHelper.get_images([media for media in medias_future.get_result()]) team_medias = defaultdict(list) for image_media in image_medias: for reference in image_media.references: team_medias[reference].append(image_media) team_and_medias = [] for team in teams: team_and_medias.append((team, team_medias.get(team.key, []))) num_teams = len(team_and_medias) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = team_and_medias[:middle_value], team_and_medias[middle_value:] oprs = [i for i in event.matchstats['oprs'].items()] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.now: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = MatchHelper.generateBracket(matches, event, event.alliance_selections) playoff_advancement = None playoff_template = None double_elim_matches = None if EventHelper.is_2015_playoff(event_key): playoff_advancement = MatchHelper.generatePlayoffAdvancement2015(matches, event.alliance_selections) playoff_template = 'playoff_table' for comp_level in ['qf', 'sf']: if comp_level in bracket_table: del bracket_table[comp_level] elif event.playoff_type == PlayoffType.ROUND_ROBIN_6_TEAM: playoff_advancement = MatchHelper.generatePlayoffAdvancementRoundRobin(matches, event.year, event.alliance_selections) playoff_template = 'playoff_round_robin_6_team' comp_levels = bracket_table.keys() for comp_level in comp_levels: if comp_level != 'f': del bracket_table[comp_level] elif event.playoff_type == PlayoffType.BO3_FINALS or event.playoff_type == PlayoffType.BO5_FINALS: comp_levels = bracket_table.keys() for comp_level in comp_levels: if comp_level != 'f': del bracket_table[comp_level] elif event.playoff_type == PlayoffType.DOUBLE_ELIM_8_TEAM: double_elim_matches = MatchHelper.organizeDoubleElimMatches(matches) district_points_sorted = None if event.district_key and event.district_points: district_points_sorted = sorted(event.district_points['points'].items(), key=lambda (team, points): -points['total']) event_insights = event.details.insights if event.details else None event_insights_template = None if event_insights: event_insights_template = 'event_partials/event_insights_{}.html'.format(event.year) district = district_future.get_result() if district_future else None event_divisions = None if event_divisions_future: event_divisions = [e.get_result() for e in event_divisions_future] elif event_codivisions_future: event_divisions = event_codivisions_future.get_result() medias_by_slugname = MediaHelper.group_by_slugname([media for media in event_medias_future.get_result()]) has_time_predictions = matches_upcoming and any(match.predicted_time for match in matches_upcoming) status_sitevar = status_sitevar_future.get_result() self.template_values.update({ "event": event, "event_down": status_sitevar and event_key in status_sitevar.contents, "district_name": district.display_name if district else None, "district_abbrev": district.abbreviation if district else None, "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, 'has_time_predictions': has_time_predictions, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, "playoff_advancement": playoff_advancement, "playoff_template": playoff_template, "district_points_sorted": district_points_sorted, "event_insights_qual": event_insights['qual'] if event_insights else None, "event_insights_playoff": event_insights['playoff'] if event_insights else None, "event_insights_template": event_insights_template, "medias_by_slugname": medias_by_slugname, "event_divisions": event_divisions, 'parent_event': parent_event_future.get_result() if parent_event_future else None, 'double_elim_matches': double_elim_matches, 'double_elim_playoff_types': PlayoffType.DOUBLE_ELIM_TYPES, }) if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION return jinja2_engine.render('event_details.html', self.template_values)
def render_team_details(cls, handler, team, year, is_canonical): media_future = media_query.TeamYearMediaQuery(team.key.id(), year).fetch_async() social_media_future = media_query.TeamSocialMediaQuery(team.key.id()).fetch_async() robot_future = Robot.get_by_id_async('{}_{}'.format(team.key.id(), year)) team_districts_future = team_query.TeamDistrictsQuery(team.key.id()).fetch_async() participation_future = team_query.TeamParticipationQuery(team.key.id()).fetch_async() events_sorted, matches_by_event_key, awards_by_event_key, valid_years = TeamDetailsDataFetcher.fetch(team, year, return_valid_years=True) if not events_sorted: return None participation = [] season_wlt_list = [] offseason_wlt_list = [] year_match_avg_list = [] current_event = None matches_upcoming = None short_cache = False for event in events_sorted: event_matches = matches_by_event_key.get(event.key, []) event_awards = AwardHelper.organizeAwards(awards_by_event_key.get(event.key, [])) matches_organized = MatchHelper.organizeMatches(event_matches) if event.now: current_event = event matches_upcoming = MatchHelper.upcomingMatches(event_matches) if event.within_a_day: short_cache = True if year == 2015: display_wlt = None match_avg = EventHelper.calculateTeamAvgScoreFromMatches(team.key_name, event_matches) year_match_avg_list.append(match_avg) qual_avg, elim_avg, _, _ = match_avg else: qual_avg = None elim_avg = None wlt = EventHelper.calculateTeamWLTFromMatches(team.key_name, event_matches) if event.event_type_enum in EventType.SEASON_EVENT_TYPES: season_wlt_list.append(wlt) else: offseason_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if event.rankings: for element in event.rankings: if str(element[1]) == str(team.team_number): team_rank = element[0] break participation.append({'event': event, 'matches': matches_organized, 'wlt': display_wlt, 'qual_avg': qual_avg, 'elim_avg': elim_avg, 'rank': team_rank, 'awards': event_awards}) season_wlt = None offseason_wlt = None if year == 2015: year_wlt = None year_qual_scores = [] year_elim_scores = [] for _, _, event_qual_scores, event_elim_scores in year_match_avg_list: year_qual_scores += event_qual_scores year_elim_scores += event_elim_scores year_qual_avg = float(sum(year_qual_scores)) / len(year_qual_scores) if year_qual_scores != [] else None year_elim_avg = float(sum(year_elim_scores)) / len(year_elim_scores) if year_elim_scores != [] else None else: year_qual_avg = None year_elim_avg = None season_wlt = {"win": 0, "loss": 0, "tie": 0} offseason_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in season_wlt_list: season_wlt["win"] += wlt["win"] season_wlt["loss"] += wlt["loss"] season_wlt["tie"] += wlt["tie"] if season_wlt["win"] + season_wlt["loss"] + season_wlt["tie"] == 0: season_wlt = None for wlt in offseason_wlt_list: offseason_wlt["win"] += wlt["win"] offseason_wlt["loss"] += wlt["loss"] offseason_wlt["tie"] += wlt["tie"] if offseason_wlt["win"] + offseason_wlt["loss"] + offseason_wlt["tie"] == 0: offseason_wlt = None medias_by_slugname = MediaHelper.group_by_slugname([media for media in media_future.get_result()]) image_medias = MediaHelper.get_images(media_future.get_result()) social_medias = sorted(social_media_future.get_result(), key=MediaHelper.social_media_sorter) preferred_image_medias = filter(lambda x: team.key in x.preferred_references, image_medias) district_name = None district_abbrev = None team_districts = team_districts_future.get_result() if year in team_districts: district_key = team_districts[year] district_abbrev = district_key[4:] district_type = DistrictType.abbrevs[district_abbrev] district_name = DistrictType.type_names[district_type] last_competed = None participation_years = participation_future.get_result() if len(participation_years) > 0: last_competed = max(participation_years) current_year = datetime.date.today().year handler.template_values.update({ "is_canonical": is_canonical, "team": team, "participation": participation, "year": year, "years": valid_years, "season_wlt": season_wlt, "offseason_wlt": offseason_wlt, "year_qual_avg": year_qual_avg, "year_elim_avg": year_elim_avg, "current_event": current_event, "matches_upcoming": matches_upcoming, "medias_by_slugname": medias_by_slugname, "social_medias": social_medias, "image_medias": image_medias, "preferred_image_medias": preferred_image_medias, "robot": robot_future.get_result(), "district_name": district_name, "district_abbrev": district_abbrev, "last_competed": last_competed, "current_year": current_year, }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION return jinja2_engine.render('team_details.html', handler.template_values)
def render_team_details(cls, handler, team, year, is_canonical): media_future = media_query.TeamYearMediaQuery(team.key.id(), year).fetch_async() robot_future = Robot.get_by_id_async('{}_{}'.format( team.key.id(), year)) team_districts_future = team_query.TeamDistrictsQuery( team.key.id()).fetch_async() events_sorted, matches_by_event_key, awards_by_event_key, valid_years = TeamDetailsDataFetcher.fetch( team, year, return_valid_years=True) if not events_sorted: return None participation = [] year_wlt_list = [] year_match_avg_list = [] current_event = None matches_upcoming = None short_cache = False for event in events_sorted: event_matches = matches_by_event_key.get(event.key, []) event_awards = AwardHelper.organizeAwards( awards_by_event_key.get(event.key, [])) matches_organized = MatchHelper.organizeMatches(event_matches) if event.now: current_event = event matches_upcoming = MatchHelper.upcomingMatches(event_matches) if event.within_a_day: short_cache = True if year == 2015: display_wlt = None match_avg = EventHelper.calculateTeamAvgScoreFromMatches( team.key_name, event_matches) year_match_avg_list.append(match_avg) qual_avg, elim_avg, _, _ = match_avg else: qual_avg = None elim_avg = None wlt = EventHelper.calculateTeamWLTFromMatches( team.key_name, event_matches) year_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if event.rankings: for element in event.rankings: if str(element[1]) == str(team.team_number): team_rank = element[0] break participation.append({ 'event': event, 'matches': matches_organized, 'wlt': display_wlt, 'qual_avg': qual_avg, 'elim_avg': elim_avg, 'rank': team_rank, 'awards': event_awards }) if year == 2015: year_wlt = None year_qual_scores = [] year_elim_scores = [] for _, _, event_qual_scores, event_elim_scores in year_match_avg_list: year_qual_scores += event_qual_scores year_elim_scores += event_elim_scores year_qual_avg = float(sum(year_qual_scores)) / len( year_qual_scores) if year_qual_scores != [] else None year_elim_avg = float(sum(year_elim_scores)) / len( year_elim_scores) if year_elim_scores != [] else None else: year_qual_avg = None year_elim_avg = None year_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in year_wlt_list: year_wlt["win"] += wlt["win"] year_wlt["loss"] += wlt["loss"] year_wlt["tie"] += wlt["tie"] if year_wlt["win"] + year_wlt["loss"] + year_wlt["tie"] == 0: year_wlt = None medias_by_slugname = MediaHelper.group_by_slugname( [media for media in media_future.get_result()]) image_medias = MediaHelper.get_images( [media for media in media_future.get_result()]) district_name = None district_abbrev = None team_districts = team_districts_future.get_result() if year in team_districts: district_key = team_districts[year] district_abbrev = district_key[4:] district_type = DistrictType.abbrevs[district_abbrev] district_name = DistrictType.type_names[district_type] handler.template_values.update({ "is_canonical": is_canonical, "team": team, "participation": participation, "year": year, "years": valid_years, "year_wlt": year_wlt, "year_qual_avg": year_qual_avg, "year_elim_avg": year_elim_avg, "current_event": current_event, "matches_upcoming": matches_upcoming, "medias_by_slugname": medias_by_slugname, "image_medias": image_medias, "robot": robot_future.get_result(), "district_name": district_name, "district_abbrev": district_abbrev, }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION return jinja2_engine.render('team_details.html', handler.template_values)
def _render(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) event.prepAwardsMatchesTeams() medias_future = media_query.EventTeamsPreferredMediasQuery(event_key).fetch_async() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) # Organize medias by team image_medias = MediaHelper.get_images([media for media in medias_future.get_result()]) team_medias = defaultdict(list) for image_media in image_medias: for reference in image_media.references: team_medias[reference].append(image_media) team_and_medias = [] for team in teams: team_and_medias.append((team, team_medias.get(team.key, []))) num_teams = len(team_and_medias) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = team_and_medias[:middle_value], team_and_medias[middle_value:] oprs = [i for i in event.matchstats['oprs'].items()] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.now: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = MatchHelper.generateBracket(matches, event.alliance_selections) is_2015_playoff = EventHelper.is_2015_playoff(event_key) if is_2015_playoff: playoff_advancement = MatchHelper.generatePlayoffAdvancement2015(matches, event.alliance_selections) for comp_level in ['qf', 'sf']: if comp_level in bracket_table: del bracket_table[comp_level] else: playoff_advancement = None district_points_sorted = None if event.district_points: district_points_sorted = sorted(event.district_points['points'].items(), key=lambda (team, points): -points['total']) event_insights = EventInsightsHelper.calculate_event_insights(cleaned_matches, event.year) event_insights_template = None if event_insights: event_insights_template = 'event_partials/event_insights_{}.html'.format(event.year) # rankings processing for ranking score per match full_rankings = event.rankings rankings_enhanced = event.rankings_enhanced if rankings_enhanced is not None: rp_index = RankingIndexes.CUMULATIVE_RANKING_SCORE[event.year] matches_index = RankingIndexes.MATCHES_PLAYED[event.year] ranking_criterion_name = full_rankings[0][rp_index] full_rankings[0].append(ranking_criterion_name + "/Match*") for row in full_rankings[1:]: team = row[1] if rankings_enhanced["ranking_score_per_match"] is not None: rp_per_match = rankings_enhanced['ranking_score_per_match'][team] row.append(rp_per_match) if rankings_enhanced["match_offset"] is not None: match_offset = rankings_enhanced["match_offset"][team] if match_offset != 0: row[matches_index] = "{} ({})".format(row[matches_index], match_offset) self.template_values.update({ "event": event, "district_name": DistrictType.type_names.get(event.event_district_enum, None), "district_abbrev": DistrictType.type_abbrevs.get(event.event_district_enum, None), "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, "playoff_advancement": playoff_advancement, "district_points_sorted": district_points_sorted, "is_2015_playoff": is_2015_playoff, "event_insights_qual": event_insights['qual'] if event_insights else None, "event_insights_playoff": event_insights['playoff'] if event_insights else None, "event_insights_template": event_insights_template, }) if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION return jinja2_engine.render('event_details.html', self.template_values)
def render_team_details(cls, handler, team, year, is_canonical): media_future = media_query.TeamYearMediaQuery(team.key.id(), year).fetch_async() social_media_future = media_query.TeamSocialMediaQuery(team.key.id()).fetch_async() robot_future = Robot.get_by_id_async('{}_{}'.format(team.key.id(), year)) team_districts_future = team_query.TeamDistrictsQuery(team.key.id()).fetch_async() participation_future = team_query.TeamParticipationQuery(team.key.id()).fetch_async() events_sorted, matches_by_event_key, awards_by_event_key, valid_years = TeamDetailsDataFetcher.fetch(team, year, return_valid_years=True) if not events_sorted: return None participation = [] season_wlt_list = [] offseason_wlt_list = [] year_match_avg_list = [] current_event = None matches_upcoming = None short_cache = False for event in events_sorted: event_matches = matches_by_event_key.get(event.key, []) event_awards = AwardHelper.organizeAwards(awards_by_event_key.get(event.key, [])) matches_organized = MatchHelper.organizeMatches(event_matches) if event.now: current_event = event matches_upcoming = MatchHelper.upcomingMatches(event_matches) if event.within_a_day: short_cache = True if year == 2015: display_wlt = None match_avg = EventHelper.calculateTeamAvgScoreFromMatches(team.key_name, event_matches) year_match_avg_list.append(match_avg) qual_avg, elim_avg, _, _ = match_avg else: qual_avg = None elim_avg = None wlt = EventHelper.calculateTeamWLTFromMatches(team.key_name, event_matches) if event.event_type_enum in EventType.SEASON_EVENT_TYPES: season_wlt_list.append(wlt) else: offseason_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if event.rankings: for element in event.rankings: if str(element[1]) == str(team.team_number): team_rank = element[0] break participation.append({'event': event, 'matches': matches_organized, 'wlt': display_wlt, 'qual_avg': qual_avg, 'elim_avg': elim_avg, 'rank': team_rank, 'awards': event_awards}) season_wlt = None offseason_wlt = None if year == 2015: year_wlt = None year_qual_scores = [] year_elim_scores = [] for _, _, event_qual_scores, event_elim_scores in year_match_avg_list: year_qual_scores += event_qual_scores year_elim_scores += event_elim_scores year_qual_avg = float(sum(year_qual_scores)) / len(year_qual_scores) if year_qual_scores != [] else None year_elim_avg = float(sum(year_elim_scores)) / len(year_elim_scores) if year_elim_scores != [] else None else: year_qual_avg = None year_elim_avg = None season_wlt = {"win": 0, "loss": 0, "tie": 0} offseason_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in season_wlt_list: season_wlt["win"] += wlt["win"] season_wlt["loss"] += wlt["loss"] season_wlt["tie"] += wlt["tie"] if season_wlt["win"] + season_wlt["loss"] + season_wlt["tie"] == 0: season_wlt = None for wlt in offseason_wlt_list: offseason_wlt["win"] += wlt["win"] offseason_wlt["loss"] += wlt["loss"] offseason_wlt["tie"] += wlt["tie"] if offseason_wlt["win"] + offseason_wlt["loss"] + offseason_wlt["tie"] == 0: offseason_wlt = None medias_by_slugname = MediaHelper.group_by_slugname([media for media in media_future.get_result()]) image_medias = MediaHelper.get_images(media_future.get_result()) social_medias = sorted(social_media_future.get_result(), key=MediaHelper.social_media_sorter) preferred_image_medias = filter(lambda x: team.key in x.preferred_references, image_medias) district_name = None district_abbrev = None team_districts = team_districts_future.get_result() for district in team_districts: if district.year == year: district_abbrev = district.abbreviation district_name = district.display_name last_competed = None participation_years = participation_future.get_result() if len(participation_years) > 0: last_competed = max(participation_years) current_year = datetime.date.today().year handler.template_values.update({ "is_canonical": is_canonical, "team": team, "participation": participation, "year": year, "years": valid_years, "season_wlt": season_wlt, "offseason_wlt": offseason_wlt, "year_qual_avg": year_qual_avg, "year_elim_avg": year_elim_avg, "current_event": current_event, "matches_upcoming": matches_upcoming, "medias_by_slugname": medias_by_slugname, "social_medias": social_medias, "image_medias": image_medias, "preferred_image_medias": preferred_image_medias, "robot": robot_future.get_result(), "district_name": district_name, "district_abbrev": district_abbrev, "last_competed": last_competed, "current_year": current_year, }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION return jinja2_engine.render('team_details.html', handler.template_values)
def render_team_details(cls, handler, team, year, is_canonical): media_key_futures = Media.query(Media.references == team.key, Media.year == year).fetch_async(500, keys_only=True) events_sorted, matches_by_event_key, awards_by_event_key, valid_years = TeamDetailsDataFetcher.fetch(team, year, return_valid_years=True) if not events_sorted: return None media_futures = ndb.get_multi_async(media_key_futures.get_result()) participation = [] year_wlt_list = [] year_match_avg_list = [] current_event = None matches_upcoming = None short_cache = False for event in events_sorted: event_matches = matches_by_event_key.get(event.key, []) event_awards = AwardHelper.organizeAwards(awards_by_event_key.get(event.key, [])) matches_organized = MatchHelper.organizeMatches(event_matches) if event.now: current_event = event matches_upcoming = MatchHelper.upcomingMatches(event_matches) if event.within_a_day: short_cache = True if year == 2015: display_wlt = None match_avg = EventHelper.calculateTeamAvgScoreFromMatches(team.key_name, event_matches) year_match_avg_list.append(match_avg) qual_avg, elim_avg, _, _ = match_avg else: qual_avg = None elim_avg = None wlt = EventHelper.calculateTeamWLTFromMatches(team.key_name, event_matches) year_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if event.rankings: for element in event.rankings: if str(element[1]) == str(team.team_number): team_rank = element[0] break participation.append({'event': event, 'matches': matches_organized, 'wlt': display_wlt, 'qual_avg': qual_avg, 'elim_avg': elim_avg, 'rank': team_rank, 'awards': event_awards}) if year == 2015: year_wlt = None year_qual_scores = [] year_elim_scores = [] for _, _, event_qual_scores, event_elim_scores in year_match_avg_list: year_qual_scores += event_qual_scores year_elim_scores += event_elim_scores year_qual_avg = float(sum(year_qual_scores)) / len(year_qual_scores) if year_qual_scores != [] else None year_elim_avg = float(sum(year_elim_scores)) / len(year_elim_scores) if year_elim_scores != [] else None else: year_qual_avg = None year_elim_avg = None year_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in year_wlt_list: year_wlt["win"] += wlt["win"] year_wlt["loss"] += wlt["loss"] year_wlt["tie"] += wlt["tie"] if year_wlt["win"] + year_wlt["loss"] + year_wlt["tie"] == 0: year_wlt = None medias_by_slugname = MediaHelper.group_by_slugname([media_future.get_result() for media_future in media_futures]) handler.template_values.update({ "is_canonical": is_canonical, "team": team, "participation": participation, "year": year, "years": valid_years, "year_wlt": year_wlt, "year_qual_avg": year_qual_avg, "year_elim_avg": year_elim_avg, "current_event": current_event, "matches_upcoming": matches_upcoming, "medias_by_slugname": medias_by_slugname }) if short_cache: handler._cache_expiration = handler.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_details.html') return template.render(path, handler.template_values)
def _render(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) event.prepAwardsMatchesTeams() medias_future = media_query.EventTeamsPreferredMediasQuery( event_key).fetch_async() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) # Organize medias by team image_medias = MediaHelper.get_images( [media for media in medias_future.get_result()]) team_medias = defaultdict(list) for image_media in image_medias: for reference in image_media.references: team_medias[reference].append(image_media) team_and_medias = [] for team in teams: team_and_medias.append((team, team_medias.get(team.key, []))) num_teams = len(team_and_medias) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = team_and_medias[:middle_value], team_and_medias[ middle_value:] oprs = [i for i in event.matchstats['oprs'].items() ] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.now: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = MatchHelper.generateBracket(matches, event.alliance_selections) is_2015_playoff = EventHelper.is_2015_playoff(event_key) if is_2015_playoff: playoff_advancement = MatchHelper.generatePlayoffAdvancement2015( matches, event.alliance_selections) for comp_level in ['qf', 'sf']: if comp_level in bracket_table: del bracket_table[comp_level] else: playoff_advancement = None district_points_sorted = None if event.district_points: district_points_sorted = sorted( event.district_points['points'].items(), key=lambda (team, points): -points['total']) event_insights = EventInsightsHelper.calculate_event_insights( cleaned_matches, event.year) event_insights_template = None if event_insights: event_insights_template = 'event_partials/event_insights_{}.html'.format( event.year) self.template_values.update({ "event": event, "district_name": DistrictType.type_names.get(event.event_district_enum, None), "district_abbrev": DistrictType.type_abbrevs.get(event.event_district_enum, None), "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, "playoff_advancement": playoff_advancement, "district_points_sorted": district_points_sorted, "is_2015_playoff": is_2015_playoff, "event_insights_qual": event_insights['qual'] if event_insights else None, "event_insights_playoff": event_insights['playoff'] if event_insights else None, "event_insights_template": event_insights_template, }) if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION return jinja2_engine.render('event_details.html', self.template_values)
def _render(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) event.prepAwardsMatchesTeams() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) num_teams = len(teams) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = teams[:middle_value], teams[middle_value:] oprs = [i for i in event.matchstats['oprs'].items() ] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.now: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = MatchHelper.generateBracket(matches, event.alliance_selections) is_2015_playoff = EventHelper.is_2015_playoff(event_key) if is_2015_playoff: playoff_advancement = MatchHelper.generatePlayoffAdvancement2015( matches, event.alliance_selections) for comp_level in ['qf', 'sf']: if comp_level in bracket_table: del bracket_table[comp_level] else: playoff_advancement = None district_points_sorted = None if event.district_points: district_points_sorted = sorted( event.district_points['points'].items(), key=lambda (team, points): -points['total']) event_insights = EventInsightsHelper.calculate_event_insights( cleaned_matches, event.year) self.template_values.update({ "event": event, "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, "playoff_advancement": playoff_advancement, "district_points_sorted": district_points_sorted, "is_2015_playoff": is_2015_playoff, "event_insights": event_insights }) if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION return jinja2_engine.render('event_details.html', self.template_values)
def _render(self, team_number, year=None, explicit_year=False): @ndb.tasklet def get_event_matches_async(event_team_key): event_team = yield event_team_key.get_async() years.add(event_team.year) # years is a "global" variable (defined below). Doing this removes the complexity of having to propagate the years up through the tasklet call chain. if (event_team.year == year): event = yield event_team.event.get_async() if not event.start_date: event.start_date = datetime.datetime(year, 12, 31) #unknown goes last matches_keys = yield Match.query( Match.event == event.key, Match.team_key_names == team.key_name).fetch_async(500, keys_only=True) matches = yield ndb.get_multi_async(matches_keys) raise ndb.Return((event, matches)) raise ndb.Return(None) @ndb.tasklet def get_events_matches_async(): event_team_keys = yield EventTeam.query(EventTeam.team == team.key).fetch_async(1000, keys_only=True) events_matches = yield map(get_event_matches_async, event_team_keys) events_matches = filter(None, events_matches) raise ndb.Return(events_matches) @ndb.tasklet def get_awards_async(): award_keys = yield Award.query(Award.year == year, Award.team == team.key).fetch_async(500, keys_only=True) awards = yield ndb.get_multi_async(award_keys) raise ndb.Return(awards) @ndb.toplevel def get_events_matches_awards(): events_matches, awards = yield get_events_matches_async(), get_awards_async() raise ndb.Return(events_matches, awards) team = Team.get_by_id("frc" + team_number) if not team: return self.redirect("/error/404") years = set() events_matches, awards = get_events_matches_awards() events_matches = sorted(events_matches, key=lambda (e, _): e.start_date) years = sorted(years) participation = list() year_wlt_list = list() current_event = None matches_upcoming = None short_cache = False for e, matches in events_matches: event_awards = AwardHelper.organizeAwards([award for award in awards if award.event == e.key]) matches_organized = MatchHelper.organizeMatches(matches) if e.now: current_event = e matches_upcoming = MatchHelper.upcomingMatches(matches) if e.within_a_day: short_cache = True wlt = EventHelper.calculateTeamWLTFromMatches(team.key_name, matches) year_wlt_list.append(wlt) if wlt["win"] + wlt["loss"] + wlt["tie"] == 0: display_wlt = None else: display_wlt = wlt team_rank = None if e.rankings: for element in e.rankings: if element[1] == team_number: team_rank = element[0] break participation.append({ 'event' : e, 'matches' : matches_organized, 'wlt': display_wlt, 'rank': team_rank, 'awards': event_awards }) year_wlt = {"win": 0, "loss": 0, "tie": 0} for wlt in year_wlt_list: year_wlt["win"] += wlt["win"] year_wlt["loss"] += wlt["loss"] year_wlt["tie"] += wlt["tie"] if year_wlt["win"] + year_wlt["loss"] + year_wlt["tie"] == 0: year_wlt = None template_values = { "explicit_year": explicit_year, "team": team, "participation": participation, "year": year, "years": years, "year_wlt": year_wlt, "current_event": current_event, "matches_upcoming": matches_upcoming } if short_cache: self._cache_expiration = self.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/team_details.html') return template.render(path, template_values)
def _render(self, event_key): event = Event.get_by_id(event_key) if not event: self.abort(404) event.prepAwardsMatchesTeams() awards = AwardHelper.organizeAwards(event.awards) cleaned_matches = MatchHelper.deleteInvalidMatches(event.matches) matches = MatchHelper.organizeMatches(cleaned_matches) teams = TeamHelper.sortTeams(event.teams) num_teams = len(teams) middle_value = num_teams / 2 if num_teams % 2 != 0: middle_value += 1 teams_a, teams_b = teams[:middle_value], teams[middle_value:] oprs = [i for i in event.matchstats['oprs'].items()] if (event.matchstats is not None and 'oprs' in event.matchstats) else [] oprs = sorted(oprs, key=lambda t: t[1], reverse=True) # sort by OPR oprs = oprs[:15] # get the top 15 OPRs if event.now: matches_recent = MatchHelper.recentMatches(cleaned_matches) matches_upcoming = MatchHelper.upcomingMatches(cleaned_matches) else: matches_recent = None matches_upcoming = None bracket_table = MatchHelper.generateBracket(matches, event.alliance_selections) is_2015_playoff = EventHelper.is_2015_playoff(event_key) if is_2015_playoff: playoff_advancement = MatchHelper.generatePlayoffAdvancement2015(matches, event.alliance_selections) for comp_level in ['qf', 'sf']: if comp_level in bracket_table: del bracket_table[comp_level] else: playoff_advancement = None district_points_sorted = None if event.district_points: district_points_sorted = sorted(event.district_points['points'].items(), key=lambda (team, points): -points['total']) self.template_values.update({ "event": event, "matches": matches, "matches_recent": matches_recent, "matches_upcoming": matches_upcoming, "awards": awards, "teams_a": teams_a, "teams_b": teams_b, "num_teams": num_teams, "oprs": oprs, "bracket_table": bracket_table, "playoff_advancement": playoff_advancement, "district_points_sorted": district_points_sorted, "is_2015_playoff": is_2015_playoff, }) if event.within_a_day: self._cache_expiration = self.SHORT_CACHE_EXPIRATION path = os.path.join(os.path.dirname(__file__), '../templates/event_details.html') return template.render(path, self.template_values)