Projekt

Obecné

Profil

Stáhnout (2.82 KB) Statistiky
| Větev: | Tag: | Revize:
1 6c1e92e3 Matej Zeman
from typing import List
2
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
3 99c92c11 Matej Zeman
from datetime import datetime
4 6c1e92e3 Matej Zeman
from sqlalchemy.orm import Session
5 ef87a1d2 Matej Zeman
from sql_app import crud, models, schemas
6 6c1e92e3 Matej Zeman
from ..database import SessionLocal, engine
7
8
models.Base.metadata.create_all(bind=engine)
9
10 4911f0ea Matej Zeman
# prefix used for all endpoints in this file
11 2229e0e1 Matej Zeman
usblogs = APIRouter(prefix="/api/v1")
12 6c1e92e3 Matej Zeman
13
14
# Dependency
15
def get_db():
16
    db = SessionLocal()
17
    try:
18
        yield db
19
    finally:
20
        db.close()
21
22
23 2229e0e1 Matej Zeman
@usblogs.post("/usb-logs", response_model=schemas.USBLog)
24 6c1e92e3 Matej Zeman
def create_device_logs(log: schemas.USBTempBase, db: Session = Depends(get_db)):
25 4911f0ea Matej Zeman
    """
26
    Endpoint called from keyman detecting client. Parses timestamp into datetime object.
27
    Finds if device and pc defined in message already exists and creates them if necessary.
28
    Saves log into database
29
    """
30 6c1e92e3 Matej Zeman
    dev = crud.find_device(db, log.device)
31 2229e0e1 Matej Zeman
    dat = datetime.strptime(log.timestamp, '%Y-%m-%d %H:%M:%S')
32 6c1e92e3 Matej Zeman
    if dev is None:
33
        dev = crud.create_device(db=db, device=log.device)
34
    pc = crud.find_pc(db, log.username, log.hostname)
35
    if pc is None:
36
        pc = crud.create_pc(db=db, user=log.username, host=log.hostname)
37
38 99c92c11 Matej Zeman
    print(crud.create_device_logs(db=db, item=log, dev_id=dev.id, pc_id=pc.id, date=dat))
39 6c1e92e3 Matej Zeman
40
41 eaf8ace4 Matej Zeman
@usblogs.post("/ld-logs", response_model=schemas.LDLog)
42
def create_ld_logs(log: schemas.LDTempBase, db: Session = Depends(get_db)):
43 4911f0ea Matej Zeman
    """
44
    Endpoint called from debugger detecting client. Parses timestamp into datetime object.
45
    Finds if head device and body device defined in message already exists and creates them if necessary.
46
    Saves log into database
47
    """
48 eaf8ace4 Matej Zeman
    head_dev = crud.find_head_device(db, log.head_device)
49
    body_dev = crud.find_body_device(db, log.body_device)
50
    if head_dev is None:
51 af6f5886 Matěj Zeman
        head_dev = crud.create_head_device(db, log.head_device)
52 eaf8ace4 Matej Zeman
    if body_dev is None:
53 af6f5886 Matěj Zeman
        body_dev = crud.create_body_device(db, log.body_device)
54 eaf8ace4 Matej Zeman
55
    pc = crud.find_pc(db, log.username, log.hostname)
56
    if pc is None:
57
        pc = crud.create_pc(db=db, user=log.username, host=log.hostname)
58
    dat = datetime.strptime(log.timestamp, '%Y-%m-%d %H:%M:%S')
59
    print(crud.create_ld_logs(db=db, item=log, head_id=head_dev.id, body_id=body_dev.id, pc_id=pc.id, date=dat))
60
61
62 2229e0e1 Matej Zeman
@usblogs.get("/logs", response_model=List[schemas.USBLog])
63 6c1e92e3 Matej Zeman
def read_logs(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
64 4911f0ea Matej Zeman
    """
65
    Returns all usb logs saved in database
66
    """
67 6c1e92e3 Matej Zeman
    items = crud.get_logs(db, skip=skip, limit=limit)
68
    return items
69
70
71
@usblogs.get("/logs/{device_id}", response_model=List[schemas.USBLog])
72
def read_log(device_id: int, db: Session = Depends(get_db)):
73 4911f0ea Matej Zeman
    """
74
    Returns one specific log by given id
75
    """
76 6c1e92e3 Matej Zeman
    db_log = crud.get_log(db, device_id=device_id)
77
    if db_log is None:
78
        raise HTTPException(status_code=404, detail="Logs not found")
79
    return db_log