feat: 终端会话增加字段: cmd_amount(命令数量) (#11136)

* feat: 终端会话增加字段: command_amount(命令数量)

* perf: 优化已产生会话的命令数量计算方式

* Update 0065_session_command_amount.py

* Update session.py

* Update session.py

* perf: 优化会话命令数量的计算逻辑

* perf: 优化命令数量获取

---------

Co-authored-by: fangfang.dong <fangfang.dong@fit2cloud.com>
Co-authored-by: Bai <baijiangjie@gmail.com>
pull/11146/head
fit2bot 2023-08-01 16:14:40 +08:00 committed by GitHub
parent 681988f450
commit d17e2cde06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 1 deletions

View File

@ -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'),
),
]

View File

@ -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))

View File

@ -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"]

View File

@ -2,3 +2,4 @@ from .applet import *
from .db_port import *
from .terminal import *
from .session_sharing import *
from .session import *

View File

@ -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()