Skip to content

Commit

Permalink
Merge branch 'govfvck-fix-realtime-bandwith' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintShit committed Feb 20, 2024
2 parents aca776f + de0d290 commit 3d61fbc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
14 changes: 7 additions & 7 deletions app/telegram/handlers/admin.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import io
import math
import re
import os
import random
import re
import string
import os
from datetime import datetime

import qrcode
import sqlalchemy
from dateutil.relativedelta import relativedelta
from telebot import types
from telebot.util import user_link, extract_arguments
from telebot.util import extract_arguments, user_link

from app import xray
from app.db import GetDB, crud
from app.models.proxy import ProxyTypes
from app.models.user import (UserCreate, UserModify, UserResponse, UserStatus,
UserStatusModify)
from app.models.user_template import UserTemplateResponse
from app.models.proxy import ProxyTypes
from app.telegram import bot
from app.telegram.utils.custom_filters import (cb_query_equals,
cb_query_startswith)
Expand All @@ -30,7 +30,7 @@
except ImportError:
from app.utils.system import realtime_bandwidth

from config import TELEGRAM_LOGGER_CHANNEL_ID, TELEGRAM_DEFAULT_VLESS_FLOW
from config import TELEGRAM_DEFAULT_VLESS_FLOW, TELEGRAM_LOGGER_CHANNEL_ID

mem_store = MemoryStorage()

Expand Down Expand Up @@ -58,8 +58,8 @@ def get_system_info():
🟢 *Active Users*: `{active_users}`
🔴 *Deactivate Users*: `{deactivate_users}`
➖➖➖➖➖➖➖
⏫ *Upload Speed*: `{up_speed}`
⏬ *Download Speed*: `{down_speed}`
⏫ *Upload Speed*: `{up_speed}/s`
⏬ *Download Speed*: `{down_speed}/s`
""".format(
cpu_cores=cpu.cores,
cpu_percent=cpu.percent,
Expand Down
34 changes: 24 additions & 10 deletions app/utils/system.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import requests
import math
import secrets
import socket
import time
from dataclasses import dataclass

import psutil
import requests

from app import scheduler

Expand Down Expand Up @@ -38,8 +39,10 @@ def __post_init__(self):
self.bytes_recv = io.bytes_recv
self.bytes_sent = io.bytes_sent
self.packets_recv = io.packets_recv
self.packet_sent = io.packets_sent
self.packets_sent = io.packets_sent
self.last_perf_counter = time.perf_counter()

# data in the form of value per seconds
incoming_bytes: int
outgoing_bytes: int
incoming_packets: int
Expand All @@ -48,11 +51,14 @@ def __post_init__(self):
bytes_recv: int = None
bytes_sent: int = None
packets_recv: int = None
packet_sent: int = None
packets_sent: int = None
last_perf_counter: float = None


@dataclass
class RealtimeBandwidthStat:
"""Real-Time bandwith in value/s unit"""

incoming_bytes: int
outgoing_bytes: int
incoming_packets: int
Expand All @@ -63,19 +69,27 @@ class RealtimeBandwidthStat:
incoming_bytes=0, outgoing_bytes=0, incoming_packets=0, outgoing_packets=0)


@scheduler.scheduled_job('interval', seconds=1, coalesce=True, max_instances=1)
# sample time is 2 seconds, values lower than this may not produce good results
@scheduler.scheduled_job("interval", seconds=2, coalesce=True, max_instances=1)
def record_realtime_bandwidth() -> None:
global rt_bw
last_perf_counter = rt_bw.last_perf_counter
io = psutil.net_io_counters()
rt_bw.incoming_bytes, rt_bw.bytes_recv = io.bytes_recv - rt_bw.bytes_recv, io.bytes_recv
rt_bw.outgoing_bytes, rt_bw.bytes_sent = io.bytes_sent - rt_bw.bytes_sent, io.bytes_sent
rt_bw.incoming_packets, rt_bw.packets_recv = io.packets_recv - rt_bw.packets_recv, io.packets_recv
rt_bw.outgoing_packets, rt_bw.packet_sent = io.packets_sent - rt_bw.packet_sent, io.packets_sent
rt_bw.last_perf_counter = time.perf_counter()
sample_time = rt_bw.last_perf_counter - last_perf_counter
rt_bw.incoming_bytes, rt_bw.bytes_recv = round((io.bytes_recv - rt_bw.bytes_recv) / sample_time), io.bytes_recv
rt_bw.outgoing_bytes, rt_bw.bytes_sent = round((io.bytes_sent - rt_bw.bytes_sent) / sample_time), io.bytes_sent
rt_bw.incoming_packets, rt_bw.packets_recv = round((io.packets_recv - rt_bw.packets_recv) / sample_time), io.packets_recv
rt_bw.outgoing_packets, rt_bw.packets_sent = round((io.packets_sent - rt_bw.packets_sent) / sample_time), io.packets_sent


def realtime_bandwidth() -> RealtimeBandwidthStat:
return RealtimeBandwidthStat(
incoming_bytes=rt_bw.incoming_bytes, outgoing_bytes=rt_bw.outgoing_bytes,
incoming_packets=rt_bw.incoming_packets, outgoing_packets=rt_bw.outgoing_packets)
incoming_bytes=rt_bw.incoming_bytes,
outgoing_bytes=rt_bw.outgoing_bytes,
incoming_packets=rt_bw.incoming_packets,
outgoing_packets=rt_bw.outgoing_packets,
)


def random_password() -> str:
Expand Down

0 comments on commit 3d61fbc

Please sign in to comment.