def intersect(metric, today): yesterday = today - datetime.timedelta(1) yesterday_signups = Metrics.signup.uniques(yesterday) today_metric = metric.uniques(today) returning_metric = RedisSet(gen_temp_key()) redis.sinterstore(returning_metric.key, [yesterday_signups.key, today_metric.key]) return safediv(returning_metric.scard(), yesterday_signups.scard())
def _get_aggregate_rlbb(groups, nav): groups = list(groups) if not groups: return [] else: rlbb = RedisLastBumpedBuffer(gen_temp_key(), size=None) buffers = [_get_buffer(group, nav).key for group in groups] redis.zunionstore(rlbb.key, buffers, aggregate='MAX') return rlbb
def update_scores(self): for algo in self.algorithms: ranking = algo.fun() temp_list = RedisList(gen_temp_key()) if ranking: for comment in ranking: temp_list.rpush(comment.id) # Atomically replace the frontpage with the new version. temp_list.rename(algo.frontpage.key) else: # If the ranking is empty, we just delete the frontpage key (the rename will fail) algo.frontpage.delete()
def numbers(request): now = time.time() trailing_7day = now - 7 * 24 * 60 * 60 fmt = lambda n, tot: "%0.0f (%0.2f%%)" % (n, float(n)/tot*100) if tot else None all_users = User.objects.all() all_images = Content.all_objects.count() all_remixes = Content.all_objects.exclude(remix_of=None).count() l7d_images = Content.all_objects.filter(timestamp__gt=trailing_7day).count() l7d_remixes = Content.all_objects.exclude(remix_of=None).filter(timestamp__gt=trailing_7day).count() max_user_id = User.objects.order_by('-id')[0].id trailing_1000_users = User.objects.filter(id__gte=max_user_id-1000) women = lambda u: u.filter(facebookuser__gender=Gender.FEMALE).count() viral = lambda u: (u.aggregate(c=Count('sent_invites__invitee')).get('c', 0) + u.aggregate(c=Count('facebook_sent_invites__invitee')).get('c', 0)) sections = [ ( 'All time', [('Images', all_images), ('Remixes', fmt(all_remixes, all_images))] ), ( 'Last 7 days', [('Images', l7d_images), ('Remixes', fmt(l7d_remixes, l7d_images))] ), ( 'Trailing 1000 users', [('Women', women(trailing_1000_users)), ('Invited users (virality)', viral(trailing_1000_users))] ), ( 'All Users', [('Women', women(all_users)), ('Invited users (virality)', viral(all_users))] ), ] trailing_7day_dau = [Metrics.view.daily_uniques(d) for d in days(7)] users = User.objects.count() daily = sum(trailing_7day_dau) / 7.0 weekly = redis.scard(Metrics.view.trailing_uniques(7, gen_temp_key())) monthly = redis.scard(Metrics.view.trailing_uniques(30, gen_temp_key())) sections += [ ( 'Uniques (by view)', [ ('all users', users), ('daily (trailing 7day average)', fmt(daily, users)), ('weekly (trailing 7day cumulative)', fmt(weekly, users)), ('monthly (trailing 30day cumulative)', fmt(monthly, users)), ], ), ] #u = lambda table, cutoff: fmt(User.objects.annotate(count=Count(table)).filter(count__gt=cutoff).count(), users) # #sections += [ # ( 'Uniques (actions)', # [ # ('all users', users), # ('stickered', u('commentsticker', 0)), # ('stickered 25 times', u('commentsticker', 24)), # ('posted', u('comment', 0)), # ('posted 25 times', u('comment', 24)), # ('remixed', u('comment__reply_content__remix_of', 0)), # ('remixed 25 times', u('comment__reply_content__remix_of', 24)), # ], # ), #] return r2r('staff/numbers.django.html', locals())