Browse Source

fix: 会话里查不到命令记录

pull/5766/head
xinwen 4 years ago committed by Jiangjie.Bai
parent
commit
36c083f674
  1. 27
      apps/terminal/api/command.py
  2. 18
      apps/terminal/backends/command/es.py
  3. 5
      apps/terminal/filters.py

27
apps/terminal/api/command.py

@ -111,6 +111,33 @@ class CommandViewSet(viewsets.ModelViewSet):
filterset_class = CommandFilter
ordering_fields = ('timestamp', )
def merge_all_storage_list(self, request, *args, **kwargs):
merged_commands = []
storages = CommandStorage.objects.all()
for storage in storages:
qs = storage.get_command_queryset()
commands = self.filter_queryset(qs)
merged_commands.extend(commands)
merged_commands.sort(key=lambda command: command.timestamp, reverse=True)
page = self.paginate_queryset(merged_commands)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
def list(self, request, *args, **kwargs):
command_storage_id = self.request.query_params.get('command_storage_id')
session_id = self.request.query_params.get('session_id')
if session_id and not command_storage_id:
# 会话里的命令列表肯定会提供 session_id,这里防止 merge 的时候取全量的数据
return self.merge_all_storage_list(request, *args, **kwargs)
return super().list(request, *args, **kwargs)
def get_queryset(self):
command_storage_id = self.request.query_params.get('command_storage_id')
storage = CommandStorage.objects.get(id=command_storage_id)

18
apps/terminal/backends/command/es.py

@ -117,11 +117,21 @@ class CommandStore():
timestamp_range['lte'] = timestamp__lte
# 处理组织
must_not = []
should = []
org_id = match.get('org_id')
if org_id == '':
real_default_org_id = '00000000-0000-0000-0000-000000000002'
if org_id in (real_default_org_id, ''):
match.pop('org_id')
must_not.append({'wildcard': {'org_id': '*'}})
should.append({
'bool':{
'must_not': [
{
'wildcard': {'org_id': '*'}
}
]}
})
should.append({'match': {'org_id': real_default_org_id}})
# 构建 body
body = {
@ -130,7 +140,7 @@ class CommandStore():
'must': [
{'match': {k: v}} for k, v in match.items()
],
'must_not': must_not,
'should': should,
'filter': [
{
'term': {k: v}

5
apps/terminal/filters.py

@ -48,10 +48,7 @@ class CommandFilter(filters.FilterSet):
@staticmethod
def get_org_id():
if current_org.is_default():
org_id = ''
else:
org_id = current_org.id
org_id = current_org.id
return org_id

Loading…
Cancel
Save