排行榜 - 代码展示

# coding:utf-8

from comm.db import db
from comm.db import redis

import datetime
import time
import calendar


def add_months(sourcedate, months):
    month = sourcedate.month - 1 + months
    year = int(sourcedate.year + month / 12)
    month = month % 12 + 1
    day = min(sourcedate.day, calendar.monthrange(year, month)[1])
    return datetime.datetime(year, month, day)


class CreateCareRank(object):
    def __init__(self):
        self.watch_id_list = []
        self.score_dic = ''
        pass

    def get_watch_info(self):
        """
        获取所有腕表id
        """
        cursor = db.watch.find()
        for i in cursor:
            self.watch_id_list.append(i['_id'])

    def count_watch_score(self):
        """
        计算腕表分数
        """
        now_date = str(time.strftime('%Y-%m',time.localtime(time.time())))
        start = datetime.datetime.strptime(now_date, '%Y-%m')
        end = add_months(start, 1)
        for watch_id in self.watch_id_list:
            self.score_dic = {'report_num': 0, 'heart_log': 0, 'service_order': 0, 'is_login': 0, 'look_new': 0, 'datetime': ''}
            query = {'watch_id': watch_id,
                     'datetime': {'$gte': str(start)[0:10],'$lte': str(end)[0:10]}
                     }
            print query
            cursor = db.care_score.find(query)
            for i in cursor:
                self.score_dic['report_num'] += i.get('report_num', 0)
                self.score_dic['heart_log'] += i.get('heart_log', 0)
                self.score_dic['service_order'] += i.get('service_order', 0)
                self.score_dic['is_login'] += i.get('is_login', 0)
                self.score_dic['look_new'] += i.get('look_new', 0)
                self.score_dic['datetime'] = i.get('datetime')[0:7]
            care_score = 0
            active_score = 0
            count = lambda x : 10 if x >= 10 else x
            count_2 = lambda x : 10 if x >= 5 else x*2
            if self.score_dic['report_num'] != 0 or self.score_dic['heart_log'] != 0 or self.score_dic['service_order'] != 0:
                care_score += 70
                care_score += count(self.score_dic['report_num'])
                care_score += count(self.score_dic['heart_log'])
                care_score += count_2(self.score_dic['service_order'])

            else:
                pass
            count_3 = lambda x : 20 if x >= 20 else x
            count_4 = lambda x : 20 if x >= 10 else x*2
            if self.score_dic['look_new'] != 0 or self.score_dic['is_login'] != 0:
                active_score += 60
                active_score += count_3(self.score_dic['is_login'])
                active_score += count_4(self.score_dic['look_new'])
            else:
                pass
            self.score_dic['care_score'] = care_score
            self.score_dic['active_score'] = active_score
            self.insert_redis(float(care_score + active_score)/2, str(watch_id))

    def insert_redis(self, score, watch_id):
        """
        插入数据到redis进行排名统计
        """
        redis.zadd('care_rank', score, watch_id)

    def create_rank(self):
        """
        创建排行榜
        """
        self.get_watch_info()
        self.count_watch_score()

if __name__ == '__main__':
    CCR_obj = CreateCareRank()
    CCR_obj.create_rank()

代码展示

你可能感兴趣的:(排行榜 - 代码展示)