Fix: online status
This commit is contained in:
@ -30,10 +30,6 @@ def traffic_status():
|
||||
print(f"Error: Failed to fetch traffic data. Details: {e}")
|
||||
return
|
||||
|
||||
if not response or response == "{}":
|
||||
print("No traffic data available.")
|
||||
return
|
||||
|
||||
try:
|
||||
online_response = subprocess.check_output(['curl', '-s', '-H', f'Authorization: {secret}', ONLINE_API_URL]).decode().strip()
|
||||
except subprocess.CalledProcessError as e:
|
||||
@ -44,7 +40,10 @@ def traffic_status():
|
||||
print("No online data available.")
|
||||
return
|
||||
|
||||
response_dict = {}
|
||||
if response and response != "{}":
|
||||
response_dict = json.loads(response)
|
||||
|
||||
online_dict = json.loads(online_response)
|
||||
|
||||
# Load the current users.json data
|
||||
@ -57,17 +56,28 @@ def traffic_status():
|
||||
print("Error: Failed to parse existing users data JSON file.")
|
||||
return
|
||||
|
||||
# Update users.json with traffic data
|
||||
for user in users_data:
|
||||
users_data[user]["status"] = "Offline"
|
||||
|
||||
for user, online_status in online_dict.items():
|
||||
if user in users_data:
|
||||
users_data[user]["status"] = "Online" if online_status == 1 else "Offline"
|
||||
else:
|
||||
users_data[user] = {
|
||||
"upload_bytes": 0,
|
||||
"download_bytes": 0,
|
||||
"status": "Online" if online_status == 1 else "Offline"
|
||||
}
|
||||
|
||||
for user, traffic_info in response_dict.items():
|
||||
tx_bytes = traffic_info.get('tx', 0)
|
||||
rx_bytes = traffic_info.get('rx', 0)
|
||||
online_status = online_dict.get(user, 0)
|
||||
|
||||
if user in users_data:
|
||||
users_data[user]["upload_bytes"] = users_data[user].get("upload_bytes", 0) + tx_bytes
|
||||
users_data[user]["download_bytes"] = users_data[user].get("download_bytes", 0) + rx_bytes
|
||||
users_data[user]["status"] = "Online" if online_status == 1 else "Offline"
|
||||
else:
|
||||
online_status = online_dict.get(user, 0)
|
||||
users_data[user] = {
|
||||
"upload_bytes": tx_bytes,
|
||||
"download_bytes": rx_bytes,
|
||||
|
||||
Reference in New Issue
Block a user