Quellcode durchsuchen

优化今日客流统计查询

zhangdongming vor 2 Jahren
Ursprung
Commit
522d760cdb
1 geänderte Dateien mit 26 neuen und 19 gelöschten Zeilen
  1. 26 19
      Controller/AlgorithmShop/AlgorithmShopController.py

+ 26 - 19
Controller/AlgorithmShop/AlgorithmShopController.py

@@ -111,14 +111,12 @@ class AlgorithmShopView(View):
 
             LOGGER.info(f'uid{uid}-DeviceTime:{d_time},tz:{tz},结果:{date_time}')
             passenger_flow_list = []
-            if enter_count > 0:
-                data['count'] = enter_count
-                data['type'] = 1
-                passenger_flow_list.append(DeviceAlgorithmPassengerFlow(**data))
-            if exit_count > 0:
-                data['count'] = exit_count
-                data['type'] = 2
-                passenger_flow_list.append(DeviceAlgorithmPassengerFlow(**data))
+            data['count'] = enter_count
+            data['type'] = 1
+            passenger_flow_list.append(DeviceAlgorithmPassengerFlow(**data))
+            data['count'] = exit_count
+            data['type'] = 2
+            passenger_flow_list.append(DeviceAlgorithmPassengerFlow(**data))
             DeviceAlgorithmPassengerFlow.objects.bulk_create(passenger_flow_list)
             # 将数据存入Redis,并设置过期时间为10分钟
             redis.CONN.setnx(key, d_time)
@@ -489,24 +487,33 @@ class AlgorithmShopView(View):
         t_list = [math.floor((c_time - timedelta(hours=i)).timestamp()) for i in range(24)]
         passenger_flow_list = []
         pf_qs = DeviceAlgorithmPassengerFlow.objects.filter(
-            device_time__in=t_list,
+            device_time__gte=t_list[23],
+            device_time__lte=t_list[0],
             type=int(flow_type),
             uid=uid
         ).order_by('device_time')
         if not pf_qs.exists():
             return passenger_flow_list
-        # 将查询到的数据时间戳转字典
-        pf_dict = {pf.device_time: pf for pf in pf_qs}
         # 循环查找最近24小时的记录(包含当前时间)
         for i in range(24):
-            current_timestamp = t_list[i]
-            if current_timestamp in pf_dict:
-                item = pf_dict[current_timestamp]
-                passenger_flow_list.append({
-                    'index': 23 - i,
-                    'count': item.count,
-                    'time': item.device_time
-                })
+            current_time = t_list[i]
+            next_time = -1 if i == 0 else t_list[i - 1]
+            is_data = False
+            count = 0
+            for item in pf_qs:
+                if next_time == -1 and next_time == item.device_time:
+                    count += item.count
+                    is_data = True
+                elif current_time <= item.device_time < next_time:
+                    count += item.count
+                    is_data = True
+            if not is_data:
+                continue
+            passenger_flow_list.append({
+                'index': 23 - i,
+                'count': count,
+                'time': current_time
+            })
         return passenger_flow_list
 
     @staticmethod