def post(self, request, node_id): node = VmessNode.get_or_none_by_node_id(node_id) if not node: return HttpResponseNotFound() log_time = get_current_datetime() node_total_traffic = 0 need_clear_cache = False trafficlog_model_list = [] user_model_list = [] for log in request.json["user_traffics"]: user_id = log["user_id"] u = int(log["ut"] * node.enlarge_scale) d = int(log["dt"] * node.enlarge_scale) # 个人流量增量 user = User.get_by_pk(user_id) user.download_traffic += d user.upload_traffic += u user.last_use_time = log_time user_model_list.append(user) if user.overflow: need_clear_cache = True # 个人流量记录 trafficlog_model_list.append( UserTrafficLog( node_type=UserTrafficLog.NODE_TYPE_VMESS, node_id=node_id, user_id=user_id, download_traffic=u, upload_traffic=d, ) ) # 节点流量增量 node_total_traffic += u + d # 节点流量记录 VmessNode.increase_used_traffic(node_id, node_total_traffic) # 流量记录 UserTrafficLog.objects.bulk_create(trafficlog_model_list) # TODO 在线IP # 个人流量记录 User.objects.bulk_update( user_model_list, ["download_traffic", "upload_traffic", "last_use_time"], ) # 节点在线人数 NodeOnlineLog.add_log( NodeOnlineLog.NODE_TYPE_VMESS, node_id, len(request.json["user_traffics"]) ) # check node && user traffic if node.overflow: node.enable = False if need_clear_cache or node.overflow: node.save() return JsonResponse(data={})
def get(self, request): user_status = [ NodeOnlineLog.get_all_node_online_user_count(), User.get_today_register_user().count(), UserCheckInLog.get_today_checkin_user_count(), User.get_never_used_user_count(), ] donate_status = [ Donate.get_donate_count_by_date(), Donate.get_donate_money_by_date(), Donate.get_donate_count_by_date(date=pendulum.today()), Donate.get_donate_money_by_date(date=pendulum.today()), ] active_nodes = SSNode.get_active_nodes() + VmessNode.get_active_nodes() node_status = { "names": [node.name for node in active_nodes], "traffics": [ round(node.used_traffic / settings.GB, 2) for node in active_nodes ], } data = { "user_status": user_status, "donate_status": donate_status, "node_status": node_status, } return JsonResponse(data)
def get(self, request): today_register_user = User.get_today_register_user().values()[:10] # find inviter for u in today_register_user: try: u["inviter"] = User.objects.get(pk=u["inviter_id"]) except User.DoesNotExist: u["inviter"] = "None" context = { "total_user_num": User.get_total_user_num(), "alive_user_count": NodeOnlineLog.get_all_node_online_user_count(), "today_checked_user_count": UserCheckInLog.get_today_checkin_user_count(), "today_register_user_count": len(today_register_user), "traffic_users": UserTraffic.get_user_order_by_traffic(count=10), "rich_users_data": Donate.get_most_donated_user_by_count(10), "today_register_user": today_register_user, } return render(request, "my_admin/user_status.html", context=context)
def post(self, request, node_id): """ 这个接口操作比较重,所以为了避免发信号 所有写操作都需要用BULK的方式 1 更新节点流量 2 更新用户流量 3 记录节点在线IP 4 关闭超出流量的节点 """ ss_node = SSNode.get_or_none_by_node_id(node_id) if not ss_node: return HttpResponseNotFound() data = request.json["data"] node_total_traffic = 0 log_time = get_current_datetime() active_tcp_connections = 0 need_clear_cache = False user_model_list = [] trafficlog_model_list = [] online_ip_log_model_list = [] for user_data in data: user_id = user_data["user_id"] u = int(user_data["upload_traffic"] * ss_node.enlarge_scale) d = int(user_data["download_traffic"] * ss_node.enlarge_scale) # 个人流量增量 user = User.get_by_pk(user_id) user.download_traffic += d user.upload_traffic += u user.last_use_time = log_time user_model_list.append(user) if user.overflow: need_clear_cache = True # 个人流量记录 trafficlog_model_list.append( UserTrafficLog( node_type=UserTrafficLog.NODE_TYPE_SS, node_id=node_id, user_id=user_id, download_traffic=u, upload_traffic=d, ) ) # 节点流量增量 node_total_traffic += u + d # active_tcp_connections active_tcp_connections += user_data["tcp_conn_num"] # online ip log for ip in user_data.get("ip_list", []): online_ip_log_model_list.append( UserOnLineIpLog(user_id=user_id, node_id=node_id, ip=ip) ) # 用户流量 User.objects.bulk_update( user_model_list, ["download_traffic", "upload_traffic", "last_use_time"], ) # 节点流量记录 SSNode.increase_used_traffic(node_id, node_total_traffic) # 流量记录 UserTrafficLog.objects.bulk_create(trafficlog_model_list) # 在线IP UserOnLineIpLog.objects.bulk_create(online_ip_log_model_list) # 节点在线人数 NodeOnlineLog.add_log( NodeOnlineLog.NODE_TYPE_SS, node_id, len(data), active_tcp_connections ) # check node && user traffic if ss_node.overflow: ss_node.enable = False if need_clear_cache or ss_node.overflow: ss_node.save() return JsonResponse(data={})
def clean_node_online_log(self): """清空所有在线记录""" count = NodeOnlineLog.objects.count() NodeOnlineLog.truncate() print(f"NodeOnlineLog removed count:{count}")