fix: 完善2代数据统计入库逻辑
This commit is contained in:
parent
2c79d8dcd9
commit
712943b4c2
|
@ -5,7 +5,7 @@ import django
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
|
||||||
|
@ -123,22 +123,27 @@ def create_mosq_post(device_id: str, post_data: Dict):
|
||||||
|
|
||||||
|
|
||||||
def update_mosq_device_statistic(device_id: str, post_data: Dict):
|
def update_mosq_device_statistic(device_id: str, post_data: Dict):
|
||||||
|
device = DeviceInfo.objects.filter(device_id=device_id).first()
|
||||||
datetime_format = "%Y-%m-%d %H:%M:%S"
|
datetime_format = "%Y-%m-%d %H:%M:%S"
|
||||||
cur_date = datetime.strptime(post_data['time'], datetime_format).date()
|
cur_date = datetime.strptime(post_data['time'], datetime_format).date()
|
||||||
old_record = DevicePostStatistic.objects.filter(device_id=device_id, date=cur_date).first()
|
cur_record = DevicePostStatistic.objects.filter(device_id=device_id, date=cur_date).first()
|
||||||
org = Org.objects.get(id=1)
|
last_record = DevicePostStatistic.objects.filter(device_id=device_id, date=cur_date - timedelta(days=1)).first()
|
||||||
incr = 0
|
if last_record and cur_record:
|
||||||
cur_total = post_data['count']
|
incr = cur_record.total - last_record.total
|
||||||
if old_record:
|
else:
|
||||||
device = DeviceInfo.objects.filter(device_id=device_id).first()
|
incr = 0
|
||||||
if device:
|
|
||||||
org = device.org
|
mqtt_total = post_data['count']
|
||||||
incr = cur_total - old_record.total
|
if cur_record:
|
||||||
DevicePostStatistic.objects.create(device_id=device_id,
|
cur_record.total = mqtt_total
|
||||||
date=cur_date,
|
cur_record.increment = incr
|
||||||
total=cur_total,
|
cur_record.save()
|
||||||
increment=incr,
|
else:
|
||||||
org=org)
|
DevicePostStatistic.objects.create(device_id=device_id,
|
||||||
|
date=cur_date,
|
||||||
|
total=mqtt_total,
|
||||||
|
increment=incr,
|
||||||
|
org=device.org)
|
||||||
|
|
||||||
|
|
||||||
def to_string(post_data: Dict, power_type: PowerType) -> str:
|
def to_string(post_data: Dict, power_type: PowerType) -> str:
|
||||||
|
|
Loading…
Reference in New Issue