From 0b60e30dbe422cb9e897f7e02b50554671ef4b39 Mon Sep 17 00:00:00 2001 From: lian Date: Fri, 27 Dec 2024 12:04:25 +0800 Subject: [PATCH] org admin show traffic --- frontend/src/pages/org-admin/org-info.js | 26 +++++++++++++++++++++--- seahub/organizations/api/admin/info.py | 26 ++++++++++++++++++++---- seahub/role_permissions/settings.py | 4 ++++ seahub/role_permissions/utils.py | 5 +---- seahub/utils/__init__.py | 7 +++++++ seahub/utils/ccnet_db.py | 11 ++++++++++ seahub/utils/file_size.py | 2 ++ 7 files changed, 70 insertions(+), 11 deletions(-) diff --git a/frontend/src/pages/org-admin/org-info.js b/frontend/src/pages/org-admin/org-info.js index c2ebc6419f7..1f809826eb1 100644 --- a/frontend/src/pages/org-admin/org-info.js +++ b/frontend/src/pages/org-admin/org-info.js @@ -12,6 +12,8 @@ class OrgInfo extends Component { super(props); this.state = { org_name: '', + traffic_this_month: '', + traffic_limit: '', storage_quota: 0, storage_usage: 0, member_quota: 0, @@ -23,12 +25,12 @@ class OrgInfo extends Component { componentDidMount() { orgAdminAPI.orgAdminGetOrgInfo().then(res => { const { - org_id, org_name, + org_id, org_name, traffic_this_month, traffic_limit, member_quota, member_usage, active_members, storage_quota, storage_usage } = res.data; this.setState({ - org_id, org_name, + org_id, org_name, traffic_this_month, traffic_limit, member_quota, member_usage, active_members, storage_quota, storage_usage }); @@ -37,10 +39,11 @@ class OrgInfo extends Component { render() { const { - org_id, org_name, + org_id, org_name, traffic_this_month, traffic_limit, member_quota, member_usage, active_members, storage_quota, storage_usage } = this.state; + const download_traffic = traffic_this_month.link_file_download + traffic_this_month.sync_file_download + traffic_this_month.web_file_download; return ( @@ -104,6 +107,23 @@ class OrgInfo extends Component {

{Utils.bytesToSize(storage_usage)}

)} +
+

{gettext('Traffic this month')}

+ {traffic_limit > 0 ? ( + <> +

{`${(download_traffic / traffic_limit * 100).toFixed(2)}%`}

+
+
+
+
+

{`${Utils.bytesToSize(download_traffic)} / ${Utils.bytesToSize(traffic_limit)}`}

+
+ + ) : ( +

{Utils.bytesToSize(download_traffic)}

+ )} +
+ diff --git a/seahub/organizations/api/admin/info.py b/seahub/organizations/api/admin/info.py index 53a9a91c9fe..39bac448a58 100644 --- a/seahub/organizations/api/admin/info.py +++ b/seahub/organizations/api/admin/info.py @@ -1,5 +1,6 @@ # Copyright (c) 2012-2016 Seafile Ltd. import logging +from datetime import datetime from rest_framework import status from rest_framework.views import APIView @@ -16,12 +17,17 @@ from seahub.api2.throttling import UserRateThrottle from seahub.api2.authentication import TokenAuthentication -from seahub.organizations.models import OrgMemberQuota, FORCE_ADFS_LOGIN, DISABLE_ORG_ENCRYPTED_LIBRARY, DISABLE_ORG_USER_CLEAN_TRASH -from seahub.utils.file_size import get_file_size_unit +from seahub.utils import get_org_traffic_by_month +from seahub.utils.ccnet_db import CcnetDB +from seahub.utils.file_size import get_file_size_unit, get_quota_from_string +from seahub.role_permissions.utils import get_enabled_role_permissions_by_role + +from seahub.organizations.api.permissions import IsOrgAdmin +from seahub.organizations.models import OrgAdminSettings, \ + OrgMemberQuota, FORCE_ADFS_LOGIN, DISABLE_ORG_ENCRYPTED_LIBRARY, \ + DISABLE_ORG_USER_CLEAN_TRASH from seahub.organizations.settings import ORG_MEMBER_QUOTA_ENABLED, \ ORG_ENABLE_ADMIN_CUSTOM_NAME -from seahub.organizations.api.permissions import IsOrgAdmin -from seahub.organizations.models import OrgAdminSettings logger = logging.getLogger(__name__) @@ -84,6 +90,18 @@ def get_org_info(request, org_id): if settings.ENABLE_MULTI_ADFS is False: info[FORCE_ADFS_LOGIN] = False + current_date = datetime.now() + info['traffic_this_month'] = get_org_traffic_by_month(org_id, current_date) + + info['traffic_limit'] = '' + role_perm_dict = get_enabled_role_permissions_by_role() + monthly_rate_limit_per_user = role_perm_dict.get('monthly_rate_limit_per_user', '') + if monthly_rate_limit_per_user: + ccnet_db = CcnetDB() + user_count = ccnet_db.get_org_user_count(org_id) + traffic_limit = get_quota_from_string(monthly_rate_limit_per_user) * user_count + info['traffic_limit'] = traffic_limit + info['storage_quota'] = storage_quota info['storage_usage'] = storage_usage info['user_default_quota'] = user_default_quota diff --git a/seahub/role_permissions/settings.py b/seahub/role_permissions/settings.py index 57084014354..28c0d88cdf3 100644 --- a/seahub/role_permissions/settings.py +++ b/seahub/role_permissions/settings.py @@ -47,6 +47,8 @@ def merge_roles(default, custom): 'can_publish_wiki': True, 'upload_rate_limit': 0, 'download_rate_limit': 0, + 'monthly_rate_limit': '', + 'monthly_rate_limit_per_user': '' }, GUEST_USER: { 'can_add_repo': False, @@ -70,6 +72,8 @@ def merge_roles(default, custom): 'can_publish_wiki': False, 'upload_rate_limit': 0, 'download_rate_limit': 0, + 'monthly_rate_limit': '', + 'monthly_rate_limit_per_user': '' }, } diff --git a/seahub/role_permissions/utils.py b/seahub/role_permissions/utils.py index 28da1942fc5..23d55f50a24 100644 --- a/seahub/role_permissions/utils.py +++ b/seahub/role_permissions/utils.py @@ -14,12 +14,9 @@ def get_available_roles(): return list(ENABLED_ROLE_PERMISSIONS.keys()) -def get_enabled_role_permissions_by_role(role): +def get_enabled_role_permissions_by_role(role=DEFAULT_USER): """Get permissions dict(perm_name: bool) of a role. """ - if not role: - role = DEFAULT_USER - if role not in list(ENABLED_ROLE_PERMISSIONS.keys()): logger.warn('%s is not a valid role, use default role.' % role) role = DEFAULT_USER diff --git a/seahub/utils/__init__.py b/seahub/utils/__init__.py index af3b3e37beb..d50c4f7dde7 100644 --- a/seahub/utils/__init__.py +++ b/seahub/utils/__init__.py @@ -852,6 +852,11 @@ def get_user_traffic_by_month(username, month): res = seafevents_api.get_user_traffic_by_month(session, username, month) return res + def get_org_traffic_by_month(org_id, month): + with _get_seafevents_session() as session: + res = seafevents_api.get_org_traffic_by_month(session, org_id, month) + return res + def get_file_history_suffix(): return seafevents_api.get_file_history_suffix(parsed_events_conf) @@ -871,6 +876,8 @@ def get_all_orgs_traffic_by_month(): pass def get_user_traffic_by_month(): pass + def get_org_traffic_by_month(): + pass def get_user_activity_stats_by_day(): pass def get_org_user_activity_stats_by_day(): diff --git a/seahub/utils/ccnet_db.py b/seahub/utils/ccnet_db.py index 678dd857bae..b47268a2213 100644 --- a/seahub/utils/ccnet_db.py +++ b/seahub/utils/ccnet_db.py @@ -2,6 +2,7 @@ import os from django.db import connection + def get_ccnet_db_name(): return os.environ.get('SEAFILE_MYSQL_DB_CCNET_DB_NAME', '') or 'ccnet_db' @@ -200,3 +201,13 @@ def get_active_users_by_user_list(self, user_list): active_users.append(user[0]) return active_users + + def get_org_user_count(self, org_id): + sql = f""" + SELECT COUNT(1) FROM `{self.db_name}`.`OrgUser` WHERE org_id={org_id} + """ + user_count = 0 + with connection.cursor() as cursor: + cursor.execute(sql) + user_count = cursor.fetchone()[0] + return user_count diff --git a/seahub/utils/file_size.py b/seahub/utils/file_size.py index 89061e1e0f5..8e09f416514 100644 --- a/seahub/utils/file_size.py +++ b/seahub/utils/file_size.py @@ -48,6 +48,8 @@ def get_quota_from_string(quota_str): quota = int(quota_str[:-1]) * get_file_size_unit('gb') elif quota_str.endswith('m'): quota = int(quota_str[:-1]) * get_file_size_unit('mb') + elif quota_str.endswith('k'): + quota = int(quota_str[:-1]) * get_file_size_unit('kb') else: return None