jumpserver/apps/terminal/backends/command/db.py

117 lines
3.8 KiB
Python
Raw Normal View History

2017-11-29 11:27:04 +00:00
# ~*~ coding: utf-8 ~*~
import datetime
from django.db import transaction
2017-11-29 11:27:04 +00:00
from django.utils import timezone
from django.db.utils import OperationalError
2017-11-29 11:27:04 +00:00
from .base import CommandBase
class CommandStore(CommandBase):
2018-01-20 14:22:09 +00:00
def __init__(self, params):
from terminal.models import Command
self.model = Command
2017-11-29 11:27:04 +00:00
def save(self, command):
"""
保存命令到数据库
"""
self.model.objects.create(
user=command["user"], asset=command["asset"],
system_user=command["system_user"], input=command["input"],
output=command["output"], session=command["session"],
org_id=command["org_id"], timestamp=command["timestamp"]
2017-11-29 11:27:04 +00:00
)
def bulk_save(self, commands):
"""
批量保存命令到数据库, command的顺序和save中一致
"""
_commands = []
for c in commands:
_commands.append(self.model(
user=c["user"], asset=c["asset"], system_user=c["system_user"],
input=c["input"], output=c["output"], session=c["session"],
org_id=c["org_id"], timestamp=c["timestamp"]
2017-11-29 11:27:04 +00:00
))
error = False
try:
with transaction.atomic():
self.model.objects.bulk_create(_commands)
except OperationalError:
error = True
except:
return False
if not error:
return True
for command in _commands:
try:
with transaction.atomic():
command.save()
except OperationalError:
command.output = str(command.output.encode())
command.save()
return True
2017-11-29 11:27:04 +00:00
2018-01-20 14:22:09 +00:00
@staticmethod
def make_filter_kwargs(
date_from=None, date_to=None,
user=None, asset=None, system_user=None,
input=None, session=None):
2017-11-29 11:27:04 +00:00
filter_kwargs = {}
date_from_default = timezone.now() - datetime.timedelta(days=7)
date_to_default = timezone.now()
if not date_from and not session:
date_from = date_from_default
if not date_to and not session:
date_to = date_to_default
if date_from is not None:
2019-07-03 08:29:39 +00:00
if isinstance(date_from, datetime.datetime):
date_from = date_from.timestamp()
filter_kwargs['timestamp__gte'] = int(date_from)
if date_to is not None:
2019-07-03 08:29:39 +00:00
if isinstance(date_to, datetime.datetime):
date_to = date_to.timestamp()
filter_kwargs['timestamp__lte'] = int(date_to)
2017-11-29 11:27:04 +00:00
if user:
filter_kwargs["user"] = user
2017-11-29 11:27:04 +00:00
if asset:
filter_kwargs['asset'] = asset
if system_user:
filter_kwargs['system_user'] = system_user
if input:
filter_kwargs['input__icontains'] = input
2017-11-29 11:27:04 +00:00
if session:
filter_kwargs['session'] = session
2018-01-20 14:22:09 +00:00
return filter_kwargs
def filter(self, date_from=None, date_to=None,
user=None, asset=None, system_user=None,
input=None, session=None):
filter_kwargs = self.make_filter_kwargs(
date_from=date_from, date_to=date_to, user=user,
asset=asset, system_user=system_user, input=input,
session=session,
)
2017-11-29 11:27:04 +00:00
queryset = self.model.objects.filter(**filter_kwargs)
2018-03-28 07:47:20 +00:00
return queryset
2017-11-29 11:27:04 +00:00
2018-01-20 14:22:09 +00:00
def count(self, date_from=None, date_to=None,
2018-03-28 07:47:20 +00:00
user=None, asset=None, system_user=None,
input=None, session=None):
2018-01-20 14:22:09 +00:00
filter_kwargs = self.make_filter_kwargs(
date_from=date_from, date_to=date_to, user=user,
asset=asset, system_user=system_user, input=input,
session=session,
)
count = self.model.objects.filter(**filter_kwargs).count()
return count
2017-11-29 11:27:04 +00:00