mirror of
https://github.com/creyD/apilog.git
synced 2026-04-13 03:40:30 +02:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98df462b61 | ||
| 6db2b3e14e | |||
| 22eaed8a75 | |||
|
|
4d0ecb2ee8 | ||
| 88e97faddb | |||
| cefb48a4b2 |
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -13,6 +13,10 @@ on:
|
|||||||
- "**/CHANGELOG.md"
|
- "**/CHANGELOG.md"
|
||||||
- "**/docs/**"
|
- "**/docs/**"
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- dev
|
||||||
|
- master
|
||||||
|
|
||||||
env:
|
env:
|
||||||
REGISTRY: ghcr.io
|
REGISTRY: ghcr.io
|
||||||
|
|||||||
29
alembic/versions/21dc1dc045b8_.py
Normal file
29
alembic/versions/21dc1dc045b8_.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 21dc1dc045b8
|
||||||
|
Revises: 74c576cf9560
|
||||||
|
Create Date: 2024-10-10 20:32:12.579725
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = "21dc1dc045b8"
|
||||||
|
down_revision: Union[str, None] = "74c576cf9560"
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
with op.batch_alter_table("logentry", schema=None) as batch_op:
|
||||||
|
batch_op.add_column(sa.Column("environment", sa.String(length=64), nullable=True))
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
with op.batch_alter_table("logentry", schema=None) as batch_op:
|
||||||
|
batch_op.drop_column("environment")
|
||||||
@@ -23,6 +23,7 @@ class LogEntry(Base):
|
|||||||
application = Column(
|
application = Column(
|
||||||
UUID(as_uuid=True), ForeignKey("application.id", ondelete="CASCADE"), nullable=False
|
UUID(as_uuid=True), ForeignKey("application.id", ondelete="CASCADE"), nullable=False
|
||||||
)
|
)
|
||||||
|
environment = Column(String(64), nullable=True, default="prod")
|
||||||
# type of the log entry
|
# type of the log entry
|
||||||
l_type = Column(Enum(LogType), nullable=False, default=LogType.INFO)
|
l_type = Column(Enum(LogType), nullable=False, default=LogType.INFO)
|
||||||
# type of the transaction
|
# type of the transaction
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ from uuid import UUID
|
|||||||
from pydantic.json_schema import SkipJsonSchema
|
from pydantic.json_schema import SkipJsonSchema
|
||||||
from fastapi_filters import FilterValues, create_filters
|
from fastapi_filters import FilterValues, create_filters
|
||||||
from fastapi_filters.ext.sqlalchemy import apply_filters
|
from fastapi_filters.ext.sqlalchemy import apply_filters
|
||||||
|
from app.models.entry import LogType, TransactionType
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
router = APIRouter(prefix="/log", tags=["logging"])
|
router = APIRouter(prefix="/log", tags=["logging"])
|
||||||
|
|
||||||
@@ -62,10 +64,6 @@ async def get_log(
|
|||||||
return LogOUT.model_validate(obj)
|
return LogOUT.model_validate(obj)
|
||||||
|
|
||||||
|
|
||||||
from app.models.entry import LogType, TransactionType
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def get_logs(
|
async def get_logs(
|
||||||
search: str | SkipJsonSchema[None] = None,
|
search: str | SkipJsonSchema[None] = None,
|
||||||
@@ -73,6 +71,7 @@ async def get_logs(
|
|||||||
filters: FilterValues = Depends(
|
filters: FilterValues = Depends(
|
||||||
create_filters(
|
create_filters(
|
||||||
created_by_id=str,
|
created_by_id=str,
|
||||||
|
environment=str,
|
||||||
l_type=LogType,
|
l_type=LogType,
|
||||||
t_type=TransactionType,
|
t_type=TransactionType,
|
||||||
application=UUID,
|
application=UUID,
|
||||||
@@ -93,3 +92,37 @@ async def get_logs(
|
|||||||
LogEntry.message.ilike(f"%{search}%") | LogEntry.author.ilike(f"%{search}%")
|
LogEntry.message.ilike(f"%{search}%") | LogEntry.author.ilike(f"%{search}%")
|
||||||
)
|
)
|
||||||
return paginate(db, order_by_query(the_select))
|
return paginate(db, order_by_query(the_select))
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/", status_code=200, operation_id="log_delete_many")
|
||||||
|
async def delete_logs(
|
||||||
|
application: UUID,
|
||||||
|
environment: str | SkipJsonSchema[None] = None,
|
||||||
|
l_type: LogType | SkipJsonSchema[None] = None,
|
||||||
|
t_type: TransactionType | SkipJsonSchema[None] = None,
|
||||||
|
object_reference: str | SkipJsonSchema[None] = None,
|
||||||
|
author: str | SkipJsonSchema[None] = None,
|
||||||
|
sub: str = Security(verify),
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
) -> int:
|
||||||
|
filters = {
|
||||||
|
"application": application,
|
||||||
|
"created_by_id": sub,
|
||||||
|
}
|
||||||
|
|
||||||
|
if environment is not None:
|
||||||
|
filters["environment"] = environment
|
||||||
|
if l_type is not None:
|
||||||
|
filters["l_type"] = l_type
|
||||||
|
if t_type is not None:
|
||||||
|
filters["t_type"] = t_type
|
||||||
|
if object_reference is not None:
|
||||||
|
filters["object_reference"] = object_reference
|
||||||
|
if author is not None:
|
||||||
|
filters["author"] = author
|
||||||
|
|
||||||
|
query = db.query(LogEntry).filter_by(**filters)
|
||||||
|
the_impact = query.count()
|
||||||
|
query.delete(synchronize_session=False)
|
||||||
|
db.commit()
|
||||||
|
return the_impact
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from pydantic.json_schema import SkipJsonSchema
|
|||||||
|
|
||||||
class LogIN(BaseSchemaModelIN):
|
class LogIN(BaseSchemaModelIN):
|
||||||
application: UUID
|
application: UUID
|
||||||
|
environment: str = "prod"
|
||||||
l_type: LogType = LogType.INFO
|
l_type: LogType = LogType.INFO
|
||||||
t_type: TransactionType = TransactionType.UNDEFINED
|
t_type: TransactionType = TransactionType.UNDEFINED
|
||||||
|
|
||||||
|
|||||||
@@ -24,14 +24,25 @@ def app_context(self, name: str = "Testing"):
|
|||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def log_examples(self):
|
def log_examples(self):
|
||||||
LOG_EXAMPLES = [
|
LOG_EXAMPLES = [
|
||||||
{"l_type": "info", "t_type": "create", "message": "User Max Mustermann created"},
|
{
|
||||||
{"l_type": "info", "t_type": "update", "message": "User Max Mustermann updated"},
|
"l_type": "info",
|
||||||
|
"t_type": "create",
|
||||||
|
"message": "User Max Mustermann created",
|
||||||
|
"environment": "dev",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"l_type": "info",
|
||||||
|
"t_type": "update",
|
||||||
|
"message": "User Max Mustermann updated",
|
||||||
|
"environment": "dev",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"l_type": "info",
|
"l_type": "info",
|
||||||
"t_type": "create",
|
"t_type": "create",
|
||||||
"author": "auth|max_muster",
|
"author": "auth|max_muster",
|
||||||
"message": "User Max Mustermann created a Unit",
|
"message": "User Max Mustermann created a Unit",
|
||||||
"object_reference": "1",
|
"object_reference": "1",
|
||||||
|
"environment": "dev",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"l_type": "info",
|
"l_type": "info",
|
||||||
@@ -40,8 +51,14 @@ def log_examples(self):
|
|||||||
"message": "User Max Mustermann updated Unit 1",
|
"message": "User Max Mustermann updated Unit 1",
|
||||||
"object_reference": "1",
|
"object_reference": "1",
|
||||||
"previous_object": {"name": "Unit 1"},
|
"previous_object": {"name": "Unit 1"},
|
||||||
|
"environment": "prod",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"l_type": "warning",
|
||||||
|
"t_type": "delete",
|
||||||
|
"message": "User Max Mustermann deleted",
|
||||||
|
"environment": "prod",
|
||||||
},
|
},
|
||||||
{"l_type": "warning", "t_type": "delete", "message": "User Max Mustermann deleted"},
|
|
||||||
]
|
]
|
||||||
with app_context(self) as app_id:
|
with app_context(self) as app_id:
|
||||||
for entry in LOG_EXAMPLES:
|
for entry in LOG_EXAMPLES:
|
||||||
@@ -129,6 +146,7 @@ class TestAPI:
|
|||||||
assert re["t_type"] == "undefined"
|
assert re["t_type"] == "undefined"
|
||||||
assert re["message"] == None
|
assert re["message"] == None
|
||||||
assert re["author"] == "system"
|
assert re["author"] == "system"
|
||||||
|
assert re["environment"] == "prod"
|
||||||
assert re["object_reference"] == None
|
assert re["object_reference"] == None
|
||||||
assert re["previous_object"] == None
|
assert re["previous_object"] == None
|
||||||
assert re["created_by_id"] == CURRENT_USER
|
assert re["created_by_id"] == CURRENT_USER
|
||||||
@@ -211,3 +229,34 @@ class TestAPI:
|
|||||||
re = self.c.get("/log/?author%5Bne%5D=auth|max_muster")
|
re = self.c.get("/log/?author%5Bne%5D=auth|max_muster")
|
||||||
assert re["total"] == 3
|
assert re["total"] == 3
|
||||||
assert len(re["results"]) == 3
|
assert len(re["results"]) == 3
|
||||||
|
|
||||||
|
# environment
|
||||||
|
re = self.c.get("/log/?environment=dev")
|
||||||
|
assert re["total"] == 3
|
||||||
|
assert len(re["results"]) == 3
|
||||||
|
|
||||||
|
# application and environment
|
||||||
|
re = self.c.get("/log/?application=" + app_id + "&environment=prod")
|
||||||
|
assert re["total"] == 2
|
||||||
|
assert len(re["results"]) == 2
|
||||||
|
|
||||||
|
def test_logging_delete(self):
|
||||||
|
with log_examples(self) as app_id:
|
||||||
|
re = self.c.delete("/log/?application=" + str(app_id) + "&environment=prod", r_code=200)
|
||||||
|
assert re == 2
|
||||||
|
|
||||||
|
re = self.c.get("/log/?application=" + str(app_id) + "&environment=prod")
|
||||||
|
assert re["total"] == 0
|
||||||
|
|
||||||
|
re = self.c.get("/log/?application=" + str(app_id) + "&environment=dev")
|
||||||
|
assert re["total"] == 3
|
||||||
|
|
||||||
|
# clear complete application
|
||||||
|
re = self.c.get("/log/?application=" + str(app_id))
|
||||||
|
assert re["total"] == 3
|
||||||
|
|
||||||
|
re = self.c.delete("/log/?application=" + str(app_id), r_code=200)
|
||||||
|
assert re == 3
|
||||||
|
|
||||||
|
re = self.c.get("/log/?application=" + str(app_id))
|
||||||
|
assert re["total"] == 0
|
||||||
|
|||||||
7
renovate.json
Normal file
7
renovate.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
"extends": [
|
||||||
|
"config:recommended",
|
||||||
|
":semanticCommitTypeAll(feat)"
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user