def traffic_api(request): ''' 接受服务端的用户流量上报 ''' token = request.GET.get('token', '') if token == settings.TOKEN: traffic_rec_list = json.loads(request.body)['data'] node_id = json.loads(request.body)['node_id'] node = Node.objects.get(node_id=node_id) for rec in traffic_rec_list: # 用户流量流量记录 user = SSUser.objects.get(pk=rec['user_id']) user.upload_traffic += rec['u'] user.download_traffic += rec['d'] user.save() traffic = traffic_format(rec['u'] + rec['d']) TrafficLog.objects.create( node_id=node_id, user_id=rec['user_id'], traffic=traffic, download_traffic=rec['d'], upload_traffic=rec['u'], log_time=round(time.time())) # 节点流量记录 node.used_traffic = node.used_traffic + rec['u'] + rec['d'] node.save() re_dict = {'ret': 1, 'data': []} else: re_dict = {'ret': -1} return JsonResponse(re_dict)
def traffic_api(request): ''' 接受服务端的用户流量上报 ''' token = request.GET.get('token', '') if token == settings.TOKEN: traffic_rec_list = json.loads(request.body)['data'] node_id = json.loads(request.body)['node_id'] # 定义循环池 node_total_traffic = 0 trafficlog_model_list = [] log_time = int(time.time()) for rec in traffic_rec_list: res = SSUser.objects.filter(pk=rec['user_id']).values_list( 'upload_traffic', 'download_traffic')[0] SSUser.objects.filter(pk=rec['user_id']).update( upload_traffic=(res[0]+rec['u']), download_traffic=(res[1]+rec['d']), last_use_time=log_time) traffic = traffic_format(rec['u'] + rec['d']) trafficlog_model_list.append(TrafficLog( node_id=node_id, user_id=rec['user_id'], traffic=traffic, download_traffic=rec['d'], upload_traffic=rec['u'], log_time=log_time)) node_total_traffic = node_total_traffic + rec['u']+rec['d'] # 节点流量记录 node = Node.objects.get(node_id=node_id) node.used_traffic += node_total_traffic node.save() # 个人流量记录 TrafficLog.objects.bulk_create(trafficlog_model_list) re_dict = {'ret': 1, 'data': []} else: re_dict = {'ret': -1} return JsonResponse(re_dict)
def save(self, *args, **kwargs): self.human_total_traffic = traffic_format(self.total_traffic) self.human_used_traffic = traffic_format(self.used_traffic) super(Node, self).save(*args, **kwargs)