Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: calculate admins traffic usage #935

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

omid-sharifzadeh
Copy link

desc: show admin traffic usage in admin panel not system traffic usage for non sudoers admins

desc: show admin traffic usage in admin panel not system traffic usage
desc: show admin traffic usage in admin panel not system traffic usage
doaei pushed a commit to doaei/Marzban that referenced this pull request Apr 13, 2024
doaei pushed a commit to doaei/Marzban that referenced this pull request Apr 13, 2024
@M03ED
Copy link
Collaborator

M03ED commented Apr 14, 2024

i had same idea for this part
can you separate it by node like what we have for users ?

@omid-sharifzadeh
Copy link
Author

i had same idea for this part can you separate it by node like what we have for users ?

yes node usages still showing whole system traffic , i'll fix it

@M03ED
Copy link
Collaborator

M03ED commented Apr 14, 2024

yes node usages still showing whole system traffic , i'll fix it

Non sudo admins can't see node usage
We should have separated table for admin node usage

Copy link
Collaborator

@M03ED M03ED left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job but your code need some small changes

usages = crud.get_nodes_usage(db, start_date, end_date)
if not admin.is_sudo:
# get non sudo admin usage
dbadmin: Union[Admin, None] = crud.get_admin(db, admin.username)
Copy link
Collaborator

@M03ED M03ED Apr 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/api/nodes/usage should remain same as before

we already have admin object and we don't need to get it again
also sudo admin should be able to see other admins usages
for this , is better to have separated function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have different API endpoint for non sudo admin node usage ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

different api to see admin usage by sudo admin

@@ -161,6 +219,7 @@ def record_user_usages():

for node_id, params in api_params.items():
record_user_stats(params, node_id, usage_coefficient[node_id])
record_admin_stats(params, node_id, usage_coefficient[node_id])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate record_admin_stats and record_user_stats , they both use DISABLE_RECORDING_NODE_USAGE variable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think its a general variable it is also used in record_node_usages()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeap it is and it's better to become separated for each one

@@ -76,6 +76,64 @@ def record_user_stats(params: list, node_id: Union[int, None],
NodeUserUsage.created_at == created_at))
safe_execute(db, stmt, params)

def record_admin_stats(params: list, node_id: Union[int, None],
consumption_factor: int = 1):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's usage_coefficient not consumption_factor

@m0x61h0x64i
Copy link

In my opinion, the table user_usage_logs with the new columns used_traffic_on_delete and admin_id can be used for
Show admin traffic usage in admin panel, not system traffic usage for non-sudo admins
And that's Enough, what do you think?

More details on this issue:
#1015

@m0x61h0x64i
Copy link

یه حرکتی بزنید

@omid-sharifzadeh
Copy link
Author

omid-sharifzadeh commented May 31, 2024

In my opinion, the table user_usage_logs with the new columns used_traffic_on_delete and admin_id can be used for Show admin traffic usage in admin panel, not system traffic usage for non-sudo admins And that's Enough, what do you think?

More details on this issue: #1015

بررسی کردم ایشو رو بنظر اوکیه احتمالا برای محاسبه مصرف نود ادمین هم بشه کار مشابه روی جدول node_user_uasage انجام داد

و اینکه تست کردید جواب نمیداد چون مرج نشده pr

@m0x61h0x64i
Copy link

m0x61h0x64i commented Jun 1, 2024

ممنون امید جان،
برای پیاده کردن محاسبه مصرف نود کاربران ادمین نظر من اینه که تیبل node_user_usages اطلاعات کافی درمورد مصرف نود کاربران رو داره، فقط میمونه مشکل حذف کاربر چون ریلیشن node_user_usages با users به صورت cascade هست که اونم همونطور که شما امید جان اشاره کردید از همون حرکت میشه استفاده کرد این مشکل رو حل کرد اما به چند روش:

‍‍۱.فیلد های admin_id و used_traffic_on_delete و deleted_user_id رو به تیبل node_user_usages اضافه کنیم

۲.فیلد admin_id به تیبل node_user_usages اضافه بشه و یه تیبل جدید node_user_usage_logs اضافه بشه که وقتی کاربر پاک شد اطلاعات node_user_usages مربوط به کاربر در یک ریکورد اونجا ذخیره بشه که میتونه دارای node_id و used_traffic و user_id و deleted_at و admin_id باشه

۳.فیلد admin_id به تیبل node_user_usages اضافه بشه و cascade رو از ریلیشن تیبل node_user_usages با تیبل users برداشته بشه یعنی cascade رو از اینجا پاک کنیم

node_usages = relationship("NodeUserUsage", back_populates="user", cascade="all, delete-orphan")

و موقع گرفتن اطلاعات مربوط به مصرف کاربران ادمین از نود اگر ادمین غیر sudo هست میتوان بر اساس ایدی ادمین غیر sudo اطلاعات لازم رو دریافت کرد و نمایش داد. اگر کمکی از دست من برمیاد بگین

@omid-sharifzadeh

@m0x61h0x64i
Copy link

یه حرکتی بزنید

@omid-sharifzadeh
Copy link
Author

omid-sharifzadeh commented Jun 4, 2024

درسته
خب چرا زحمت بکشیم از اول بنویسیم الان کد هایی که از قبل نوشتم مشکلی داره ؟
@m0x61h0x64i

@m0x61h0x64i
Copy link

درسته خب چرا زحمت بکشیم از اول بنویسیم الان کد هایی که از قبل نوشتم مشکلی داره ؟ @m0x61h0x64i

ممنون امید جان، بازم شما یه حرکتی زدی تشکر میکنم.
کد رو الان خوندم. در مورد ستون اضافه شده به تیبل User که این هست
admin_usages = relationship("NodeAdminUsage", back_populates="node", cascade="all, delete-orphan")
اگر کاربر پاک شه مقدار usage قابل نمایش داخل پنل ادمین کم خواهد شد؟

@omid-sharifzadeh
Copy link
Author

omid-sharifzadeh commented Jun 5, 2024

نه
ترافیک محاسبه شده ادمین ها مثله ترافیک کل پنل محاسبه میشه داخل جدول admins ستون used_traffic ذخیره میشه حتی اگه یوزر پاک بشه از ترافیک مصرف شده ادمین کم نمیشه
@m0x61h0x64i

@m0x61h0x64i
Copy link

ممنون امید جان، پس چرا مرج نمیشه؟
@M03ED @SaintShit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants