2018-01-11 12:10:27 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2018-10-23 11:22:18 +00:00
|
|
|
|
import os
|
2019-01-15 02:23:30 +00:00
|
|
|
|
import uuid
|
2018-01-12 07:43:26 +00:00
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
from django.core.cache import cache
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2018-10-23 11:22:18 +00:00
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework import generics, serializers
|
2020-05-08 08:48:26 +00:00
|
|
|
|
from rest_framework.viewsets import GenericViewSet
|
2019-05-21 08:24:01 +00:00
|
|
|
|
|
2020-06-05 06:43:40 +00:00
|
|
|
|
from common.permissions import IsValidUser
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from .http import HttpResponseTemporaryRedirect
|
2021-03-08 02:08:51 +00:00
|
|
|
|
from .const import KEY_CACHE_RESOURCE_IDS
|
2019-08-31 04:07:03 +00:00
|
|
|
|
from .utils import get_logger
|
2020-05-08 08:48:26 +00:00
|
|
|
|
from .mixins import CommonApiMixin
|
2019-05-21 08:24:01 +00:00
|
|
|
|
|
|
|
|
|
__all__ = [
|
2020-06-04 12:00:39 +00:00
|
|
|
|
'LogTailApi', 'ResourcesIDCacheApi', 'CommonGenericViewSet'
|
2019-05-21 08:24:01 +00:00
|
|
|
|
]
|
|
|
|
|
|
2019-08-31 04:07:03 +00:00
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
2019-02-26 04:38:20 +00:00
|
|
|
|
class OutputSerializer(serializers.Serializer):
|
|
|
|
|
output = serializers.CharField()
|
|
|
|
|
is_end = serializers.BooleanField()
|
|
|
|
|
mark = serializers.CharField()
|
2018-04-01 15:45:37 +00:00
|
|
|
|
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
class LogTailApi(generics.RetrieveAPIView):
|
2020-06-05 06:43:40 +00:00
|
|
|
|
permission_classes = (IsValidUser,)
|
2019-01-15 02:23:30 +00:00
|
|
|
|
buff_size = 1024 * 10
|
|
|
|
|
serializer_class = OutputSerializer
|
|
|
|
|
end = False
|
2019-02-20 09:51:53 +00:00
|
|
|
|
mark = ''
|
|
|
|
|
log_path = ''
|
2018-04-02 07:54:49 +00:00
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
def is_file_finish_write(self):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def get_log_path(self):
|
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
2019-02-20 09:51:53 +00:00
|
|
|
|
def get_no_file_message(self, request):
|
|
|
|
|
return 'Not found the log'
|
|
|
|
|
|
2019-02-13 07:55:11 +00:00
|
|
|
|
def filter_line(self, line):
|
|
|
|
|
"""
|
|
|
|
|
过滤行,可能替换一些信息
|
|
|
|
|
:param line:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
return line
|
|
|
|
|
|
2019-02-20 09:51:53 +00:00
|
|
|
|
def read_from_file(self):
|
2019-06-19 03:31:38 +00:00
|
|
|
|
with open(self.log_path, 'rt', encoding='utf8') as f:
|
2019-02-20 09:51:53 +00:00
|
|
|
|
offset = cache.get(self.mark, 0)
|
2019-01-15 02:23:30 +00:00
|
|
|
|
f.seek(offset)
|
|
|
|
|
data = f.read(self.buff_size).replace('\n', '\r\n')
|
2019-02-13 07:55:11 +00:00
|
|
|
|
|
2019-02-20 09:51:53 +00:00
|
|
|
|
new_mark = str(uuid.uuid4())
|
|
|
|
|
cache.set(new_mark, f.tell(), 5)
|
2019-01-15 02:23:30 +00:00
|
|
|
|
|
|
|
|
|
if data == '' and self.is_file_finish_write():
|
|
|
|
|
self.end = True
|
2019-02-13 07:55:11 +00:00
|
|
|
|
_data = ''
|
|
|
|
|
for line in data.split('\r\n'):
|
|
|
|
|
new_line = self.filter_line(line)
|
|
|
|
|
if line == '':
|
|
|
|
|
continue
|
|
|
|
|
_data += new_line + '\r\n'
|
2019-02-20 09:51:53 +00:00
|
|
|
|
return _data, self.end, new_mark
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.mark = request.query_params.get("mark") or str(uuid.uuid4())
|
|
|
|
|
self.log_path = self.get_log_path()
|
|
|
|
|
|
|
|
|
|
if not self.log_path or not os.path.isfile(self.log_path):
|
|
|
|
|
msg = self.get_no_file_message(self.request)
|
|
|
|
|
return Response({"data": msg}, status=200)
|
|
|
|
|
|
|
|
|
|
data, end, new_mark = self.read_from_file()
|
|
|
|
|
return Response({"data": data, 'end': end, 'mark': new_mark})
|
2019-05-21 08:24:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResourcesIDCacheApi(APIView):
|
2020-06-05 06:43:40 +00:00
|
|
|
|
permission_classes = (IsValidUser,)
|
2020-06-05 02:42:03 +00:00
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
|
spm = str(uuid.uuid4())
|
2020-06-05 02:42:03 +00:00
|
|
|
|
resources = request.data.get('resources')
|
|
|
|
|
if resources is not None:
|
2021-03-08 02:08:51 +00:00
|
|
|
|
cache_key = KEY_CACHE_RESOURCE_IDS.format(spm)
|
2020-06-05 02:42:03 +00:00
|
|
|
|
cache.set(cache_key, resources, 300)
|
2019-05-21 08:24:01 +00:00
|
|
|
|
return Response({'spm': spm})
|
2019-07-11 10:12:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
|
|
def redirect_plural_name_api(request, *args, **kwargs):
|
|
|
|
|
resource = kwargs.get("resource", "")
|
2019-08-31 04:07:03 +00:00
|
|
|
|
org_full_path = request.get_full_path()
|
|
|
|
|
full_path = org_full_path.replace(resource, resource+"s", 1)
|
|
|
|
|
logger.debug("Redirect {} => {}".format(org_full_path, full_path))
|
2019-07-11 10:12:14 +00:00
|
|
|
|
return HttpResponseTemporaryRedirect(full_path)
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommonGenericViewSet(CommonApiMixin, GenericViewSet):
|
|
|
|
|
pass
|