예제 #1
0
def traffic_api(request):
    '''
    接受服务端的用户流量上报
    JSON_DATA:
    {
        'node_id': ,
        'data': [
            {
                'user_id': ,
                'u': ,
                'd':
            },
            ...
        ]
    }
    RES_DATA:
    {
        'user_id': 'ok',
        ...
    }
    '''
    data = request.json
    node_id = data['node_id']
    traffic_rec_list = data['data']
    # 定义循环池
    node_total_traffic = 0
    trafficlog_model_list = []
    log_time = int(time.time())

    res_data = dict()
    for rec in traffic_rec_list:
        try:
            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']
            res_data[rec['user_id']] = 'ok'
        except Exception as e:
            res_data[rec['user_id']] = 'fail'
    # 节点流量记录
    node = Node.objects.get(node_id=node_id)
    node.used_traffic += node_total_traffic
    node.save()
    # 个人流量记录
    TrafficLog.objects.bulk_create(trafficlog_model_list)
    res = {'ret': 1, 'data': res_data}
    return JsonResponse(res)
예제 #2
0
def checkin(request):
    '''用户签到'''
    ss_user = request.user.ss_user
    if not ss_user.get_check_in():
        # 距离上次签到时间大于一天 增加随机流量
        ll = randint(settings.MIN_CHECKIN_TRAFFIC, settings.MAX_CHECKIN_TRAFFIC)
        ss_user.transfer_enable += ll
        ss_user.last_check_in_time = timezone.now()
        ss_user.save()
        data = {
            'title': '签到成功!',
            'subtitle': '获得{}流量!'.format(traffic_format(ll)),
            'status': 'success',
        }
    else:
        data = {
            'title': '签到失败!',
            'subtitle': '距离上次签到不足一天',
            'status': 'error',
        }
    return JsonResponse(data)
예제 #3
0
 def totla_transfer(self):
     return traffic_format(self.transfer_enable)
예제 #4
0
 def save(self, *args, **kwargs):
     self.human_total_traffic = traffic_format(self.total_traffic)
     self.human_used_traffic = traffic_format(self.used_traffic)
     if self.node_type == 1:
         self.custom_method = 0
     super(Node, self).save(*args, **kwargs)
예제 #5
0
 def get_transfer(self):
     '''返回用户的总流量'''
     return traffic_format(self.transfer_enable)
예제 #6
0
 def human_used_traffic(self):
     return traffic_format(self.used_traffic)
예제 #7
0
 def human_increased_traffic(self):
     return traffic_format(self.increased_traffic)
예제 #8
0
 def total_transfer(self):
     '''增加的流量'''
     return traffic_format(self.transfer)
예제 #9
0
 def human_used_traffic(self):
     """已用流量"""
     return traffic_format(self.used_traffic)
예제 #10
0
 def human_used_traffic(self):
     '''已用流量'''
     return traffic_format(self.used_traffic)
예제 #11
0
 def human_total_traffic(self):
     '''总流量'''
     return traffic_format(self.total_traffic)
예제 #12
0
 def get_traffic(self):
     '''返回用户使用的总流量'''
     return traffic_format(self.download_traffic + self.upload_traffic)
예제 #13
0
 def save(self, *args, **kwargs):
     self.human_total_traffic = traffic_format(self.total_traffic)
     self.human_used_traffic = traffic_format(self.used_traffic)
     if self.node_type == 1:
         self.custom_method = 0
     super(Node, self).save(*args, **kwargs)
예제 #14
0
 def get_unused_traffic(self):
     '''返回用户的剩余流量'''
     return traffic_format(
         self.transfer_enable - self.upload_traffic - self.download_traffic)
예제 #15
0
 def unused_traffic(self):
     return traffic_format(self.transfer_enable - self.upload_traffic -
                           self.download_traffic)
예제 #16
0
 def get_transfer(self):
     '''增加的流量'''
     return traffic_format(self.transfer)
예제 #17
0
 def human_total_traffic(self):
     """总流量"""
     return traffic_format(self.total_traffic)
예제 #18
0
 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)
예제 #19
0
 def get_user_traffic(cls, node_id, user_id):
     logs = cls.objects.filter(node_id=node_id, user_id=user_id)
     return traffic_format(sum([l.used_traffic for l in logs]))
예제 #20
0
 def get_traffic(self):
     '''返回用户使用的总流量'''
     return traffic_format(self.download_traffic + self.upload_traffic)
예제 #21
0
 def total_traffic(self):
     return traffic_format(self.download_traffic + self.upload_traffic)
예제 #22
0
 def get_transfer(self):
     '''返回用户的总流量'''
     return traffic_format(self.transfer_enable)
예제 #23
0
 def human_total_traffic(self):
     return traffic_format(self.total_traffic)
예제 #24
0
 def get_unused_traffic(self):
     '''返回用户的剩余流量'''
     return traffic_format(self.transfer_enable - self.upload_traffic -
                           self.download_traffic)
예제 #25
0
 def total_transfer(self):
     """增加的流量"""
     return traffic_format(self.transfer)
예제 #26
0
 def human_remain_traffic(self):
     return traffic_format(self.total_traffic - self.used_traffic)