2020-12-10 12:50:22 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.db import models
|
2021-03-26 11:09:34 +00:00
|
|
|
from django.forms.models import model_to_dict
|
|
|
|
from django.core.cache import cache
|
2020-12-10 12:50:22 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Status(models.Model):
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
session_online = models.IntegerField(verbose_name=_("Session Online"), default=0)
|
2021-03-26 11:09:34 +00:00
|
|
|
cpu_load = models.FloatField(verbose_name=_("CPU Load"), default=0)
|
2020-12-10 12:50:22 +00:00
|
|
|
memory_used = models.FloatField(verbose_name=_("Memory Used"))
|
2021-03-26 11:09:34 +00:00
|
|
|
disk_used = models.FloatField(verbose_name=_("Disk Used"), default=0)
|
|
|
|
connections = models.IntegerField(verbose_name=_("Connections"), default=0)
|
|
|
|
threads = models.IntegerField(verbose_name=_("Threads"), default=0)
|
|
|
|
boot_time = models.FloatField(verbose_name=_("Boot Time"), default=0)
|
|
|
|
terminal = models.ForeignKey('terminal.Terminal', null=True, on_delete=models.CASCADE)
|
2020-12-10 12:50:22 +00:00
|
|
|
date_created = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
CACHE_KEY = 'TERMINAL_STATUS_{}'
|
|
|
|
|
2020-12-10 12:50:22 +00:00
|
|
|
class Meta:
|
|
|
|
db_table = 'terminal_status'
|
|
|
|
get_latest_by = 'date_created'
|
2022-02-17 12:13:31 +00:00
|
|
|
verbose_name = _("Status")
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
def save_to_cache(self):
|
|
|
|
if not self.terminal:
|
|
|
|
return
|
|
|
|
key = self.CACHE_KEY.format(self.terminal.id)
|
|
|
|
data = model_to_dict(self)
|
|
|
|
cache.set(key, data, 60*3)
|
|
|
|
return data
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_terminal_latest_status(cls, terminal):
|
|
|
|
from ..utils import ComputeStatUtil
|
|
|
|
stat = cls.get_terminal_latest_stat(terminal)
|
|
|
|
return ComputeStatUtil.compute_component_status(stat)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_terminal_latest_stat(cls, terminal):
|
|
|
|
key = cls.CACHE_KEY.format(terminal.id)
|
|
|
|
data = cache.get(key)
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
data.pop('terminal', None)
|
|
|
|
stat = cls(**data)
|
|
|
|
stat.terminal = terminal
|
2021-08-06 11:16:18 +00:00
|
|
|
stat.is_alive = terminal.is_alive
|
2021-11-17 10:54:32 +00:00
|
|
|
stat.keep_one_decimal_place()
|
2021-03-26 11:09:34 +00:00
|
|
|
return stat
|
|
|
|
|
2021-11-17 10:54:32 +00:00
|
|
|
def keep_one_decimal_place(self):
|
|
|
|
keys = ['cpu_load', 'memory_used', 'disk_used']
|
|
|
|
for key in keys:
|
|
|
|
value = getattr(self, key, 0)
|
|
|
|
if not isinstance(value, (int, float)):
|
|
|
|
continue
|
|
|
|
value = '%.1f' % value
|
|
|
|
setattr(self, key, float(value))
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
def save(self, force_insert=False, force_update=False, using=None,
|
|
|
|
update_fields=None):
|
|
|
|
self.terminal.set_alive(ttl=120)
|
|
|
|
return self.save_to_cache()
|
|
|
|
# return super().save()
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
|