コード例 #1
0
    def test_match_updated(self):
        affected_refs = {
            'key':
            {ndb.Key(Match, '2015casj_qm1'),
             ndb.Key(Match, '2015casj_qm2')},
            'event': {ndb.Key(Event, '2015casj'),
                      ndb.Key(Event, '2015cama')},
            'team_keys': {ndb.Key(Team, 'frc254'),
                          ndb.Key(Team, 'frc604')},
            'year': {2014, 2015},
        }
        cache_keys = [
            q.cache_key
            for q in get_affected_queries.match_updated(affected_refs)
        ]

        self.assertEqual(len(cache_keys), 16)
        self.assertTrue(MatchQuery('2015casj_qm1').cache_key in cache_keys)
        self.assertTrue(MatchQuery('2015casj_qm2').cache_key in cache_keys)
        self.assertTrue(
            MatchGdcvDataQuery('2015casj_qm1').cache_key in cache_keys)
        self.assertTrue(
            MatchGdcvDataQuery('2015casj_qm2').cache_key in cache_keys)
        self.assertTrue(EventMatchesQuery('2015casj').cache_key in cache_keys)
        self.assertTrue(EventMatchesQuery('2015cama').cache_key in cache_keys)
        self.assertTrue(
            EventMatchesGdcvDataQuery('2015casj').cache_key in cache_keys)
        self.assertTrue(
            EventMatchesGdcvDataQuery('2015cama').cache_key in cache_keys)
        self.assertTrue(
            TeamEventMatchesQuery('frc254', '2015casj').cache_key in
            cache_keys)
        self.assertTrue(
            TeamEventMatchesQuery('frc254', '2015cama').cache_key in
            cache_keys)
        self.assertTrue(
            TeamEventMatchesQuery('frc604', '2015casj').cache_key in
            cache_keys)
        self.assertTrue(
            TeamEventMatchesQuery('frc604', '2015cama').cache_key in
            cache_keys)
        self.assertTrue(
            TeamYearMatchesQuery('frc254', 2014).cache_key in cache_keys)
        self.assertTrue(
            TeamYearMatchesQuery('frc254', 2015).cache_key in cache_keys)
        self.assertTrue(
            TeamYearMatchesQuery('frc604', 2014).cache_key in cache_keys)
        self.assertTrue(
            TeamYearMatchesQuery('frc604', 2015).cache_key in cache_keys)
コード例 #2
0
    def _render(self, team_key, year, model_type=None):
        matches, self._last_modified = TeamYearMatchesQuery(
            team_key, int(year)).fetch(dict_version=3, return_updated=True)
        if model_type is not None:
            matches = filter_match_properties(matches, model_type)

        return json.dumps(matches, ensure_ascii=True, indent=2, sort_keys=True)
コード例 #3
0
def match_updated(affected_refs):
    event_keys = filter(None, affected_refs['event'])
    team_keys = filter(None, affected_refs['team_keys'])
    years = filter(None, affected_refs['year'])

    queries_and_keys = []
    for event_key in event_keys:
        queries_and_keys.append((EventMatchesQuery(event_key.id())))
        for team_key in team_keys:
            queries_and_keys.append((TeamEventMatchesQuery(team_key.id(), event_key.id())))

    for team_key in team_keys:
        for year in years:
            queries_and_keys.append((TeamYearMatchesQuery(team_key.id(), year)))

    return queries_and_keys
コード例 #4
0
def match_updated(affected_refs):
    match_keys = _filter(affected_refs['key'])
    event_keys = _filter(affected_refs['event'])
    team_keys = _filter(affected_refs['team_keys'])
    years = _filter(affected_refs['year'])

    queries_and_keys = []
    for match_key in match_keys:
        queries_and_keys.append((MatchQuery(match_key.id())))
        queries_and_keys.append((MatchGdcvDataQuery(match_key.id())))

    for event_key in event_keys:
        queries_and_keys.append((EventMatchesQuery(event_key.id())))
        queries_and_keys.append((EventMatchesGdcvDataQuery(event_key.id())))
        for team_key in team_keys:
            queries_and_keys.append(
                (TeamEventMatchesQuery(team_key.id(), event_key.id())))

    for team_key in team_keys:
        for year in years:
            queries_and_keys.append((TeamYearMatchesQuery(team_key.id(),
                                                          year)))

    return queries_and_keys
コード例 #5
0
    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))
コード例 #6
0
    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))