Ejemplo n.º 1
0
    def _save(self, redis=None):
        statsd.incr('karma.{t}'.format(t=self.action_type))

        key = hash_key(self.userid)

        if not redis:
            redis = redis_client(name='karma')

        # Point counters:
        # Increment total points
        redis.hincrby(key, 'points:total', self.points)
        # Increment points daily count
        redis.hincrby(key, 'points:{d}'.format(
            d=self.date), self.points)
        # Increment points monthly count
        redis.hincrby(key, 'points:{y}-{m:02d}'.format(
            y=self.date.year, m=self.date.month), self.points)
        # Increment points yearly count
        redis.hincrby(key, 'points:{y}'.format(
            y=self.date.year), self.points)

        # Action counters:
        # Increment action total count
        redis.hincrby(key, '{t}:total'.format(t=self.action_type), 1)
        # Increment action daily count
        redis.hincrby(key, '{t}:{d}'.format(
             t=self.action_type, d=self.date), 1)
        # Increment action monthly count
        redis.hincrby(key, '{t}:{y}-{m:02d}'.format(
            t=self.action_type, y=self.date.year, m=self.date.month), 1)
        # Increment action yearly count
        redis.hincrby(key, '{t}:{y}'.format(
            t=self.action_type, y=self.date.year), 1)
Ejemplo n.º 2
0
def init_karma():
    """Flushes the karma redis backend and populates with fresh data.

    Goes through all questions/answers/votes and save karma actions for them.
    """
    if not waffle.switch_is_active('karma'):
        return

    redis_client('karma').flushdb()

    questions = Question.objects.all()
    for chunk in chunked(questions.values_list('pk', flat=True), 200):
        _process_question_chunk.apply_async(args=[chunk])

    votes = AnswerVote.objects.all()
    for chunk in chunked(votes.values_list('pk', flat=True), 1000):
        _process_answer_vote_chunk.apply_async(args=[chunk])
Ejemplo n.º 3
0
def redis_info(request):
    """Admin view that displays redis INFO output for all defined backends."""
    redis_info = {}
    for key in django_settings.REDIS_BACKENDS.keys():
        info = redis_client(key).info()
        redis_info[key] = [{'key': k, 'value': info[k]} for k in sorted(info)]

    return render_to_response('kadmin/redis.html',
                              {'redis_info': redis_info,
                               'title': 'Redis Information'},
                              RequestContext(request, {}))
Ejemplo n.º 4
0
def _process_answer_vote_chunk(data, **kwargs):
    """Save karma data for a chunk of answer votes."""
    redis = redis_client(name='karma')
    v_qs = AnswerVote.objects.select_related('answer')
    for vote in v_qs.filter(pk__in=data):
        if vote.helpful:
            action_class = AnswerMarkedHelpfulAction
        else:
            action_class = AnswerMarkedNotHelpfulAction
        action_class(vote.answer.creator_id, vote.created).save(async=False,
                                                                redis=redis)
Ejemplo n.º 5
0
def _process_question_chunk(data, **kwargs):
    """Save karma data for a chunk of questions."""
    redis = redis_client(name='karma')
    q_qs = Question.objects.select_related('solution').defer('content')
    for question in q_qs.filter(pk__in=data):
        first = True
        a_qs = question.answers.order_by('created').select_related('creator')
        for answer in a_qs.values_list('creator', 'created'):
            AnswerAction(answer[0], answer[1]).save(async=False, redis=redis)
            if first:
                FirstAnswerAction(answer[0], answer[1]).save(async=False,
                                                             redis=redis)
                first = False
        soln = question.solution
        if soln:
            SolutionAction(soln.creator, soln.created).save(async=False,
                                                            redis=redis)
Ejemplo n.º 6
0
def redis_info(request):
    """Admin view that displays redis INFO+CONFIG output for all backends."""
    redis_info = {}
    for key in django_settings.REDIS_BACKENDS.keys():
        redis_info[key] = {}
        client = redis_client(key)
        redis_info[key]['connection'] = django_settings.REDIS_BACKENDS[key]
        try:
            cfg = client.config_get()
            redis_info[key]['config'] = [{'key': k, 'value': cfg[k]} for k in
                                         sorted(cfg)]
            info = client.info()
            redis_info[key]['info'] = [{'key': k, 'value': info[k]} for k in
                                       sorted(info)]
        except ConnectionError:
            redis_info[key]['down'] = True

    return render_to_response('kadmin/redis.html',
                              {'redis_info': redis_info,
                               'title': 'Redis Information'},
                              RequestContext(request, {}))
Ejemplo n.º 7
0
 def __init__(self, redis=None):
     if not redis:
         redis = redis_client(name='karma')
     self.redis = redis
Ejemplo n.º 8
0
        rabbitmq_results = 'There was an error connecting to RabbitMQ!'
        rabbitmq_status = False
    status_summary['rabbitmq'] = rabbitmq_status

    # Check Celery.
    # start = time.time()
    # pong = celery.task.ping()
    # rabbit_results = r = {'duration': time.time() - start}
    # status_summary['rabbit'] = pong == 'pong' and r['duration'] < 1

    # Check Redis.
    redis_results = {}
    if hasattr(settings, 'REDIS_BACKENDS'):
        for backend in settings.REDIS_BACKENDS:
            try:
                c = redis_client(backend)
                redis_results[backend] = c.info()
            except ConnectionError:
                redis_results[backend] = False
    status_summary['redis'] = all(redis_results.values())

    if not all(status_summary.values()):
        status = 500

    return jingo.render(request, 'services/monitor.html',
                        {'memcache_results': memcache_results,
                         'libraries_results': libraries_results,
                         'filepath_results': filepath_results,
                         'rabbitmq_results': rabbitmq_results,
                         'redis_results': redis_results,
                         'status_summary': status_summary},
Ejemplo n.º 9
0
 def __init__(self):
     self.redis = redis_client(name='karma')