container state added

This commit is contained in:
Vyacheslav Anzhiganov 2015-12-16 14:54:40 +03:00
parent a777aa846c
commit 4ef5a79ba7
3 changed files with 93 additions and 6 deletions

View file

@ -29,12 +29,20 @@ class ControllerContainers:
return True
def get_items(self):
query = models.Containers.select().where(models.Containers.user == self.user_id)
state = ControllerContainersStatisticsState()
query = models.Containers.select(
models.Containers.id,
models.Containers.ipv4,
models.Containers.ipv6,
models.Containers.status,
models.ContainersStatisticsState.size,
).join(models.ContainersStatisticsState).where(models.Containers.user == self.user_id)
print query
containers = dict()
containers["total"] = query.count()
containers["items"] = []
for item in query.execute():
print (item.containersstatisticsstate.size)
containers['items'].append(item)
return containers
@ -82,3 +90,78 @@ class ControllerContainersStatistics:
if not traffic_sum:
traffic_sum = 0
return format(float(traffic_sum / 1024) / 1024 / 1024, '.3f')
class ControllerContainersStatisticsState:
def get(self, container_id):
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):
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

View file

@ -12,9 +12,6 @@ from flask import url_for
from flask import request
from flask import Blueprint
from flask import jsonify
from cloudnsru import CloudnsClient
from app import models
from app.cloud.controllers.common import ControllerCommon
from app.cloud.controllers.common import ControllerMessagesEmail
@ -23,6 +20,7 @@ from app.cloud.controllers.users import ControllerSSHKey
from app.cloud.controllers.billing import ControllerBilling
from app.cloud.controllers.datacenters import ControllerDataCenters
from app.cloud.controllers.containers import ControllerContainers
from app.cloud.controllers.containers import ControllerContainersStatisticsState
from app.cloud.controllers.tasks import ControllerTasks
viewContainers = Blueprint('containers', __name__, url_prefix='/containers')
@ -111,6 +109,7 @@ def create():
status
)
ControllerContainersStatisticsState().set(new_container['container_id'], dict())
if container_create:
# create task for create new container
ControllerTasks(session['user_id']).create(

View file

@ -15,6 +15,7 @@
<tr>
<th width="45%">ID</th>
<th>IP</th>
<th>Диск</th>
<th>Статус</th>
<th width="10%">&nbsp;</th>
</tr>
@ -28,7 +29,7 @@
{% for container in containers['items'] %}
<tr>
<td>
{{ container.id }}
{{ container['id'] }}
</td>
<td>
{% if container['ipv4'] %}
@ -38,6 +39,10 @@
IPv6: {{ container['ipv6'] }}
{% endif %}
</td>
<td>
{% set size_gb = (container.containersstatisticsstate.size / 1024 / 1024) %}
{{ '%0.2f' | format(size_gb|float) }}GB
</td>
<td>
{% if container['status'] == 0 %}
Неактивно