Ver Fonte

定时更新联通流量使用总量

zhangdongming há 2 anos atrás
pai
commit
b4c98d60fe
1 ficheiros alterados com 35 adições e 0 exclusões
  1. 35 0
      Controller/UnicomCombo/UnicomComboTaskController.py

+ 35 - 0
Controller/UnicomCombo/UnicomComboTaskController.py

@@ -50,6 +50,9 @@ class UnicomComboTaskView(View):
             month = today.month
             self.query_unused_combo_and_activate(request_dict.get('iccid'), year, month, '666')
             return response.json(0)
+        elif operation == 'updateFlowUsed':  # 更新流量使用
+            self.unicom_flow_used(request_dict, response)
+            return response.json(0)
 
     @classmethod
     def check_activate_combo(cls, request_dict, response):
@@ -306,3 +309,35 @@ class UnicomComboTaskView(View):
             UnicomFlowPush.objects.create(**push_data)
         except Exception as e:
             logger.info('-->出错了~,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+    @staticmethod
+    def unicom_flow_used(request_dict, response):
+        """
+        查询设备每张卡流量使用情况
+        @param request_dict:
+        @param response:
+        @return:
+        """
+        page_size = int(request_dict.get('pageSize', 1))
+        device_count = UnicomDeviceInfo.objects.filter(card_type=0).count()
+        total_pages = device_count // page_size + (device_count % page_size > 0)  # 计算总页数
+        for page_number in range(1, total_pages + 1):
+            u_device_qs = UnicomDeviceInfo.objects.filter(card_type=0).values('id', 'iccid', 'sim_used_flow').order_by(
+                '-created_time')[(page_number - 1) * page_size:page_number * page_size]
+            asy = threading.Thread(target=UnicomComboTaskView.thread_collect_flow_used, args=(u_device_qs,))
+            asy.start()
+        return response.json(0)
+
+    @staticmethod
+    def thread_collect_flow_used(u_device_qs):
+        for item in u_device_qs:
+            try:
+                unicom_api = UnicomObjeect()
+                n_time = int(time.time())
+                # 队列已使用总流量总量
+                flow_total_usage = unicom_api.get_flow_usage_total(item['iccid'])
+                UnicomDeviceInfo.objects.filter(id=item['id']).update(updated_time=n_time,
+                                                                      sim_used_flow=flow_total_usage)
+            except Exception as e:
+                print(repr(e))
+                continue