# -*- coding:UTF-8 -*-
# @Time  : 2020/7/28 8:53
# @File  : cal_ni_and_put_to_backend.py
# @email : litao@igengmei.com
# @author : litao
import redis
import json
import datetime
import time
import sys
from maintenance.func_send_email_with_file import send_file_email

rds = redis.StrictRedis(host='154.8.190.251', port=6379, db=19)


class push_rule(object):
    def __init__(self, repost_count_ni=None, comment_count_ni=None, favorite_count_ni=None, time_range=5):
        """
        传入增量计算规则
        如 5分钟点赞量增长200
        faverite_count_ni = 200
        time_range = 5
        :param repost_count_ni: Int 转发增长值
        :param comment_count_ni: Int 评论增长值
        :param favorite_count_ni: Int 点赞增长值
        :param time_range: Int 间隔分钟
        """
        self._repost_count_ni = repost_count_ni
        self._comment_count_ni = comment_count_ni
        self._favorite_count_ni = favorite_count_ni
        self._time_range = time_range
        try:
            self.repost_per_min = self._repost_count_ni / time_range
        except:
            self.repost_per_min = -100
        try:
            self.comment_per_min = self._comment_count_ni / time_range
        except:
            self.comment_per_min = -100
        try:
            self.favorite_per_min = self._favorite_count_ni / time_range
        except:
            self.favorite_per_min = -100

    def parse_data(self, fetch_time_last=None, repost_count_last=None, comment_count_last=None,
                   favorite_count_last=None, fetch_time=None, repost_count=None, comment_count=None,
                   favorite_count=None, parse_mode="and") -> bool:
        """

        :param fetch_time_last:
        :param repost_count_last:
        :param comment_count_last:
        :param favorite_count_last:
        :param fetch_time:
        :param repost_count:
        :param comment_count:
        :param favoratie_count:
        :param parse_mode: str "and" or "or" 用于判断条件是同时满足还是满足任一条件
        :return:
        """
        if fetch_time_last and fetch_time:
            time_diff = (fetch_time_last - fetch_time) / 60 / 1e3
        else:
            raise KeyError("time input error")
        if isinstance(comment_count_last, int) and isinstance(comment_count, int):
            comment_diff = comment_count_last - comment_count
        else:
            comment_diff = -100
        if isinstance(favorite_count_last, int) and isinstance(favorite_count, int):
            favoratie_diff = favorite_count_last - favorite_count
        else:
            favoratie_diff = -100
        if isinstance(repost_count_last, int) and isinstance(repost_count, int):
            repost_diff = repost_count_last - repost_count
        else:
            repost_diff = -100

        if parse_mode == "and":
            if comment_diff / time_diff >= self.comment_per_min and favoratie_diff / time_diff >= self.favorite_per_min and repost_diff / time_diff >= self.repost_per_min:
                return True
            else:
                return False
        elif parse_mode == "or":
            if comment_diff / time_diff >= self.comment_per_min or favoratie_diff / time_diff >= self.favorite_per_min or repost_diff / time_diff >= self.repost_per_min:
                return True
            else:
                return False
        else:
            return False


def scan_from_redis(push_rule_class):
    # len_id_list = rds.llen("doc_id")
    while True:
        doc_id = rds.lpop("doc_id")
        if doc_id:
            res = rds.llen(doc_id)
            if res < 2:
                continue
            res_list = rds.lrange(doc_id, 0, 1)
            fetch_time = 0
            repost_count = 0
            comment_count = 0
            favorite_count = 0
            for count, re in enumerate(res_list):
                one_data = json.loads(re)
                if count == 0:
                    fetch_time = one_data.get("fetch_time")
                    repost_count = one_data.get("repost_count")
                    comment_count = one_data.get("comment_count")
                    favorite_count = one_data.get("favorite_count")
                    continue

                bool_res = push_rule_class.parse_data(fetch_time_last=fetch_time, repost_count_last=repost_count,
                                                      comment_count_last=comment_count,
                                                      favorite_count_last=favorite_count,
                                                      comment_count=one_data.get("comment_count"),
                                                      favorite_count=one_data.get("favorite_count"),
                                                      repost_count=one_data.get("repost_count"), parse_mode="and",
                                                      fetch_time=one_data.get("fetch_time"))
                print(bool_res)
                if bool_res:
                    pass

            print(res_list)

        else:
            time.sleep(5)


def task_main():
    # 实例化数据判断规则
    push_rule_class = push_rule(favorite_count_ni=0.000000001, time_range=5)
    # 循环处理抓取数据
    scan_from_redis(push_rule_class)


task_main()
