def test_award_updated(self): affected_refs = { 'event': {ndb.Key(Event, '2015casj'), ndb.Key(Event, '2015cama')}, 'team_list': {ndb.Key(Team, 'frc254'), ndb.Key(Team, 'frc604')}, 'year': {2014, 2015} } cache_keys = [ q.cache_key for q in get_affected_queries.award_updated(affected_refs) ] self.assertEqual(len(cache_keys), 12) self.assertTrue(EventAwardsQuery('2015casj').cache_key in cache_keys) self.assertTrue(EventAwardsQuery('2015cama').cache_key in cache_keys) self.assertTrue(TeamAwardsQuery('frc254').cache_key in cache_keys) self.assertTrue(TeamAwardsQuery('frc604').cache_key in cache_keys) self.assertTrue( TeamYearAwardsQuery('frc254', 2014).cache_key in cache_keys) self.assertTrue( TeamYearAwardsQuery('frc254', 2015).cache_key in cache_keys) self.assertTrue( TeamYearAwardsQuery('frc604', 2014).cache_key in cache_keys) self.assertTrue( TeamYearAwardsQuery('frc604', 2015).cache_key in cache_keys) self.assertTrue( TeamEventAwardsQuery('frc254', '2015casj').cache_key in cache_keys) self.assertTrue( TeamEventAwardsQuery('frc254', '2015cama').cache_key in cache_keys) self.assertTrue( TeamEventAwardsQuery('frc604', '2015casj').cache_key in cache_keys) self.assertTrue( TeamEventAwardsQuery('frc604', '2015cama').cache_key in cache_keys)
def test_award_updated(self): affected_refs = { 'event': {ndb.Key(Event, '2015casj'), ndb.Key(Event, '2015cama')}, 'team_list': {ndb.Key(Team, 'frc254'), ndb.Key(Team, 'frc604')}, 'year': {2014, 2015}, 'event_type_enum': {EventType.REGIONAL, EventType.DISTRICT}, 'award_type_enum': {AwardType.WINNER, AwardType.CHAIRMANS}, } cache_keys = [q.cache_key for q in get_affected_queries.award_updated(affected_refs)] self.assertEqual(len(cache_keys), 20) self.assertTrue(EventAwardsQuery('2015casj').cache_key in cache_keys) self.assertTrue(EventAwardsQuery('2015cama').cache_key in cache_keys) self.assertTrue(TeamAwardsQuery('frc254').cache_key in cache_keys) self.assertTrue(TeamAwardsQuery('frc604').cache_key in cache_keys) self.assertTrue(TeamYearAwardsQuery('frc254', 2014).cache_key in cache_keys) self.assertTrue(TeamYearAwardsQuery('frc254', 2015).cache_key in cache_keys) self.assertTrue(TeamYearAwardsQuery('frc604', 2014).cache_key in cache_keys) self.assertTrue(TeamYearAwardsQuery('frc604', 2015).cache_key in cache_keys) self.assertTrue(TeamEventAwardsQuery('frc254', '2015casj').cache_key in cache_keys) self.assertTrue(TeamEventAwardsQuery('frc254', '2015cama').cache_key in cache_keys) self.assertTrue(TeamEventAwardsQuery('frc604', '2015casj').cache_key in cache_keys) self.assertTrue(TeamEventAwardsQuery('frc604', '2015cama').cache_key in cache_keys) for team_key in ['frc254', 'frc604']: for event_type in [EventType.REGIONAL, EventType.DISTRICT]: for award_type in [AwardType.WINNER, AwardType.CHAIRMANS]: self.assertTrue(TeamEventTypeAwardsQuery(team_key, event_type, award_type).cache_key in cache_keys)
def _render(self, team_key): self._set_team(team_key) awards = TeamAwardsQuery(self.team_key).fetch() awards_list = [ModelToDict.awardConverter(award) for award in awards] return json.dumps(awards_list, ensure_ascii=True)
def award_updated(affected_refs): event_keys = _filter(affected_refs['event']) team_keys = _filter(affected_refs['team_list']) years = _filter(affected_refs['year']) event_types = _filter(affected_refs['event_type_enum']) award_types = _filter(affected_refs['award_type_enum']) queries_and_keys = [] for event_key in event_keys: queries_and_keys.append((EventAwardsQuery(event_key.id()))) for team_key in team_keys: queries_and_keys.append( (TeamEventAwardsQuery(team_key.id(), event_key.id()))) for team_key in team_keys: queries_and_keys.append((TeamAwardsQuery(team_key.id()))) for year in years: queries_and_keys.append((TeamYearAwardsQuery(team_key.id(), year))) for event_type in event_types: for award_type in award_types: queries_and_keys.append( (TeamEventTypeAwardsQuery(team_key.id(), event_type, award_type))) return queries_and_keys
def award_updated(affected_refs): event_keys = filter(None, affected_refs['event']) team_keys = filter(None, affected_refs['team_list']) years = filter(None, affected_refs['year']) queries_and_keys = [] for event_key in event_keys: queries_and_keys.append((EventAwardsQuery(event_key.id()))) for team_key in team_keys: queries_and_keys.append((TeamEventAwardsQuery(team_key.id(), event_key.id()))) for team_key in team_keys: queries_and_keys.append((TeamAwardsQuery(team_key.id()))) for year in years: queries_and_keys.append((TeamYearAwardsQuery(team_key.id(), year))) return queries_and_keys
def update_team_awards_index(cls, team): awards_future = TeamAwardsQuery(team.key.id()).fetch_async() events_future = TeamEventsQuery(team.key.id()).fetch_async() medias_future = TeamMediaQuery(team.key.id()).fetch_async() events_by_year = defaultdict(list) for event in events_future.get_result(): events_by_year[event.year].append(event) event.prep_details() # For rankings awards_by_event = defaultdict(list) for award in awards_future.get_result(): awards_by_event[award.event.id()].append(award) medias_by_year = defaultdict(list) for media in medias_future.get_result(): medias_by_year[media.year].append(media) # General stuff that's the same for indexes partial_fields = [ search.NumberField(name='number', value=team.team_number), search.TextField(name='name', value=team.name), search.TextField(name='nickname', value=team.nickname) ] # field_counts = defaultdict(int) for year, events in events_by_year.items(): year_matches_future = TeamYearMatchesQuery(team.key.id(), year).fetch_async() qual_seeds = defaultdict(int) comp_levels = defaultdict(int) year_awards = set() for event in events: if event.event_type_enum not in EventType.SEASON_EVENT_TYPES: continue if event.rankings: for row in event.rankings: if str(row[1]) == str(team.team_number): seed = int(row[0]) for s in xrange(seed, 9): qual_seeds[s] += 1 break awards = awards_by_event.get(event.key.id(), []) award_types = set([a.award_type_enum for a in awards]) for award_type in award_types: if award_type in AwardType.SEARCHABLE: year_awards.add(award_type) if award_type == AwardType.WINNER: comp_levels['win'] += 1 event_levels = defaultdict(set) for match in year_matches_future.get_result(): if match.comp_level in { 'sf', 'f' } and match.comp_level not in event_levels[match.event.id()]: comp_levels[match.comp_level] += 1 event_levels[match.event.id()].add(match.comp_level) has_cad = False for media in medias_by_year[year]: if media.media_type_enum in MediaType.robot_types: has_cad = True break fields = partial_fields + [ search.AtomField(name='award', value=str(award)) for award in year_awards ] + [search.NumberField(name='year', value=year)] if comp_levels: for level, count in comp_levels.items(): fields += [ search.NumberField(name='comp_level_{}'.format(level), value=count) ] if qual_seeds: for seed, count in qual_seeds.items(): fields += [ search.NumberField(name='seed_{}'.format(seed), value=count) ] if has_cad: fields += [search.NumberField(name='has_cad', value=1)] search.Index(name=cls.TEAM_AWARDS_INDEX).put( search.Document(doc_id='{}_{}'.format(team.key.id(), year), fields=fields))
def _render(self, team_key): awards, self._last_modified = TeamAwardsQuery(team_key).fetch(dict_version=3, return_updated=True) return json.dumps(awards, ensure_ascii=True, indent=2, sort_keys=True)
def update_team_year_index(cls, team): awards_future = TeamAwardsQuery(team.key.id()).fetch_async() events_future = TeamEventsQuery(team.key.id()).fetch_async() events_by_year = defaultdict(list) for event in events_future.get_result(): events_by_year[event.year].append(event) awards_by_year = defaultdict(list) for award in awards_future.get_result(): awards_by_year[award.year].append(award) # General stuff that's the same for all years same_fields = [ search.NumberField(name='number', value=team.team_number), search.TextField(name='name', value=team.name), search.TextField(name='nickname', value=team.nickname) ] if team.normalized_location and team.normalized_location.lat_lng: same_fields += [ search.GeoField(name='location', value=search.GeoPoint( team.normalized_location.lat_lng.lat, team.normalized_location.lat_lng.lon)) ] # Construct overall and year specific fields overall_fields = same_fields + [search.NumberField(name='year', value=0)] overall_event_types = set() overall_event_award_types = set() overall_award_types = set() overall_bb_count = 0 overall_divwin_count = 0 overall_cmpwin_count = 0 for year, events in events_by_year.items(): year_fields = same_fields + [search.NumberField(name='year', value=year)] # Events year_event_types = set() for event in events: # Allow searching by event type if event.event_type_enum not in overall_event_types: overall_fields += [search.AtomField(name='event_type', value=str(event.event_type_enum))] overall_event_types.add(event.event_type_enum) if event.event_type_enum not in year_event_types: year_fields += [search.AtomField(name='event_type', value=str(event.event_type_enum))] year_event_types.add(event.event_type_enum) # Awards year_event_award_types = set() year_award_types = set() year_bb_count = 0 year_divwin_count = 0 year_cmpwin_count = 0 for award in awards_by_year.get(year, []): if award.event_type_enum not in EventType.SEASON_EVENT_TYPES: continue # Allow searching by award type and event type ea_type = '{}_{}'.format(award.event_type_enum, award.award_type_enum) if ea_type not in overall_event_award_types: overall_fields += [search.AtomField(name='event_award_type', value=ea_type)] overall_event_award_types.add(ea_type) if ea_type not in year_event_award_types: year_fields += [search.AtomField(name='event_award_type', value=ea_type)] year_event_award_types.add(ea_type) # Allow searching by award type if award.award_type_enum not in overall_award_types: overall_fields += [search.AtomField(name='award_type', value=str(award.award_type_enum))] overall_award_types.add(award.award_type_enum) if award.award_type_enum not in year_award_types: year_fields += [search.AtomField(name='award_type', value=str(award.award_type_enum))] year_award_types.add(award.award_type_enum) # Allow searching/sorting by blue banners if award.award_type_enum in AwardType.BLUE_BANNER_AWARDS: overall_bb_count += 1 year_bb_count += 1 # Allow searching/sorting by div/cmp winner if award.award_type_enum == AwardType.WINNER: if award.event_type_enum == EventType.CMP_DIVISION: overall_divwin_count += 1 year_divwin_count += 1 elif award.event_type_enum == EventType.CMP_FINALS: overall_cmpwin_count += 1 year_cmpwin_count += 1 year_fields += [ search.NumberField(name='bb_count', value=year_bb_count), search.NumberField(name='divwin_count', value=year_divwin_count), search.NumberField(name='cmpwin_count', value=year_cmpwin_count), ] # Put year index search.Index(name=cls.TEAM_YEAR_INDEX).put( search.Document(doc_id='{}_{}'.format(team.key.id(), year), fields=year_fields)) overall_fields += [ search.NumberField(name='bb_count', value=overall_bb_count), search.NumberField(name='divwin_count', value=overall_divwin_count), search.NumberField(name='cmpwin_count', value=overall_cmpwin_count), ] # Put overall index search.Index(name=cls.TEAM_YEAR_INDEX).put( search.Document(doc_id='{}_0'.format(team.key.id()), fields=overall_fields))
def update_team_awards_index(cls, team): awards_future = TeamAwardsQuery(team.key.id()).fetch_async() events_future = TeamEventsQuery(team.key.id()).fetch_async() medias_future = TeamMediaQuery(team.key.id()).fetch_async() events_by_year = defaultdict(list) for event in events_future.get_result(): events_by_year[event.year].append(event) event.prep_details() # For rankings awards_by_event = defaultdict(list) for award in awards_future.get_result(): awards_by_event[award.event.id()].append(award) medias_by_year = defaultdict(list) for media in medias_future.get_result(): medias_by_year[media.year].append(media) # General stuff that's the same for indexes partial_fields = [ search.NumberField(name='number', value=team.team_number), search.TextField(name='name', value=team.name), search.TextField(name='nickname', value=team.nickname) ] # field_counts = defaultdict(int) for year, events in events_by_year.items(): year_matches_future = TeamYearMatchesQuery(team.key.id(), year).fetch_async() qual_seeds = defaultdict(int) comp_levels = defaultdict(int) year_awards = set() for event in events: if event.event_type_enum not in EventType.SEASON_EVENT_TYPES: continue if event.rankings: for row in event.rankings: if str(row[1]) == str(team.team_number): seed = int(row[0]) for s in xrange(seed, 9): qual_seeds[s] += 1 break awards = awards_by_event.get(event.key.id(), []) award_types = set([a.award_type_enum for a in awards]) for award_type in award_types: if award_type in AwardType.SEARCHABLE: year_awards.add(award_type) if award_type == AwardType.WINNER: comp_levels['win'] += 1 event_levels = defaultdict(set) for match in year_matches_future.get_result(): if match.comp_level in {'sf', 'f'} and match.comp_level not in event_levels[match.event.id()]: comp_levels[match.comp_level] += 1 event_levels[match.event.id()].add(match.comp_level) has_cad = False for media in medias_by_year[year]: if media.media_type_enum in MediaType.robot_types: has_cad = True break fields = partial_fields + [ search.AtomField(name='award', value=str(award)) for award in year_awards ] + [search.NumberField(name='year', value=year)] if comp_levels: for level, count in comp_levels.items(): fields += [search.NumberField(name='comp_level_{}'.format(level), value=count)] if qual_seeds: for seed, count in qual_seeds.items(): fields += [search.NumberField(name='seed_{}'.format(seed), value=count)] if has_cad: fields += [ search.NumberField(name='has_cad', value=1) ] search.Index(name=cls.TEAM_AWARDS_INDEX).put( search.Document(doc_id='{}_{}'.format(team.key.id(), year), fields=fields))
def update_team_awards_index(cls, team): awards_future = TeamAwardsQuery(team.key.id()).fetch_async() events_future = TeamEventsQuery(team.key.id()).fetch_async() medias_future = TeamMediaQuery(team.key.id()).fetch_async() events_by_year = defaultdict(list) for event in events_future.get_result(): events_by_year[event.year].append(event) awards_by_event = defaultdict(list) for award in awards_future.get_result(): awards_by_event[award.event.id()].append(award) # General stuff that's the same for indexes fields = [ search.NumberField(name='number', value=team.team_number), search.TextField(name='name', value=team.name), search.TextField(name='nickname', value=team.nickname) ] field_counts = defaultdict(int) for year, events in events_by_year.items(): season_awards = set() season_awards_event = defaultdict(set) for event in events: if event.event_type_enum not in EventType.SEASON_EVENT_TYPES: continue awards = awards_by_event.get(event.key.id(), []) award_types = set([a.award_type_enum for a in awards]) award_types = filter(lambda a: a in AwardType.SEARCHABLE, award_types) # To search by event for award_set in cls._construct_powerset(award_types): set_name = cls._construct_set_name(award_set) field_counts['e_{}'.format(set_name)] += 1 field_counts['e_{}_y{}'.format(set_name, event.year)] += 1 field_counts['e_{}_e{}'.format(set_name, event.event_type_enum)] += 1 field_counts['e_{}_e{}_y{}'.format(set_name, event.event_type_enum, event.year)] += 1 season_awards = season_awards.union(award_types) season_awards_event[event.event_type_enum] = season_awards_event[event.event_type_enum].union(award_types) # To search by year for award_set in cls._construct_powerset(season_awards): set_name = cls._construct_set_name(award_set) field_counts['s_{}'.format(set_name)] += 1 field_counts['s_{}_y{}'.format(set_name, year)] += 1 for event_type, awards in season_awards_event.items(): for award_set in cls._construct_powerset(awards): set_name = cls._construct_set_name(award_set) field_counts['s_{}_e{}'.format(set_name, event_type)] += 1 field_counts['s_{}_e{}_y{}'.format(set_name, event_type, year)] += 1 fields += [search.NumberField(name=field, value=count) for field, count in field_counts.items()] # Medias robot_photo_years = set() cad_model_years = set() for media in medias_future.get_result(): if media.media_type_enum in MediaType.image_types and team.key in media.preferred_references: robot_photo_years.add(media.year) elif media.media_type_enum in MediaType.robot_types: cad_model_years.add(media.year) fields += [search.AtomField(name='robot_photo_year', value=str(year)) for year in robot_photo_years] fields += [search.AtomField(name='cad_model_year', value=str(year)) for year in cad_model_years] search.Index(name=cls.TEAM_AWARDS_INDEX).put( search.Document(doc_id='{}'.format(team.key.id()), fields=fields))