Commit a9aaa26d by Karsa Zoltán István

setup redis for stats

parent db0fd796
......@@ -45,5 +45,5 @@ def get_stat_from_dc(datacenter, retry = False):
)
data = json.loads(req.content)
stat[key] = [i["datapoints"][0][0] for i in data]
print(stat)
return stat
......@@ -6,6 +6,7 @@ from sredis.sredis import (
check_user,
create_puser,
add_datacenter,
get_all_stats,
set_token,
create_superpuser,
get_rtokens,
......@@ -129,18 +130,36 @@ def update_datacenter_stat(
return stats
@app.get("/stats/")
def get_datacenter_stat(
username=Depends(get_current_user)
):
stats = get_all_stats()
stats = [
ResourceStat(
datacenter_name=x.datacenter_name,
mem_max=x.mem_max,
mem_used=x.mem_used,
cpu_usage=x.cpu_usage,
vm_count=x.vm_count
) for x in stats
]
return stats
@app.get("/tokens/")
def get_tokens(username=Depends(get_current_user)):
return get_rtokens(username=username)
@app.on_event("startup")
@repeat_every(seconds=300)
@repeat_every(seconds=300, raise_exceptions=True)
def update_datacenter_healt():
update_datacenter_status()
@app.on_event("startup")
@repeat_every(seconds=200)
@repeat_every(seconds=200, raise_exceptions=True)
def update_datacenter_stats_rep():
update_datacenter_stats()
......@@ -62,8 +62,15 @@ def update_datacenter_stats():
all_keys = r.hgetall("datacenters_hash")
for dc in all_keys.values():
logger.info(f"Get stats from {dc}")
get_stat_from_dc(dc, retry=True)
# update_stats(stats)
stats = get_stat_from_dc(dc, retry=True)
rs = ResourceStat(
datacenter_name=dc,
mem_max=4096,
mem_used=int(stats["mem_usage"][0] * 4096 / 100.0) if stats["mem_usage"][0] else None,
cpu_usage=stats["cpu_percent"][0],
vm_count=int(stats["vmcount"][0])
)
update_stats(rs)
def add_datacenter(datacenter: str):
......@@ -144,13 +151,29 @@ def update_stats(stat: ResourceStat):
dc = DataCenterResource.find(
DataCenterResource.datacenter_name == stat.datacenter_name
).all()
if not dc:
dc = DataCenterResource(**stat)
if not dc or len(dc) == 0:
dc = DataCenterResource(
datacenter_name=stat.datacenter_name,
mem_max=stat.mem_max,
mem_used=stat.mem_used,
cpu_usage=stat.cpu_usage,
vm_count=stat.vm_count
)
dc.save()
else:
dc.update(**stat)
dc = dc[0]
dc.mem_max = stat.mem_max if stat.mem_max else dc.mem_max
dc.mem_used = stat.mem_used if stat.mem_used else dc.mem_used
dc.cpu_usage = stat.cpu_usage if stat.cpu_usage else dc.cpu_usage
dc.vm_count = stat.vm_count if stat.vm_count else dc.vm_count
dc.save()
def get_all_stats():
dcs = DataCenterResource.find().all()
return dcs
def save_rpvm(rpname, username, datacenter, id, token):
rpvm = ResourcePool(
rpname=rpname, owner=username, datacenter=datacenter, id=id, token=token
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment