From e7b9d90e00909d52d058dc01b737196c6ce81dee Mon Sep 17 00:00:00 2001 From: zakhy Date: Thu, 15 Aug 2024 12:12:31 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20Redis.=20?= =?UTF-8?q?=D0=9F=D1=80=D0=B8=20=D0=BE=D1=82=D1=81=D1=83=D1=82=D1=81=D1=82?= =?UTF-8?q?=D0=B2=D0=B8=D0=B8=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20-=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0?= =?UTF-8?q?=D0=B5=D1=82=D1=81=D1=8F=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scales_control/redis_repository.py | 61 ++++++++----------- app/apps/util/redis_api.py | 2 +- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/app/apps/main/services/scales_control/redis_repository.py b/app/apps/main/services/scales_control/redis_repository.py index b04364c..0331005 100644 --- a/app/apps/main/services/scales_control/redis_repository.py +++ b/app/apps/main/services/scales_control/redis_repository.py @@ -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))) + "%" diff --git a/app/apps/util/redis_api.py b/app/apps/util/redis_api.py index 54720b2..c6499a2 100644 --- a/app/apps/util/redis_api.py +++ b/app/apps/util/redis_api.py @@ -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}")