diff --git a/seahub/api2/endpoints/admin/sysinfo.py b/seahub/api2/endpoints/admin/sysinfo.py index c807fcc01f..3b7dad759e 100644 --- a/seahub/api2/endpoints/admin/sysinfo.py +++ b/seahub/api2/endpoints/admin/sysinfo.py @@ -14,6 +14,7 @@ from seahub.utils.licenseparse import parse_license from seahub.api2.authentication import TokenAuthentication from seahub.api2.throttling import UserRateThrottle +from seahub.api2.models import TokenV2 try: from seahub.settings import MULTI_TENANCY @@ -87,6 +88,7 @@ class SysInfo(APIView): inactive_users = inactive_db_users + inactive_ldap_users if inactive_ldap_users > 0 \ else inactive_db_users + # get license info is_pro = is_pro_version() if is_pro: license_dict = parse_license() @@ -104,10 +106,39 @@ class SysInfo(APIView): with_license = False max_users = 0 + # count total file number + try: + total_files_count = seafile_api.get_total_file_number() + except Exception as e: + logger.error(e) + total_files_count = 0 + + # count total storage + try: + total_storage = seafile_api.get_total_storage() + except Exception as e: + logger.error(e) + total_storage = 0 + + # count devices number + try: + total_devices_count = TokenV2.objects.get_total_devices_count() + except Exception as e: + logger.error(e) + total_devices_count = 0 + + # count current connected devices + try: + current_connected_devices_count = TokenV2.objects.get_current_connected_devices_count() + except Exception as e: + logger.error(e) + current_connected_devices_count= 0 + info = { 'users_count': active_users + inactive_users, 'active_users_count': active_users, 'repos_count': repos_count, + 'total_files_count': total_files_count, 'groups_count': groups_count, 'org_count': org_count, 'multi_tenancy_enabled': multi_tenancy_enabled, @@ -116,6 +147,9 @@ class SysInfo(APIView): 'license_expiration': license_dict.get('Expiration', ''), 'license_maxusers': max_users, 'license_to': license_dict.get('Name', ''), + 'total_storage': total_storage, + 'total_devices_count': total_devices_count, + 'current_connected_devices_count': current_connected_devices_count, } return Response(info) diff --git a/seahub/api2/models.py b/seahub/api2/models.py index d1be29c1f0..625588d46b 100644 --- a/seahub/api2/models.py +++ b/seahub/api2/models.py @@ -2,7 +2,6 @@ import uuid import hmac import datetime -import time from hashlib import sha1 from django.db import models @@ -46,6 +45,19 @@ class TokenV2Manager(models.Manager): return devices + def get_total_devices_count(self): + devices = super(TokenV2Manager, self).filter(wiped_at=None) + return len(devices) + + def get_current_connected_devices_count(self): + # get number of devices last one hour accessed + devices = super(TokenV2Manager, self).filter(wiped_at=None) + date_from = datetime.datetime.now() - datetime.timedelta(hours=1) + + # greater than or equal to. + results = devices.filter(last_accessed__gte=date_from) + return len(results) + def get_user_devices(self, username): '''List user devices, most recently used first''' devices = super(TokenV2Manager, self).filter(user=username).filter(wiped_at=None) diff --git a/seahub/base/accounts.py b/seahub/base/accounts.py index 8ea8fbba07..143d81d409 100644 --- a/seahub/base/accounts.py +++ b/seahub/base/accounts.py @@ -19,7 +19,7 @@ from seahub.auth import login from seahub.constants import DEFAULT_USER from seahub.profile.models import Profile, DetailedProfile from seahub.role_permissions.utils import get_enabled_role_permissions_by_role -from seahub.utils import is_valid_username, is_user_password_strong, \ +from seahub.utils import is_user_password_strong, \ clear_token, get_system_admins from seahub.utils.mail import send_html_email_with_dj_template, MAIL_PRIORITY from seahub.utils.licenseparse import user_number_over_limit diff --git a/seahub/base/models.py b/seahub/base/models.py index 2f42ca88d3..603e9943f9 100644 --- a/seahub/base/models.py +++ b/seahub/base/models.py @@ -3,8 +3,7 @@ import os import datetime import hashlib import logging -import json -from django.db import models, IntegrityError +from django.db import models from django.utils import timezone from pysearpc import SearpcError diff --git a/seahub/templates/js/sysadmin-templates.html b/seahub/templates/js/sysadmin-templates.html index 9e29af56e8..b439566af2 100644 --- a/seahub/templates/js/sysadmin-templates.html +++ b/seahub/templates/js/sysadmin-templates.html @@ -106,8 +106,14 @@ <% } %> -
{% trans "Libraries" %}
-
<%- repos_count %>
+
{% trans "Libraries" %} / {% trans "Files" %}
+
<%- repos_count %> / <%- total_files_count %>
+ +
{% trans "Storage Used" %}
+
<%- formatted_storage %>
+ +
{% trans "Total Devices" %} / {% trans "Current Connected Devices" %}
+
<%- total_devices_count %> / <%- current_connected_devices_count %>
<% if (is_pro) { %>
{% trans "Active Users" %} / {% trans "Total Users" %} / {% trans "Limits" %}
diff --git a/static/scripts/common.js b/static/scripts/common.js index b42047d97c..2a30ab9950 100644 --- a/static/scripts/common.js +++ b/static/scripts/common.js @@ -903,6 +903,34 @@ define([ } }, + quotaSizeFormat: function(bytes, precision) { + var kilobyte = 1000; + var megabyte = kilobyte * 1000; + var gigabyte = megabyte * 1000; + var terabyte = gigabyte * 1000; + + var precision = precision || 0; + + if ((bytes >= 0) && (bytes < kilobyte)) { + return bytes + ' B'; + + } else if ((bytes >= kilobyte) && (bytes < megabyte)) { + return (bytes / kilobyte).toFixed(precision) + ' KB'; + + } else if ((bytes >= megabyte) && (bytes < gigabyte)) { + return (bytes / megabyte).toFixed(precision) + ' MB'; + + } else if ((bytes >= gigabyte) && (bytes < terabyte)) { + return (bytes / gigabyte).toFixed(precision) + ' GB'; + + } else if (bytes >= terabyte) { + return (bytes / terabyte).toFixed(precision) + ' TB'; + + } else { + return bytes + ' B'; + } + }, + groupId2Name: function(group_id) { var group_name; var groups = app.pageOptions.groups; diff --git a/static/scripts/sysadmin-app/views/dashboard.js b/static/scripts/sysadmin-app/views/dashboard.js index 9f7d94891e..ea704bdf7d 100644 --- a/static/scripts/sysadmin-app/views/dashboard.js +++ b/static/scripts/sysadmin-app/views/dashboard.js @@ -61,7 +61,9 @@ define([ reset: function() { this.$loadingTip.hide(); - this.$sysinfo.html(this.template(this.sysinfo.toJSON())); + var json_data = this.sysinfo.toJSON(); + json_data['formatted_storage'] = Common.quotaSizeFormat(json_data['total_storage'], 1) + this.$sysinfo.html(this.template(json_data)); } }); diff --git a/tests/api/endpoints/admin/test_sysinfo.py b/tests/api/endpoints/admin/test_sysinfo.py index 585d22be63..55395da0bb 100644 --- a/tests/api/endpoints/admin/test_sysinfo.py +++ b/tests/api/endpoints/admin/test_sysinfo.py @@ -22,7 +22,7 @@ class SysinfoTest(BaseTestCase): resp = self.client.get(url) json_resp = json.loads(resp.content) - assert len(json_resp) == 11 + assert len(json_resp) == 15 assert json_resp['is_pro'] is False assert json_resp['multi_tenancy_enabled'] is False assert json_resp['license_maxusers'] == 0 @@ -49,6 +49,6 @@ class SysinfoTest(BaseTestCase): resp = self.client.get(url) json_resp = json.loads(resp.content) - assert len(json_resp) == 11 + assert len(json_resp) == 15 assert json_resp['license_maxusers'] == 500 assert json_resp['license_to'] == test_user