feat: enm 修改测点缓存 已存在

This commit is contained in:
zty 2025-01-02 16:33:27 +08:00
parent 70e344ed68
commit f2e5ce1d8d
4 changed files with 13 additions and 2 deletions

View File

@ -30,6 +30,12 @@ class MpointSerializer(CustomModelSerializer):
fields = "__all__" fields = "__all__"
read_only_fields = EXCLUDE_FIELDS + ["belong_dept", "cate"] read_only_fields = EXCLUDE_FIELDS + ["belong_dept", "cate"]
def create(self, validated_data):
code = validated_data["code"]
if Mpoint.objects.get_queryset(all=True).filter(code=code).exists():
raise ParseError("测点编号已存在")
return super().create(validated_data)
def get_last_data(self, obj): def get_last_data(self, obj):
cache_mp = cache.get(Mpoint.cache_key(obj.code)) cache_mp = cache.get(Mpoint.cache_key(obj.code))
if isinstance(cache_mp, dict): if isinstance(cache_mp, dict):

View File

@ -152,7 +152,8 @@ class MpointCache:
if mpoint_data is None or force_update: if mpoint_data is None or force_update:
try: try:
mpoint = Mpoint.objects.get(code=code) mpoint = Mpoint.objects.get(code=code)
except Exception: except Exception as e:
myLogger.error(f"测点缓存获取失败: {e}, {code}")
cache.set(key, {}, timeout=None) cache.set(key, {}, timeout=None)
return {} return {}
mpoint_data = MpointSerializer(instance=mpoint).data mpoint_data = MpointSerializer(instance=mpoint).data

View File

@ -199,7 +199,7 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
else: else:
# 这里判断有可能清零了 # 这里判断有可能清零了
max_val = max(mrs.aggregate(max=Max(f'val_{val_type}'))["max"], first_val) max_val = max(mrs.aggregate(max=Max(f'val_{val_type}'))["max"], first_val)
myLogger.info(f'{mpoint.code}--{dt}--{last_val}--{first_val}--清零') myLogger.info(f'{mpoint.id}--{mpoint.code}--{dt}--{last_val}--{first_val}--清零')
val = max_val - first_val + last_val val = max_val - first_val + last_val
# if mpoint.code == 'x水泥+P.O42.5 散装': # if mpoint.code == 'x水泥+P.O42.5 散装':

View File

@ -19,6 +19,7 @@ from django.utils.timezone import localtime
from apps.enm.services import get_analyse_data_mgroups_duration from apps.enm.services import get_analyse_data_mgroups_duration
from django.db.models import Sum from django.db.models import Sum
import logging import logging
from django.core.cache import cache
myLogger = logging.getLogger('log') myLogger = logging.getLogger('log')
class MpointViewSet(CustomModelViewSet): class MpointViewSet(CustomModelViewSet):
""" """
@ -60,8 +61,11 @@ class MpointViewSet(CustomModelViewSet):
@transaction.atomic @transaction.atomic
def perform_update(self, serializer): def perform_update(self, serializer):
old_code = serializer.instance.code
instance: Mpoint = serializer.save() instance: Mpoint = serializer.save()
if instance.code: if instance.code:
if old_code != instance.code:
cache.delete(Mpoint.cache_key(old_code))
MpointCache(instance.code).get(True) MpointCache(instance.code).get(True)
if instance.enabled is False: if instance.enabled is False:
mc = MpointCache(instance.code) mc = MpointCache(instance.code)