Skip to content

Commit

Permalink
client error log improvement (#6020)
Browse files Browse the repository at this point in the history
* client error log improvement

* Update device_errors.py

---------

Co-authored-by: 孙永强 <11704063+s-yongqiang@user.noreply.gitee.com>
Co-authored-by: r350178982 <32759763+r350178982@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 0a417c0 commit 9ae5af8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
31 changes: 17 additions & 14 deletions seahub/api2/endpoints/admin/device_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
from seahub.utils import is_pro_version

from seahub.utils.db_api import SeafileDB

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -47,8 +49,9 @@ def get(self, request, format=None):

return_results = []
try:
device_errors = seafile_api.list_repo_sync_errors(start, limit)
except SearpcError as e:
seafile_db = SeafileDB()
device_errors = seafile_db.get_devices_error(start, limit)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
Expand All @@ -58,26 +61,26 @@ def get(self, request, format=None):
has_next_page = True
else:
has_next_page = False

for error in device_errors:
result = {}
result['email'] = error.email if error.email else ''
result['name'] = email2nickname(error.email)
result['device_ip'] = error.peer_ip if error.peer_ip else ''
result['repo_name'] = error.repo_name if error.repo_name else ''
result['repo_id'] = error.repo_id if error.repo_id else ''
result['error_msg'] = error.error_con if error.error_con else ''

tokens = TokenV2.objects.filter(device_id=error.peer_id)
result = dict()
result['email'] = error.get('email', '')
result['name'] = email2nickname(error.get('email', ''))
result['device_ip'] = error.get('peer_ip', '')
result['repo_name'] = error.get('repo_name', '')
result['repo_id'] = error.get('repo_id', '')
result['error_msg'] = error.get('error_con', '')

tokens = TokenV2.objects.filter(device_id=error.get('peer_id', ''))
if tokens:
result['device_name'] = tokens[0].device_name
result['client_version'] = tokens[0].client_version
else:
result['device_name'] = ''
result['client_version'] = ''

if error.error_time:
result['error_time'] = timestamp_to_isoformat_timestr(error.error_time)
error_time = error.get('error_time')
if error_time:
result['error_time'] = timestamp_to_isoformat_timestr(error_time)
else:
result['error_time'] = ''

Expand Down
56 changes: 56 additions & 0 deletions seahub/utils/db_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,59 @@ def get_folder_group_share_list(self, repo_id, org_id=''):
share_info_list.append(info)

return share_info_list

def get_devices_error(self, start, limit):

if start == -1 and limit == -1:
sql = f"""
SELECT
u.repo_id, o.owner_id, u.email, e.token,
p.peer_id, p.peer_ip, p.peer_name, p.sync_time, p.client_ver, e.error_time, e.error_con, i.name
FROM
`{self.db_name}`.`RepoSyncError` e
LEFT JOIN `{self.db_name}`.`RepoUserToken` u ON e.token = u.token
LEFT JOIN `{self.db_name}`.`RepoInfo` i ON u.repo_id = i.repo_id
LEFT JOIN `{self.db_name}`.`RepoTokenPeerInfo` p ON e.token = p.token
CROSS JOIN `{self.db_name}`.`RepoOwner` o
WHERE
u.repo_id = o.repo_id
ORDER BY
e.error_time DESC
"""
else:
sql =f"""
SELECT
u.repo_id, o.owner_id, u.email, e.token,
p.peer_id, p.peer_ip, p.peer_name, p.sync_time, p.client_ver, e.error_time, e.error_con, i.name
FROM
`{self.db_name}`.`RepoSyncError` e
LEFT JOIN `{self.db_name}`.`RepoUserToken` u ON e.token = u.token
LEFT JOIN `{self.db_name}`.`RepoInfo` i ON u.repo_id = i.repo_id
LEFT JOIN `{self.db_name}`.`RepoTokenPeerInfo` p ON e.token = p.token
CROSS JOIN `{self.db_name}`.`RepoOwner` o
WHERE
u.repo_id = o.repo_id
ORDER BY
e.error_time DESC
LIMIT {limit} OFFSET {start}
"""

device_errors = []
with connection.cursor() as cursor:
cursor.execute(sql)
for item in cursor.fetchall():
info = {}
info['repo_id'] = item[0]
info['email'] = item[2]
info['peer_id'] = item[4]
info['peer_ip'] = item[5]
info['device_name'] = item[6]
info['client_version'] = item[8]
info['error_time'] = item[9]
info['error_con'] = item[10]
info['repo_name'] = item[11]

device_errors.append(info)

return device_errors

0 comments on commit 9ae5af8

Please sign in to comment.