fix: 去除is_online字段引发的变动

This commit is contained in:
caoqianming 2024-04-16 14:59:07 +08:00
parent 6e84e70852
commit 640d1a1bce
6 changed files with 35 additions and 26 deletions

View File

@ -5,11 +5,9 @@ from apps.em.models import Ecate, Equipment
@admin.register(Ecate) @admin.register(Ecate)
class EcateAdmin(admin.ModelAdmin): class EcateAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'code', 'type', list_display = ("id", "name", "code", "type", "is_for_safe", "is_for_enp", "is_car")
'is_for_safe', 'is_for_enp', 'is_car')
@admin.register(Equipment) @admin.register(Equipment)
class EquipmentAdmin(admin.ModelAdmin): class EquipmentAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'type', list_display = ("id", "name", "type", "cate", "state", "running_state")
'cate', 'state', 'is_online', 'running_state')

View File

@ -100,23 +100,19 @@ class EquipmentViewSet(CustomModelViewSet):
queryset = self.filter_queryset(self.get_queryset()) queryset = self.filter_queryset(self.get_queryset())
result = queryset.aggregate( result = queryset.aggregate(
count=Count("id"), count=Count("id"),
count_online=Count(Case(When(is_online=1, then=1), output_field=IntegerField())),
count_offline=Count(Case(When(is_online=0, then=1), output_field=IntegerField())),
count_running=Count(Case(When(running_state=10, then=1), output_field=IntegerField())), count_running=Count(Case(When(running_state=10, then=1), output_field=IntegerField())),
count_standby=Count(Case(When(running_state=20, then=1), output_field=IntegerField())), count_standby=Count(Case(When(running_state=20, then=1), output_field=IntegerField())),
count_stop=Count(Case(When(running_state=30, then=1), output_field=IntegerField())), count_stop=Count(Case(When(running_state=30, then=1), output_field=IntegerField())),
count_fail=Count(Case(When(running_state=40, then=1), output_field=IntegerField())), count_fail=Count(Case(When(running_state=40, then=1), output_field=IntegerField())),
count_unknown=Count(Case(When(running_state=50, then=1), output_field=IntegerField())), count_offline=Count(Case(When(running_state=50, then=1), output_field=IntegerField())),
) )
json_result = { json_result = {
"count": result["count"], "count": result["count"],
"count_online": result["count_online"],
"count_offline": result["count_offline"],
"count_running": result["count_running"], "count_running": result["count_running"],
"count_standby": result["count_standby"], "count_standby": result["count_standby"],
"count_stop": result["count_stop"], "count_stop": result["count_stop"],
"count_fail": result["count_fail"], "count_fail": result["count_fail"],
"count_unknown": result["count_unknown"], "count_offline": result["count_offline"],
} }
return Response(json_result) return Response(json_result)

View File

@ -17,7 +17,6 @@ class Migration(migrations.Migration):
CREATE TABLE public.enp_envdata ( CREATE TABLE public.enp_envdata (
"timex" timestamptz NOT NULL, "timex" timestamptz NOT NULL,
"equipment_id" text NOT NULL, "equipment_id" text NOT NULL,
"is_online" INT DEFAULT 1,
"running_state" INT DEFAULT 10, "running_state" INT DEFAULT 10,
"dust_rtd" float, "dust_rtd" float,
"dust_zs" float, "dust_zs" float,
@ -48,7 +47,6 @@ SELECT create_hypertable('enp_envdata', 'timex');
name="EnvData", name="EnvData",
fields=[ fields=[
("time", models.DateTimeField(primary_key=True, serialize=False, verbose_name="采集时间")), ("time", models.DateTimeField(primary_key=True, serialize=False, verbose_name="采集时间")),
("is_online", models.PositiveSmallIntegerField(default=1, verbose_name="是否在线")),
("running_state", models.PositiveSmallIntegerField(default=10, verbose_name="运行状态")), ("running_state", models.PositiveSmallIntegerField(default=10, verbose_name="运行状态")),
("dust_rtd", models.FloatField(blank=True, null=True, verbose_name="颗粒物实测(mg/m3)")), ("dust_rtd", models.FloatField(blank=True, null=True, verbose_name="颗粒物实测(mg/m3)")),
("dust_zs", models.FloatField(blank=True, null=True, verbose_name="颗粒物折算(mg/m3)")), ("dust_zs", models.FloatField(blank=True, null=True, verbose_name="颗粒物折算(mg/m3)")),

View File

@ -53,10 +53,30 @@ class EnvData(models.Model):
环保监测数据 环保监测数据
""" """
enp_fields = [
"running_state",
"dust_rtd",
"dust_zs",
"temperature",
"pressure",
"speed",
"humdity",
"flux",
"pm25",
"pm10",
"tsp",
"wind_direction",
"wind_speed",
"so2_rtd",
"so2_zs",
"nox_rtd",
"nox_zs",
"o2",
]
RUNING_STATE_CHOICES = ((10, "运行"), (20, "待机"), (30, "停机"), (40, "故障"), (50, "未知")) RUNING_STATE_CHOICES = ((10, "运行"), (20, "待机"), (30, "停机"), (40, "故障"), (50, "未知"))
equipment = models.ForeignKey(Equipment, verbose_name="关联设备", on_delete=models.CASCADE) equipment = models.ForeignKey(Equipment, verbose_name="关联设备", on_delete=models.CASCADE)
timex = models.DateTimeField("采集时间", primary_key=True) timex = models.DateTimeField("采集时间", primary_key=True)
is_online = models.PositiveSmallIntegerField("是否在线", default=1)
running_state = models.PositiveSmallIntegerField("运行状态", default=10) running_state = models.PositiveSmallIntegerField("运行状态", default=10)
dust_rtd = models.FloatField("颗粒物实测(mg/m3)", null=True, blank=True) dust_rtd = models.FloatField("颗粒物实测(mg/m3)", null=True, blank=True)
dust_zs = models.FloatField("颗粒物折算(mg/m3)", null=True, blank=True) dust_zs = models.FloatField("颗粒物折算(mg/m3)", null=True, blank=True)

View File

@ -37,7 +37,7 @@ class Drain2Serializer(CustomModelSerializer):
now = datetime.now() now = datetime.now()
today = str(now)[:10] + " 00:00:00" today = str(now)[:10] + " 00:00:00"
today_last = str(now)[:10] + " 23:59:59" today_last = str(now)[:10] + " 23:59:59"
odata = equips.values("id", "name", "type", "is_online", "running_state") odata = equips.values("id", "name", "type", "running_state")
eids = [f"'{e['id']}'" for e in odata] eids = [f"'{e['id']}'" for e in odata]
eids_str = ",".join(eids) eids_str = ",".join(eids)
sql_str = f""" sql_str = f"""
@ -56,7 +56,7 @@ FROM
running_state, running_state,
CASE CASE
WHEN is_online = 1 THEN WHEN running_state != 50 THEN
TIME - LAG ( TIME ) OVER ( PARTITION BY equipment_id ORDER BY TIME ) ELSE'0' TIME - LAG ( TIME ) OVER ( PARTITION BY equipment_id ORDER BY TIME ) ELSE'0'
END AS duration_online, END AS duration_online,
CASE CASE
@ -66,7 +66,7 @@ FROM
END AS duration_run, END AS duration_run,
CASE CASE
WHEN running_state = 20 THEN WHEN running_state in (20, 30, 40) THEN
TIME - LAG ( TIME ) OVER ( PARTITION BY equipment_id ORDER BY TIME ) ELSE'0' TIME - LAG ( TIME ) OVER ( PARTITION BY equipment_id ORDER BY TIME ) ELSE'0'
END AS duration_standby, END AS duration_standby,
CASE CASE
@ -77,10 +77,9 @@ FROM
FROM FROM
enp_envdata enp_envdata
WHERE WHERE
running_state IN ( 10, 20 ) TIMEX >= '{today}' -- 替换成想查询的开始时间
AND TIME >= '{today}' -- 替换成想查询的开始时间
AND TIME <= '{today_last}' -- 替换成想查询的结束时间 AND TIMEX <= '{today_last}' -- 替换成想查询的结束时间
AND equipment_id IN ( {eids_str} ) AND equipment_id IN ( {eids_str} )
) AS durations ) AS durations

View File

@ -2,7 +2,6 @@ import os
import sys import sys
import django import django
import logging import logging
from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.utils import timezone from django.utils import timezone
import time import time
@ -22,17 +21,16 @@ def run():
now = timezone.now() now = timezone.now()
equips = Equipment.objects.all() equips = Equipment.objects.all()
for equip in equips: for equip in equips:
is_online = 0 is_offline = True
cache_key = f"equipment_{equip.id}" cache_key = f"equipment_{equip.id}"
cache_value = cache.get(cache_key, None) cache_value = cache.get(cache_key, None)
if cache_value: if cache_value:
last_timex = cache_value.get("running_state_timex", None) last_timex = cache_value.get("running_state_timex", None)
if last_timex and (now - last_timex).total_seconds() <= 20: if last_timex and (now - last_timex).total_seconds() <= 20:
is_online = 1 is_offline = False
if is_online == 0: if is_offline:
equip.is_online = 0 equip.running_state = RuningState.OFFLINE
equip.running_state = RuningState.UNKNOWN equip.save(update_fields=["running_state"])
equip.save(update_fields=["is_online", "running_state"])
time.sleep(5) time.sleep(5)