mirror of
https://github.com/creyD/apilog.git
synced 2026-04-12 19:30:29 +02:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cefb48a4b2 | |||
| 4f50f6bb7e | |||
| a43ec6abd8 |
@@ -15,7 +15,7 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
||||
RUN pip install 'uvicorn[standard]'
|
||||
|
||||
EXPOSE 9000
|
||||
CMD ["uvicorn", "app.main:app", "-w", "6" , "--host", "0.0.0.0", "--port", "9000"]
|
||||
CMD ["uvicorn", "app.main:app", "--workers", "6" , "--host", "0.0.0.0", "--port", "9000"]
|
||||
|
||||
# Install curl
|
||||
RUN apt-get update && apt-get install -y curl && apt-get clean
|
||||
|
||||
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")
|
||||
54
alembic/versions/74c576cf9560_.py
Normal file
54
alembic/versions/74c576cf9560_.py
Normal file
@@ -0,0 +1,54 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 74c576cf9560
|
||||
Revises: 95201f00f6b9
|
||||
Create Date: 2024-10-10 17:38:19.834168
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "74c576cf9560"
|
||||
down_revision: Union[str, None] = "95201f00f6b9"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"logentry",
|
||||
sa.Column("application", sa.UUID(), nullable=False),
|
||||
sa.Column(
|
||||
"l_type",
|
||||
sa.Enum("INFO", "WARNING", "ERROR", "CRITICAL", name="logtype"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"t_type",
|
||||
sa.Enum("CREATE", "UPDATE", "DELETE", "UNDEFINED", name="transactiontype"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("message", sa.String(length=512), nullable=True),
|
||||
sa.Column("author", sa.String(length=512), nullable=False),
|
||||
sa.Column("object_reference", sa.String(length=512), nullable=True),
|
||||
sa.Column("previous_object", sa.JSON(), nullable=True),
|
||||
sa.Column("id", sa.UUID(), nullable=False),
|
||||
sa.Column(
|
||||
"created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=True
|
||||
),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("created_by_id", sa.String(), nullable=True),
|
||||
sa.ForeignKeyConstraint(
|
||||
["application"], ["application.id"], ondelete="CASCADE", name="fk_logentry_application"
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name="pk_logentry"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_table("logentry")
|
||||
@@ -23,6 +23,7 @@ class LogEntry(Base):
|
||||
application = Column(
|
||||
UUID(as_uuid=True), ForeignKey("application.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
environment = Column(String(64), nullable=True, default="prod")
|
||||
# type of the log entry
|
||||
l_type = Column(Enum(LogType), nullable=False, default=LogType.INFO)
|
||||
# type of the transaction
|
||||
|
||||
@@ -73,6 +73,7 @@ async def get_logs(
|
||||
filters: FilterValues = Depends(
|
||||
create_filters(
|
||||
created_by_id=str,
|
||||
environment=str,
|
||||
l_type=LogType,
|
||||
t_type=TransactionType,
|
||||
application=UUID,
|
||||
|
||||
@@ -6,6 +6,7 @@ from pydantic.json_schema import SkipJsonSchema
|
||||
|
||||
class LogIN(BaseSchemaModelIN):
|
||||
application: UUID
|
||||
environment: str = "prod"
|
||||
l_type: LogType = LogType.INFO
|
||||
t_type: TransactionType = TransactionType.UNDEFINED
|
||||
|
||||
|
||||
@@ -24,14 +24,25 @@ def app_context(self, name: str = "Testing"):
|
||||
@contextlib.contextmanager
|
||||
def log_examples(self):
|
||||
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",
|
||||
"t_type": "create",
|
||||
"author": "auth|max_muster",
|
||||
"message": "User Max Mustermann created a Unit",
|
||||
"object_reference": "1",
|
||||
"environment": "dev",
|
||||
},
|
||||
{
|
||||
"l_type": "info",
|
||||
@@ -40,8 +51,14 @@ def log_examples(self):
|
||||
"message": "User Max Mustermann updated Unit 1",
|
||||
"object_reference": "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:
|
||||
for entry in LOG_EXAMPLES:
|
||||
@@ -129,6 +146,7 @@ class TestAPI:
|
||||
assert re["t_type"] == "undefined"
|
||||
assert re["message"] == None
|
||||
assert re["author"] == "system"
|
||||
assert re["environment"] == "prod"
|
||||
assert re["object_reference"] == None
|
||||
assert re["previous_object"] == None
|
||||
assert re["created_by_id"] == CURRENT_USER
|
||||
@@ -211,3 +229,13 @@ class TestAPI:
|
||||
re = self.c.get("/log/?author%5Bne%5D=auth|max_muster")
|
||||
assert re["total"] == 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
|
||||
|
||||
Reference in New Issue
Block a user