예제 #1
0
 def monthly_counts_js(self, user, start, end):
     hist = [0] * 12
     wpcs = self.weekly_play_counts(user, start, end)
     for date, wpc in wpcs:
         bucket = (ldates.date_of_index(date)).month - 1
         hist[bucket] += wpc
     return hist
예제 #2
0
 def record_week_totals(self, user, start, end, num=10):
     """
     Returns a generator of most songs played in any week between
     start and end.
     """
     for idx, total in \
             self.weekly_play_counts(user, start, end, num, order_by_plays=True):
         yield idx, ldates.date_of_index(idx), total
예제 #3
0
 def record_weeks(self, user, start, end, num=10):
     """
     Returns a generator of artists most played in a single week between
     start and end.
     """
     query = self.user_weeks_between(user, start, end).order_by('-plays')[:num]
     for week in query:
         date = ldates.date_of_index(week.week_idx)
         yield week, date
예제 #4
0
 def record_unique_artists_in_week(self, user, start, end, num=10):
     """
     Returns a generator of weeks with most unique artists scrobbled.
     """
     qs = self.user_weeks_between(user, start, end) \
              .values('week_idx') \
              .annotate(Count('artist')) \
              .order_by('-artist__count')[:num]
     for r in qs:
         idx = r['week_idx']
         yield idx, ldates.date_of_index(idx), r['artist__count']
예제 #5
0
    def weekly_play_counts(self, user, start, end, count=None, just_counts=False,
            order_by_plays=False):

        # Use cache, fill cache if data not there.
        cache_key = "%s:%d:%d:weekly_play_counts" % (user.username, start, end)
        cached = cache.get(cache_key)
        if not cached:
            logging.info("Weekly play counts not in cache: fetching from database")
            qs = self.user_weeks_between(user, start, end) \
                     .values('week_idx')                   \
                     .annotate(Sum('plays'))               \
                     .order_by('week_idx')

            # list() forces evaluation of queryset.
            cached = list(qs)
            cache.set(cache_key, cached)

        def y(i, pc):
            return pc if just_counts else (i, pc)
                
        # last_index handles weeks when nothing was played
        if order_by_plays:
            cached.sort(key=itemgetter('plays__sum'), reverse=True)

        if count: cached = cached[:count]

        last_index = start - 1
        for d in cached:
            index = d['week_idx']
            # need to catch up.
            if not order_by_plays and index != (last_index + 1):
                for idx in xrange(last_index+1, index):
                    logging.info("0 plays on week " + str(ldates.date_of_index(idx)))
                    yield y(idx, 0)
            yield y(index, d['plays__sum'])
            last_index = index