Commit ab22253e by Karsa Zoltán István

update dc stats

parent 0af3e59f
...@@ -44,3 +44,11 @@ class ResourcePoolSchema(BaseModel): ...@@ -44,3 +44,11 @@ class ResourcePoolSchema(BaseModel):
class VMFromRP(BaseModel): class VMFromRP(BaseModel):
rpname: str = Field(min_length=3, max_length=50) rpname: str = Field(min_length=3, max_length=50)
username: str = Field() username: str = Field()
class ResourceStat(BaseModel):
datacenter_name: str
mem_max: int
mem_used: int
cpu_usage: float
vm_count: int
...@@ -10,7 +10,9 @@ from sredis.sredis import ( ...@@ -10,7 +10,9 @@ from sredis.sredis import (
create_superpuser, create_superpuser,
get_rtokens, get_rtokens,
reset, reset,
update_datacenter_stats,
update_datacenter_status, update_datacenter_status,
update_stats,
) )
import logging import logging
import requests import requests
...@@ -21,6 +23,7 @@ from core.models import ( ...@@ -21,6 +23,7 @@ from core.models import (
UserLoginSchema, UserLoginSchema,
DataCenterSchema, DataCenterSchema,
ResourcePoolSchema, ResourcePoolSchema,
ResourceStat,
) )
from core.auth import signJWT from core.auth import signJWT
from core.bearer import get_current_user, admin_user from core.bearer import get_current_user, admin_user
...@@ -108,6 +111,15 @@ def set_tokens(tokens: List[Token] = None, username=Depends(get_current_user)): ...@@ -108,6 +111,15 @@ def set_tokens(tokens: List[Token] = None, username=Depends(get_current_user)):
return tokens return tokens
@app.post("/updatestat/")
def update_datacenter_stat(
stats: List[ResourceStat] = None, username=Depends(admin_user)
):
for stat in stats:
update_stats(stat)
return stats
@app.get("/tokens/") @app.get("/tokens/")
def get_tokens(username=Depends(get_current_user)): def get_tokens(username=Depends(get_current_user)):
return get_rtokens(username=username) return get_rtokens(username=username)
...@@ -117,3 +129,9 @@ def get_tokens(username=Depends(get_current_user)): ...@@ -117,3 +129,9 @@ def get_tokens(username=Depends(get_current_user)):
@repeat_every(seconds=300) @repeat_every(seconds=300)
def update_datacenter_healt(): def update_datacenter_healt():
update_datacenter_status() update_datacenter_status()
@app.on_event("startup")
@repeat_every(seconds=200)
def update_datacenter_stats_rep():
update_datacenter_stats()
...@@ -20,3 +20,11 @@ class ResourcePool(HashModel): ...@@ -20,3 +20,11 @@ class ResourcePool(HashModel):
owner: str = Field(default="") owner: str = Field(default="")
datacenter: str = Field(default="") datacenter: str = Field(default="")
id: int = Field() id: int = Field()
class DataCenterResource(HashModel):
datacenter_name: str = Field(index=True)
mem_max: int = Field()
mem_used: int = Field()
cpu_usage: float = Field()
vm_count: int = Field()
import redis import redis
import requests import requests
from core.models import UserLoginSchema from core.models import UserLoginSchema, ResourceStat
from .models import PUser from .models import PUser, DataCenterResource
from passlib.hash import pbkdf2_sha256 from passlib.hash import pbkdf2_sha256
from core.models import User, DataCenterSchema from core.models import User, DataCenterSchema
from core.auth import hash_pass from core.auth import hash_pass
...@@ -56,6 +56,13 @@ def update_datacenter_status(): ...@@ -56,6 +56,13 @@ def update_datacenter_status():
update_status(datacenter=dc) update_status(datacenter=dc)
def update_datacenter_stats():
logging.info("Update datacenters stats")
all_keys = r.hgetall("datacenters_hash")
for dc in all_keys.values():
logging.info("Get stats from", dc)
def add_datacenter(datacenter: str): def add_datacenter(datacenter: str):
cnt = int(r.get("datacenters_cnt")) cnt = int(r.get("datacenters_cnt"))
r.hset("datacenters_hash", cnt, datacenter) r.hset("datacenters_hash", cnt, datacenter)
...@@ -128,3 +135,14 @@ def create_superpuser(user: User): ...@@ -128,3 +135,14 @@ def create_superpuser(user: User):
puser.save() puser.save()
return True return True
return False return False
def update_stats(stat: ResourceStat):
dc = DataCenterResource.find(
DataCenterResource.datacenter_name == stat.datacenter_name
).all()
if not dc:
dc = DataCenterResource(**stat)
else:
dc.update(**stat)
dc.save()
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