Рефакторинг модуля работы с Redis. При отсутствии значение - возвращается 0

This commit is contained in:
zakhy 2024-08-15 12:12:31 +07:00
parent 2b1d5348ac
commit e7b9d90e00
2 changed files with 26 additions and 37 deletions

View File

@ -24,24 +24,21 @@ class RedisRepository(DashboardBaseService):
latest_date = None
with self.service as redis_service:
for position in positions:
try:
if position.tag == 'SIEMENSPLC.AZG01.ReportProdDB.ProdPos13':
pass
position_value = redis_service.hget_value(f'{position.tag}.{interval}', 'Value')
if position_value is None:
position_value = "0"
position_date_str = redis_service.hget_value(f'{position.tag}.{interval}', 'Date')
if position_date_str is not None:
position_date = datetime.strptime(position_date_str, "%Y-%m-%d %H:%M:%S.%f")
if position.tag == 'SIEMENSPLC.AZG01.ReportProdDB.ProdPos13':
pass
position_value = redis_service.hget_value(f'{position.tag}.{interval}', 'Value')
if position_value is None:
position_value = "0"
position_date_str = redis_service.hget_value(f'{position.tag}.{interval}', 'Date')
if position_date_str is not None:
position_date = datetime.strptime(position_date_str, "%Y-%m-%d %H:%M:%S.%f")
scales[position.title] = int(float(position_value)) if int(float(position_value)) >= 0 else 0
if latest_date:
if latest_date < position_date:
latest_date = position_date
else:
scales[position.title] = int(float(position_value)) if int(float(position_value)) >= 0 else 0
if latest_date:
if latest_date < position_date:
latest_date = position_date
except KeyError:
scales[position.position.title] = '0'
else:
latest_date = position_date
try:
res["scales"] = scales
res["percent_values"] = self.get_percent_values(
@ -65,14 +62,10 @@ class RedisRepository(DashboardBaseService):
previous_shift_tag = self.get_shift_tag(
position, interval, is_current=False
)
try:
previous_value = int(float(redis_service.hget_value(previous_shift_tag, 'Value')))
previous_date_str = redis_service.hget_value(previous_shift_tag, 'Date')
previous_date = datetime.strptime(previous_date_str, "%Y-%m-%d %H:%M:%S.%f")
previous_work_shift[position.title] = previous_value
except KeyError:
previous_work_shift[position.title] = '0'
logging.error(traceback.format_exc())
previous_value = int(float(redis_service.hget_value(previous_shift_tag, 'Value')))
previous_date_str = redis_service.hget_value(previous_shift_tag, 'Date')
previous_date = datetime.strptime(previous_date_str, "%Y-%m-%d %H:%M:%S.%f")
previous_work_shift[position.title] = previous_value
if latest_previous_shift_date:
if previous_date and latest_previous_shift_date < previous_date:
@ -80,15 +73,12 @@ class RedisRepository(DashboardBaseService):
else:
latest_previous_shift_date = previous_date
try:
current_shift_tag = self.get_shift_tag(position, interval, is_current=True)
current_value = int(float(redis_service.hget_value(current_shift_tag, 'Value')))
current_date_str = redis_service.hget_value(current_shift_tag, 'Date')
current_date = datetime.strptime(current_date_str, "%Y-%m-%d %H:%M:%S.%f")
current_work_shift[position.title] = current_value
except KeyError:
current_work_shift[position.title] = '0'
logging.error(traceback.format_exc())
current_shift_tag = self.get_shift_tag(position, interval, is_current=True)
current_value = int(float(redis_service.hget_value(current_shift_tag, 'Value')))
current_date_str = redis_service.hget_value(current_shift_tag, 'Date')
current_date = datetime.strptime(current_date_str, "%Y-%m-%d %H:%M:%S.%f")
current_work_shift[position.title] = current_value
if latest_current_shift_date:
if current_date and latest_current_shift_date < current_date:
latest_current_shift_date = current_date
@ -128,9 +118,8 @@ class RedisRepository(DashboardBaseService):
def get_scales_value_with_plan(self):
with self.service as redis_service:
try:
scales_value = int(float(redis_service.hget_value(self.control_conveyor_month, 'Value')))
except KeyError:
scales_value = int(float(redis_service.hget_value(self.control_conveyor_month, 'Value')))
if scales_value == '0':
return {"value": 0, "percent": 0, "goal": 0}
planned_value = int(self.fabric_goal)
percent = str(round(scales_value * 100 / int(planned_value))) + "%"

View File

@ -75,7 +75,7 @@ class RedisSimpleService(RedisBaseService):
try:
value = self.connection.get(key)
if value is None:
raise KeyError
return '0'
return value.decode('utf-8')
except (redis.RedisError, AttributeError) as e:
print(f"Error getting value: {e}")