from sqlalchemy.orm import Session from sqlalchemy import desc from . import models, schemas def get_hosts(db: Session): hosts = db.query(models.Host).all() return hosts def get_host_by_hostname(db: Session, hostname: str): db_host = db.query(models.Host).filter(models.Host.hostname == hostname).first() if db_host: return db_host return {} def get_host_id_for_hostname(db: Session, hostname: str): try: db_host_id = db.query(models.Host).filter(models.Host.hostname == hostname).first().id except AttributeError: return -1 return db_host_id def create_host(db: Session, host: schemas.HostCreate): db_host = models.Host( hostname=host.hostname ) db.add(db_host) db.commit() db.refresh(db_host) return db_host def delete_host(db: Session, host_id: int): db_host = db.query(models.Host).filter(models.Host.id == host_id).first() db.delete(db_host) db.commit() return { "message": "Host successfully deleted" } def delete_host_by_hostname(db: Session, hostname: str): db_host = db.query(models.Host).filter(models.Host.hostname == hostname).first() db.delete(db_host) db.commit() return { "message": "Host successfully deleted" } def get_cpus_by_host_id(db: Session, host_id: int): 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): db_cpu = models.CPU( time = cpu.time, name = cpu.name, socket = cpu.socket, cores = cpu.cores, usage = cpu.usage, host_id = cpu.host_id ) db.add(db_cpu) db.commit() db.refresh(db_cpu) return db_cpu