feat: cal_mpointstats分组执行

This commit is contained in:
caoqianming 2023-08-02 14:12:46 +08:00
parent ad2f903c89
commit 7619e5156c
8 changed files with 132 additions and 46 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2023-08-02 04:34
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('enm', '0016_auto_20230801_1549'),
]
operations = [
migrations.RenameField(
model_name='enstat',
old_name='pcoal_val',
new_name='pcoal_heat',
),
]

View File

@ -69,7 +69,7 @@ class EnStat(BaseModel):
total_production = models.FloatField('总产量', default=0, help_text='t')
elec_consume = models.FloatField('总电耗', default=0, help_text='kw·h')
elec_coal_consume = models.FloatField('电量折标煤', default=0, help_text='tce')
pcoal_val = models.FloatField('煤粉热值', default=0)
pcoal_heat = models.FloatField('煤粉热值', default=0)
pcoal_consume = models.FloatField('煤粉消耗', default=0, help_text='t')
pcoal_coal_consume = models.FloatField('煤粉折标煤', default=0, help_text='tce')
water_consume = models.FloatField('水消耗', default=0, help_text='t')

View File

@ -19,7 +19,7 @@ from django.core.cache import cache
from apps.enm.services import translate_eval_formula
import logging
from django.db.models import F
from apps.wpm.services import get_pcoal_val
from apps.wpm.services import get_pcoal_heat
import traceback
myLogger = logging.getLogger('log')
@ -152,22 +152,33 @@ def cal_mpointstats(is_now=1, year=None, month=None, day=None, hour=None):
# 先统计不带公式的测点
mpoints_without_formula = Mpoint.objects.filter(is_auto=True, formula='')
mpoints_without_formula_group = []
for item in mpoints_without_formula:
cal_mpointstat_hour(item.id, year, month, day, hour)
mpoints_without_formula_group.append(cal_mpointstat_hour.s(item.id, year, month, day, hour))
# cal_mpointstat_hour(item.id, year, month, day, hour)
# 再统计其他测点
mpoints_other = Mpoint.objects.filter(is_auto=True).exclude(formula='')
mpoints_other_group = []
for item in mpoints_other:
cal_mpointstat_hour(item.id, year, month, day, hour)
mpoints_other_group.append(cal_mpointstat_hour.s(item.id, year, month, day, hour))
# cal_mpointstat_hour(item.id, year, month, day, hour)
# 开始计算enstat
mgroups = Mgroup.objects.all().order_by('sort')
mgroups_group = []
for mgroup in mgroups:
cal_enstat('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True, [])
mgroups_group.append(cal_enstat.s('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True, ['material', 'run_hour']))
# cal_enstat('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True)
mgroups_t = mgroups.filter(name__in=['回转窑', '水泥磨'])
mgroups_t_group = []
for mgroup in mgroups_t:
mgroups_t_group.append(cal_enstat.s('hour_s', None, mgroup.id, year, month, day, hour, None, None, None, True, ['pcoal']))
# 最后计算enstat2
cal_enstat2(year_s=year, month_s=month)
task_chain = chain(group(*mpoints_without_formula_group), group(*mpoints_other_group), group(*mgroups_group), group(*mgroups_t_group), group(cal_enstat2.s(year_s=year, month_s=month)))
task_chain.delay()
@shared_task(base=CustomTask)
def cal_mpointstat_manual(mpointId: str, sflogId: str, mgroupId: str, year: int, month: int, day: int, hour: int, year_s: int, month_s: int, day_s: int, next_cal=0):
@ -223,11 +234,11 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
start_index = types_list.index(type)
new_types_list = types_list[start_index:]
for type in new_types_list:
cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, False, cal_attrs)
cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, day_s, False)
return
if not cal_attrs:
this_cal_attrs = ['production', 'coal', 'run_hour']
this_cal_attrs = ['material', 'pcoal', 'run_hour']
else:
this_cal_attrs = cal_attrs[:]
@ -260,7 +271,8 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
elif type == 'year_s':
enstat, _ = EnStat.objects.get_or_create(type="year_s", mgroup=mgroup, year_s=year_s,
defaults={'type': 'year_s', 'mgroup': mgroup, 'year_s': year_s})
if 'production' in this_cal_attrs:
if 'material' in this_cal_attrs:
# 消耗物料统计(包括电耗)
input_materials = []
has_product = False
@ -335,18 +347,19 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
other_cost_unit = other_cost_unit + item['cost_unit']
other_cost_data.append(item)
enstat.production_cost_unit = imaterial_cost_unit + other_cost_unit
enstat.save()
if enstat.mgroup.cate == 'section':
if 'production' in this_cal_attrs:
if 'material' in this_cal_attrs:
# 算能耗
if enstat.mgroup.name != '回转窑':
try:
enstat.en_consume_unit = enstat.elec_coal_consume / enstat.total_production
except Exception as e:
pass
enstat.save()
# 计算一些其他数据
if type == 'month_sf' and 'production' in cal_attrs: # 如果计算的是班月,把主要设备电耗数据拉过来
if type == 'month_sf' and 'material' in cal_attrs: # 如果计算的是班月,把主要设备电耗数据拉过来
res = MpointStat.objects.filter(year_s=year_s, month_s=month_s, team=team, mgroup=mgroup, mpoint__ep_monitored__power_kw__gte=100).annotate(
equipment=F('mpoint__ep_monitored__id', equipment_name=F('mpoint__ep_monitored__name')), consume=F('val')).values('equipment', 'equipment_name', 'consume')
res = list(res)
@ -356,8 +369,7 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
except Exception as e:
pass
enstat.equip_elec_data = res
enstat.save()
if enstat.mgroup.name == '回转窑': # 算单位产品(综合电耗/标煤耗/综合能耗)
# 综合电耗
if enstat.type in ['hour_s', 'day_s', 'year_s', 'month_s']:
@ -369,10 +381,10 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
pass
# 算总煤耗
if 'coal' in this_cal_attrs:
if 'pcoal' in this_cal_attrs:
if type in ['hour_s', 'sflog', 'day_s']:
enstat.pcoal_val = get_pcoal_val(enstat.year_s, enstat.month_s, enstat.day_s)
enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_val/7000
enstat.pcoal_heat = get_pcoal_heat(enstat.year_s, enstat.month_s, enstat.day_s)
enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat/7000
elif type == 'month_st':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=year_s, month_s=month_s, team=enstat.team).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
elif type == 'month_s':
@ -383,32 +395,25 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s,
# 算单位产品标煤耗
try:
enstat.coal_consume_unit = enstat.pcoal_coal_consume /enstat.total_production
except:
except ZeroDivisionError:
pass
# 综合能耗
enstat.cen_consume_unit = enstat.coal_consume_unit + 0.1229 * enstat.elec_consume_unit
# # 同步更新水泥磨的综合能耗,这步有可能不成功,因为水泥磨是后算的
# if type not in ['month_st', 'sflog']:
# next_enstat = EnStat.objects.filter(type=enstat.type, year_s=enstat.year_s, month_s=enstat.month_s, day_s=enstat.day_s, hour=enstat.hour, mgroup__name='水泥磨').first()
# if next_enstat:
# next_enstat.cen_consume_unit = next_enstat.elec_consume_unit*0.1229 + 0.7*enstat.cen_consume_unit
# next_enstat.save()
if enstat.mgroup.name == '水泥磨' and enstat.type not in ['month_st', 'sflog'] and 'coal' in this_cal_attrs:
enstat.save()
if enstat.mgroup.name == '水泥磨' and enstat.type not in ['month_st', 'sflog'] and 'pcoal' in this_cal_attrs:
pre_enstat = EnStat.objects.filter(type=enstat.type, year_s=enstat.year_s, month_s=enstat.month_s, day_s=enstat.day_s, hour=enstat.hour, mgroup__name='回转窑').first()
if pre_enstat:
# 综合能耗
enstat.cen_consume_unit = enstat.elec_consume_unit*0.1229 + 0.7*pre_enstat.cen_consume_unit
enstat.save()
# 运转时长相关
if type != 'hour_s' and 'run_hour' in this_cal_attrs:
enstat.total_hour_now, enstat.shut_hour = get_total_hour_now_and_shut_hour(enstat)
enstat.run_hour = enstat.total_hour_now - enstat.shut_hour
enstat.run_rate = (enstat.run_hour / enstat.total_hour_now)*100
enstat.production_hour = enstat.total_production / enstat.total_hour_now
enstat.save()
enstat.save()
@ -504,3 +509,33 @@ def cal_enstat2(year_s: int, month_s: int):
except ZeroDivisionError:
pass
enstat2.save()
def cal_enstat_pcoal_change(enstat, new_pcoal_heat):
type = enstat.type
if type in ['hour_s', 'sflog', 'day_s']:
enstat.pcoal_heat = new_pcoal_heat
enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat/7000
elif type == 'month_st':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=enstat.year_s, month_s=enstat.month_s, team=enstat.team).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
elif type == 'month_s':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=enstat.year_s, month_s=enstat.month_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
elif type == 'year_s':
enstat.pcoal_coal_consume = EnStat.objects.filter(type='day_s', mgroup=enstat.mgroup, year_s=enstat.year_s).aggregate(sum=Sum('pcoal_coal_consume'))['sum']
# 算单位产品标煤耗
try:
enstat.coal_consume_unit = enstat.pcoal_coal_consume /enstat.total_production
except ZeroDivisionError:
pass
# 综合能耗
enstat.cen_consume_unit = enstat.coal_consume_unit + 0.1229 * enstat.elec_consume_unit
# 同步更新水泥磨的综合能耗,这步有可能不成功,因为水泥磨是后算的, 但是当pcoal_change时这个就有用了
if type not in ['month_st', 'sflog']:
next_enstat = EnStat.objects.filter(type=enstat.type, year_s=enstat.year_s, month_s=enstat.month_s, day_s=enstat.day_s, hour=enstat.hour, mgroup__name='水泥磨').first()
if next_enstat:
next_enstat.cen_consume_unit = next_enstat.elec_consume_unit*0.1229 + 0.7*enstat.cen_consume_unit
next_enstat.save()
enstat.save()

View File

@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2023-08-02 04:34
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('wpm', '0007_auto_20230728_1705'),
]
operations = [
migrations.RenameField(
model_name='sflog',
old_name='pcoal_val',
new_name='pcoal_heat',
),
]

View File

@ -29,7 +29,7 @@ class SfLog(CommonADModel):
last_test_time = models.DateTimeField('最后质检时间', null=True, blank=True)
total_hour_now = models.FloatField('总时长动', default=0)
shut_hour = models.FloatField('停机时长', default=0)
pcoal_val = models.FloatField('煤粉热值', null=True, blank=True)
pcoal_heat = models.FloatField('煤粉热值', null=True, blank=True)
@property
def get_ymd(self):

View File

@ -4,6 +4,7 @@ from rest_framework import serializers
from apps.wpm.models import SfLog, StLog, StSfLog
from apps.system.models import Dictionary
from apps.wpm.tasks import cal_enstat_when_pcoal_heat_change
class StLogSerializer(CustomModelSerializer):
mgroup_name = serializers.CharField(source='mgroup.name', read_only=True)
@ -26,6 +27,13 @@ class SfLogSerializer(CustomModelSerializer):
'leader': {'required': True}
}
def update(self, instance, validated_data):
old_pcoal_heat = instance.pcoal_heat
instance = super().update(instance, validated_data)
new_pcoal_heat = instance.pcoal_heat
if new_pcoal_heat != old_pcoal_heat:
cal_enstat_when_pcoal_heat_change.delay(instance.id)
return instance
# def to_internal_value(self, data):
# if hasattr(self.Meta, 'update_fields') and self.context['request'].method in ['PUT', 'PATCH']:
# u_fields = self.Meta.update_fields

View File

@ -23,23 +23,23 @@ def make_sflogs(mgroup: Mgroup, start_date: datetime.date, end_date: datetime.da
})
current_date = current_date + datetime.timedelta(days=1)
def get_pcoal_val(year_s: int, month_s: int, day_s: int):
def get_pcoal_heat(year_s: int, month_s: int, day_s: int):
"""
获取煤粉热值
只有回转窑需要录入煤粉热值
"""
key = f'pgoal_val_{year_s}_{month_s}_{day_s}'
pcoal_val = cache.get(key, None)
if pcoal_val is not None:
return pcoal_val
pcoal_heat = cache.get(key, None)
if pcoal_heat is not None:
return pcoal_heat
else:
try:
qs = SfLog.objects.get(end_time__year=year_s, end_time__month=month_s, end_time__day=day_s,
mgroup__name='回转窑', shift__name='夜班')
if qs.pcoal_val is None:
qs.pcoal_val = 0
if qs.pcoal_heat is None:
qs.pcoal_heat = 0
qs.save()
cache.set(f'pgoal_val_{year_s}_{month_s}_{day_s}', qs.pcoal_val)
return qs.pcoal_val
cache.set(f'pgoal_val_{year_s}_{month_s}_{day_s}', qs.pcoal_heat)
return qs.pcoal_heat
except:
return 0

View File

@ -91,13 +91,20 @@ def cal_shut_hour(stlogId: str):
@shared_task(base=CustomTask)
def cal_enstat_when_pcoal_val_change(sflogId):
def cal_enstat_when_pcoal_heat_change(sflogId):
from apps.enm.models import EnStat
from apps.enm.tasks import cal_enstat_pcoal_change, cal_enstat2
sflog = SfLog.objects.get(id=sflogId)
mgroup = sflog.mgroup
pcoal_val = sflog.pcoal_val
pcoal_heat = sflog.pcoal_heat
year_s, month_s, day_s = sflog.get_ymd
enstats = EnStat.objects.filter(mgroup__name='回转窑')
# 只会影响到回转窑及水泥磨数据
enstats = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, month_s=month_s, day_s=day_s, type__in = ['hour_s', 'sflog', 'day_s'])
for enstat in enstats:
if type in ['hour_s', 'sflog', 'day_s']:
pass
cal_enstat_pcoal_change(enstat, pcoal_heat)
enstats_other = EnStat.objects.filter(mgroup__name='回转窑', year_s=year_s, month_s=month_s, day_s=day_s, type__in = ['month_st', 'month_s', 'year_s'])
for enstat in enstats_other:
cal_enstat_pcoal_change(enstat, pcoal_heat)
cal_enstat2(year_s, month_s)