Skip to content

Commit

Permalink
optimze and reconstruct code (#5807)
Browse files Browse the repository at this point in the history
* optimze and reconstruct code

* uupdate

* fix question answer

* get diff files in seafile ai

* seafile_ai_read_commit_id_from_db
  • Loading branch information
JoinTyang authored Dec 4, 2023
1 parent 389d3a3 commit 59cd78a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 109 deletions.
101 changes: 34 additions & 67 deletions seahub/ai/apis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import os
import logging

from rest_framework.authentication import SessionAuthentication
Expand All @@ -14,8 +13,9 @@

from seahub.views import check_folder_permission
from seahub.utils.repo import parse_repo_perm
from seahub.ai.utils import create_library_sdoc_index, get_sdoc_info_recursively, similarity_search_in_library, \
update_library_sdoc_index, delete_library_index, query_task_status, query_library_index_state, question_answering_search_in_library
from seahub.ai.utils import create_library_sdoc_index, similarity_search_in_library, update_library_sdoc_index, \
delete_library_index, query_task_status, query_library_index_state, question_answering_search_in_library,\
get_file_download_token

from seaserv import seafile_api

Expand All @@ -39,24 +39,14 @@ def post(self, request):
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

parent_dir = '/'
username = request.user.username

# permission check
if parse_repo_perm(check_folder_permission(request, repo_id, parent_dir)).can_download is False:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, [])
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

params = {
'repo_id': repo_id,
'last_modify': repo.last_modify,
'sdoc_info_list': sdoc_info_list
}

try:
Expand Down Expand Up @@ -92,21 +82,9 @@ def post(self, request):
if not repo_id:
return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid')

parent_dir = '/'
username = request.user.username

try:
sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, [])
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

sdoc_files_info = {file.get('path'): file for file in sdoc_info_list}
params = {
'query': query,
'associate_id': repo_id,
'sdoc_files_info': sdoc_files_info,
'repo_id': repo_id,
'count': count,
}

Expand All @@ -122,8 +100,8 @@ def post(self, request):

return Response(resp_json, resp.status_code)

class QuestionAnsweringSearchInLibrary(APIView):

class QuestionAnsweringSearchInLibrary(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated, )
throttle_classes = (UserRateThrottle, )
Expand All @@ -143,21 +121,9 @@ def post(self, request):
if not repo_id:
return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid')

parent_dir = '/'
username = request.user.username

try:
sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, [])
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

sdoc_files_info = {file.get('path'): file for file in sdoc_info_list}
params = {
'query': query,
'associate_id': repo_id,
'sdoc_files_info': sdoc_files_info,
'repo_id': repo_id,
'count': count,
}

Expand All @@ -173,6 +139,7 @@ def post(self, request):

return Response(resp_json, resp.status_code)


class LibrarySdocIndex(APIView):
authentication_classes = (TokenAuthentication, SessionAuthentication)
permission_classes = (IsAuthenticated, )
Expand All @@ -190,24 +157,14 @@ def put(self, request):
return api_error(status.HTTP_404_NOT_FOUND, error_msg)

parent_dir = '/'
username = request.user.username

# permission check
if parse_repo_perm(check_folder_permission(request, repo_id, parent_dir)).can_download is False:
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, [])
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

params = {
'associate_id': repo_id,
'last_modify': repo.last_modify,
'sdoc_info_list': sdoc_info_list,
'repo_id': repo_id
}

try:
Expand Down Expand Up @@ -286,34 +243,44 @@ def get(self, request):
return Response(resp_json, resp.status_code)


class RepoFiles(APIView):
class FileDownloadToken(APIView):
authentication_classes = (SeafileAiAuthentication, )
throttle_classes = (UserRateThrottle, )

def get(self, request):
repo_id = request.GET.get('repo_id')
path = request.GET.get('path')

if not repo_id:
return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid')

repo = seafile_api.get_repo(repo_id)
if not repo:
error_msg = 'Library %s not found.' % repo_id
return api_error(status.HTTP_404_NOT_FOUND, error_msg)
if not path:
return api_error(status.HTTP_400_BAD_REQUEST, 'path invalid')

parent_dir = '/'
file_id = seafile_api.get_file_id_by_path(repo_id, path)
username = request.user.username

try:
sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, [])
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)
download_token = get_file_download_token(repo_id, file_id, username)

library_files_info = {
'associate_id': repo_id,
'last_modify': repo.last_modify,
'sdoc_info_list': sdoc_info_list,
'download_token': download_token
}

return Response(library_files_info, status.HTTP_200_OK)


# class RepoCommit(APIView):
# authentication_classes = (SeafileAiAuthentication, )
# throttle_classes = (UserRateThrottle, )
#
# def get(self, request):
# repo_id = request.GET.get('repo_id')
# if not repo_id:
# return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid')
#
# commit_id = get_latest_commit_id(repo_id)
#
# repo_info = {
# 'commit_id': commit_id
# }
#
# return Response(repo_info, status.HTTP_200_OK)
47 changes: 7 additions & 40 deletions seahub/ai/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import os
import logging
import requests
import jwt
import time
import stat
import posixpath
from urllib.parse import urljoin

from seahub.settings import SEAFILE_AI_SERVER_URL, SEAFILE_AI_SECRET_KEY
Expand All @@ -21,40 +18,6 @@ def gen_headers():
return {"Authorization": "Token %s" % token}


def get_sdoc_info_recursively(username, repo_id, path, sdoc_info_list, include_sys_dir=False):
dirs = seafile_api.list_dir_by_path(repo_id, path)
for dirent in dirs:

if stat.S_ISDIR(dirent.mode):
if not include_sys_dir and path =='/' and dirent.obj_name in ['images', 'Revisions']:
continue

sub_path = posixpath.join(path, dirent.obj_name)
get_sdoc_info_recursively(username, repo_id, sub_path, sdoc_info_list)
continue

sdoc_parent_dir = path
filename = dirent.obj_name
file_id = dirent.obj_id
mtime = dirent.mtime
size = dirent.size
if not filename.endswith('.sdoc'):
continue

sdoc_path = os.path.join(sdoc_parent_dir, filename)
download_token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'download', username,
use_onetime=True)
sdoc_info = {
'path': sdoc_path,
'download_token': download_token,
'mtime': mtime,
'size': size,
}
sdoc_info_list.append(sdoc_info)

return sdoc_info_list


def create_library_sdoc_index(params):
headers = gen_headers()
url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/library-sdoc-indexes/')
Expand Down Expand Up @@ -84,7 +47,7 @@ def update_library_sdoc_index(params):
def delete_library_index(repo_id):
headers = gen_headers()
url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/library-sdoc-index/')
params = {'associate_id': repo_id}
params = {'repo_id': repo_id}
resp = requests.delete(url, headers=headers, json=params)
return resp

Expand All @@ -96,8 +59,12 @@ def query_task_status(task_id):
return resp


def query_library_index_state(associate_id):
def query_library_index_state(repo_id):
headers = gen_headers()
url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/library-index-state/')
resp = requests.get(url, headers=headers, params={'associate_id': associate_id})
resp = requests.get(url, headers=headers, params={'repo_id': repo_id})
return resp


def get_file_download_token(repo_id, file_id, username):
return seafile_api.get_fileserver_access_token(repo_id, file_id, 'download', username, use_onetime=True)
3 changes: 1 addition & 2 deletions seahub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@

from seahub.ocm.settings import OCM_ENDPOINT

from seahub.ai.apis import LibrarySdocIndexes, SimilaritySearchInLibrary, LibrarySdocIndex, RepoFiles, TaskStatus, \
from seahub.ai.apis import LibrarySdocIndexes, SimilaritySearchInLibrary, LibrarySdocIndex, TaskStatus, \
LibraryIndexState, QuestionAnsweringSearchInLibrary

urlpatterns = [
Expand Down Expand Up @@ -968,7 +968,6 @@
re_path(r'^api/v2.1/ai/similarity-search-in-library/$', SimilaritySearchInLibrary.as_view(), name='api-v2.1-ai-similarity-search-in-library'),
re_path(r'^api/v2.1/ai/question-answering-search-in-library/$', QuestionAnsweringSearchInLibrary.as_view(), name='api-v2.1-ai-question-answering-search-in-library'),
re_path(r'^api/v2.1/ai/library-sdoc-index/$', LibrarySdocIndex.as_view(), name='api-v2.1-ai-library-sdoc-index'),
re_path(r'^api/v2.1/ai/repo/files/$', RepoFiles.as_view(), name='api-v2.1-ai-repo-files'),
re_path(r'^api/v2.1/ai/task-status/$', TaskStatus.as_view(), name='api-v2.1-ai-task-status'),
re_path(r'^api/v2.1/ai/library-index-state/$', LibraryIndexState.as_view(), name='api-v2.1-ai-library-index-state'),
]

0 comments on commit 59cd78a

Please sign in to comment.