예제 #1
0
파일: models.py 프로젝트: eiritana/canvas
 def __init__(self, name):
     self.name = name.lower().strip().replace('#', '')
     self.base_key = 'tag:{}:posts'.format(self.name)
     self.new = RedisSortedSet(self.base_key)
     self.images_only = RedisLastBumpedBuffer(self.base_key + ':images', 1000)
     self.popular = RedisLastBumpedBuffer(self.base_key + ':popular', 1000)
     self.post_count = RedisKey(self.base_key + ':count')
예제 #2
0
 def __init__(self,
              user_id,
              stream_size=1000,
              activity_types=ACTIVITY_TYPES):
     self._user_id = user_id
     self._activity_types = activity_types
     self._buffer = RedisLastBumpedBuffer(
         'user:{}:stream_v6'.format(user_id),
         stream_size,
         getter=self._make_activity)
     self._read = RedisLastBumpedBuffer(
         'user:{}:stream_read'.format(user_id), stream_size)
예제 #3
0
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
예제 #4
0
    def __init__(self,
                 user_id,
                 stream_size=450,
                 activity_types=ACTIVITY_TYPES,
                 buffer_key_postfix='stream_v6'):
        self._activity_types = {}

        for activity_type in activity_types:
            if isinstance(activity_type, basestring):
                activity_type = _load_activity_type(activity_type)

            self._activity_types[activity_type.TYPE] = activity_type

        self._user_id = user_id
        self._buffer = RedisLastBumpedBuffer(
            'user:{}:{}'.format(user_id, buffer_key_postfix), stream_size)

        #TODO if we add support for this in DrawQuest, we need to use one stream size for both
        # iPhone and iPad if those streams differ.
        self._read = RedisLastBumpedBuffer(
            'user:{}:stream_read'.format(user_id), stream_size)
예제 #5
0
import time

from canvas.models import Visibility
from canvas.redis_models import redis, RedisLastBumpedBuffer
from drawquest import knobs
from drawquest.apps.quests.details_models import QuestDetails

top_quests_buffer = RedisLastBumpedBuffer('top_quests', 500)


def get_quest_score(quest):
    try:
        details = quest.details()
    except AttributeError:
        details = quest

    if not quest.ugq and not quest.scheduledquest_set.exists():
        return -1

    visibility = getattr(details, 'visibility', None)
    if visibility in Visibility.invisible_choices or visibility == Visibility.CURATED:
        return -1

    first_appeared_on = quest.first_appeared_on()

    if first_appeared_on is None:
        return -1

    tdelta = time.time() - first_appeared_on + 10 * 60
    weight = ((tdelta**1.9) if tdelta > 0 else 1)
    pop_score = details.drawing_count
예제 #6
0
파일: models.py 프로젝트: eiritana/canvas
class Tag(object):
    top = property(lambda self: DateKey(lambda key: RedisLastBumpedBuffer(key, 30*30), self.base_key, ':top'))

    updates_channel = property(lambda self: RealtimeChannel('tu:%s' %  self.name, 5, ttl=24*60*60))

    def __repr__(self):
        return self.name

    def __init__(self, name):
        self.name = name.lower().strip().replace('#', '')
        self.base_key = 'tag:{}:posts'.format(self.name)
        self.new = RedisSortedSet(self.base_key)
        self.images_only = RedisLastBumpedBuffer(self.base_key + ':images', 1000)
        self.popular = RedisLastBumpedBuffer(self.base_key + ':popular', 1000)
        self.post_count = RedisKey(self.base_key + ':count')

    def to_client(self):
        return self.name

    def tag_comment(self, comment, timestamp=None):
        if timestamp is None:
            timestamp = Services.time.time()

        self.new.zadd(int(comment.id), timestamp)
        all_tags.sadd(self.name)

        if comment.reply_content is not None:
            self.images_only.bump(int(comment.id), score=timestamp)
            count = self.post_count.incr()
            self.updates_channel.publish({'post': comment.id, 'tag': self.name, 'count': count})

    def untag_comment(self, comment):
        self.new.zrem(comment.id)
        self.images_only.remove(comment.id)
        self.popular.remove(comment.id)

    def get_absolute_url(self):
        return '/x/' + self.name.replace('#','')

    def user_is_following(self, user):
        if not user.is_authenticated():
            return False

        return self.name in user.redis.followed_tags

    def merge_top_scores(self, day=None):
        """
        Merges daily top scores into monthly and monthly into yearly top scores for this group for the given day
        and the 365 days before it.

        If `day` is `None`, defaults to today.
        """
        if not day:
            day = Services.time.today()
        # Merge today + last 365 days
        days = [day - datetime.timedelta(n) for n in range(366)]

        months = defaultdict(list)
        for day in days:
            months[(day.year, day.month)].append(day)

        years = defaultdict(list)

        for (year, month) in months.keys():
            years[year].append(month)

        for (year, month), days in months.iteritems():
            dest = self.top.month(datetime.date(year, month, 1))
            source_keys = [self.top.day(day).key for day in days]
            redis.zunionstore(dest.key, source_keys, aggregate='max')
            dest.truncate(2)

        for year, year_months in years.iteritems():
            dest = self.top.year(datetime.date(year, 1, 1))
            source_keys = [self.top.month(datetime.date(year, month, 1)).key for month in year_months]
            redis.zunionstore(dest.key, source_keys, aggregate='max')
            dest.truncate(5)
예제 #7
0
 def after_setUp(self):
     redis.delete('rblf_key')
     self.lbf = RedisLastBumpedBuffer('rblf_key', 3)