From 4ef5a79ba787e66665f8f8ce4a88cb709b5fbc72 Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Wed, 16 Dec 2015 14:54:40 +0300 Subject: [PATCH] container state added --- app/cloud/controllers/containers/__init__.py | 87 +++++++++++++++++++- app/cloud/views/containers/__init__.py | 5 +- app/templates/default/containers/index.html | 7 +- 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/app/cloud/controllers/containers/__init__.py b/app/cloud/controllers/containers/__init__.py index f4dafe5..c4914f7 100644 --- a/app/cloud/controllers/containers/__init__.py +++ b/app/cloud/controllers/containers/__init__.py @@ -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 diff --git a/app/cloud/views/containers/__init__.py b/app/cloud/views/containers/__init__.py index 539a8c3..7a20516 100644 --- a/app/cloud/views/containers/__init__.py +++ b/app/cloud/views/containers/__init__.py @@ -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( diff --git a/app/templates/default/containers/index.html b/app/templates/default/containers/index.html index e916ff9..953df05 100644 --- a/app/templates/default/containers/index.html +++ b/app/templates/default/containers/index.html @@ -15,6 +15,7 @@ ID IP + Диск Статус   @@ -28,7 +29,7 @@ {% for container in containers['items'] %} - {{ container.id }} + {{ container['id'] }} {% if container['ipv4'] %} @@ -38,6 +39,10 @@ IPv6: {{ container['ipv6'] }} {% endif %} + + {% set size_gb = (container.containersstatisticsstate.size / 1024 / 1024) %} + {{ '%0.2f' | format(size_gb|float) }}GB + {% if container['status'] == 0 %} Неактивно