From d17e2cde060afe727f8c9ba09bf054bd35f2e901 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 1 Aug 2023 16:14:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=88=E7=AB=AF=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=97=E6=AE=B5:=20cmd=5Famount(=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E6=95=B0=E9=87=8F)=20(#11136)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 终端会话增加字段: command_amount(命令数量) * perf: 优化已产生会话的命令数量计算方式 * Update 0065_session_command_amount.py * Update session.py * Update session.py * perf: 优化会话命令数量的计算逻辑 * perf: 优化命令数量获取 --------- Co-authored-by: fangfang.dong Co-authored-by: Bai --- .../migrations/0065_session_command_amount.py | 18 +++++++++++++++++ apps/terminal/models/session/session.py | 20 +++++++++++++++++++ apps/terminal/serializers/session.py | 3 ++- apps/terminal/signal_handlers/__init__.py | 1 + apps/terminal/signal_handlers/session.py | 10 ++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 apps/terminal/migrations/0065_session_command_amount.py create mode 100644 apps/terminal/signal_handlers/session.py diff --git a/apps/terminal/migrations/0065_session_command_amount.py b/apps/terminal/migrations/0065_session_command_amount.py new file mode 100644 index 000000000..1733d7a19 --- /dev/null +++ b/apps/terminal/migrations/0065_session_command_amount.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.10 on 2023-07-31 10:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('terminal', '0064_auto_20230728_1001'), + ] + + operations = [ + migrations.AddField( + model_name='session', + name='cmd_amount', + field=models.IntegerField(default=-1, verbose_name='Command amount'), + ), + ] diff --git a/apps/terminal/models/session/session.py b/apps/terminal/models/session/session.py index 35c11727e..02c0a0f95 100644 --- a/apps/terminal/models/session/session.py +++ b/apps/terminal/models/session/session.py @@ -44,6 +44,7 @@ class Session(OrgModelMixin): date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True, default=timezone.now) date_end = models.DateTimeField(verbose_name=_("Date end"), null=True) comment = models.TextField(blank=True, null=True, verbose_name=_("Comment")) + cmd_amount = models.IntegerField(default=-1, verbose_name=_("Command amount")) upload_to = 'replay' ACTIVE_CACHE_KEY_PREFIX = 'SESSION_ACTIVE_{}' @@ -177,6 +178,25 @@ class Session(OrgModelMixin): @property def command_amount(self): + if self.need_update_cmd_amount: + cmd_amount = self.compute_command_amount() + self.cmd_amount = cmd_amount + self.save() + elif self.need_compute_cmd_amount: + cmd_amount = self.compute_command_amount() + else: + cmd_amount = self.cmd_amount + return cmd_amount + + @property + def need_update_cmd_amount(self): + return self.is_finished and self.need_compute_cmd_amount + + @property + def need_compute_cmd_amount(self): + return self.cmd_amount == -1 + + def compute_command_amount(self): command_store = get_multi_command_storage() return command_store.count(session=str(self.id)) diff --git a/apps/terminal/serializers/session.py b/apps/terminal/serializers/session.py index 999028133..072ddbb3f 100644 --- a/apps/terminal/serializers/session.py +++ b/apps/terminal/serializers/session.py @@ -30,7 +30,8 @@ class SessionSerializer(BulkOrgResourceModelSerializer): "user", "asset", "user_id", "asset_id", 'account', 'account_id', "protocol", 'type', "login_from", "remote_addr", "is_success", "is_finished", "has_replay", "has_command", - "date_start", "date_end", "comment", "terminal_display" + "date_start", "date_end", "comment", "terminal_display", + 'command_amount', ] fields_fk = ["terminal", ] fields_custom = ["can_replay", "can_join", "can_terminate"] diff --git a/apps/terminal/signal_handlers/__init__.py b/apps/terminal/signal_handlers/__init__.py index 5ba51e77a..0021ba500 100644 --- a/apps/terminal/signal_handlers/__init__.py +++ b/apps/terminal/signal_handlers/__init__.py @@ -2,3 +2,4 @@ from .applet import * from .db_port import * from .terminal import * from .session_sharing import * +from .session import * diff --git a/apps/terminal/signal_handlers/session.py b/apps/terminal/signal_handlers/session.py new file mode 100644 index 000000000..566173678 --- /dev/null +++ b/apps/terminal/signal_handlers/session.py @@ -0,0 +1,10 @@ +from django.db.models.signals import pre_save +from django.dispatch import receiver + +from terminal.models import Session + + +@receiver(pre_save, sender=Session) +def on_session_pre_save(sender, instance, **kwargs): + if instance.need_update_cmd_amount: + instance.cmd_amount = instance.compute_command_amount()