new statistics
This commit is contained in:
parent
45de50d15a
commit
a777aa846c
5 changed files with 104 additions and 39 deletions
|
@ -1,6 +1,7 @@
|
|||
# coding: utf-8
|
||||
|
||||
import uuid
|
||||
import datetime
|
||||
from peewee import fn
|
||||
from app import models
|
||||
|
||||
|
@ -60,33 +61,24 @@ class ControllerContainers:
|
|||
return True
|
||||
|
||||
|
||||
class ControllerRulesStatistics:
|
||||
def __init__(self, rule_id):
|
||||
self.rule_id = rule_id
|
||||
class ControllerContainersStatistics:
|
||||
def __init__(self, container_id):
|
||||
self.container_id = container_id
|
||||
|
||||
def requests_total_get(self, days):
|
||||
return models.RulesTraffic.select().\
|
||||
where(models.RulesTraffic.rule == self.rule_id, models.RulesTraffic.timestamp > days).count()
|
||||
def size_get(self, days=7):
|
||||
last_days = datetime.datetime.now() - datetime.timedelta(days=days)
|
||||
return models.ContainersStatistics.select().where(
|
||||
models.ContainersStatistics.container == self.container_id,
|
||||
models.ContainersStatistics.created > last_days
|
||||
)
|
||||
|
||||
def traffic_total_get(self, days):
|
||||
traffic_sum = models.RulesTraffic.\
|
||||
select(fn.sum(models.RulesTraffic.body_bytes_sent).alias('sum')).\
|
||||
where(models.RulesTraffic.rule == self.rule_id, models.RulesTraffic.timestamp > days)[0].sum
|
||||
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')
|
||||
|
||||
def traffic_by_country_get(self, days):
|
||||
traffic_by_country = dict()
|
||||
tbc = models.RulesTraffic.\
|
||||
select(models.RulesTraffic.country, fn.sum(models.RulesTraffic.body_bytes_sent).alias('sum')).\
|
||||
where(models.RulesTraffic.rule == self.rule_id, models.RulesTraffic.timestamp > days).\
|
||||
group_by(models.RulesTraffic.country)
|
||||
|
||||
for t in tbc:
|
||||
if not t.sum:
|
||||
t.sum = 0
|
||||
# traffic_by_country[t.country] = t.sum
|
||||
traffic_by_country[t.country] = format(float(t.sum / 1024) / 1024 / 1024, '.3f')
|
||||
return traffic_by_country
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# import datetime
|
||||
import datetime
|
||||
from app import models
|
||||
|
||||
|
||||
|
@ -14,3 +14,66 @@ class ControllerServerStatistics:
|
|||
net_total=net_total
|
||||
)
|
||||
return True
|
||||
|
||||
def state_exists(self, container_id):
|
||||
if models.ContainersStatisticsState.select().where(
|
||||
models.ContainersStatisticsState.container == container_id
|
||||
).count() == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
def state_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 state_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
|
||||
|
|
|
@ -86,18 +86,22 @@ def report_container_status():
|
|||
container_id = statistics['container_id']
|
||||
|
||||
if 'cpu_use' not in statistics:
|
||||
return False
|
||||
return jsonify({})
|
||||
if 'memory_use' not in statistics:
|
||||
return False
|
||||
return jsonify({})
|
||||
if 'tx_bytes' not in statistics:
|
||||
return False
|
||||
return jsonify({})
|
||||
if 'rx_bytes' not in statistics:
|
||||
return False
|
||||
return jsonify({})
|
||||
if 'total_bytes' not in statistics:
|
||||
return False
|
||||
return jsonify({})
|
||||
|
||||
# create or update state
|
||||
if ControllerServerStatistics().state_exists(container_id):
|
||||
ControllerServerStatistics().state_update(container_id, statistics)
|
||||
else:
|
||||
ControllerServerStatistics().state_create(container_id, statistics)
|
||||
|
||||
# print container_id, ,
|
||||
print statistics
|
||||
if ControllerContainersServer().exists(container_id):
|
||||
ControllerServerStatistics().write(
|
||||
container_id,
|
||||
|
@ -107,12 +111,6 @@ def report_container_status():
|
|||
int(statistics['tx_bytes']),
|
||||
int(statistics['rx_bytes']),
|
||||
int(statistics['total_bytes'])
|
||||
|
||||
# int(statistics['rx_bytes']) / 1024 / 1024,
|
||||
# int(statistics['total_bytes']) / 1024 / 1024
|
||||
)
|
||||
# import datetime
|
||||
# print container_id
|
||||
# print ControllerContainersServer().exists(container_id)
|
||||
# print datetime.datetime.now
|
||||
|
||||
return jsonify({})
|
||||
|
|
|
@ -142,6 +142,17 @@ class ContainersStatistics(PgSQLModel):
|
|||
net_total = BigIntegerField(default=0, null=False)
|
||||
|
||||
|
||||
class ContainersStatisticsState(PgSQLModel):
|
||||
container = ForeignKeyField(Containers)
|
||||
updated = DateTimeField(default=datetime.datetime.now)
|
||||
cpu = BigIntegerField(default=0, null=False)
|
||||
memory = BigIntegerField(default=0, null=False)
|
||||
size = BigIntegerField(default=0, null=False)
|
||||
net_tx = BigIntegerField(default=0, null=False)
|
||||
net_rx = BigIntegerField(default=0, null=False)
|
||||
net_total = BigIntegerField(default=0, null=False)
|
||||
|
||||
|
||||
class Tasks(PgSQLModel):
|
||||
id = UUIDField(primary_key=True, unique=True)
|
||||
datacenter = ForeignKeyField(DataCenters, related_name='dcstasks')
|
||||
|
|
|
@ -16,5 +16,6 @@ models.database.create_tables({
|
|||
models.SSHKeys,
|
||||
models.Containers,
|
||||
models.ContainersStatistics,
|
||||
models.ContainersStatisticsState,
|
||||
models.Tasks
|
||||
}, safe=True)
|
||||
|
|
Loading…
Add table
Reference in a new issue