def load_log_configure(): """ 加载配置文件信息到缓存中 :return: """ log_conf_file = open('configure/system_log.conf', 'rt', encoding='utf-8') while True: # 读取配置文件每一行的配置信息 log_conf_str = log_conf_file.readline() if not log_conf_str: break log_confs = log_conf_str.split('=') action_type = log_confs[ACTION_TYPE_INDEX] conf_dict = {'msg': log_confs[MSG_INDEX]} if DATA_FIELD_INDEX < len(log_confs): conf_dict['data_field'] = log_confs[DATA_FIELD_INDEX].strip( ).split(';') else: conf_dict['data_field'] = [] conf_cache_api.set_hash('log_configure', action_type, json.dumps(conf_dict)) log_conf_file.close()
def get_configure_by_name(name): """根据配置名获取配置值""" val = cache.get_hash('configure', name) if val is None: val = Configure.objects.get(name=name).val cache.set_hash('configure', name, val) return val
def init_configure(): """ 初始化系统配置 :return: """ conf_list = [{ 'label': '检查未执行操作时间间隔(秒)', 'name': 'unexecuted_opr_check_time', 'val': 5, }, { 'label': '定时查询流量计时间间隔(分)', 'name': 'query_meter_time', 'val': 30, }, { 'label': '登录过期时间', 'name': 'session_expire_time', 'val': '关闭浏览器过期', }] for conf in conf_list: try: c = Configure.objects.get(name=conf['name']) conf_cache_api.set_hash('configure', conf['name'], c.val) except Configure.DoesNotExist: Configure.objects.create(**conf) conf_cache_api.set_hash('configure', conf['name'], conf['val'])
def set_query_meter_time(check_time): """ 获取定时查询流量计的时间间隔 :return: """ key = 'query_meter_time' conf = Configure.objects.get(name=key) conf.val = str(check_time) with transaction.atomic(): conf.save() cache.set_hash('configure', key, check_time)
def set_unexecuted_opr_check_time(check_time): """ 获取未执行的操作的检查时间 例如:每5秒检查是否有操作还未执行 :return: """ key = 'unexecuted_opr_check_time' conf = Configure.objects.get(name=key) conf.val = str(check_time) with transaction.atomic(): conf.save() cache.set_hash('configure', key, check_time)
def get_query_meter_time(): """ 获取未执行的操作的检查时间 例如:每5秒检查是否有操作还未执行 :return: """ key = 'query_meter_time' check_time = cache.get_hash('configure', key) # 如果不存在缓存中,则从数据库中读取 if check_time is None: check_time = Configure.objects.get(name=key).val check_time = int(check_time) cache.set_hash('configure', key, check_time) return int(check_time)
def update_configure(conf_info): must_dict = { "name": str, "val": StrCheck.check_value, } param_check(conf_info, must_dict) conf = Configure.objects.get(name=conf_info['name']) conf.val = conf_info['val'] with transaction.atomic(): conf.save() # 更新缓存 cache.set_hash('configure', conf_info['name'], conf_info['val'])
def find_register_by_opr_type(opr_type): register_str = conf_cache_api.get_hash('control_register', opr_type) if register_str is None: register = core.find_one_control_register({'opr_type': opr_type}) register_str = json.dumps({ 'field_val': register.field_val, 'const_data': register.const_data }) # 重新设置缓存 conf_cache_api.set_hash('control_register', opr_type, register_str) else: register_dict = json.loads(register_str.decode('utf-8')) register = ControlRegister() register.field_val = register_dict['field_val'] register.const_data = register_dict['const_data'] return register
def update_control_register(register_info): must_dict = { 'id': int, 'field_val': int, 'remark': StrCheck.check_remark, } param_check(register_info, must_dict, extra=True) register_str = json.dumps({ 'field_val': register_info['field_val'], 'const_data': register_info['const_data'] }) with transaction.atomic(): register = core.update_control_register(register_info) # 重新设置缓存 conf_cache_api.set_hash('control_register', register.opr_type, register_str)
def sync_meter_history_data(): """ 同步所有仪表的所有历史数据 :return: """ now_date = datetime.datetime.now().date() month_begin_date = datetime.datetime(year=now_date.year - 1, month=now_date.month, day=1).date() datas = MeterHistoryData.objects.all().order_by('time').values( 'data', 'meter__id', 'time') year_data_map = {} month_data_map = {} flow_data_map = {} for meter_data in datas: meter_id = meter_data['meter__id'] # 统计最近几年 year_str = meter_data['time'].strftime('%Y年') year_map = year_data_map.get(meter_id, {}) year_map[year_str] = year_map.get(year_str, 0) + meter_data['data'] year_data_map[meter_id] = year_map # 统计最近12个月 if month_begin_date <= meter_data['time']: month_str = meter_data['time'].strftime('%Y年%m月') month_map = month_data_map.get(meter_id, {}) month_map[month_str] = month_map.get(month_str, 0) + meter_data['data'] month_data_map[meter_id] = month_map flow_data_map[meter_id] = flow_data_map.get(meter_id, 0) + meter_data['data'] LOG.info("month_statistic:{}".format(month_data_map)) # 更新缓存中的数据 for key, val in year_data_map.items(): conf_cache_api.set_hash('year_statistic', key, json.dumps(val)) for key, val in month_data_map.items(): conf_cache_api.set_hash('month_statistic', key, json.dumps(val)) for key, val in flow_data_map.items(): conf_cache_api.set_hash('flow_statistic', key, val)