import datetime
from db_helper import RedisHelper
from my_config import set_config
from log import Log
config_, _ = set_config()
log_ = Log()
redis_helper = RedisHelper()
region_code_list = [code for region, code in config_.REGION_CODE.items()]
for _, city_list in config_.REGION_CITY_MAPPING.items():
    for city_code in city_list:
        region_code_list.append(city_code)

key_prefix_list = [
    config_.RECALL_KEY_NAME_PREFIX_REGION_BY_H,
    config_.RECALL_KEY_NAME_PREFIX_DUP1_REGION_24H_H,
    config_.RECALL_KEY_NAME_PREFIX_DUP2_REGION_24H_H,
    config_.RECALL_KEY_NAME_PREFIX_DUP3_REGION_24H_H,
    config_.RECALL_KEY_NAME_PREFIX_DUP2_REGION_48H_H,
    config_.RECALL_KEY_NAME_PREFIX_DUP3_REGION_48H_H,
    config_.RECALL_KEY_NAME_PREFIX_30DAY
]

rule_param_list = [rule_param for rule_param in config_.RULE_PARAMS_REGION_APP_TYPE['params_list']]

now_date = datetime.datetime.strftime(datetime.datetime.today(), '%Y%m%d')
now_h = datetime.datetime.now().hour
h_list = range(now_h + 1)

for rule_param in rule_param_list:
    log_.info(rule_param)
    data_key = rule_param.get('data')
    rule_key = rule_param.get('rule')
    for h in h_list:
        for key_prefix in key_prefix_list:
            for region in region_code_list:
                key = f"{key_prefix}{region}:{data_key}:{rule_key}:{now_date}:{h}"
                if redis_helper.key_exists(key_name=key):
                    redis_helper.update_expire_time(key_name=key, expire_time=2 * 24 * 3600)
            log_.info(f"{key_prefix} end!")
        log_.info(f"h = {h} end!")
    log_.info(f"{rule_param} end!")
