mirror of
https://github.com/imartinez/privateGPT.git
synced 2025-08-11 04:12:03 +00:00
Update with filter for audit
This commit is contained in:
parent
8bc7fb0039
commit
97317b82e0
@ -46,6 +46,39 @@ def list_auditlog(
|
|||||||
return logs
|
return logs
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("filter/", response_model=List[schemas.Audit])
|
||||||
|
def filter_auditlog(
|
||||||
|
db: Session = Depends(deps.get_db),
|
||||||
|
filter_in= Depends(schemas.AuditFilter),
|
||||||
|
current_user: models.User = Security(
|
||||||
|
deps.get_current_user,
|
||||||
|
scopes=[Role.SUPER_ADMIN["name"]],
|
||||||
|
),
|
||||||
|
) -> List[schemas.Audit]:
|
||||||
|
"""
|
||||||
|
Retrieve a list of audit logs with pagination support.
|
||||||
|
"""
|
||||||
|
def get_fullname(id):
|
||||||
|
user = crud.user.get_by_id(db, id=id)
|
||||||
|
if user:
|
||||||
|
return user.username
|
||||||
|
return ""
|
||||||
|
|
||||||
|
logs = crud.audit.filter(db, obj_in=filter_in)
|
||||||
|
logs = [
|
||||||
|
schemas.Audit(
|
||||||
|
id=dep.id,
|
||||||
|
model=dep.model,
|
||||||
|
username=get_fullname(dep.user_id),
|
||||||
|
details=dep.details,
|
||||||
|
action=dep.action,
|
||||||
|
timestamp=dep.timestamp,
|
||||||
|
ip_address=dep.ip_address,
|
||||||
|
)
|
||||||
|
for dep in logs
|
||||||
|
]
|
||||||
|
return logs
|
||||||
|
|
||||||
|
|
||||||
@router.post("", response_model=schemas.Audit)
|
@router.post("", response_model=schemas.Audit)
|
||||||
def get_auditlog(
|
def get_auditlog(
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
|
from private_gpt.users import crud
|
||||||
from private_gpt.users.crud.base import CRUDBase
|
from private_gpt.users.crud.base import CRUDBase
|
||||||
from private_gpt.users.models.audit import Audit
|
from private_gpt.users.models.audit import Audit
|
||||||
from private_gpt.users.schemas.audit import AuditCreate, AuditUpdate
|
from private_gpt.users.schemas.audit import AuditCreate, AuditUpdate, AuditFilter
|
||||||
from sqlalchemy import desc
|
from sqlalchemy import desc
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@ -13,6 +14,29 @@ class CRUDAudit(CRUDBase[Audit, AuditCreate, AuditUpdate]):
|
|||||||
) -> List[Audit]:
|
) -> List[Audit]:
|
||||||
return db.query(self.model).order_by(desc(self.model.timestamp)).offset(skip).limit(limit).all()
|
return db.query(self.model).order_by(desc(self.model.timestamp)).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
|
def filter(
|
||||||
|
self, db: Session, *, obj_in : AuditFilter
|
||||||
|
) -> List[Audit]:
|
||||||
|
|
||||||
|
def get_id(username):
|
||||||
|
user = crud.user.get_by_name(db, name=username)
|
||||||
|
if user:
|
||||||
|
return user.id
|
||||||
|
return None
|
||||||
|
query = db.query(Audit)
|
||||||
|
if obj_in.model:
|
||||||
|
query = query.filter(Audit.model == obj_in.model)
|
||||||
|
if obj_in.username:
|
||||||
|
query = query.filter(Audit.user_id == get_id(obj_in.username))
|
||||||
|
if obj_in.action:
|
||||||
|
query = query.filter(Audit.action == obj_in.action)
|
||||||
|
if obj_in.start_date:
|
||||||
|
query = query.filter(Audit.timestamp >= obj_in.start_date)
|
||||||
|
if obj_in.end_date:
|
||||||
|
query = query.filter(Audit.timestamp <= obj_in.end_date)
|
||||||
|
|
||||||
|
return query.order_by(desc(self.model.timestamp)).offset(obj_in.skip).limit(obj_in.limit).all()
|
||||||
|
|
||||||
def get_by_id(self, db: Session, *, id: str) -> Optional[Audit]:
|
def get_by_id(self, db: Session, *, id: str) -> Optional[Audit]:
|
||||||
return db.query(self.model).filter(Audit.id == id).first()
|
return db.query(self.model).filter(Audit.id == id).first()
|
||||||
|
|
||||||
|
@ -11,12 +11,20 @@ from private_gpt.users.schemas.chat import ChatHistoryCreate, ChatHistoryCreate,
|
|||||||
|
|
||||||
class CRUDChat(CRUDBase[ChatHistory, ChatHistoryCreate, ChatHistoryCreate]):
|
class CRUDChat(CRUDBase[ChatHistory, ChatHistoryCreate, ChatHistoryCreate]):
|
||||||
def get_by_id(self, db: Session, *, id: uuid.UUID) -> Optional[ChatHistory]:
|
def get_by_id(self, db: Session, *, id: uuid.UUID) -> Optional[ChatHistory]:
|
||||||
return (
|
chat_history = (
|
||||||
db.query(self.model)
|
db.query(self.model)
|
||||||
.filter(ChatHistory.conversation_id == id)
|
.filter(ChatHistory.conversation_id == id)
|
||||||
.order_by(asc(getattr(ChatHistory, 'created_at')))
|
.order_by(asc(getattr(ChatHistory, 'created_at')))
|
||||||
.first()
|
.first()
|
||||||
|
)
|
||||||
|
if chat_history:
|
||||||
|
chat_history.chat_items = (
|
||||||
|
db.query(ChatItem)
|
||||||
|
.filter(ChatItem.conversation_id == id)
|
||||||
|
.order_by(asc(getattr(ChatItem, 'index')))
|
||||||
|
.all()
|
||||||
)
|
)
|
||||||
|
return chat_history
|
||||||
|
|
||||||
def get_chat_history(
|
def get_chat_history(
|
||||||
self, db: Session, *,user_id:int, skip: int = 0, limit: int = 100
|
self, db: Session, *,user_id:int, skip: int = 0, limit: int = 100
|
||||||
|
@ -18,7 +18,7 @@ from .documents import (
|
|||||||
from .department import (
|
from .department import (
|
||||||
Department, DepartmentCreate, DepartmentUpdate, DepartmentAdminCreate, DepartmentDelete
|
Department, DepartmentCreate, DepartmentUpdate, DepartmentAdminCreate, DepartmentDelete
|
||||||
)
|
)
|
||||||
from .audit import AuditBase, AuditCreate, AuditUpdate, Audit, GetAudit
|
from .audit import AuditBase, AuditCreate, AuditUpdate, Audit, GetAudit, AuditFilter
|
||||||
from .chat import (
|
from .chat import (
|
||||||
ChatHistory, ChatHistoryBase, ChatHistoryCreate, ChatHistoryUpdate, ChatDelete,
|
ChatHistory, ChatHistoryBase, ChatHistoryCreate, ChatHistoryUpdate, ChatDelete,
|
||||||
ChatItem, ChatItemBase, ChatItemCreate, ChatItemUpdate, CreateChatHistory
|
ChatItem, ChatItemBase, ChatItemCreate, ChatItemUpdate, CreateChatHistory
|
||||||
|
@ -38,4 +38,14 @@ class Audit(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class GetAudit(BaseModel):
|
class GetAudit(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
|
|
||||||
|
|
||||||
|
class AuditFilter(BaseModel):
|
||||||
|
skip: Optional[int],
|
||||||
|
limit: Optional[int],
|
||||||
|
model: Optional[str] = None
|
||||||
|
username: Optional[str] = None
|
||||||
|
action: Optional[str] = None
|
||||||
|
start_date: Optional[datetime] = None
|
||||||
|
end_date: Optional[datetime] = None
|
@ -20,6 +20,7 @@ class ChatItem(ChatItemBase):
|
|||||||
id: int
|
id: int
|
||||||
created_at: datetime
|
created_at: datetime
|
||||||
updated_at: datetime
|
updated_at: datetime
|
||||||
|
index: int
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
@ -28,6 +29,7 @@ class ChatItem(ChatItemBase):
|
|||||||
class ChatHistoryBase(BaseModel):
|
class ChatHistoryBase(BaseModel):
|
||||||
user_id: int
|
user_id: int
|
||||||
title: Optional[str]
|
title: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ChatHistoryCreate(ChatHistoryBase):
|
class ChatHistoryCreate(ChatHistoryBase):
|
||||||
@ -46,9 +48,15 @@ class ChatHistory(ChatHistoryBase):
|
|||||||
updated_at: datetime
|
updated_at: datetime
|
||||||
chat_items: List[ChatItem]
|
chat_items: List[ChatItem]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ordered_chat_items(self):
|
||||||
|
return sorted(self.chat_items, key=lambda x: x.index)
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ChatDelete(BaseModel):
|
class ChatDelete(BaseModel):
|
||||||
conversation_id: uuid.UUID
|
conversation_id: uuid.UUID
|
||||||
|
|
||||||
|
0
private_gpt/users/utils/export.py
Normal file
0
private_gpt/users/utils/export.py
Normal file
Loading…
Reference in New Issue
Block a user