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(6) labels = ['{}-{}-{}'.format(t.year, t.month, t.day) for t in last_week] new_labels = ['{}-{}'.format(t.split('-')[1], t.split('-')[2]) for t in labels] trafficdata = [TrafficLog.getTrafficByDay( node_id, user_id, t) for t in labels] title = '节点 {} 当月共消耗:{} GB'.format(node_name, TrafficLog.getUserTraffic(node_id, user_id)) configs = { 'title': title, 'labels': new_labels, 'data': trafficdata, 'data_title': '流量统计图', 'x_label': '日期 最近七天', 'y_label': '流量 单位:GB' } result = json.dumps(configs, ensure_ascii=False) return HttpResponse(result, content_type='application/json')
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 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(6) 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)
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(6) 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' } result = json.dumps(configs, ensure_ascii=False) return HttpResponse(result, content_type='application/json')
def nodeData(request): ''' 返回节点信息 所有节点名 各自消耗的流量 ''' nodeName = [node.name for node in Node.objects.filter(show='显示')] nodeTraffic = [TrafficLog.totalTraffic( node.node_id) for node in Node.objects.filter(show='显示')] data = { 'nodeName': nodeName, 'nodeTraffic': nodeTraffic, } result = json.dumps(data, ensure_ascii=False) return HttpResponse(result, content_type='application/json')
def nodeData(request): ''' 返回节点信息 所有节点名 各自消耗的流量 ''' nodeName = [node.name for node in Node.objects.all()] nodeTraffic = [TrafficLog.totalTraffic( node.node_id) for node in Node.objects.all()] data = { 'nodeName': nodeName, 'nodeTraffic': nodeTraffic, } result = json.dumps(data, ensure_ascii=False) return HttpResponse(result, content_type='application/json')