Skip to content

Commit

Permalink
feat: added environment for log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
creyD committed Oct 10, 2024
1 parent 4f50f6b commit cefb48a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
29 changes: 29 additions & 0 deletions alembic/versions/21dc1dc045b8_.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions app/models/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/routes/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions app/schema/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class LogIN(BaseSchemaModelIN):
application: UUID
environment: str = "prod"
l_type: LogType = LogType.INFO
t_type: TransactionType = TransactionType.UNDEFINED

Expand Down
34 changes: 31 additions & 3 deletions app/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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:
Expand Down Expand Up @@ -129,6 +146,7 @@ def test_logging_standards(self):
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
Expand Down Expand Up @@ -211,3 +229,13 @@ def test_logging_filter(self):
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

0 comments on commit cefb48a

Please sign in to comment.