Revize aba60b70
Přidáno uživatelem Matěj Zeman před asi 3 roky(ů)
server/sql_app/crud.py | ||
---|---|---|
1 |
from sqlalchemy.orm import Session |
|
2 |
|
|
3 |
from . import models, schemas |
|
4 |
|
|
5 |
|
|
6 |
def get_device(db: Session, device_id: int): |
|
7 |
return db.query(models.Device).filter(models.Device.id == device_id).first() |
|
8 |
|
|
9 |
|
|
10 |
def get_devices(db: Session, skip: int = 0, limit: int = 100): |
|
11 |
return db.query(models.Device).offset(skip).limit(limit).all() |
|
12 |
|
|
13 |
|
|
14 |
def find_device(db: Session, device: schemas.DeviceBase): |
|
15 |
return db.query(models.Device).filter(models.Device.product_id == device.product_id and |
|
16 |
models.Device.vendor_id == device.vendor_id and |
|
17 |
models.Device.serial_number == device.serial_number).first() |
|
18 |
|
|
19 |
|
|
20 |
def create_device(db: Session, device: schemas.DeviceBase): |
|
21 |
db_device = models.Device(vendor_id=device.vendor_id, product_id=device.product_id, |
|
22 |
serial_number=device.serial_number) |
|
23 |
db.add(db_device) |
|
24 |
db.commit() |
|
25 |
db.refresh(db_device) |
|
26 |
return db_device |
|
27 |
|
|
28 |
|
|
29 |
def get_logs(db: Session, skip: int = 0, limit: int = 100): |
|
30 |
return db.query(models.USBLog).offset(skip).limit(limit).all() |
|
31 |
|
|
32 |
|
|
33 |
def get_log(db: Session, device_id: int, skip: int = 0, limit: int = 100): |
|
34 |
return db.query(models.USBLog).filter(models.USBLog.device_id == device_id).offset(skip).limit(limit).all() |
|
35 |
|
|
36 |
|
|
37 |
def create_device_logs(db: Session, item: schemas.USBTempBase, dev_id: int): |
|
38 |
db_log = models.USBLog(username=item.username, hostname=item.hostname, timestamp=item.timestamp, |
|
39 |
status=item.status, device_id=dev_id) |
|
40 |
db.add(db_log) |
|
41 |
db.commit() |
|
42 |
db.refresh(db_log) |
|
43 |
return db_log |
server/sql_app/database.py | ||
---|---|---|
1 |
from sqlalchemy import create_engine |
|
2 |
from sqlalchemy.ext.declarative import declarative_base |
|
3 |
from sqlalchemy.orm import sessionmaker |
|
4 |
|
|
5 |
SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db" |
|
6 |
|
|
7 |
engine = create_engine( |
|
8 |
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} |
|
9 |
) |
|
10 |
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
|
11 |
|
|
12 |
Base = declarative_base() |
|
13 |
|
server/sql_app/main.py | ||
---|---|---|
1 |
from fastapi import FastAPI |
|
2 |
import fastapi |
|
3 |
from pydantic import BaseModel |
|
4 |
from typing import Optional, List |
|
5 |
|
|
6 |
app = FastAPI() |
|
7 |
|
|
8 |
class Device(BaseModel): |
|
9 |
vendor_id: int |
|
10 |
product_id: int |
|
11 |
serial_number: str |
|
12 |
|
|
13 |
class USBLog(BaseModel): |
|
14 |
username: str |
|
15 |
hostname: str |
|
16 |
timestamp: str |
|
17 |
device: Device |
|
18 |
status:str |
|
19 |
|
|
20 |
@app.post("/api/v1/usb-logs/") |
|
21 |
async def root_post(usb_log: USBLog): |
|
22 |
print(usb_log) |
|
23 |
return {"message": "Sucess"} |
|
24 |
|
|
25 |
@app.post("/api/v1/devices/") |
|
26 |
async def root_dev_post(device: Device): |
|
27 |
print(device) |
|
28 |
return {"message": "Sucess"} |
|
29 |
|
|
30 |
@app.get("/") |
|
31 |
async def root_read(): |
|
32 |
print("Hello") |
|
33 |
return {"message":"Hello World"} |
|
1 |
from typing import List |
|
2 |
|
|
3 |
from fastapi import Depends, FastAPI, HTTPException |
|
4 |
from sqlalchemy.orm import Session |
|
5 |
from sql_app import crud, models, schemas |
|
6 |
import uvicorn |
|
7 |
from database import SessionLocal, engine |
|
8 |
|
|
9 |
models.Base.metadata.create_all(bind=engine) |
|
10 |
|
|
11 |
app = FastAPI(root_path="/api/v1") |
|
12 |
|
|
13 |
|
|
14 |
# Dependency |
|
15 |
def get_db(): |
|
16 |
db = SessionLocal() |
|
17 |
try: |
|
18 |
yield db |
|
19 |
finally: |
|
20 |
db.close() |
|
21 |
|
|
22 |
|
|
23 |
@app.post("/device/", response_model=schemas.Device) |
|
24 |
def create_device(device: schemas.DeviceCreate, db: Session = Depends(get_db)): |
|
25 |
print(crud.create_device(db=db, device=device)) |
|
26 |
|
|
27 |
|
|
28 |
@app.get("/devices/", response_model=List[schemas.Device]) |
|
29 |
def read_devices(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
|
30 |
devices = crud.get_devices(db, skip=skip, limit=limit) |
|
31 |
return devices |
|
32 |
|
|
33 |
|
|
34 |
@app.get("/device/{device_id}", response_model=schemas.Device) |
|
35 |
def read_device(device_id: int, db: Session = Depends(get_db)): |
|
36 |
db_device = crud.get_device(db, device_id=device_id) |
|
37 |
if db_device is None: |
|
38 |
raise HTTPException(status_code=404, detail="Device not found") |
|
39 |
return db_device |
|
40 |
|
|
41 |
|
|
42 |
@app.post("/usb-logs/", response_model=schemas.USBLog) |
|
43 |
def create_device_logs(log: schemas.USBTempBase, db: Session = Depends(get_db)): |
|
44 |
dev = crud.find_device(db, log.device) |
|
45 |
if dev is None: |
|
46 |
dev = crud.create_device(db=db, device=log.device) |
|
47 |
|
|
48 |
print(crud.create_device_logs(db=db, item=log, dev_id=dev.id)) |
|
49 |
|
|
50 |
|
|
51 |
@app.get("/logs/", response_model=List[schemas.USBLog]) |
|
52 |
def read_logs(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): |
|
53 |
items = crud.get_logs(db, skip=skip, limit=limit) |
|
54 |
return items |
|
55 |
|
|
56 |
|
|
57 |
@app.get("/logs/{device_id}", response_model=List[schemas.USBLog]) |
|
58 |
def read_log(device_id: int, db: Session = Depends(get_db)): |
|
59 |
db_log = crud.get_log(db, device_id=device_id) |
|
60 |
if db_log is None: |
|
61 |
raise HTTPException(status_code=404, detail="Logs not found") |
|
62 |
return db_log |
|
63 |
|
|
64 |
|
|
65 |
if __name__ == "__main__": |
|
66 |
uvicorn.run(app, host="192.168.0.22", port=8000) |
server/sql_app/models.py | ||
---|---|---|
1 |
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String |
|
2 |
from sqlalchemy.orm import relationship |
|
3 |
|
|
4 |
from .database import Base |
|
5 |
|
|
6 |
|
|
7 |
class Device(Base): |
|
8 |
__tablename__ = "devices" |
|
9 |
|
|
10 |
id = Column(Integer, primary_key=True, index=True) |
|
11 |
vendor_id = Column(Integer, index=True, nullable=False) |
|
12 |
product_id = Column(Integer, index=True, nullable=False) |
|
13 |
serial_number = Column(String, index=True, nullable=False) |
|
14 |
|
|
15 |
logs = relationship("USBLog", back_populates="device") |
|
16 |
|
|
17 |
|
|
18 |
class USBLog(Base): |
|
19 |
__tablename__ = "usb_logs" |
|
20 |
|
|
21 |
id = Column(Integer, primary_key=True, index=True) |
|
22 |
username = Column(String, index=True, nullable=False) |
|
23 |
hostname = Column(String, index=True, nullable=False) |
|
24 |
timestamp = Column(String, index=True, nullable=False) |
|
25 |
status = Column(String, index=True, nullable=False) |
|
26 |
device_id = Column(Integer, ForeignKey("devices.id")) |
|
27 |
|
|
28 |
device = relationship("Device", back_populates="logs") |
server/sql_app/schemas.py | ||
---|---|---|
1 |
from typing import List, Optional |
|
2 |
|
|
3 |
from pydantic import BaseModel |
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
class USBLogBase(BaseModel): |
|
8 |
timestamp: str |
|
9 |
status: str |
|
10 |
|
|
11 |
|
|
12 |
class USBLogCreate(USBLogBase): |
|
13 |
pass |
|
14 |
|
|
15 |
|
|
16 |
class USBLog(USBLogBase): |
|
17 |
id: int |
|
18 |
device_id: int |
|
19 |
|
|
20 |
class Config: |
|
21 |
orm_mode = True |
|
22 |
|
|
23 |
|
|
24 |
class DeviceBase(BaseModel): |
|
25 |
vendor_id: int |
|
26 |
product_id: int |
|
27 |
serial_number: str |
|
28 |
|
|
29 |
|
|
30 |
class DeviceCreate(DeviceBase): |
|
31 |
pass |
|
32 |
|
|
33 |
|
|
34 |
class Device(DeviceBase): |
|
35 |
id: int |
|
36 |
logs: List[USBLog] = [] |
|
37 |
|
|
38 |
class Config: |
|
39 |
orm_mode = True |
|
40 |
|
|
41 |
|
|
42 |
class USBTempBase(BaseModel): |
|
43 |
username: str |
|
44 |
hostname: str |
|
45 |
timestamp: str |
|
46 |
device: DeviceBase |
|
47 |
status: str |
|
48 |
|
|
49 |
|
|
50 |
class USBTempCreate(USBTempBase): |
|
51 |
pass |
|
52 |
|
|
53 |
|
|
54 |
class USBTemp(USBTempBase): |
|
55 |
id: int |
|
56 |
device_id: int |
|
57 |
|
|
58 |
class Config: |
|
59 |
orm_mode = True |
Také k dispozici: Unified diff
re #9425 Implemented Entities for communicating with db