Beispiel #1
0
def traffic_query(request):
    """
    流量查请求
    """
    node_id = request.POST.get("node_id", 0)
    node_name = request.POST.get("node_name", "")
    user_id = request.user.pk
    now = pendulum.now()
    last_week = [now.subtract(days=i).date() for i in range(6, -1, -1)]
    labels = ["{}-{}".format(t.month, t.day) for t in last_week]
    traffic_data = [
        TrafficLog.get_traffic_by_date(node_id, user_id, t) for t in last_week
    ]
    total = TrafficLog.get_user_traffic(node_id, user_id)
    title = "节点 {} 当月共消耗:{}".format(node_name, total)

    configs = {
        "title": title,
        "labels": labels,
        "data": traffic_data,
        "data_title": node_name,
        "x_label": "日期 最近七天",
        "y_label": "流量 单位:MB",
    }
    return JsonResponse(configs)
Beispiel #2
0
def traffic_query(request):
    '''
    流量查请求
    '''
    node_id = request.POST.get('node_id', 0)
    node_name = request.POST.get('node_name', '')
    user_id = request.user.pk
    now = pendulum.now()
    last_week = [now.subtract(days=i).date() for i in range(6, -1, -1)]
    labels = ['{}-{}'.format(t.month, t.day) for t in last_week]
    traffic_data = [
        TrafficLog.get_traffic_by_date(node_id, user_id, t) for t in last_week
    ]
    total = TrafficLog.get_user_traffic(node_id, user_id)
    title = '节点 {} 当月共消耗:{}'.format(node_name, total)

    configs = {
        'title': title,
        'labels': labels,
        'data': traffic_data,
        'data_title': node_name,
        'x_label': '日期 最近七天',
        'y_label': '流量 单位:MB'
    }
    return JsonResponse(configs)
Beispiel #3
0
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)
        res = {'ret': 1, 'data': []}
    else:
        res = {'ret': -1}
    return JsonResponse(res)
Beispiel #4
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)
Beispiel #5
0
def traffic_query(request):
    '''
    流量查请求
    '''
    node_id = request.POST.get('node_id', 0)
    node_name = request.POST.get('node_name', '')
    user_id = request.user.ss_user.user_id
    last_week = get_date_list(7)
    labels = ['{}-{}'.format(t.month, t.day) for t in last_week]
    trafficdata = [TrafficLog.getTrafficByDay(
        node_id, user_id, t) for t in last_week]
    title = '节点 {} 当月共消耗:{} GB'.format(
        node_name, TrafficLog.getUserTraffic(node_id, user_id))
    configs = {
        'title': title,
        'labels': labels,
        'data': trafficdata,
        'data_title': node_name,
        'x_label': '日期 最近七天',
        'y_label': '流量 单位:GB'
    }
    return JsonResponse(configs)
Beispiel #6
0
def traffic_query(request):
    '''
    流量查请求
    '''
    node_id = request.POST.get('node_id', 0)
    node_name = request.POST.get('node_name', '')
    user_id = request.user.ss_user.user_id
    last_week = get_date_list(7)
    labels = ['{}-{}'.format(t.month, t.day) for t in last_week]
    trafficdata = [
        TrafficLog.getTrafficByDay(node_id, user_id, t) for t in last_week
    ]
    title = '节点 {} 当月共消耗:{} GB'.format(
        node_name, TrafficLog.getUserTraffic(node_id, user_id))
    configs = {
        'title': title,
        'labels': labels,
        'data': trafficdata,
        'data_title': node_name,
        'x_label': '日期 最近七天',
        'y_label': '流量 单位:GB'
    }
    return JsonResponse(configs)
Beispiel #7
0
def traffic_api(request):
    """
    接受服务端的用户流量上报
    """
    data = request.json
    node_id = data["node_id"]
    traffic_list = data["data"]
    log_time = int(time.time())

    node_total_traffic = 0
    trafficlog_model_list = []

    for rec in traffic_list:
        user_id = rec["user_id"]
        u = rec["u"]
        d = rec["d"]
        # 个人流量增量
        Suser.objects.filter(user_id=user_id).update(
            download_traffic=F("download_traffic") + d,
            upload_traffic=F("upload_traffic") + u,
            last_use_time=log_time,
        )
        # 个人流量记录
        trafficlog_model_list.append(
            TrafficLog(
                node_id=node_id,
                user_id=user_id,
                traffic=traffic_format(u + d),
                download_traffic=u,
                upload_traffic=d,
                log_time=log_time,
            )
        )
        # 节点流量增量
        node_total_traffic += u + d
    # 节点流量记录
    Node.objects.filter(node_id=node_id).update(
        used_traffic=F("used_traffic") + node_total_traffic
    )
    # 流量记录
    TrafficLog.objects.bulk_create(trafficlog_model_list)
    return JsonResponse({"ret": 1, "data": []})
Beispiel #8
0
def traffic_api(request):
    '''
    接受服务端的用户流量上报
    '''
    data = request.json
    node_id = data['node_id']
    traffic_list = data['data']
    log_time = int(time.time())

    node_total_traffic = 0
    trafficlog_model_list = []
    ssuer_model_list = []

    for rec in traffic_list:
        # 个人流量增量
        ssuer_model_list.append(
            SSUser(pk=rec['user_id'],
                   upload_traffic=(F('upload_traffic') + rec['u']),
                   download_traffic=(F('download_traffic') + rec['d']),
                   last_use_time=log_time))
        # 个人流量记录
        trafficlog_model_list.append(
            TrafficLog(node_id=node_id,
                       user_id=rec['user_id'],
                       traffic=traffic_format(rec['u'] + rec['d']),
                       download_traffic=rec['d'],
                       upload_traffic=rec['u'],
                       log_time=log_time))
        # 节点流量增量
        node_incr = rec['u'] + rec['d']
        node_total_traffic += node_incr
    # 节点流量记录
    Node.objects.filter(node_id=node_id).update(
        used_traffic=F('used_traffic') + node_total_traffic)
    # 流量记录
    TrafficLog.objects.bulk_create(trafficlog_model_list)
    SSUser.objects.bulk_update(
        ssuer_model_list,
        update_fields=['upload_traffic', 'download_traffic', 'last_use_time'])

    return JsonResponse({'ret': 1, 'data': []})
Beispiel #9
0
def traffic_api(request):
    '''
    接受服务端的用户流量上报
    '''
    data = request.json
    node_id = data['node_id']
    traffic_list = data['data']
    log_time = int(time.time())

    node_total_traffic = 0
    trafficlog_model_list = []

    for rec in traffic_list:
        user_id = rec['user_id']
        u = rec['u']
        d = rec['d']
        # 个人流量增量
        Suser.objects.filter(user_id=user_id).update(
            download_traffic=F("download_traffic") + d,
            upload_traffic=F('upload_traffic') + u,
            last_use_time=log_time)
        # 个人流量记录
        trafficlog_model_list.append(
            TrafficLog(node_id=node_id,
                       user_id=user_id,
                       traffic=traffic_format(u + d),
                       download_traffic=u,
                       upload_traffic=d,
                       log_time=log_time))
        # 节点流量增量
        node_total_traffic += (u + d)
    # 节点流量记录
    Node.objects.filter(node_id=node_id).update(
        used_traffic=F('used_traffic') + node_total_traffic)
    # 流量记录
    TrafficLog.objects.bulk_create(trafficlog_model_list)
    return JsonResponse({'ret': 1, 'data': []})
Beispiel #10
0
def clean_traffic_log():
    '''清空所有流量记录'''
    count = TrafficLog.objects.count()
    TrafficLog.truncate()
    print('Time: {} traffic record removed!:{}'.format(timezone.now(), count))