From c807525ec5c99c9dd7fb7363c08aa4d7073916cc Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Sat, 15 Oct 2016 02:36:58 +0300 Subject: [PATCH] =?UTF-8?q?#27=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=B8=D0=BA=D0=B0=20=D1=81=D0=B5=D1=82=D0=B5=D0=B2=D0=BE?= =?UTF-8?q?=D0=B9=20=D0=BD=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/default/compute/vms/stats.html | 11 +++-- SWSCloudWeb/views/compute/vms/__init__.py | 44 +++++++++++-------- SWSStatisticsClient/__init__.py | 2 +- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/SWSCloudWeb/templates/default/compute/vms/stats.html b/SWSCloudWeb/templates/default/compute/vms/stats.html index aef048f..9de8794 100644 --- a/SWSCloudWeb/templates/default/compute/vms/stats.html +++ b/SWSCloudWeb/templates/default/compute/vms/stats.html @@ -9,14 +9,17 @@ $.each( data.network, function( key, val ) { items.push({ x: new Date(val.year, val.month, val.day, val.hour, val.minute), - y: val.read_bytes + y: val.read_bytes_diff }); }); $("#chartContainer").CanvasJSChart({ + axisY:{ + title: "Трафик за сутки (Мб)" + }, data: [ { toolTipContent: "{y} мбайт", - type: "line", + type: "stepLine", markerSize: 5, color: "#F08080", dataPoints: items @@ -35,7 +38,9 @@
-
+
+

Сетевой трафик

+
{% endblock %} diff --git a/SWSCloudWeb/views/compute/vms/__init__.py b/SWSCloudWeb/views/compute/vms/__init__.py index 1c425aa..a0548e8 100644 --- a/SWSCloudWeb/views/compute/vms/__init__.py +++ b/SWSCloudWeb/views/compute/vms/__init__.py @@ -317,6 +317,28 @@ def stats(vm_id): vm_details=vm_details) +def calc_diff_list(data_list): + """ + :param data_list: + :return: + """ + # массив с результатами вычисления + diff = list() + # добавля + # diff.append(data_list[0]) + for cur, prev in zip(data_list, data_list[1:]): + # print prev, cur + a = cur['read_bytes'] if cur['read_bytes'] else 0 + b = prev['read_bytes'] if prev['read_bytes'] else 0 + + calc = int(b) - int(a) + # diff.append(calc if calc > 0 else calc * -1) + cur['read_bytes_diff'] = (calc if calc > 0 else calc * -1) + diff.append(cur) + # diff.__delitem__(0) + return diff + + @viewVMs.route('/stats/.json') @requires_login def stats_json(vm_id): @@ -325,33 +347,19 @@ def stats_json(vm_id): if not ControllerVMS(session['user_id']).exists(vm_id): return jsonify(status=1, message='instance not exists') - cnt = 0 - l = 0 + # stats_items = list() statistics = list() for s in SWSStatisticsClient().get_vm(vm_id).get('items'): reported = dateutil.parser.parse(s.get('time')) read_bytes = int(s.get('stats', {}).get('network', {}).get('read_bytes', 0)) - n = read_bytes + # stats_items.append(read_bytes) - # if l - int(s.get('stats', {}).get('network', {}).get('read_bytes', 0)) < 0: - # n = l - # else: - # n = l - int(s.get('stats', {}).get('network', {}).get('read_bytes', 0)) - # - # l = int(s.get('stats', {}).get('network', {}).get('read_bytes', 0)) - # print reported, read_bytes, l, n - - # if cnt == 0: - # cnt += 1 - # continue statistics.append(dict( year=reported.strftime('%Y'), month=reported.strftime('%m'), day=reported.strftime('%d'), hour=reported.strftime('%H'), minute=reported.strftime('%M'), - read_bytes=n, - # write_bytes=s['stats']['network']['write_bytes'], + read_bytes=read_bytes / 1024 / 1024, )) - - return jsonify(network=statistics, status=0) + return jsonify(network=calc_diff_list(statistics), status=0) diff --git a/SWSStatisticsClient/__init__.py b/SWSStatisticsClient/__init__.py index ac5b785..437988d 100644 --- a/SWSStatisticsClient/__init__.py +++ b/SWSStatisticsClient/__init__.py @@ -2,7 +2,7 @@ import requests class SWSStatisticsClient(object): - def get_vm(self, vm_id, limit=25): + def get_vm(self, vm_id, limit=96): r = requests.get('http://localhost:8089/stats/v1/compute/vms/%s' % vm_id, params={'limit': limit}) if r.status_code == 200: return r.json()