prvi poskus
This commit is contained in:
@@ -13,18 +13,19 @@ import threading
|
||||
|
||||
if os.environ.get("DATABASE_URL") == None:
|
||||
os.environ["DATABASE_URL"] = "sqlite:///./wall_messages.db"
|
||||
|
||||
else
|
||||
DATABASE_URL = os.environ["DATABASE_URL"]
|
||||
DATABASE_PASSWORD = os.environ["DATABASE_PASSWORD"]
|
||||
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
cpu_threshold = 80
|
||||
ram_threshold = 80
|
||||
disk_threshold = 80
|
||||
|
||||
cpu_normal = True
|
||||
ram_normal = True
|
||||
disk_normal = True
|
||||
|
||||
DATABASE_URL = "sqlite:///./wall_messages.db"
|
||||
|
||||
|
||||
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
||||
SessionLocal = sessionmaker(bind=engine)
|
||||
@@ -79,7 +80,7 @@ def check_and_log_overages():
|
||||
cpu_limit = limits.cpu_threshold
|
||||
ram_limit = limits.ram_threshold
|
||||
disk_limit = limits.disk_threshold
|
||||
|
||||
print("LIMITS: ",cpu_limit, ram_limit, disk_limit)
|
||||
cpu = psutil.cpu_percent(interval=0.5)
|
||||
ram = psutil.virtual_memory().percent
|
||||
disk = psutil.disk_usage('/').percent
|
||||
@@ -115,9 +116,8 @@ def check_and_log_overages():
|
||||
|
||||
threading.Thread(target=check_and_log_overages, daemon=True).start()
|
||||
|
||||
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
class Info(BaseModel):
|
||||
uptime: datetime.datetime
|
||||
cpu: float
|
||||
@@ -136,46 +136,56 @@ class Thresholds(BaseModel):
|
||||
disk_threshold: int
|
||||
|
||||
|
||||
@app.get("/info", response_class=Info)
|
||||
@app.get("/info", response_model=Info)
|
||||
def get_info():
|
||||
cpu = psutil.cpu_percent(interval=0.5)
|
||||
ram = psutil.virtual_memory().percent
|
||||
disk = psutil.disk_usage('/').percent
|
||||
uptime = time.time() - start_time
|
||||
info = Info(
|
||||
uptime=uptime,
|
||||
cpu=cpu,
|
||||
ram=ram,
|
||||
disk=disk,
|
||||
cpu_threshold=cpu_threshold,
|
||||
ram_threshold=ram_threshold,
|
||||
disk_threshold=disk_threshold,
|
||||
cpu_normal=cpu <= cpu_threshold,
|
||||
ram_normal=ram <= ram_threshold,
|
||||
disk_normal=disk <= disk_threshold
|
||||
db = SessionLocal()
|
||||
limits = db.query(LimitsModel).first()
|
||||
if not limits:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail="Limits not found")
|
||||
delta_seconds = int(time.time() - start_time)
|
||||
system_information = Info(
|
||||
uptime=datetime.datetime.now() - datetime.timedelta(seconds=delta_seconds),
|
||||
cpu=psutil.cpu_percent(interval=0.5),
|
||||
ram=psutil.virtual_memory().percent,
|
||||
disk=psutil.disk_usage('/').percent,
|
||||
cpu_threshold=limits.cpu_threshold,
|
||||
ram_threshold=limits.ram_threshold,
|
||||
disk_threshold=limits.disk_threshold,
|
||||
cpu_normal=cpu_normal,
|
||||
ram_normal=ram_normal,
|
||||
disk_normal=disk_normal
|
||||
)
|
||||
return info
|
||||
print(system_information)
|
||||
return system_information
|
||||
|
||||
@app.get("/limits", response_model=Thresholds)
|
||||
def get_limits():
|
||||
db = SessionLocal()
|
||||
limits = db.query(LimitsModel).first()
|
||||
if not limits:
|
||||
db.close()
|
||||
raise HTTPException(status_code=404, detail="Limits not found")
|
||||
return {
|
||||
"cpu_threshold": cpu_threshold,
|
||||
"ram_threshold": ram_threshold,
|
||||
"disk_threshold": disk_threshold
|
||||
"cpu_threshold": limits.cpu_threshold,
|
||||
"ram_threshold": limits.ram_threshold,
|
||||
"disk_threshold": limits.disk_threshold
|
||||
}
|
||||
|
||||
@app.post("/limits")
|
||||
def set_limits(thresholds: Thresholds):
|
||||
print (thresholds)
|
||||
db = SessionLocal()
|
||||
limits = db.query(LimitsModel).first()
|
||||
cpu_threshold = thresholds.cpu_threshold
|
||||
ram_threshold = thresholds.ram_threshold
|
||||
disk_threshold = thresholds.disk_threshold
|
||||
if limits:
|
||||
limits.cpu_threshold = thresholds.cpu_threshold
|
||||
limits.ram_threshold = thresholds.ram_threshold
|
||||
limits.disk_threshold = thresholds.disk_threshold
|
||||
limits.cpu_threshold = cpu_threshold
|
||||
limits.ram_threshold = ram_threshold
|
||||
limits.disk_threshold = disk_threshold
|
||||
db.commit()
|
||||
db.refresh(limits)
|
||||
db.close()
|
||||
|
||||
return {
|
||||
"cpu": cpu_threshold,
|
||||
"ram": ram_threshold,
|
||||
|
||||
Reference in New Issue
Block a user