1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-28 03:10:45 +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:
awu0403 2025-03-07 16:42:23 +08:00 committed by GitHub
parent ebeafd74ab
commit 98e01baa8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 39 additions and 3 deletions

View File

@ -11,7 +11,7 @@ from rest_framework.decorators import api_view
from urllib.parse import quote
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.throttling import UserRateThrottle
from seahub.api2.utils import api_error

View File

@ -329,3 +329,11 @@ def delete_user_monitored_cache(params):
url = urljoin(SEAFEVENTS_SERVER_URL, '/delete-repo-monitored-user-cache')
resp = requests.post(url, json=params, headers=headers)
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

View File

@ -236,6 +236,7 @@ urlpatterns = [
path('', react_fake_view, name='libraries'),
re_path(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
path('metrics/', get_metrics, name='metrics'),
# revert repo
re_path(r'^repo/history/revert/(?P<repo_id>[-0-9a-f]{36})/$', repo_revert_history, name='repo_revert_history'),

View File

@ -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)
else:
seafile_api.set_repo_owner(repo_id, new_owner)

View File

@ -1,5 +1,6 @@
# Copyright (c) 2012-2016 Seafile Ltd.
# encoding: utf-8
import base64
import os
import stat
import json
@ -18,7 +19,7 @@ from django.shortcuts import render, redirect
from django.utils.translation import gettext as _
from django.views.decorators.http import condition
from django.http import HttpResponse, Http404, \
HttpResponseRedirect
HttpResponseRedirect, HttpResponseForbidden
import seaserv
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.timeutils import utc_to_local
from seahub.utils.auth import get_login_bg_image_path
from seahub.api2.endpoints.utils import get_seafevents_metrics
import seahub.settings as settings
from seahub.settings import AVATAR_FILE_STORAGE, ENABLE_REPO_SNAPSHOT_LABEL, \
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', {})
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
logger = logging.getLogger(__name__)
@ -1158,3 +1164,25 @@ def react_fake_view(request, **kwargs):
return_dict['google_map_id'] = settings.GOOGLE_MAP_ID
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')