Updated to add CPU and fix various bugs

master
Ronald 1 year ago
parent a92472fe2b
commit 1374b9405a

@ -1,6 +1,6 @@
fastapi fastapi
uvicorn uvicorn
pymysql psycopg2
sqlalchemy sqlalchemy
pydantic pydantic
typing-extensions typing-extensions

@ -1,4 +1,5 @@
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlalchemy import desc
from . import models, schemas from . import models, schemas
@ -31,7 +32,7 @@ def create_host(db: Session, host: schemas.HostCreate):
return db_host return db_host
def delete_host(db: Session, host_id: str): def delete_host(db: Session, host_id: int):
db_host = db.query(models.Host).filter(models.Host.id == host_id).first() db_host = db.query(models.Host).filter(models.Host.id == host_id).first()
db.delete(db_host) db.delete(db_host)
db.commit() db.commit()
@ -50,13 +51,19 @@ def delete_host_by_hostname(db: Session, hostname: str):
} }
def get_cpus_by_host_id(db: Session, host_id: int): def get_cpus_by_host_id(db: Session, host_id: int):
return db.query(models.CPU).filter(models.CPU.host_id == host_id) return \
db.query(models.CPU).filter(models.CPU.host_id == host_id).order_by(desc(models.CPU.time))
def create_cpu(db: Session, cpu: schemas.CPUCreate): def create_cpu(db: Session, cpu: schemas.CPUCreate):
db_cpu = models.CPU( db_cpu = models.CPU(
name=cpu.name, time = cpu.time,
socket=cpu.socket,
host_id=cpu.host_id name = cpu.name,
socket = cpu.socket,
cores = cpu.cores,
usage = cpu.usage,
host_id = cpu.host_id
) )
db.add(db_cpu) db.add(db_cpu)

@ -2,8 +2,8 @@ from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://" + \ SQLALCHEMY_DATABASE_URL = "postgresql+psycopg2://lsm_api:H3Fa53XFNapQbo64eXnhEpJWCeq8aws8kKWD8pSecB70nn1q@192.168.178.187" + \
"/lsm?charset=utf8mb4" "/lsm"
engine = create_engine(SQLALCHEMY_DATABASE_URL) engine = create_engine(SQLALCHEMY_DATABASE_URL)

@ -1,3 +1,4 @@
from datetime import datetime, UTC
from typing import List from typing import List
from fastapi import FastAPI, Depends, HTTPException from fastapi import FastAPI, Depends, HTTPException
@ -20,7 +21,6 @@ def get_db():
@app.get("/hosts/", response_model=List[schemas.Host]) @app.get("/hosts/", response_model=List[schemas.Host])
def get_hosts(db: Session = Depends(get_db)): def get_hosts(db: Session = Depends(get_db)):
hosts = crud.get_hosts(db) hosts = crud.get_hosts(db)
return hosts return hosts
@app.get("/get_host_by_hostname/{hostname}") @app.get("/get_host_by_hostname/{hostname}")
@ -36,7 +36,6 @@ def create_host(host: schemas.HostCreate, db: Session = Depends(get_db)):
db_host = crud.get_host_by_hostname(db, hostname=host.hostname) db_host = crud.get_host_by_hostname(db, hostname=host.hostname)
if db_host: if db_host:
raise HTTPException(status_code=400, detail="Host already exists") raise HTTPException(status_code=400, detail="Host already exists")
return crud.create_host(db=db, host=host) return crud.create_host(db=db, host=host)
@app.delete("/delete_host/{host_id}") @app.delete("/delete_host/{host_id}")
@ -47,15 +46,13 @@ def delete_host(host_id: int, db: Session = Depends(get_db)):
def delete_host_by_hostname(hostname: str, db: Session = Depends(get_db)): def delete_host_by_hostname(hostname: str, db: Session = Depends(get_db)):
return crud.delete_host_by_hostname(db, hostname=hostname) return crud.delete_host_by_hostname(db, hostname=hostname)
@app.get("/get_cpus_by_host_id/{host_id}") @app.get("/get_cpus_by_host_id/{host_id}", response_model=List[schemas.CPU])
def get_cpus_by_host_id(host_id: int, db: Session = Depends(get_db)) -> List[schemas.CPU]: def get_cpus_by_host_id(host_id: int, db: Session = Depends(get_db)) -> List[schemas.CPU]:
return crud.get_cpus_by_host_id(db, host_id=host_id) return crud.get_cpus_by_host_id(db, host_id=host_id)
@app.post("/add_cpu/", response_model=schemas.CPUCreate, status_code=201) @app.post("/add_cpu_to_host/", response_model=schemas.CPUCreate, status_code=201)
def create_cpu(cpu: schemas.CPUCreate, db: Session = Depends(get_db)): def create_cpu(cpu: schemas.CPUCreate, db: Session = Depends(get_db)):
db_cpus = crud.get_cpus_by_host_id(db, host_id=cpu.host_id) if cpu.time == datetime.fromtimestamp(0, UTC):
for db_cpu in db_cpus: cpu.time = datetime.now()
if cpu.socket == db_cpu.socket:
raise HTTPException(status_code=400, detail="CPU already exists")
return crud.create_cpu(db, cpu=cpu) return crud.create_cpu(db, cpu=cpu)

@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, Float, ForeignKey, String from sqlalchemy import Column, DateTime, Integer, Float, ForeignKey, String
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from .database import Base from .database import Base
@ -7,31 +7,33 @@ from .database import Base
class Host(Base): class Host(Base):
__tablename__ = "hosts" __tablename__ = "hosts"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
hostname = Column(String(255), unique=True, index=True) hostname = Column(String(255), unique=True, index=True)
cpus = relationship("CPU", back_populates="host") cpus = relationship("CPU", back_populates="host")
memory = relationship("Memory", back_populates="host") memory = relationship("Memory", back_populates="host")
class CPU(Base): class CPU(Base):
__tablename__ = "cpus" __tablename__ = "cpus"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
socket = Column(Integer, index=True) time = Column(DateTime, unique=False, index=True)
name = Column(String(255), index=True) socket = Column(Integer, index=True)
usage = Column(Float, index=True) name = Column(String(255), index=True)
usage = Column(Float, index=True)
cores = Column(Integer, index=True)
host_id = Column(Integer, ForeignKey("hosts.id")) host_id = Column(Integer, ForeignKey("hosts.id"))
host = relationship("Host", back_populates="cpus") host = relationship("Host", back_populates="cpus")
class Memory(Base): class Memory(Base):
__tablename__ = "memory" __tablename__ = "memory"
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
type = Column(String(12), index=True) type = Column(String(12), index=True)
total = Column(Integer, index=True) total = Column(Integer, index=True)
used = Column(Integer, index=True) used = Column(Integer, index=True)
usage = Column(Float, index=True) usage = Column(Float, index=True)
host_id = Column(Integer, ForeignKey("hosts.id")) host_id = Column(Integer, ForeignKey("hosts.id"))
host = relationship("Host", back_populates="memory") host = relationship("Host", back_populates="memory")

@ -1,17 +1,20 @@
from datetime import datetime, UTC
from typing import Optional from typing import Optional
from pydantic import BaseModel from pydantic import BaseModel
class CPUBase(BaseModel): class CPUBase(BaseModel):
name: str time: Optional[datetime] = datetime.fromtimestamp(0, UTC)
name: str
socket: int socket: int
cores: int
class CPUCreate(CPUBase): class CPUCreate(CPUBase):
host_id: int host_id: int
usage: float
class CPU(CPUBase): class CPU(CPUBase):
id: int id: int
usage: Optional[float]
class Config: class Config:
from_attributes = True from_attributes = True

Loading…
Cancel
Save