Projekt

Obecné

Profil

Stáhnout (2.82 KB) Statistiky
| Větev: | Tag: | Revize:
1
from typing import List
2
from fastapi import Depends, FastAPI, HTTPException, APIRouter, Form
3
from datetime import datetime
4
from sqlalchemy.orm import Session
5
from sql_app import crud, models, schemas
6
from ..database import SessionLocal, engine
7

    
8
models.Base.metadata.create_all(bind=engine)
9

    
10
# prefix used for all endpoints in this file
11
usblogs = APIRouter(prefix="/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
@usblogs.post("/usb-logs", response_model=schemas.USBLog)
24
def create_device_logs(log: schemas.USBTempBase, db: Session = Depends(get_db)):
25
    """
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
    dev = crud.find_device(db, log.device)
31
    dat = datetime.strptime(log.timestamp, '%Y-%m-%d %H:%M:%S')
32
    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
    print(crud.create_device_logs(db=db, item=log, dev_id=dev.id, pc_id=pc.id, date=dat))
39

    
40

    
41
@usblogs.post("/ld-logs", response_model=schemas.LDLog)
42
def create_ld_logs(log: schemas.LDTempBase, db: Session = Depends(get_db)):
43
    """
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
    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
        head_dev = crud.create_head_device(db, log.head_device)
52
    if body_dev is None:
53
        body_dev = crud.create_body_device(db, log.body_device)
54

    
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
@usblogs.get("/logs", response_model=List[schemas.USBLog])
63
def read_logs(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
64
    """
65
    Returns all usb logs saved in database
66
    """
67
    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
    """
74
    Returns one specific log by given id
75
    """
76
    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
(14-14/16)