def leaderboard(): users = {} lower_10, upper_10 = make_upper_lower(offset=datetime.timedelta(minutes=2)) for slc in ShareSlice.get_span(share_type=("acc", ), ret_query=True, lower=lower_10, upper=upper_10): try: address_version(slc.user) except Exception: pass else: user = users.setdefault(slc.user, {}) user.setdefault(slc.algo, [0, set()]) user[slc.algo][0] += slc.value user[slc.algo][1].add(slc.time) # Loop through and convert a summation of shares into a hashrate. Converts # to hashes per second for user, algo_shares in users.iteritems(): for algo_key, (shares, minutes) in algo_shares.items(): algo_obj = algos[algo_key] algo_shares[algo_key] = algo_obj.hashes_per_share * (shares / (len(minutes) * 60)) algo_shares.setdefault('normalized', 0) algo_shares['normalized'] += users[user][algo_key] * algo_obj.normalize_mult sorted_users = sorted(users.iteritems(), key=lambda x: x[1]['normalized'], reverse=True) # This is really bad.... XXX: Needs rework! if users: anon = anon_users() for i, (user, data) in enumerate(sorted_users): if user in anon: sorted_users[i] = ("Anonymous", data) cache.set("leaderboard", sorted_users, timeout=15 * 60)
def leaderboard(): users = {} lower_10, upper_10 = make_upper_lower(offset=datetime.timedelta(minutes=1)) for slc in ShareSlice.get_span(ret_query=True, lower=lower_10, upper=upper_10): try: address_version(slc.user) except Exception: pass else: user = users.setdefault(slc.user, {}) user.setdefault(slc.algo, [0, set()]) user[slc.algo][0] += slc.value user[slc.algo][1].add(slc.time) # Loop through and convert a summation of shares into a hashrate. Converts # to hashes per second for user, algo_shares in users.iteritems(): for algo_key, (shares, minutes) in algo_shares.items(): algo_obj = algos[algo_key] algo_shares[algo_key] = algo_obj.hashes_per_share * (shares / (len(minutes) * 60)) algo_shares.setdefault('normalized', 0) algo_shares['normalized'] += users[user][algo_key] * algo_obj.normalize_mult sorted_users = sorted(users.iteritems(), key=lambda x: x[1]['normalized'], reverse=True) # This is really bad.... XXX: Needs rework! if users: anon = anon_users() for i, (user, data) in enumerate(sorted_users): if user in anon: sorted_users[i] = ("Anonymous", data) cache.set("leaderboard", sorted_users, timeout=15 * 60)
def test_span(self): start = datetime.datetime.utcnow() shares = list(xrange(300)) for x in shares: now = start - datetime.timedelta(minutes=x) v = ShareSlice(time=now, value=x, **self.slice_test_data) self.db.session.add(v) self.db.session.commit() lower, upper = make_upper_lower() res = ShareSlice.get_span(stamp=True, lower=lower, upper=upper) self.assertEqual(len(res[0]['values']), 10) self.assertEqual(sum(res[0]['values'].values()), 45) res = ShareSlice.get_span(stamp=True) self.assertEqual(sum(res[0]['values'].values()), 44850)
def test_compress(self): start = datetime.datetime.utcnow().replace(second=0, microsecond=0) for x in xrange(1500): now = start - datetime.timedelta(minutes=x) v = ShareSlice(time=now, value=x, **self.slice_test_data) self.db.session.add(v) self.db.session.commit() ShareSlice.compress(0) self.db.session.commit() lower, upper = make_upper_lower() res = ShareSlice.get_span(stamp=True, lower=lower, upper=upper) self.assertEqual(len(res[0]['values']), 10) self.assertEqual(sum(res[0]['values'].values()), 45) res = ShareSlice.get_span(stamp=True) print(res) self.assertEqual(sum(res[0]['values'].values()), 1124250) spans = {0: 0, 1: 0, 2: 0} for slc in ShareSlice.get_span(ret_query=True): spans[slc.span] += 1 print spans assert spans[0] <= 65 assert spans[1] >= 48 ShareSlice.compress(1) self.db.session.commit() spans = {0: 0, 1: 0, 2: 0} for slc in ShareSlice.get_span(ret_query=True): spans[slc.span] += 1 print spans assert spans[0] <= 65 assert spans[1] <= 288 assert spans[2] >= 1 res = ShareSlice.get_span(stamp=True) self.assertEqual(sum(res[0]['values'].values()), 1124250)