mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-21 08:25:21 +00:00
add metrics api (#7539)
* add metrics api update * update * update * Update __init__.py --------- Co-authored-by: 孙永强 <11704063+s-yongqiang@user.noreply.gitee.com>
This commit is contained in:
parent
ebeafd74ab
commit
98e01baa8e
@ -11,7 +11,7 @@ from rest_framework.decorators import api_view
|
|||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from seahub.api2.authentication import TokenAuthentication
|
from seahub.api2.authentication import TokenAuthentication
|
||||||
from seahub.api2.endpoints.utils import check_time_period_valid, export_logs_to_excel, event_export_status
|
from seahub.api2.endpoints.utils import check_time_period_valid, export_logs_to_excel
|
||||||
from seahub.api2.permissions import IsProVersion
|
from seahub.api2.permissions import IsProVersion
|
||||||
from seahub.api2.throttling import UserRateThrottle
|
from seahub.api2.throttling import UserRateThrottle
|
||||||
from seahub.api2.utils import api_error
|
from seahub.api2.utils import api_error
|
||||||
|
@ -329,3 +329,11 @@ def delete_user_monitored_cache(params):
|
|||||||
url = urljoin(SEAFEVENTS_SERVER_URL, '/delete-repo-monitored-user-cache')
|
url = urljoin(SEAFEVENTS_SERVER_URL, '/delete-repo-monitored-user-cache')
|
||||||
resp = requests.post(url, json=params, headers=headers)
|
resp = requests.post(url, json=params, headers=headers)
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
|
def get_seafevents_metrics():
|
||||||
|
payload = {'exp': int(time.time()) + 300, }
|
||||||
|
token = jwt.encode(payload, SECRET_KEY, algorithm='HS256')
|
||||||
|
headers = {"Authorization": "Token %s" % token}
|
||||||
|
url = urljoin(SEAFEVENTS_SERVER_URL, '/metrics')
|
||||||
|
resp = requests.get(url, headers=headers)
|
||||||
|
return resp
|
||||||
|
@ -236,6 +236,7 @@ urlpatterns = [
|
|||||||
|
|
||||||
path('', react_fake_view, name='libraries'),
|
path('', react_fake_view, name='libraries'),
|
||||||
re_path(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
|
re_path(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
|
||||||
|
path('metrics/', get_metrics, name='metrics'),
|
||||||
|
|
||||||
# revert repo
|
# revert repo
|
||||||
re_path(r'^repo/history/revert/(?P<repo_id>[-0-9a-f]{36})/$', repo_revert_history, name='repo_revert_history'),
|
re_path(r'^repo/history/revert/(?P<repo_id>[-0-9a-f]{36})/$', repo_revert_history, name='repo_revert_history'),
|
||||||
|
@ -1565,4 +1565,3 @@ def transfer_repo(repo_id, new_owner, is_share, org_id=None):
|
|||||||
seafile_api.transfer_repo_to_group(repo_id, group_id, PERMISSION_READ_WRITE)
|
seafile_api.transfer_repo_to_group(repo_id, group_id, PERMISSION_READ_WRITE)
|
||||||
else:
|
else:
|
||||||
seafile_api.set_repo_owner(repo_id, new_owner)
|
seafile_api.set_repo_owner(repo_id, new_owner)
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
# Copyright (c) 2012-2016 Seafile Ltd.
|
# Copyright (c) 2012-2016 Seafile Ltd.
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
import base64
|
||||||
import os
|
import os
|
||||||
import stat
|
import stat
|
||||||
import json
|
import json
|
||||||
@ -18,7 +19,7 @@ from django.shortcuts import render, redirect
|
|||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
from django.views.decorators.http import condition
|
from django.views.decorators.http import condition
|
||||||
from django.http import HttpResponse, Http404, \
|
from django.http import HttpResponse, Http404, \
|
||||||
HttpResponseRedirect
|
HttpResponseRedirect, HttpResponseForbidden
|
||||||
|
|
||||||
import seaserv
|
import seaserv
|
||||||
from seaserv import get_repo, get_commits, \
|
from seaserv import get_repo, get_commits, \
|
||||||
@ -51,6 +52,7 @@ from seahub.utils.repo import get_library_storages, parse_repo_perm, is_repo_adm
|
|||||||
from seahub.utils.file_op import check_file_lock
|
from seahub.utils.file_op import check_file_lock
|
||||||
from seahub.utils.timeutils import utc_to_local
|
from seahub.utils.timeutils import utc_to_local
|
||||||
from seahub.utils.auth import get_login_bg_image_path
|
from seahub.utils.auth import get_login_bg_image_path
|
||||||
|
from seahub.api2.endpoints.utils import get_seafevents_metrics
|
||||||
import seahub.settings as settings
|
import seahub.settings as settings
|
||||||
from seahub.settings import AVATAR_FILE_STORAGE, ENABLE_REPO_SNAPSHOT_LABEL, \
|
from seahub.settings import AVATAR_FILE_STORAGE, ENABLE_REPO_SNAPSHOT_LABEL, \
|
||||||
SHARE_LINK_EXPIRE_DAYS_MIN, ENABLE_METADATA_MANAGEMENT, \
|
SHARE_LINK_EXPIRE_DAYS_MIN, ENABLE_METADATA_MANAGEMENT, \
|
||||||
@ -73,6 +75,10 @@ from seahub.organizations.models import OrgAdminSettings, DISABLE_ORG_USER_CLEAN
|
|||||||
LIBRARY_TEMPLATES = getattr(settings, 'LIBRARY_TEMPLATES', {})
|
LIBRARY_TEMPLATES = getattr(settings, 'LIBRARY_TEMPLATES', {})
|
||||||
CUSTOM_NAV_ITEMS = getattr(settings, 'CUSTOM_NAV_ITEMS', '')
|
CUSTOM_NAV_ITEMS = getattr(settings, 'CUSTOM_NAV_ITEMS', '')
|
||||||
|
|
||||||
|
ENABLE_METRIC = getattr(settings, 'ENABLE_METRIC', False)
|
||||||
|
METRIC_AUTH_USER = getattr(settings, 'METRIC_AUTH_USER', None)
|
||||||
|
METRIC_AUTH_PWD = getattr(settings, 'METRIC_AUTH_PWD', None)
|
||||||
|
|
||||||
|
|
||||||
# Get an instance of a logger
|
# Get an instance of a logger
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -1158,3 +1164,25 @@ def react_fake_view(request, **kwargs):
|
|||||||
return_dict['google_map_id'] = settings.GOOGLE_MAP_ID
|
return_dict['google_map_id'] = settings.GOOGLE_MAP_ID
|
||||||
|
|
||||||
return render(request, "react_app.html", return_dict)
|
return render(request, "react_app.html", return_dict)
|
||||||
|
|
||||||
|
|
||||||
|
def check_metric_auth(auth_header):
|
||||||
|
try:
|
||||||
|
auth_decoded = base64.b64decode(auth_header.split(' ')[1]).decode('utf-8')
|
||||||
|
username, password = auth_decoded.split(':')
|
||||||
|
if username == METRIC_AUTH_USER and password == METRIC_AUTH_PWD:
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(e)
|
||||||
|
return False
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_metrics(request):
|
||||||
|
if not ENABLE_METRIC:
|
||||||
|
return Http404
|
||||||
|
auth_header = request.META.get('HTTP_AUTHORIZATION')
|
||||||
|
if not auth_header or not check_metric_auth(auth_header):
|
||||||
|
return HttpResponseForbidden('Invalid Authentication')
|
||||||
|
metrics = get_seafevents_metrics()
|
||||||
|
return HttpResponse(metrics, content_type='text/plain')
|
||||||
|
Loading…
Reference in New Issue
Block a user