

@cronjobs.register
def cache_most_unhelpful_kb_articles():
    'Calculate and save the most unhelpful KB articles in the past month.'
    REDIS_KEY = settings.HELPFULVOTES_UNHELPFUL_KEY
    old_formatted = _get_old_unhelpful()
    final = _get_current_unhelpful(old_formatted)
    if (final == {
        
    }):
        return

    def _mean(vals):
        'Argument: List of floats'
        if (len(vals) == 0):
            return None
        return (sum(vals) / len(vals))

    def _bayes_avg(C, m, R, v):
        return (((C * m) + (R * v)) / (m + v))
    mean_perc = _mean([float(final[key]['currperc']) for key in final.keys()])
    mean_total = _mean([float(final[key]['total']) for key in final.keys()])
    sorted_final = [(key, final[key]['total'], final[key]['currperc'], final[key]['diffperc'], _bayes_avg(mean_perc, mean_total, final[key]['currperc'], final[key]['total'])) for key in final.keys()]
    sorted_final.sort(key=(lambda entry: entry[4]))
    redis = redis_client('helpfulvotes')
    redis.delete(REDIS_KEY)
    max_total = max([b[1] for b in sorted_final])
    for entry in sorted_final:
        doc = Document.objects.get(pk=entry[0])
        redis.rpush(REDIS_KEY, ('%s::%s::%s::%s::%s::%s::%s' % (entry[0], entry[1], entry[2], entry[3], (1 - (entry[1] / max_total)), doc.slug, doc.title)))

dataset/ETHPy150Open mozilla/kitsune/kitsune/dashboards/cron.py cache_most_unhelpful_kb_articles/original