def get(self, year): year = int(year) year_dcmps = DistrictChampsInYearQuery(year).fetch() districts_to_write = [] for dcmp in year_dcmps: district_abbrev = DistrictType.type_abbrevs[ dcmp.event_district_enum] district_key = District.renderKeyName(year, district_abbrev) logging.info("Creating {}".format(district_key)) district = District( id=district_key, year=year, abbreviation=district_abbrev, display_name=DistrictType.type_names[dcmp.event_district_enum], elasticsearch_name=next( (k for k, v in DistrictType.elasticsearch_names.iteritems() if v == dcmp.event_district_enum), None)) districts_to_write.append(district) logging.info("Writing {} new districts".format( len(districts_to_write))) DistrictManipulator.createOrUpdate(districts_to_write, run_post_update_hook=False) for dcmp in year_dcmps: district_abbrev = DistrictType.type_abbrevs[ dcmp.event_district_enum] district_key = District.renderKeyName(year, district_abbrev) district_events_future = DistrictEventsQuery( district_key).fetch_async() district_events = district_events_future.get_result() logging.info("Found {} events to update".format( len(district_events))) events_to_write = [] for event in district_events: event.district_key = ndb.Key(District, district_key) events_to_write.append(event) EventManipulator.createOrUpdate(events_to_write) for dcmp in year_dcmps: district_abbrev = DistrictType.type_abbrevs[ dcmp.event_district_enum] district_key = District.renderKeyName(year, district_abbrev) districtteams_future = DistrictTeam.query( DistrictTeam.year == year, DistrictTeam.district == DistrictType.abbrevs.get( district_abbrev, None)).fetch_async() districtteams = districtteams_future.get_result() logging.info("Found {} DistrictTeams to update".format( len(districtteams))) districtteams_to_write = [] for districtteam in districtteams: districtteam.district_key = ndb.Key(District, district_key) districtteams_to_write.append(districtteam) DistrictTeamManipulator.createOrUpdate(districtteams_to_write)
def test_event_updated(self): affected_refs = { 'key': {ndb.Key(Event, '2015casj'), ndb.Key(Event, '2015cama')}, 'year': {2014, 2015}, 'district_key': {ndb.Key(District, '2015fim'), ndb.Key(District, '2014mar')} } cache_keys = [ q.cache_key for q in get_affected_queries.event_updated(affected_refs) ] self.assertEqual(len(cache_keys), 10) self.assertTrue(EventQuery('2015casj').cache_key in cache_keys) self.assertTrue(EventQuery('2015cama').cache_key in cache_keys) self.assertTrue(EventListQuery(2014).cache_key in cache_keys) self.assertTrue(EventListQuery(2015).cache_key in cache_keys) self.assertTrue(DistrictEventsQuery('2015fim').cache_key in cache_keys) self.assertTrue(DistrictEventsQuery('2014mar').cache_key in cache_keys) self.assertTrue(TeamEventsQuery('frc254').cache_key in cache_keys) self.assertTrue(TeamEventsQuery('frc604').cache_key in cache_keys) self.assertTrue( TeamYearEventsQuery('frc254', 2015).cache_key in cache_keys) self.assertTrue( TeamYearEventsQuery('frc604', 2015).cache_key in cache_keys)
def event_updated(affected_refs): event_keys = _filter(affected_refs['key']) years = _filter(affected_refs['year']) event_district_keys = _filter(affected_refs['district_key']) event_team_keys_future = EventTeam.query(EventTeam.event.IN([event_key for event_key in event_keys])).fetch_async(None, keys_only=True) events_future = ndb.get_multi_async(event_keys) queries_and_keys = [] for event_key in event_keys: queries_and_keys.append((EventQuery(event_key.id()))) queries_and_keys.append(EventDivisionsQuery(event_key.id())) for year in years: queries_and_keys.append((EventListQuery(year))) for event_district_key in event_district_keys: queries_and_keys.append((DistrictEventsQuery(event_district_key.id()))) for et_key in event_team_keys_future.get_result(): team_key = et_key.id().split('_')[1] year = int(et_key.id()[:4]) queries_and_keys.append((TeamEventsQuery(team_key))) queries_and_keys.append((TeamYearEventsQuery(team_key, year))) events_with_parents = filter(lambda e: e.get_result() is not None and e.get_result().parent_event is not None, events_future) parent_keys = set([e.get_result().parent_event for e in events_with_parents]) for parent_key in parent_keys: queries_and_keys.append((EventDivisionsQuery(parent_key.id()))) return queries_and_keys
def test_district_updated(self): affected_refs = { 'key': {ndb.Key(District, '2016ne')}, 'year': {2015, 2016}, 'abbreviation': {'ne', 'chs'} } cache_keys = [ q.cache_key for q in get_affected_queries.district_updated(affected_refs) ] self.assertEqual(len(cache_keys), 11) self.assertTrue(DistrictsInYearQuery(2015).cache_key in cache_keys) self.assertTrue(DistrictsInYearQuery(2016).cache_key in cache_keys) self.assertTrue(DistrictHistoryQuery('ne').cache_key in cache_keys) self.assertTrue(DistrictHistoryQuery('chs').cache_key in cache_keys) self.assertTrue(DistrictQuery('2016ne').cache_key in cache_keys) self.assertTrue(TeamDistrictsQuery('frc604').cache_key in cache_keys) # Necessary because APIv3 Event models include the District model self.assertTrue(EventQuery('2016necmp').cache_key in cache_keys) self.assertTrue(EventListQuery(2016).cache_key in cache_keys) self.assertTrue(DistrictEventsQuery('2016ne').cache_key in cache_keys) self.assertTrue(TeamEventsQuery('frc125').cache_key in cache_keys) self.assertTrue( TeamYearEventsQuery('frc125', 2016).cache_key in cache_keys)
def get(self, year): year = int(year) year_dcmps = DistrictChampsInYearQuery(year).fetch() districts_to_write = [] for dcmp in year_dcmps: district_abbrev = DistrictType.type_abbrevs[dcmp.event_district_enum] district_key = District.renderKeyName(year, district_abbrev) logging.info("Creating {}".format(district_key)) district = District( id=district_key, year=year, abbreviation=district_abbrev, display_name=DistrictType.type_names[dcmp.event_district_enum], elasticsearch_name=next((k for k, v in DistrictType.elasticsearch_names.iteritems() if v == dcmp.event_district_enum), None) ) districts_to_write.append(district) logging.info("Writing {} new districts".format(len(districts_to_write))) DistrictManipulator.createOrUpdate(districts_to_write, run_post_update_hook=False) for dcmp in year_dcmps: district_abbrev = DistrictType.type_abbrevs[dcmp.event_district_enum] district_key = District.renderKeyName(year, district_abbrev) district_events_future = DistrictEventsQuery(district_key).fetch_async() district_events = district_events_future.get_result() logging.info("Found {} events to update".format(len(district_events))) events_to_write = [] for event in district_events: event.district_key = ndb.Key(District, district_key) events_to_write.append(event) EventManipulator.createOrUpdate(events_to_write) for dcmp in year_dcmps: district_abbrev = DistrictType.type_abbrevs[dcmp.event_district_enum] district_key = District.renderKeyName(year, district_abbrev) districtteams_future = DistrictTeam.query(DistrictTeam.year == year, DistrictTeam.district == DistrictType.abbrevs.get(district_abbrev, None)).fetch_async() districtteams = districtteams_future.get_result() logging.info("Found {} DistrictTeams to update".format(len(districtteams))) districtteams_to_write = [] for districtteam in districtteams: districtteam.district_key = ndb.Key(District, district_key) districtteams_to_write.append(districtteam) DistrictTeamManipulator.createOrUpdate(districtteams_to_write)
def _render(self, district_abbrev, year=None): self._set_district(district_abbrev) events = DistrictEventsQuery('{}{}'.format(self.year, self.district_abbrev)).fetch() events = [ModelToDict.eventConverter(event) for event in events] return json.dumps(events, ensure_ascii=True)
def _render(self, district_key, model_type=None): events, self._last_modified = DistrictEventsQuery(district_key).fetch( dict_version=3, return_updated=True) if model_type is not None: events = filter_event_properties(events, model_type) return json.dumps(events, ensure_ascii=True, indent=True, sort_keys=True)
def get(self, district_key): district = District.get_by_id(district_key) if not district: self.response.out.write( "District {} does not exist!".format(district_key)) return events_future = DistrictEventsQuery(district_key).fetch_async() teams_future = DistrictTeamsQuery(district_key).fetch_async() events = events_future.get_result() for event in events: event.prep_details() EventHelper.sort_events(events) team_totals = DistrictHelper.calculate_rankings( events, teams_future, district.year) rankings = [] current_rank = 1 for key, points in team_totals: point_detail = {} point_detail["rank"] = current_rank point_detail["team_key"] = key point_detail["event_points"] = [] for event, event_points in points["event_points"]: event_points['event_key'] = event.key.id() event_points['district_cmp'] = ( event.event_type_enum == EventType.DISTRICT_CMP or event.event_type_enum == EventType.DISTRICT_CMP_DIVISION) point_detail["event_points"].append(event_points) point_detail["rookie_bonus"] = points.get("rookie_bonus", 0) point_detail["point_total"] = points["point_total"] rankings.append(point_detail) current_rank += 1 if rankings: district.rankings = rankings DistrictManipulator.createOrUpdate(district) if 'X-Appengine-Taskname' not in self.request.headers: # Only write out if not in taskqueue self.response.out.write( "Finished calculating rankings for: {}".format(district_key))
def _render(self, district_abbrev, year=None): self._set_district(district_abbrev, self.year) if self.year < 2009: return json.dumps([], ensure_ascii=True) events_future = DistrictEventsQuery( District.renderKeyName(self.year, district_abbrev)).fetch_async() district_teams_future = DistrictTeamsQuery("{}{}".format( year, district_abbrev)).fetch_async() events = events_future.get_result() if not events: return json.dumps([], ensure_ascii=True) EventHelper.sort_events(events) team_totals = DistrictHelper.calculate_rankings( events, district_teams_future.get_result(), self.year) rankings = [] current_rank = 1 for key, points in team_totals: point_detail = {} point_detail["rank"] = current_rank point_detail["team_key"] = key point_detail["event_points"] = {} for event in points["event_points"]: event_key = event[0].key_name point_detail["event_points"][event_key] = event[1] event_details = Event.get_by_id(event_key) point_detail["event_points"][event[0].key_name][ 'district_cmp'] = True if event_details.event_type_enum == EventType.DISTRICT_CMP else False if "rookie_bonus" in points: point_detail["rookie_bonus"] = points["rookie_bonus"] else: point_detail["rookie_bonus"] = 0 point_detail["point_total"] = points["point_total"] rankings.append(point_detail) current_rank += 1 return json.dumps(rankings)
def get(self, district_key): district = District.get_by_id(district_key) if not district: self.response.out.write("District {} does not exist!".format(district_key)) return events_future = DistrictEventsQuery(district_key).fetch_async() teams_future = DistrictTeamsQuery(district_key).fetch_async() events = events_future.get_result() for event in events: event.prep_details() EventHelper.sort_events(events) team_totals = DistrictHelper.calculate_rankings(events, teams_future, district.year) rankings = [] current_rank = 1 for key, points in team_totals: point_detail = {} point_detail["rank"] = current_rank point_detail["team_key"] = key point_detail["event_points"] = [] for event, event_points in points["event_points"]: event_points['event_key'] = event.key.id() event_points['district_cmp'] = ( event.event_type_enum == EventType.DISTRICT_CMP or event.event_type_enum == EventType.DISTRICT_CMP_DIVISION) point_detail["event_points"].append(event_points) point_detail["rookie_bonus"] = points.get("rookie_bonus", 0) point_detail["point_total"] = points["point_total"] rankings.append(point_detail) current_rank += 1 if rankings: district.rankings = rankings DistrictManipulator.createOrUpdate(district) if 'X-Appengine-Taskname' not in self.request.headers: # Only write out if not in taskqueue self.response.out.write("Finished calculating rankings for: {}".format(district_key))
def _render(self, district_abbrev, year=None): self._set_district(district_abbrev, self.year) if self.year < 2009: return json.dumps([], ensure_ascii=True) events_future = DistrictEventsQuery(District.renderKeyName(self.year, district_abbrev)).fetch_async() district_teams_future = DistrictTeamsQuery("{}{}".format(year, district_abbrev)).fetch_async() events = events_future.get_result() if not events: return json.dumps([], ensure_ascii=True) EventHelper.sort_events(events) team_totals = DistrictHelper.calculate_rankings(events, district_teams_future.get_result(), self.year) rankings = [] current_rank = 1 for key, points in team_totals: point_detail = {} point_detail["rank"] = current_rank point_detail["team_key"] = key point_detail["event_points"] = {} for event in points["event_points"]: event_key = event[0].key_name point_detail["event_points"][event_key] = event[1] event_details = Event.get_by_id(event_key) point_detail["event_points"][event[0].key_name]['district_cmp'] = True if event_details.event_type_enum == EventType.DISTRICT_CMP else False if "rookie_bonus" in points: point_detail["rookie_bonus"] = points["rookie_bonus"] else: point_detail["rookie_bonus"] = 0 point_detail["point_total"] = points["point_total"] rankings.append(point_detail) current_rank += 1 return json.dumps(rankings)
def event_updated(affected_refs): event_keys = filter(None, affected_refs['key']) years = filter(None, affected_refs['year']) event_district_keys = filter(None, affected_refs['event_district_key']) event_team_keys_future = EventTeam.query(EventTeam.event.IN([event_key for event_key in event_keys])).fetch_async(None, keys_only=True) queries_and_keys = [] for year in years: queries_and_keys.append((EventListQuery(year))) for event_district_key in event_district_keys: queries_and_keys.append((DistrictEventsQuery(event_district_key))) for et_key in event_team_keys_future.get_result(): team_key = et_key.id().split('_')[1] year = int(et_key.id()[:4]) queries_and_keys.append((TeamEventsQuery(team_key))) queries_and_keys.append((TeamYearEventsQuery(team_key, year))) return queries_and_keys
def _render(self, district_abbrev, year=None, explicit_year=False): district = DistrictQuery('{}{}'.format(year, district_abbrev)).fetch() if not district: self.abort(404) events_future = DistrictEventsQuery(district.key_name).fetch_async() # needed for district teams district_teams_future = DistrictTeamsQuery(district.key_name).fetch_async() # needed for valid_years history_future = DistrictHistoryQuery(district.abbreviation).fetch_async() # needed for valid_districts districts_in_year_future = DistrictsInYearQuery(district.year).fetch_async() # needed for active team statuses live_events = [] if year == datetime.datetime.now().year: # Only show active teams for current year live_events = EventHelper.getWeekEvents() live_eventteams_futures = [] for event in live_events: live_eventteams_futures.append(EventTeamsQuery(event.key_name).fetch_async()) events = events_future.get_result() EventHelper.sort_events(events) events_by_key = {} for event in events: events_by_key[event.key.id()] = event week_events = EventHelper.groupByWeek(events) valid_districts = set() districts_in_year = districts_in_year_future.get_result() for dist in districts_in_year: valid_districts.add((dist.display_name, dist.abbreviation)) valid_districts = sorted(valid_districts, key=lambda (name, _): name) teams = TeamHelper.sortTeams(district_teams_future.get_result()) team_keys = set([t.key.id() for t in 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:] # Currently Competing Team Status event_team_keys = [] for event, teams_future in zip(live_events, live_eventteams_futures): for team in teams_future.get_result(): if team.key.id() in team_keys: event_team_keys.append(ndb.Key(EventTeam, '{}_{}'.format(event.key.id(), team.key.id()))) # Should be in context cache ndb.get_multi(event_team_keys) # Warms context cache live_events_with_teams = [] for event, teams_future in zip(live_events, live_eventteams_futures): teams_and_statuses = [] has_teams = False for team in teams_future.get_result(): if team.key.id() in team_keys: has_teams = True event_team = EventTeam.get_by_id('{}_{}'.format(event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper.generate_team_at_event_alliance_status_string(team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper.generate_team_at_event_playoff_status_string(team.key.id(), event_team.status), } teams_and_statuses.append(( team, event_team.status, status_str )) if has_teams: teams_and_statuses.sort(key=lambda x: x[0].team_number) live_events_with_teams.append((event, teams_and_statuses)) live_events_with_teams.sort(key=lambda x: x[0].name) live_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) live_events_with_teams.sort(key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) # Get valid years district_history = history_future.get_result() valid_years = map(lambda d: d.year, district_history) valid_years = sorted(valid_years) self.template_values.update({ 'explicit_year': explicit_year, 'year': year, 'valid_years': valid_years, 'valid_districts': valid_districts, 'district_name': district.display_name, 'district_abbrev': district_abbrev, 'week_events': week_events, 'events_by_key': events_by_key, 'rankings': district.rankings, 'advancement': district.advancement, 'teams_a': teams_a, 'teams_b': teams_b, 'live_events_with_teams': live_events_with_teams, }) return jinja2_engine.render('district_details.html', self.template_values)
def _render(self, district_abbrev, year=None, explicit_year=False): district = DistrictQuery('{}{}'.format(year, district_abbrev)).fetch() if not district: self.abort(404) events_future = DistrictEventsQuery(district.key_name).fetch_async() # needed for district teams district_teams_future = DistrictTeamsQuery(district.key_name).fetch_async() # needed for valid_years history_future = DistrictHistoryQuery(district.abbreviation).fetch_async() # needed for valid_districts districts_in_year_future = DistrictsInYearQuery(district.year).fetch_async() # Temp disabled on 2017-02-18 -fangeugene # # Needed for active team statuses # live_events = [] # if year == datetime.datetime.now().year: # Only show active teams for current year # live_events = EventHelper.getWeekEvents() # live_eventteams_futures = [] # for event in live_events: # live_eventteams_futures.append(EventTeamsQuery(event.key_name).fetch_async()) events = events_future.get_result() EventHelper.sort_events(events) events_by_key = {} for event in events: events_by_key[event.key.id()] = event week_events = EventHelper.groupByWeek(events) valid_districts = set() districts_in_year = districts_in_year_future.get_result() for dist in districts_in_year: valid_districts.add((dist.display_name, dist.abbreviation)) valid_districts = sorted(valid_districts, key=lambda (name, _): name) teams = TeamHelper.sortTeams(district_teams_future.get_result()) 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:] # Temp disabled on 2017-02-18 -fangeugene # # Currently Competing Team Status # live_events_with_teams = EventTeamStatusHelper.buildEventTeamStatus(live_events, live_eventteams_futures, teams) # live_events_with_teams.sort(key=lambda x: x[0].name) # Get valid years district_history = history_future.get_result() valid_years = map(lambda d: d.year, district_history) valid_years = sorted(valid_years) self.template_values.update({ 'explicit_year': explicit_year, 'year': year, 'valid_years': valid_years, 'valid_districts': valid_districts, 'district_name': district.display_name, 'district_abbrev': district_abbrev, 'week_events': week_events, 'events_by_key': events_by_key, 'rankings': district.rankings, 'teams_a': teams_a, 'teams_b': teams_b, # 'live_events_with_teams': live_events_with_teams, # Temp disabled on 2017-02-18 -fangeugene }) return jinja2_engine.render('district_details.html', self.template_values)
def _render(self, district_abbrev, year=None, explicit_year=False): district = DistrictQuery('{}{}'.format(year, district_abbrev)).fetch() if not district: self.abort(404) events_future = DistrictEventsQuery(district.key_name).fetch_async() # needed for district teams district_teams_future = DistrictTeamsQuery( district.key_name).fetch_async() # needed for valid_years history_future = DistrictHistoryQuery( district.abbreviation).fetch_async() # needed for valid_districts districts_in_year_future = DistrictsInYearQuery( district.year).fetch_async() # needed for active team statuses live_events = [] if year == datetime.datetime.now( ).year: # Only show active teams for current year live_events = EventHelper.getWeekEvents() live_eventteams_futures = [] for event in live_events: live_eventteams_futures.append( EventTeamsQuery(event.key_name).fetch_async()) events = events_future.get_result() EventHelper.sort_events(events) events_by_key = {} for event in events: events_by_key[event.key.id()] = event week_events = EventHelper.groupByWeek(events) valid_districts = set() districts_in_year = districts_in_year_future.get_result() for dist in districts_in_year: valid_districts.add((dist.display_name, dist.abbreviation)) valid_districts = sorted(valid_districts, key=lambda (name, _): name) teams = TeamHelper.sortTeams(district_teams_future.get_result()) team_keys = set([t.key.id() for t in 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:] # Currently Competing Team Status event_team_keys = [] for event, teams_future in zip(live_events, live_eventteams_futures): for team in teams_future.get_result(): if team.key.id() in team_keys: event_team_keys.append( ndb.Key(EventTeam, '{}_{}'.format( event.key.id(), team.key.id()))) # Should be in context cache ndb.get_multi(event_team_keys) # Warms context cache live_events_with_teams = [] for event, teams_future in zip(live_events, live_eventteams_futures): teams_and_statuses = [] has_teams = False for team in teams_future.get_result(): if team.key.id() in team_keys: has_teams = True event_team = EventTeam.get_by_id('{}_{}'.format( event.key.id(), team.key.id())) # Should be in context cache status_str = { 'alliance': EventTeamStatusHelper. generate_team_at_event_alliance_status_string( team.key.id(), event_team.status), 'playoff': EventTeamStatusHelper. generate_team_at_event_playoff_status_string( team.key.id(), event_team.status), } teams_and_statuses.append( (team, event_team.status, status_str)) if has_teams: teams_and_statuses.sort(key=lambda x: x[0].team_number) live_events_with_teams.append((event, teams_and_statuses)) live_events_with_teams.sort(key=lambda x: x[0].name) live_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) live_events_with_teams.sort( key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) # Get valid years district_history = history_future.get_result() valid_years = map(lambda d: d.year, district_history) valid_years = sorted(valid_years) self.template_values.update({ 'explicit_year': explicit_year, 'year': year, 'valid_years': valid_years, 'valid_districts': valid_districts, 'district_name': district.display_name, 'district_abbrev': district_abbrev, 'week_events': week_events, 'events_by_key': events_by_key, 'rankings': district.rankings, 'advancement': district.advancement, 'teams_a': teams_a, 'teams_b': teams_b, 'live_events_with_teams': live_events_with_teams, }) return jinja2_engine.render('district_details.html', self.template_values)