maj
This commit is contained in:
@@ -49,71 +49,88 @@ async def submit_benchmark(
|
||||
# Update device timestamp
|
||||
device.updated_at = datetime.utcnow()
|
||||
|
||||
# 2. Create hardware snapshot
|
||||
# 2. Get or create hardware snapshot
|
||||
hw = payload.hardware
|
||||
snapshot = HardwareSnapshot(
|
||||
device_id=device.id,
|
||||
captured_at=datetime.utcnow(),
|
||||
|
||||
# CPU
|
||||
cpu_vendor=hw.cpu.vendor if hw.cpu else None,
|
||||
cpu_model=hw.cpu.model if hw.cpu else None,
|
||||
cpu_microarchitecture=hw.cpu.microarchitecture if hw.cpu else None,
|
||||
cpu_cores=hw.cpu.cores if hw.cpu else None,
|
||||
cpu_threads=hw.cpu.threads if hw.cpu else None,
|
||||
cpu_base_freq_ghz=hw.cpu.base_freq_ghz if hw.cpu else None,
|
||||
cpu_max_freq_ghz=hw.cpu.max_freq_ghz if hw.cpu else None,
|
||||
cpu_cache_l1_kb=hw.cpu.cache_l1_kb if hw.cpu else None,
|
||||
cpu_cache_l2_kb=hw.cpu.cache_l2_kb if hw.cpu else None,
|
||||
cpu_cache_l3_kb=hw.cpu.cache_l3_kb if hw.cpu else None,
|
||||
cpu_flags=json.dumps(hw.cpu.flags) if hw.cpu and hw.cpu.flags else None,
|
||||
cpu_tdp_w=hw.cpu.tdp_w if hw.cpu else None,
|
||||
# Check if we have an existing snapshot for this device
|
||||
existing_snapshot = db.query(HardwareSnapshot).filter(
|
||||
HardwareSnapshot.device_id == device.id
|
||||
).order_by(HardwareSnapshot.captured_at.desc()).first()
|
||||
|
||||
# RAM
|
||||
ram_total_mb=hw.ram.total_mb if hw.ram else None,
|
||||
ram_used_mb=hw.ram.used_mb if hw.ram else None, # NEW
|
||||
ram_free_mb=hw.ram.free_mb if hw.ram else None, # NEW
|
||||
ram_shared_mb=hw.ram.shared_mb if hw.ram else None, # NEW
|
||||
ram_slots_total=hw.ram.slots_total if hw.ram else None,
|
||||
ram_slots_used=hw.ram.slots_used if hw.ram else None,
|
||||
ram_ecc=hw.ram.ecc if hw.ram else None,
|
||||
ram_layout_json=json.dumps([slot.dict() for slot in hw.ram.layout]) if hw.ram and hw.ram.layout else None,
|
||||
# If we have an existing snapshot, update it instead of creating a new one
|
||||
if existing_snapshot:
|
||||
snapshot = existing_snapshot
|
||||
snapshot.captured_at = datetime.utcnow() # Update timestamp
|
||||
else:
|
||||
# Create new snapshot if none exists
|
||||
snapshot = HardwareSnapshot(
|
||||
device_id=device.id,
|
||||
captured_at=datetime.utcnow()
|
||||
)
|
||||
|
||||
# GPU
|
||||
gpu_summary=f"{hw.gpu.vendor} {hw.gpu.model}" if hw.gpu and hw.gpu.model else None,
|
||||
gpu_vendor=hw.gpu.vendor if hw.gpu else None,
|
||||
gpu_model=hw.gpu.model if hw.gpu else None,
|
||||
gpu_driver_version=hw.gpu.driver_version if hw.gpu else None,
|
||||
gpu_memory_dedicated_mb=hw.gpu.memory_dedicated_mb if hw.gpu else None,
|
||||
gpu_memory_shared_mb=hw.gpu.memory_shared_mb if hw.gpu else None,
|
||||
gpu_api_support=json.dumps(hw.gpu.api_support) if hw.gpu and hw.gpu.api_support else None,
|
||||
# Update all fields (whether new or existing snapshot)
|
||||
# CPU
|
||||
snapshot.cpu_vendor = hw.cpu.vendor if hw.cpu else None
|
||||
snapshot.cpu_model = hw.cpu.model if hw.cpu else None
|
||||
snapshot.cpu_microarchitecture = hw.cpu.microarchitecture if hw.cpu else None
|
||||
snapshot.cpu_cores = hw.cpu.cores if hw.cpu else None
|
||||
snapshot.cpu_threads = hw.cpu.threads if hw.cpu else None
|
||||
snapshot.cpu_base_freq_ghz = hw.cpu.base_freq_ghz if hw.cpu else None
|
||||
snapshot.cpu_max_freq_ghz = hw.cpu.max_freq_ghz if hw.cpu else None
|
||||
snapshot.cpu_cache_l1_kb = hw.cpu.cache_l1_kb if hw.cpu else None
|
||||
snapshot.cpu_cache_l2_kb = hw.cpu.cache_l2_kb if hw.cpu else None
|
||||
snapshot.cpu_cache_l3_kb = hw.cpu.cache_l3_kb if hw.cpu else None
|
||||
snapshot.cpu_flags = json.dumps(hw.cpu.flags) if hw.cpu and hw.cpu.flags else None
|
||||
snapshot.cpu_tdp_w = hw.cpu.tdp_w if hw.cpu else None
|
||||
|
||||
# Storage
|
||||
storage_summary=f"{len(hw.storage.devices)} device(s)" if hw.storage and hw.storage.devices else None,
|
||||
storage_devices_json=json.dumps([d.dict() for d in hw.storage.devices]) if hw.storage and hw.storage.devices else None,
|
||||
partitions_json=json.dumps([p.dict() for p in hw.storage.partitions]) if hw.storage and hw.storage.partitions else None,
|
||||
# RAM
|
||||
snapshot.ram_total_mb = hw.ram.total_mb if hw.ram else None
|
||||
snapshot.ram_used_mb = hw.ram.used_mb if hw.ram else None
|
||||
snapshot.ram_free_mb = hw.ram.free_mb if hw.ram else None
|
||||
snapshot.ram_shared_mb = hw.ram.shared_mb if hw.ram else None
|
||||
snapshot.ram_slots_total = hw.ram.slots_total if hw.ram else None
|
||||
snapshot.ram_slots_used = hw.ram.slots_used if hw.ram else None
|
||||
snapshot.ram_ecc = hw.ram.ecc if hw.ram else None
|
||||
snapshot.ram_layout_json = json.dumps([slot.dict() for slot in hw.ram.layout]) if hw.ram and hw.ram.layout else None
|
||||
|
||||
# Network
|
||||
network_interfaces_json=json.dumps([i.dict() for i in hw.network.interfaces]) if hw.network and hw.network.interfaces else None,
|
||||
# GPU
|
||||
snapshot.gpu_summary = f"{hw.gpu.vendor} {hw.gpu.model}" if hw.gpu and hw.gpu.model else None
|
||||
snapshot.gpu_vendor = hw.gpu.vendor if hw.gpu else None
|
||||
snapshot.gpu_model = hw.gpu.model if hw.gpu else None
|
||||
snapshot.gpu_driver_version = hw.gpu.driver_version if hw.gpu else None
|
||||
snapshot.gpu_memory_dedicated_mb = hw.gpu.memory_dedicated_mb if hw.gpu else None
|
||||
snapshot.gpu_memory_shared_mb = hw.gpu.memory_shared_mb if hw.gpu else None
|
||||
snapshot.gpu_api_support = json.dumps(hw.gpu.api_support) if hw.gpu and hw.gpu.api_support else None
|
||||
|
||||
# OS / Motherboard
|
||||
os_name=hw.os.name if hw.os else None,
|
||||
os_version=hw.os.version if hw.os else None,
|
||||
kernel_version=hw.os.kernel_version if hw.os else None,
|
||||
architecture=hw.os.architecture if hw.os else None,
|
||||
virtualization_type=hw.os.virtualization_type if hw.os else None,
|
||||
motherboard_vendor=hw.motherboard.vendor if hw.motherboard else None,
|
||||
motherboard_model=hw.motherboard.model if hw.motherboard else None,
|
||||
bios_version=hw.motherboard.bios_version if hw.motherboard else None,
|
||||
bios_date=hw.motherboard.bios_date if hw.motherboard else None,
|
||||
# Storage
|
||||
snapshot.storage_summary = f"{len(hw.storage.devices)} device(s)" if hw.storage and hw.storage.devices else None
|
||||
snapshot.storage_devices_json = json.dumps([d.dict() for d in hw.storage.devices]) if hw.storage and hw.storage.devices else None
|
||||
snapshot.partitions_json = json.dumps([p.dict() for p in hw.storage.partitions]) if hw.storage and hw.storage.partitions else None
|
||||
|
||||
# Misc
|
||||
sensors_json=json.dumps(hw.sensors.dict()) if hw.sensors else None,
|
||||
raw_info_json=json.dumps(hw.raw_info.dict()) if hw.raw_info else None
|
||||
)
|
||||
# Network
|
||||
snapshot.network_interfaces_json = json.dumps([i.dict() for i in hw.network.interfaces]) if hw.network and hw.network.interfaces else None
|
||||
|
||||
db.add(snapshot)
|
||||
db.flush() # Get snapshot.id
|
||||
# OS / Motherboard
|
||||
snapshot.os_name = hw.os.name if hw.os else None
|
||||
snapshot.os_version = hw.os.version if hw.os else None
|
||||
snapshot.kernel_version = hw.os.kernel_version if hw.os else None
|
||||
snapshot.architecture = hw.os.architecture if hw.os else None
|
||||
snapshot.virtualization_type = hw.os.virtualization_type if hw.os else None
|
||||
snapshot.motherboard_vendor = hw.motherboard.vendor if hw.motherboard else None
|
||||
snapshot.motherboard_model = hw.motherboard.model if hw.motherboard else None
|
||||
snapshot.bios_vendor = hw.motherboard.bios_vendor if hw.motherboard and hasattr(hw.motherboard, 'bios_vendor') else None
|
||||
snapshot.bios_version = hw.motherboard.bios_version if hw.motherboard else None
|
||||
snapshot.bios_date = hw.motherboard.bios_date if hw.motherboard else None
|
||||
|
||||
# Misc
|
||||
snapshot.sensors_json = json.dumps(hw.sensors.dict()) if hw.sensors else None
|
||||
snapshot.raw_info_json = json.dumps(hw.raw_info.dict()) if hw.raw_info else None
|
||||
|
||||
# Add to session only if it's a new snapshot
|
||||
if not existing_snapshot:
|
||||
db.add(snapshot)
|
||||
|
||||
db.flush() # Get snapshot.id for new snapshots
|
||||
|
||||
# 3. Create benchmark
|
||||
results = payload.results
|
||||
|
||||
@@ -11,9 +11,11 @@ from app.db.session import get_db
|
||||
from app.schemas.device import DeviceListResponse, DeviceDetail, DeviceSummary, DeviceUpdate
|
||||
from app.schemas.benchmark import BenchmarkSummary
|
||||
from app.schemas.hardware import HardwareSnapshotResponse
|
||||
from app.schemas.document import DocumentResponse
|
||||
from app.models.device import Device
|
||||
from app.models.benchmark import Benchmark
|
||||
from app.models.hardware_snapshot import HardwareSnapshot
|
||||
from app.models.document import Document
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -160,6 +162,24 @@ async def get_device(
|
||||
motherboard_model=last_snapshot.motherboard_model
|
||||
)
|
||||
|
||||
# Get documents for this device
|
||||
documents = db.query(Document).filter(
|
||||
Document.device_id == device.id
|
||||
).all()
|
||||
|
||||
documents_list = [
|
||||
DocumentResponse(
|
||||
id=doc.id,
|
||||
device_id=doc.device_id,
|
||||
doc_type=doc.doc_type,
|
||||
filename=doc.filename,
|
||||
mime_type=doc.mime_type,
|
||||
size_bytes=doc.size_bytes,
|
||||
uploaded_at=doc.uploaded_at.isoformat()
|
||||
)
|
||||
for doc in documents
|
||||
]
|
||||
|
||||
return DeviceDetail(
|
||||
id=device.id,
|
||||
hostname=device.hostname,
|
||||
@@ -172,7 +192,8 @@ async def get_device(
|
||||
created_at=device.created_at.isoformat(),
|
||||
updated_at=device.updated_at.isoformat(),
|
||||
last_benchmark=last_bench_summary,
|
||||
last_hardware_snapshot=last_snapshot_data
|
||||
last_hardware_snapshot=last_snapshot_data,
|
||||
documents=documents_list
|
||||
)
|
||||
|
||||
|
||||
@@ -246,7 +267,9 @@ async def update_device(
|
||||
for key, value in update_dict.items():
|
||||
setattr(device, key, value)
|
||||
|
||||
device.updated_at = db.query(Device).filter(Device.id == device_id).first().updated_at
|
||||
# Update timestamp
|
||||
from datetime import datetime
|
||||
device.updated_at = datetime.utcnow()
|
||||
|
||||
db.commit()
|
||||
db.refresh(device)
|
||||
|
||||
Reference in New Issue
Block a user