feat: 测点统计表及相关逻辑cal_val修改

This commit is contained in:
caoqianming 2023-06-30 10:21:34 +08:00
parent 72782163ab
commit 3b427cce59
3 changed files with 122 additions and 3 deletions

View File

@ -0,0 +1,45 @@
# Generated by Django 3.2.12 on 2023-06-30 02:20
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('wpm', '0003_auto_20230628_0859'),
('enm', '0002_initial'),
]
operations = [
migrations.AddField(
model_name='mpointstat',
name='day_s',
field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='班日'),
),
migrations.AddField(
model_name='mpointstat',
name='month_s',
field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='班月'),
),
migrations.AddField(
model_name='mpointstat',
name='sflog',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='wpm.sflog', verbose_name='关联值班记录'),
),
migrations.AddField(
model_name='mpointstat',
name='year_s',
field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='班年'),
),
migrations.AlterField(
model_name='mpointstat',
name='type',
field=models.CharField(default='hour', help_text='year/month/day/year_s/month_s/day_s/sflog/hour', max_length=50, verbose_name='统计维度'),
),
migrations.AlterField(
model_name='mpointstat',
name='year',
field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name=''),
),
]

View File

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from apps.utils.models import BaseModel, CommonBModel from apps.utils.models import BaseModel, CommonBModel
from apps.wpm.models import SfLog
class Mpoint(CommonBModel): class Mpoint(CommonBModel):
@ -27,10 +28,25 @@ class MpLog(BaseModel):
class MpointStat(BaseModel): class MpointStat(BaseModel):
"""测点统计表 """测点统计表
""" """
type = models.CharField('统计维度', max_length=50, default='hour', help_text='year/month/day/hour') type = models.CharField('统计维度', max_length=50, default='hour', help_text='year/month/day/year_s/month_s/day_s/sflog/hour')
year = models.PositiveSmallIntegerField('') year = models.PositiveSmallIntegerField('', null=True, blank=True)
month = models.PositiveSmallIntegerField('', null=True, blank=True) month = models.PositiveSmallIntegerField('', null=True, blank=True)
day = models.PositiveSmallIntegerField('', null=True, blank=True) day = models.PositiveSmallIntegerField('', null=True, blank=True)
year_s = models.PositiveSmallIntegerField('班年', null=True, blank=True)
month_s = models.PositiveSmallIntegerField('班月', null=True, blank=True)
day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True)
hour = models.PositiveSmallIntegerField('', null=True, blank=True) hour = models.PositiveSmallIntegerField('', null=True, blank=True)
sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True)
mpoint = models.ForeignKey(Mpoint, verbose_name='关联测点', on_delete=models.CASCADE) mpoint = models.ForeignKey(Mpoint, verbose_name='关联测点', on_delete=models.CASCADE)
val = models.FloatField('统计值', default=0) val = models.FloatField('统计值', default=0)
# class EnStat(BaseModel):
# """
# 能源数据统计表
# """
# type = models.CharField('统计种类', max_length=50, default='elec', help_text='year_s/month_s/day_s/sflog')
# sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True)
# mpoint = models.ForeignKey(Mpoint, verbose_name='关联测点', on_delete=models.CASCADE)

View File

@ -9,6 +9,10 @@ from apps.enm.models import MpLog, Mpoint, MpointStat
from apps.wpm.models import SfLog from apps.wpm.models import SfLog
import datetime import datetime
from django.db.models import Sum from django.db.models import Sum
from dateutil import tz
from django.conf import settings
from django.utils.timezone import localtime
from apps.wpm.services import make_sflogs
def get_current_and_previous_time(): def get_current_and_previous_time():
now = datetime.datetime.now() now = datetime.datetime.now()
@ -42,6 +46,8 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
计算某一测点, 某一时间点某一小时的统计值 计算某一测点, 某一时间点某一小时的统计值
""" """
mpoint = Mpoint.objects.get(id=mpointId) mpoint = Mpoint.objects.get(id=mpointId)
mytz = tz.gettz(settings.TIME_ZONE)
dt = datetime.datetime(year=year, month=month, day=day, hour=hour, tzinfo=mytz)
if mpoint.cate == 'elec': # 是否是电能 if mpoint.cate == 'elec': # 是否是电能
params = {'mpoint': mpoint, 'type': 'hour'} params = {'mpoint': mpoint, 'type': 'hour'}
params['year'], params['month'], params['day'], params['hour'] = year, month, day, hour params['year'], params['month'], params['day'], params['hour'] = year, month, day, hour
@ -57,6 +63,19 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
ms, _ = MpointStat.objects.get_or_create(**params, defaults=params) ms, _ = MpointStat.objects.get_or_create(**params, defaults=params)
ms.val = val ms.val = val
ms.save() ms.save()
# 绑定值班记录
sflog = SfLog.objects.filter(strat_time__lte=dt, end_time__gt=dt, mgroup=mpoint.mgroup).first()
year_s, month_s, day_s = 0, 0, 0
if sflog:
ms.sflog = sflog
end_time_local = localtime(sflog.end_time)
year_s, month_s, day_s = end_time_local.year, end_time_local.month, end_time_local.day
ms.year_s = year_s
ms.month_s = month_s
ms.day_s = day_s
ms.save()
else:
raise Exception('未找到值班记录')
# 更新更高级别的值 # 更新更高级别的值
sum_dict_day = MpointStat.objects.filter(type='hour', mpoint=mpoint, year=year, month=month, day=day).aggregate(sum=Sum('val')) sum_dict_day = MpointStat.objects.filter(type='hour', mpoint=mpoint, year=year, month=month, day=day).aggregate(sum=Sum('val'))
params_day = {'type':'day', 'mpoint': mpoint, 'year': year, 'month': month, 'day': day} params_day = {'type':'day', 'mpoint': mpoint, 'year': year, 'month': month, 'day': day}
@ -76,6 +95,45 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
ms_year.val = sum_dict_year['sum'] ms_year.val = sum_dict_year['sum']
ms_year.save() ms_year.save()
if year_s and month_s and day_s:
# 这种是距离点相减
# params_s = {'mpoint': mpoint, 'type': 'sflog'}
# mrs = MpLog.objects.filter(
# mpoint=mpoint,
# tag_update__gte=sflog.create_time, tag_update__lt=sflog.end_time).order_by('tag_update')
# val = 0
# if mrs.exists():
# val = mrs.last().tag_val - mrs.first().tag_val
# params_s_default = {'mpoint': mpoint, 'type': 'sflog', 'year_s': year_s, 'month_s': month_s, 'day_s': day_s}
# ms, _ = MpointStat.objects.get_or_create(**params_s, defaults=params_s_default)
# ms.val = val
# ms.save()
# 这种是加和
sum_dict_sflog_s = MpointStat.objects.filter(type='hour', mpoint=mpoint, year_s=year_s, month_s=month_s, day_s=day_s, sflog=sflog).aggregate(sum=Sum('val'))
params_sflog_s = {'type':'sflog', 'mpoint': mpoint, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s}
ms_sflog_s, _ = MpointStat.objects.get_or_create(**params_sflog_s, defaults=params_sflog_s)
ms_sflog_s.val = sum_dict_sflog_s['sum']
ms_sflog_s.save()
sum_dict_day_s = MpointStat.objects.filter(type='hour', mpoint=mpoint, year_s=year_s, month_s=month_s, day_s=day_s).aggregate(sum=Sum('val'))
params_day_s = {'type':'day_s', 'mpoint': mpoint, 'year_s': year_s, 'month_s': month_s, 'day_s': day_s}
ms_day_s, _ = MpointStat.objects.get_or_create(**params_day_s, defaults=params_day_s)
ms_day_s.val = sum_dict_day_s['sum']
ms_day_s.save()
sum_dict_month_s = MpointStat.objects.filter(type='hour', mpoint=mpoint, year_s=year_s, month=month_s).aggregate(sum=Sum('val'))
params_month_s = {'type':'month', 'mpoint': mpoint, 'year': year_s, 'month': month_s}
ms_month_s, _ = MpointStat.objects.get_or_create(**params_month_s, defaults=params_month_s)
ms_month_s.val = sum_dict_month_s['sum']
ms_month_s.save()
sum_dict_year_s = MpointStat.objects.filter(type='hour', mpoint=mpoint, year_s=year_s).aggregate(sum=Sum('val'))
params_year_s = {'type':'year', 'mpoint': mpoint, 'year': year_s}
ms_year_s, _ = MpointStat.objects.get_or_create(**params_year_s, defaults=params_year_s)
ms_year_s.val = sum_dict_year_s['sum']
ms_year_s.save()
@shared_task(base=CustomTask) @shared_task(base=CustomTask)
def cal_mpointstats(is_now=1): def cal_mpointstats(is_now=1):
""" """