Example #1
0
 def test_leaders(self):
     session = self.session
     today = util.utcnow().date()
     test_data = []
     for i in range(20):
         test_data.append((u'nick-%s' % i, 30))
     highest = u'nick-high-too-long_'
     highest += (128 - len(highest)) * u'x'
     test_data.append((highest, 40))
     lowest = u'nick-low'
     test_data.append((lowest, 20))
     for nick, value in test_data:
         user = User(nickname=nick)
         session.add(user)
         session.flush()
         score = Score(key=ScoreKey.location,
                       userid=user.id, time=today, value=value)
         session.add(score)
     session.commit()
     # check the result
     result = leaders(session)
     self.assertEqual(len(result), 22)
     self.assertEqual(result[0]['nickname'], highest[:24] + u'...')
     self.assertEqual(result[0]['num'], 40)
     self.assertTrue(lowest in [r['nickname'] for r in result])
Example #2
0
 def test_leaders(self):
     from ichnaea.content.stats import leaders
     session = self.db_master_session
     test_data = []
     for i in range(20):
         test_data.append((u'nick-%s' % i, 7))
     highest = u'nick-high-too-long_'
     highest += (128 - len(highest)) * u'x'
     test_data.append((highest, 10))
     lowest = u'nick-low'
     test_data.append((lowest, 5))
     for nick, value in test_data:
         user = User(nickname=nick)
         session.add(user)
         session.flush()
         score = Score(userid=user.id, value=value)
         score.name = 'location'
         session.add(score)
     session.commit()
     # check the result
     result = leaders(session)
     self.assertEqual(len(result), 22)
     self.assertEqual(result[0]['nickname'], highest[:24] + u'...')
     self.assertEqual(result[0]['num'], 10)
     self.assertTrue(lowest in [r['nickname'] for r in result])
Example #3
0
 def test_leaders(self):
     session = self.db_master_session
     today = util.utcnow().date()
     test_data = []
     for i in range(20):
         test_data.append((u'nick-%s' % i, 30))
     highest = u'nick-high-too-long_'
     highest += (128 - len(highest)) * u'x'
     test_data.append((highest, 40))
     lowest = u'nick-low'
     test_data.append((lowest, 20))
     for nick, value in test_data:
         user = User(nickname=nick)
         session.add(user)
         session.flush()
         score = Score(key=ScoreKey.location,
                       userid=user.id,
                       time=today,
                       value=value)
         session.add(score)
     session.commit()
     # check the result
     result = leaders(session)
     self.assertEqual(len(result), 22)
     self.assertEqual(result[0]['nickname'], highest[:24] + u'...')
     self.assertEqual(result[0]['num'], 40)
     self.assertTrue(lowest in [r['nickname'] for r in result])
Example #4
0
    def leaders_view(self):
        redis_client = self.request.registry.redis_client
        cache_key = CACHE_KEYS['leaders']
        cached = redis_client.get(cache_key)

        if cached:
            data = loads(cached)
        else:
            session = self.request.db_ro_session
            data = list(enumerate(leaders(session)))
            data = [{
                'pos': l[0] + 1,
                'num': l[1]['num'],
                'nickname': l[1]['nickname'],
                'anchor': l[1]['nickname'],
            } for l in data]
            redis_client.set(cache_key, dumps(data), ex=600)

        half = len(data) // 2 + len(data) % 2
        leaders1 = data[:half]
        leaders2 = data[half:]
        return {
            'page_title': 'Leaderboard',
            'leaders1': leaders1,
            'leaders2': leaders2,
        }
Example #5
0
    def leaders_view(self):
        redis_client = self.request.registry.redis_client
        cache_key = redis_client.cache_keys['leaders']
        cached = redis_client.get(cache_key)

        if cached:
            data = internal_loads(cached)
        else:
            session = self.request.db_ro_session
            data = list(enumerate(leaders(session)))
            data = [
                {
                    'pos': l[0] + 1,
                    'num': l[1]['num'],
                    'nickname': l[1]['nickname'],
                    'anchor': l[1]['nickname'],
                } for l in data]
            redis_client.set(cache_key, internal_dumps(data), ex=1800)

        half = len(data) // 2 + len(data) % 2
        leaders1 = data[:half]
        leaders2 = data[half:]
        return {
            'page_title': 'Leaderboard',
            'leaders1': leaders1,
            'leaders2': leaders2,
        }
Example #6
0
 def test_leaders(self):
     from ichnaea.content.stats import leaders
     session = self.db_master_session
     test_data = []
     for i in range(20):
         test_data.append((u'nick-%s' % i, 7))
     highest = u'nick-high-too-long_'
     highest += (128 - len(highest)) * u'x'
     test_data.append((highest, 10))
     lowest = u'nick-low'
     test_data.append((lowest, 5))
     for nick, value in test_data:
         user = User(nickname=nick)
         session.add(user)
         session.flush()
         score = Score(userid=user.id, value=value)
         score.name = 'location'
         session.add(score)
     session.commit()
     # check the result
     result = leaders(session)
     self.assertEqual(len(result), 22)
     self.assertEqual(result[0]['nickname'], highest[:24] + u'...')
     self.assertEqual(result[0]['num'], 10)
     self.assertTrue(lowest in [r['nickname'] for r in result])
Example #7
0
 def leaders_view(self):
     session = self.request.db_slave_session
     result = leaders(session)
     half = len(result) // 2 + len(result) % 2
     leaders1 = result[:half]
     leaders2 = result[half:]
     return {
         'page_title': 'Leaderboard',
         'leaders1': leaders1,
         'leaders2': leaders2,
     }
Example #8
0
 def leaders_view(self):
     session = self.request.db_slave_session
     result = list(enumerate(leaders(session)))
     result = [
         {"pos": l[0] + 1, "num": l[1]["num"], "nickname": l[1]["nickname"], "anchor": l[1]["nickname"]}
         for l in result
     ]
     half = len(result) // 2 + len(result) % 2
     leaders1 = result[:half]
     leaders2 = result[half:]
     return {"page_title": "Leaderboard", "leaders1": leaders1, "leaders2": leaders2}
Example #9
0
 def leaders_view(self):
     session = self.request.db_slave_session
     result = leaders(session)
     half = len(result) // 2 + len(result) % 2
     leaders1 = result[:half]
     leaders2 = result[half:]
     return {
         'page_title': 'Leaderboard',
         'leaders1': leaders1,
         'leaders2': leaders2,
     }
Example #10
0
 def leaders_view(self):
     session = self.request.db_slave_session
     result = list(enumerate(leaders(session)))
     result = [{
         'pos': l[0] + 1,
         'num': l[1]['num'],
         'nickname': l[1]['nickname'],
     } for l in result]
     half = len(result) // 2 + len(result) % 2
     leaders1 = result[:half]
     leaders2 = result[half:]
     return {
         'page_title': 'Leaderboard',
         'leaders1': leaders1,
         'leaders2': leaders2,
     }
Example #11
0
    def leaders_view(self):
        data = self._get_cache('leaders')
        if data is None:
            data = [{
                'pos': lead[0] + 1,
                'num': lead[1]['num'],
                'nickname': lead[1]['nickname'],
                'anchor': lead[1]['nickname'],
            } for lead in enumerate(leaders(self.session))]
            self._set_cache('leaders', data)

        half = len(data) // 2 + len(data) % 2
        return {
            'page_title': 'Leaderboard',
            'leaders1': data[:half],
            'leaders2': data[half:],
        }
Example #12
0
 def leaders_view(self):
     session = self.request.db_slave_session
     result = list(enumerate(leaders(session)))
     result = [
         {
             'pos': l[0] + 1,
             'num': l[1]['num'],
             'nickname': l[1]['nickname'],
         } for l in result]
     half = len(result) // 2 + len(result) % 2
     leaders1 = result[:half]
     leaders2 = result[half:]
     return {
         'page_title': 'Leaderboard',
         'leaders1': leaders1,
         'leaders2': leaders2,
     }