feat: enm 修改测点缓存 已存在
This commit is contained in:
parent
70e344ed68
commit
f2e5ce1d8d
|
@ -30,6 +30,12 @@ class MpointSerializer(CustomModelSerializer):
|
|||
fields = "__all__"
|
||||
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):
|
||||
cache_mp = cache.get(Mpoint.cache_key(obj.code))
|
||||
if isinstance(cache_mp, dict):
|
||||
|
|
|
@ -152,7 +152,8 @@ class MpointCache:
|
|||
if mpoint_data is None or force_update:
|
||||
try:
|
||||
mpoint = Mpoint.objects.get(code=code)
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
myLogger.error(f"测点缓存获取失败: {e}, {code}")
|
||||
cache.set(key, {}, timeout=None)
|
||||
return {}
|
||||
mpoint_data = MpointSerializer(instance=mpoint).data
|
||||
|
|
|
@ -199,7 +199,7 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
|
|||
else:
|
||||
# 这里判断有可能清零了
|
||||
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
|
||||
# if mpoint.code == 'x水泥+P.O42.5 散装':
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ from django.utils.timezone import localtime
|
|||
from apps.enm.services import get_analyse_data_mgroups_duration
|
||||
from django.db.models import Sum
|
||||
import logging
|
||||
from django.core.cache import cache
|
||||
myLogger = logging.getLogger('log')
|
||||
class MpointViewSet(CustomModelViewSet):
|
||||
"""
|
||||
|
@ -60,8 +61,11 @@ class MpointViewSet(CustomModelViewSet):
|
|||
|
||||
@transaction.atomic
|
||||
def perform_update(self, serializer):
|
||||
old_code = serializer.instance.code
|
||||
instance: Mpoint = serializer.save()
|
||||
if instance.code:
|
||||
if old_code != instance.code:
|
||||
cache.delete(Mpoint.cache_key(old_code))
|
||||
MpointCache(instance.code).get(True)
|
||||
if instance.enabled is False:
|
||||
mc = MpointCache(instance.code)
|
||||
|
|
Loading…
Reference in New Issue