192 lines
6.7 KiB
Python
192 lines
6.7 KiB
Python
# coding: utf-8
|
|
|
|
import datetime
|
|
from peewee import fn
|
|
from SWSCloudCore import models
|
|
|
|
|
|
class ControllerContainers:
|
|
def __init__(self, user_id):
|
|
self.user_id = user_id
|
|
|
|
def create(self, container_id, datacenter_id, server_id, ipv4, ipv6, status=0):
|
|
# create a new record
|
|
try:
|
|
models.Containers.create(
|
|
id=container_id,
|
|
datacenter=datacenter_id,
|
|
server=server_id,
|
|
user=self.user_id,
|
|
ipv4=ipv4,
|
|
ipv6=ipv6,
|
|
status=status
|
|
)
|
|
except Exception as e:
|
|
# TODO: write to log
|
|
print e
|
|
return False
|
|
return True
|
|
|
|
def get_items(self):
|
|
# state = ControllerContainersStatisticsState()
|
|
query = models.Containers.select(
|
|
models.Containers.id,
|
|
models.Containers.ipv4,
|
|
models.Containers.ipv6,
|
|
models.Containers.status,
|
|
models.ContainersStatisticsState.size,
|
|
models.ContainersStatisticsState.cpu,
|
|
models.ContainersStatisticsState.memory,
|
|
models.ContainersStatisticsState.net_total,
|
|
).join(models.ContainersStatisticsState).where(
|
|
models.Containers.user == self.user_id
|
|
)
|
|
#
|
|
containers = dict()
|
|
containers["total"] = query.count()
|
|
containers["items"] = []
|
|
for item in query.execute():
|
|
containers['items'].append({
|
|
'id': str(item.id),
|
|
'ipv4': item.ipv4,
|
|
'ipv6': item.ipv6,
|
|
'status': item.status,
|
|
'size': item.containersstatisticsstate.size,
|
|
'cpu': item.containersstatisticsstate.cpu,
|
|
'memory': item.containersstatisticsstate.memory,
|
|
'net_tx': item.containersstatisticsstate.net_tx,
|
|
'net_rx': item.containersstatisticsstate.net_rx,
|
|
'net_total': item.containersstatisticsstate.net_total,
|
|
})
|
|
return containers
|
|
|
|
def get_item(self, container_id):
|
|
# get rule details
|
|
return models.Containers.select().where(
|
|
models.Containers.user == self.user_id,
|
|
models.Containers.id == container_id
|
|
).get()
|
|
|
|
def set_status(self, rule_id, status=0):
|
|
# Устанавливаем статус в самом правиле
|
|
rule_status = models.Containers.update(status=status).where(models.Containers.id == rule_id)
|
|
rule_status.execute()
|
|
|
|
return True
|
|
|
|
def check_exists_item(self, rule_id):
|
|
if models.Containers.select().where(
|
|
models.Containers.user == self.user_id,
|
|
models.Containers.id == rule_id
|
|
).count() == 0:
|
|
return False
|
|
return True
|
|
|
|
|
|
class ControllerContainersStatistics:
|
|
def __init__(self, container_id):
|
|
self.container_id = container_id
|
|
|
|
def size_get(self, days=7):
|
|
last_days = datetime.datetime.now() - datetime.timedelta(days=days)
|
|
results = models.ContainersStatistics.select().where(
|
|
models.ContainersStatistics.container == self.container_id,
|
|
models.ContainersStatistics.created > last_days
|
|
)
|
|
return results
|
|
|
|
def traffic_total_get(self, days):
|
|
last_days = datetime.datetime.now() - datetime.timedelta(days=days)
|
|
traffic_sum = models.ContainersStatistics.select(
|
|
fn.sum(models.ContainersStatistics.net_total).alias('sum')
|
|
).where(
|
|
models.ContainersStatistics.container == self.container_id,
|
|
models.ContainersStatistics.created > last_days)[0].sum
|
|
if not traffic_sum:
|
|
traffic_sum = 0
|
|
return format(float(traffic_sum / 1024) / 1024 / 1024, '.3f')
|
|
|
|
|
|
class ControllerContainersStatisticsState:
|
|
def get(self, container_id):
|
|
"""
|
|
Получение данных последнего отчёта
|
|
:param container_id:
|
|
:return:
|
|
"""
|
|
return models.ContainersStatisticsState.select().where(
|
|
models.ContainersStatisticsState.container == container_id
|
|
).execute()
|
|
|
|
def set(self, container_id, statistics):
|
|
if self.exists(container_id):
|
|
return self.update(container_id, statistics)
|
|
return self.create(container_id, statistics)
|
|
|
|
def exists(self, container_id):
|
|
"""
|
|
Проверка наличияя записи отчёта в таблице
|
|
:param container_id:
|
|
:return:
|
|
"""
|
|
if models.ContainersStatisticsState.select().where(
|
|
models.ContainersStatisticsState.container == container_id
|
|
).count() == 0:
|
|
return False
|
|
return True
|
|
|
|
def create(self, container_id, statistics):
|
|
if 'cpu_use' not in statistics:
|
|
statistics['cpu_use'] = 0
|
|
if 'memory_use' not in statistics:
|
|
statistics['memory_use'] = 0
|
|
if 'size' not in statistics:
|
|
statistics['size'] = 0
|
|
if 'tx_bytes' not in statistics:
|
|
statistics['tx_bytes'] = 0
|
|
if 'rx_bytes' not in statistics:
|
|
statistics['rx_bytes'] = 0
|
|
if 'total_bytes' not in statistics:
|
|
statistics['total_bytes'] = 0
|
|
|
|
try:
|
|
models.ContainersStatisticsState.create(
|
|
container=container_id,
|
|
cpu=statistics['cpu_use'],
|
|
memory=statistics['memory_use'],
|
|
size=statistics['size'],
|
|
net_tx=statistics['tx_bytes'],
|
|
net_rx=statistics['rx_bytes'],
|
|
net_total=statistics['total_bytes']
|
|
)
|
|
except Exception as e:
|
|
# TODO: report
|
|
return False
|
|
return True
|
|
|
|
def update(self, container_id, statistics):
|
|
if 'cpu_use' not in statistics:
|
|
statistics['cpu_use'] = 0
|
|
if 'memory_use' not in statistics:
|
|
statistics['memory_use'] = 0
|
|
if 'size' not in statistics:
|
|
statistics['size'] = 0
|
|
if 'tx_bytes' not in statistics:
|
|
statistics['tx_bytes'] = 0
|
|
if 'rx_bytes' not in statistics:
|
|
statistics['rx_bytes'] = 0
|
|
if 'total_bytes' not in statistics:
|
|
statistics['total_bytes'] = 0
|
|
|
|
state = models.ContainersStatisticsState.update(
|
|
updated=datetime.datetime.now(),
|
|
cpu=statistics['cpu_use'],
|
|
memory=statistics['memory_use'],
|
|
size=statistics['size'],
|
|
net_tx=statistics['tx_bytes'],
|
|
net_rx=statistics['rx_bytes'],
|
|
net_total=statistics['total_bytes']
|
|
).where(models.ContainersStatisticsState.container == container_id)
|
|
state.execute()
|
|
|
|
return True
|