Skip to content

Commit

Permalink
org admin show traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
imwhatiam committed Dec 27, 2024
1 parent bd6a35c commit 0b60e30
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
26 changes: 23 additions & 3 deletions frontend/src/pages/org-admin/org-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
});
Expand All @@ -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 (
<Fragment>
<MainPanelTopbar/>
Expand Down Expand Up @@ -104,6 +107,23 @@ class OrgInfo extends Component {
<p>{Utils.bytesToSize(storage_usage)}</p>
)}
</div>
<div className="info-content-item">
<h4 className="info-content-item-heading">{gettext('Traffic this month')}</h4>
{traffic_limit > 0 ? (
<>
<p className="info-content-space-text">{`${(download_traffic / traffic_limit * 100).toFixed(2)}%`}</p>
<div className="progress-container">
<div className="progress">
<div className="progress-bar" role="progressbar" style={{ width: `${download_traffic / traffic_limit * 100}%` }} aria-valuenow={download_traffic / traffic_limit * 100} aria-valuemin="0" aria-valuemax="100"></div>
</div>
<p className="progress-text m-0">{`${Utils.bytesToSize(download_traffic)} / ${Utils.bytesToSize(traffic_limit)}`}</p>
</div>
</>
) : (
<p>{Utils.bytesToSize(download_traffic)}</p>
)}
</div>

</div>
</div>
</div>
Expand Down
26 changes: 22 additions & 4 deletions seahub/organizations/api/admin/info.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions seahub/role_permissions/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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': ''
},
}

Expand Down
5 changes: 1 addition & 4 deletions seahub/role_permissions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions seahub/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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():
Expand Down
11 changes: 11 additions & 0 deletions seahub/utils/ccnet_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions seahub/utils/file_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0b60e30

Please sign in to comment.