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>
This commit is contained in:
fit2bot
2023-08-01 16:14:40 +08:00
committed by GitHub
parent 681988f450
commit d17e2cde06
5 changed files with 51 additions and 1 deletions

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