fix: enm tasks bug

This commit is contained in:
caoqianming 2023-06-14 16:50:11 +08:00
parent 204e951a6f
commit afa6cec3a0
1 changed files with 5 additions and 5 deletions

View File

@ -5,7 +5,7 @@ from celery import shared_task
from apps.utils.sql import DbConnection
from server.settings import get_sysconfig
from django.core.cache import cache
from apps.enm.models import Mrecord, Mpoint, HourStat
from apps.enm.models import MpLog, Mpoint, HourStat
import datetime
def get_current_and_previous_hour():
@ -32,7 +32,7 @@ def get_tag_val():
with DbConnection(config['enm']['db_host'], config['enm']['db_user'], config['enm']['db_password'], config['enm']['db_database']) as cursor:
last_tag_id = cache.get('last_tag_id')
if last_tag_id is None:
mr = Mrecord.objects.all().order_by('-tag_update', 'tag_id').first()
mr = MpLog.objects.all().order_by('-tag_update', 'tag_id').first()
if mr is None:
last_tag_id = 0
else:
@ -41,7 +41,7 @@ def get_tag_val():
cursor.execute("select id, val, tag_code, update_time from tag_value where id > %s order by id", (last_tag_id))
results = cursor.fetchall() # 获取数据后保存至本地
for row in results:
mr_one = Mrecord()
mr_one = MpLog()
mr_one.tag_id, mr_one.tag_val, mr_one.tag_code, mr_one.tag_update = row
mr_one.mpoint, _ = Mpoint.objects.get_or_create(code=mr_one.tag_code, defaults={'name': mr_one.tag_code, 'code': mr_one.tag_code, 'unit': 'unknown'})
mr_one.save()
@ -58,7 +58,7 @@ def cal_hourstat():
start_time = c_t_r[0]
params = {'mpoint': mpoint}
params['year'], params['month'], params['day'], params['hour'] = start_time.year, start_time.month, start_time.day, start_time.hour
mrs = Mrecord.objects.filter(mpoint=mpoint, tag_update__gte=c_t_r[0], tag_update__lte=c_t_r[1]).order_by('tag_update')
mrs = MpLog.objects.filter(mpoint=mpoint, tag_update__gte=c_t_r[0], tag_update__lte=c_t_r[1]).order_by('tag_update')
val = 0
if mrs.exists():
val = mrs.last() - mrs.first()
@ -72,7 +72,7 @@ def cal_hourstat():
params['year'], params['month'], params['day'], params['hour'] = start_time2.year, start_time2.month, start_time2.day, start_time2.hour
hs, _ = HourStat.objects.get_or_create(**params, defaults=params)
if not hs.is_calculated:
mrs = Mrecord.objects.filter(mpoint=mpoint, tag_update__gte=p_t_r[0], tag_update__lte=p_t_r[1]).order_by('tag_update')
mrs = MpLog.objects.filter(mpoint=mpoint, tag_update__gte=p_t_r[0], tag_update__lte=p_t_r[1]).order_by('tag_update')
val = 0
if mrs.exists():
val = mrs.last() - mrs.first()