mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-18 08:16:07 +00:00
Merge pull request #7253 from haiwen/org-month-traffic
show traffic on org admin page
This commit is contained in:
@@ -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,12 @@ 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;
|
||||
let download_traffic = traffic_this_month.link_file_download + traffic_this_month.sync_file_download + traffic_this_month.web_file_download;
|
||||
download_traffic = download_traffic ? download_traffic : 0
|
||||
return (
|
||||
<Fragment>
|
||||
<MainPanelTopbar/>
|
||||
@@ -104,6 +108,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>
|
||||
|
@@ -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.permissions import IsProVersion
|
||||
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
|
||||
|
@@ -47,6 +47,8 @@ DEFAULT_ENABLED_ROLE_PERMISSIONS = {
|
||||
'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 @@ DEFAULT_ENABLED_ROLE_PERMISSIONS = {
|
||||
'can_publish_wiki': False,
|
||||
'upload_rate_limit': 0,
|
||||
'download_rate_limit': 0,
|
||||
'monthly_rate_limit': '',
|
||||
'monthly_rate_limit_per_user': ''
|
||||
},
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -852,6 +852,11 @@ if EVENTS_CONFIG_FILE:
|
||||
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 @@ else:
|
||||
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():
|
||||
|
@@ -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 @@ class CcnetDB:
|
||||
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
|
||||
|
@@ -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
|
||||
|
||||
|
@@ -11,4 +11,4 @@ class UtilsTest(BaseTestCase):
|
||||
assert DEFAULT_USER in get_available_roles()
|
||||
|
||||
def test_get_enabled_role_permissions_by_role(self):
|
||||
assert len(list(get_enabled_role_permissions_by_role(DEFAULT_USER).keys())) == 21
|
||||
assert len(list(get_enabled_role_permissions_by_role(DEFAULT_USER).keys())) == 23
|
||||
|
Reference in New Issue
Block a user