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