diff --git a/apps/terminal/models/session/session.py b/apps/terminal/models/session/session.py index 3d898e5eb..388250419 100644 --- a/apps/terminal/models/session/session.py +++ b/apps/terminal/models/session/session.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import os import uuid +from datetime import timedelta from django.conf import settings from django.core.cache import cache @@ -233,6 +234,14 @@ class Session(OrgModelMixin): target_ip = instance.get_target_ip() if instance else '' return target_ip + @property + def duration(self): + date_end = self.date_end or timezone.now() + delta = date_end - self.date_start + # 去掉毫秒的显示 + delta = timedelta(seconds=int(delta.total_seconds())) + return str(delta) + @classmethod def generate_fake(cls, count=100, is_finished=True): import random diff --git a/apps/terminal/serializers/session.py b/apps/terminal/serializers/session.py index 74c87dfb0..fd6d80af0 100644 --- a/apps/terminal/serializers/session.py +++ b/apps/terminal/serializers/session.py @@ -37,13 +37,14 @@ 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", "is_locked", + "date_start", "date_end", "duration", "comment", "terminal_display", "is_locked", 'command_amount', 'error_reason' ] fields_fk = ["terminal", ] fields_custom = ["can_replay", "can_join", "can_terminate"] fields = fields_small + fields_fk + fields_custom extra_kwargs = { + "Duration": {'label': _('Duration')}, "protocol": {'label': _('Protocol')}, 'user_id': {'label': _('User ID')}, 'asset_id': {'label': _('Asset ID')},