2020-08-04 23:27:13 +02:00
|
|
|
import bcrypt, time
|
2020-08-11 15:02:42 +02:00
|
|
|
from sqlalchemy.orm import Session, load_only
|
2020-08-11 11:48:46 +02:00
|
|
|
from app import models, schemas
|
2020-08-04 23:27:13 +02:00
|
|
|
|
|
|
|
|
2020-08-07 02:25:33 +02:00
|
|
|
User=models.User
|
|
|
|
Service=models.Service
|
2020-08-11 15:02:42 +02:00
|
|
|
Metric=models.Metric
|
2020-08-07 02:25:33 +02:00
|
|
|
|
2020-08-04 23:27:13 +02:00
|
|
|
def get_user(db: Session, user_id: int):
|
2020-08-07 02:25:33 +02:00
|
|
|
return db.query(User).filter(User.id == user_id).first()
|
2020-08-04 23:27:13 +02:00
|
|
|
|
2020-08-07 02:00:35 +02:00
|
|
|
|
2020-08-04 23:27:13 +02:00
|
|
|
def del_user(db: Session, user_id: int):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(User).filter(User.id == user_id).delete()
|
2020-08-07 02:00:35 +02:00
|
|
|
return db.commit()
|
|
|
|
|
2020-08-04 23:27:13 +02:00
|
|
|
|
|
|
|
def get_user_by_username(db: Session, name: str):
|
2020-08-07 02:25:33 +02:00
|
|
|
return db.query(User).filter(User.name == name).first()
|
2020-08-04 23:27:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
2020-08-07 02:25:33 +02:00
|
|
|
return db.query(User).offset(skip).limit(limit).all()
|
2020-08-04 23:27:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_user(db: Session, user: schemas.UserCreate):
|
2020-08-07 02:00:35 +02:00
|
|
|
user.password = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt())
|
2020-08-07 02:25:33 +02:00
|
|
|
db_user = User(**user.dict())
|
2020-08-04 23:27:13 +02:00
|
|
|
db_user.created_unix = time.time()
|
|
|
|
db.add(db_user)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(db_user)
|
|
|
|
return db_user
|
|
|
|
|
|
|
|
|
2020-08-07 02:00:35 +02:00
|
|
|
def update_user(db: Session, user_id: int, user: schemas.UserUpdate):
|
2020-08-11 11:48:46 +02:00
|
|
|
updated_user = User(**user.dict(exclude_unset=True))
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_user.password is not None):
|
|
|
|
updated_user.password = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt())
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(User).filter(User.id==user_id).update({User.password: updated_user.password})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_user.name is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(User).filter(User.id==user_id).update({User.name: updated_user.name})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_user.full_name is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(User).filter(User.id==user_id).update({User.full_name: updated_user.full_name})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_user.is_active is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(User).filter(User.id==user_id).update({User.is_active: updated_user.is_active})
|
2020-08-11 15:02:42 +02:00
|
|
|
if (updated_user.is_superuser is not None):
|
|
|
|
db.query(User).filter(User.id==user_id).update({User.is_superuser: updated_user.is_superuser})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_user is not user):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(User).filter(User.id==user_id).update({User.updated_unix: time.time()})
|
2020-08-07 02:00:35 +02:00
|
|
|
return db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_service(db: Session, service_id: int):
|
2020-08-07 02:25:33 +02:00
|
|
|
return db.query(Service).filter(Service.id == service_id).first()
|
2020-08-07 02:00:35 +02:00
|
|
|
|
|
|
|
|
2020-08-04 23:27:13 +02:00
|
|
|
def get_services(db: Session, skip: int = 0, limit: int = 100):
|
2020-08-07 02:25:33 +02:00
|
|
|
return db.query(Service).offset(skip).limit(limit).all()
|
2020-08-04 23:27:13 +02:00
|
|
|
|
2020-08-07 02:00:35 +02:00
|
|
|
|
2020-08-04 23:27:13 +02:00
|
|
|
def create_service(db: Session, service: schemas.ServiceCreate):
|
2020-08-07 02:25:33 +02:00
|
|
|
db_item = Service(**service.dict())
|
2020-08-04 23:27:13 +02:00
|
|
|
db_item.created_unix = time.time()
|
|
|
|
db.add(db_item)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(db_item)
|
|
|
|
return db_item
|
|
|
|
|
2020-08-07 02:00:35 +02:00
|
|
|
|
|
|
|
def del_service(db: Session, service_id: int):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id == service_id).delete()
|
2020-08-07 02:00:35 +02:00
|
|
|
return db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
def update_service(db: Session, service_id: int, s: schemas.ServiceUpdate):
|
2020-08-11 11:48:46 +02:00
|
|
|
updated_s = Service(**s.dict(exclude_unset=True))
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s.name is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.name: updated_s.name})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s.owner_id is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.owner_id: updated_s.owner_id})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s.is_private is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.is_private: updated_s.is_private})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s.description is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.description: updated_s.description})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s.service_type is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.service_type: updated_s.service_type})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s.url is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.url: updated_s.url})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s.is_active is not None):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.is_active: updated_s.is_active})
|
2020-08-07 02:00:35 +02:00
|
|
|
if (updated_s is not s):
|
2020-08-07 02:25:33 +02:00
|
|
|
db.query(Service).filter(Service.id==service_id).update({Service.updated_unix: time.time()})
|
2020-08-07 02:00:35 +02:00
|
|
|
return db.commit()
|
|
|
|
|
2020-08-11 11:48:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def login_info(db: Session, name: str, password: str):
|
|
|
|
try:
|
|
|
|
usr = db.query(User).filter(User.name == name).first()
|
|
|
|
if bcrypt.hashpw(password.encode(), usr.password) == usr.password:
|
|
|
|
print("[*] match")
|
|
|
|
else:
|
|
|
|
print("[x] no match")
|
|
|
|
return None
|
|
|
|
except Exception as e:
|
|
|
|
print('exception', e)
|
|
|
|
return
|
|
|
|
return usr
|
|
|
|
|
2020-08-11 15:02:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_urls(db: Session):
|
|
|
|
# return db.query(Service).filter(Service.is_active == True).options(load_only(Service.url)).all()
|
|
|
|
return db.query(Service).filter(Service.is_active == True).all()
|
|
|
|
|
|
|
|
|
|
|
|
def get_metrics(db: Session, skip: int = 0, limit: int = 90):
|
|
|
|
return db.query(Metrics).offset(skip).limit(limit).all()
|
|
|
|
|
|
|
|
def create_metric(db: Session, me: schemas.MetricCreate):
|
|
|
|
db_metric = User(**me.dict())
|
|
|
|
if (me.http_response_code >=500):
|
|
|
|
db_metric.status=2
|
|
|
|
elif (me.http_response_code >=400):
|
|
|
|
db_metric.status=1
|
|
|
|
elif (me.http_response_code >=200):
|
|
|
|
db_metric.status=0
|
|
|
|
db.add(db_metric)
|
|
|
|
db.commit()
|
|
|
|
return db.commit()
|
|
|
|
|