mirror of
https://github.com/creyD/apilog.git
synced 2026-04-12 19:30:29 +02:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e0c8f0173 | ||
|
|
0c769ba843 | ||
| 18cce99967 | |||
| 4e7f352a15 | |||
| 263d962912 | |||
|
|
3d4e5e3f4b | ||
|
|
6cdae87f42 |
@@ -1,3 +1,8 @@
|
||||
# apilog
|
||||
|
||||
Tiny logging API server, for taking logs via HTTP POST requests.
|
||||
|
||||
## TODO
|
||||
|
||||
[ ] Application Patch
|
||||
[ ] Team CRUD
|
||||
|
||||
29
alembic/versions/1e695b024786_.py
Normal file
29
alembic/versions/1e695b024786_.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 1e695b024786
|
||||
Revises: 21dc1dc045b8
|
||||
Create Date: 2025-01-20 11:36:14.692849
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "1e695b024786"
|
||||
down_revision: Union[str, None] = "21dc1dc045b8"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
with op.batch_alter_table("application", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("retention_days", sa.Integer(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("application", schema=None) as batch_op:
|
||||
batch_op.drop_column("retention_days")
|
||||
@@ -1,6 +1,7 @@
|
||||
from creyPY.fastapi.models.base import Base
|
||||
from sqlalchemy import Column, String
|
||||
from sqlalchemy import Column, Integer, String
|
||||
|
||||
|
||||
class Application(Base):
|
||||
name = Column(String(512), nullable=False, unique=True)
|
||||
retention_days = Column(Integer, nullable=True)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from creyPY.fastapi.models.base import Base
|
||||
from sqlalchemy import Column, String, ForeignKey, Enum, JSON
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
|
||||
from enum import Enum as pyenum
|
||||
|
||||
from creyPY.fastapi.models.base import Base
|
||||
from sqlalchemy import JSON, Column, Enum, ForeignKey, String
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
|
||||
|
||||
class TransactionType(pyenum):
|
||||
CREATE = "create"
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
from pydantic.json_schema import SkipJsonSchema
|
||||
|
||||
from app.schema.common import BaseSchemaModelIN, BaseSchemaModelOUT
|
||||
|
||||
|
||||
class AppIN(BaseSchemaModelIN):
|
||||
name: str
|
||||
retention_days: int | SkipJsonSchema[None] = 30
|
||||
|
||||
|
||||
class AppOUT(BaseSchemaModelOUT, AppIN):
|
||||
|
||||
36
app/setup.py
36
app/setup.py
@@ -1,12 +1,32 @@
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from creyPY.fastapi.db.session import SQLALCHEMY_DATABASE_URL, name
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from creyPY.fastapi.db.session import SQLALCHEMY_DATABASE_URL, get_db, name
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from app.models.app import Application
|
||||
from app.models.entry import LogEntry
|
||||
from app.services.db.session import create_if_not_exists
|
||||
|
||||
|
||||
def delete_old_logs(sess: Session | None = None):
|
||||
session = sess or next(get_db())
|
||||
|
||||
for app in session.query(Application).filter(Application.retention_days.isnot(None)):
|
||||
cutoff = datetime.now() - timedelta(days=app.retention_days)
|
||||
print(
|
||||
f"Deleting logs older than {app.retention_days} days (cutoff: {cutoff}) for {app.name}",
|
||||
)
|
||||
session.query(LogEntry).filter(
|
||||
LogEntry.application == app.id, LogEntry.created_at < cutoff
|
||||
).delete()
|
||||
|
||||
session.commit()
|
||||
|
||||
|
||||
def setup(db_name=name):
|
||||
# Create Database
|
||||
create_if_not_exists(db_name)
|
||||
@@ -18,3 +38,17 @@ def setup(db_name=name):
|
||||
"script_location", os.path.join(os.path.dirname(os.path.dirname(__file__)), "alembic")
|
||||
)
|
||||
command.upgrade(config, "head")
|
||||
|
||||
# Start retention deletion
|
||||
scheduler = BackgroundScheduler()
|
||||
scheduler.add_job(
|
||||
delete_old_logs,
|
||||
"interval",
|
||||
id="deletor",
|
||||
days=1,
|
||||
max_instances=1,
|
||||
replace_existing=True,
|
||||
next_run_time=datetime.now(),
|
||||
)
|
||||
scheduler.start()
|
||||
print("Deletion scheduler started")
|
||||
|
||||
118
app/test_main.py
118
app/test_main.py
@@ -1,3 +1,6 @@
|
||||
import contextlib
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from creyPY.fastapi.db.session import SQLALCHEMY_DATABASE_URL, get_db
|
||||
from creyPY.fastapi.models.base import Base
|
||||
from creyPY.fastapi.testing import GenericClient
|
||||
@@ -5,16 +8,55 @@ from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy_utils import create_database, database_exists, drop_database
|
||||
|
||||
from app.models.entry import LogEntry
|
||||
from app.services.auth import verify
|
||||
import contextlib
|
||||
from app.setup import delete_old_logs
|
||||
|
||||
from .main import app
|
||||
|
||||
CURRENT_USER = "api-key|testing"
|
||||
ENTRY_EXAMPLES = [
|
||||
{
|
||||
"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",
|
||||
"t_type": "update",
|
||||
"author": "auth|max_muster",
|
||||
"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",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def app_context(self, name: str = "Testing"):
|
||||
app_id = self.create_app(name)
|
||||
def app_context(self, name: str = "Testing", retention_days: int | None = None):
|
||||
app_id = self.create_app(name, retention_days)
|
||||
try:
|
||||
yield app_id
|
||||
finally:
|
||||
@@ -23,45 +65,8 @@ 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",
|
||||
"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",
|
||||
"t_type": "update",
|
||||
"author": "auth|max_muster",
|
||||
"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",
|
||||
},
|
||||
]
|
||||
with app_context(self) as app_id:
|
||||
for entry in LOG_EXAMPLES:
|
||||
for entry in ENTRY_EXAMPLES:
|
||||
self.log_message({"application": app_id, **entry})
|
||||
yield app_id
|
||||
|
||||
@@ -86,6 +91,7 @@ class TestAPI:
|
||||
global CURRENT_USER
|
||||
return CURRENT_USER
|
||||
|
||||
self.db_instance = get_db_test()
|
||||
app.dependency_overrides[get_db] = get_db_test
|
||||
app.dependency_overrides[verify] = get_test_sub
|
||||
self.c = GenericClient(app)
|
||||
@@ -94,8 +100,8 @@ class TestAPI:
|
||||
drop_database(self.engine.url)
|
||||
|
||||
# HELPERS
|
||||
def create_app(self, name: str = "Testing"):
|
||||
re = self.c.post("/app/", {"name": name})
|
||||
def create_app(self, name: str = "Testing", retention_days: int | None = None):
|
||||
re = self.c.post("/app/", {"name": name, "retention_days": retention_days})
|
||||
return re["id"]
|
||||
|
||||
def destroy_app(self, app_id):
|
||||
@@ -260,3 +266,29 @@ class TestAPI:
|
||||
|
||||
re = self.c.get("/log/?application=" + str(app_id))
|
||||
assert re["total"] == 0
|
||||
|
||||
def test_retention_delete(self):
|
||||
sess = next(self.db_instance)
|
||||
|
||||
with app_context(self, retention_days=2) as app_id:
|
||||
for i in range(5):
|
||||
sess.add(
|
||||
LogEntry(
|
||||
application=app_id,
|
||||
created_at=datetime.now() - timedelta(days=i),
|
||||
created_by_id=CURRENT_USER,
|
||||
)
|
||||
)
|
||||
sess.commit()
|
||||
|
||||
assert sess.query(LogEntry).count() == 5
|
||||
|
||||
re = self.c.get("/log/?application=" + str(app_id))
|
||||
assert re["total"] == 5
|
||||
|
||||
delete_old_logs(sess)
|
||||
|
||||
assert sess.query(LogEntry).count() == 2
|
||||
|
||||
# delete all logs
|
||||
re = self.c.delete("/log/?application=" + str(app_id), r_code=200)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
annotated-types==0.7.0
|
||||
anyio==4.8.0
|
||||
certifi==2024.8.30
|
||||
certifi==2024.12.14
|
||||
creyPY==1.2.5
|
||||
fastapi==0.115.5
|
||||
fastapi-pagination==0.12.31
|
||||
h11==0.14.0
|
||||
httpcore==1.0.6
|
||||
httpx==0.27.2
|
||||
httpx==0.28.1
|
||||
idna==3.10
|
||||
psycopg==3.2.4
|
||||
psycopg-binary==3.2.4
|
||||
@@ -21,12 +21,12 @@ typing_extensions==4.12.2
|
||||
|
||||
Mako==1.3.5 # Alembic
|
||||
MarkupSafe==3.0.1 # Alembic
|
||||
alembic==1.13.3 # Alembic
|
||||
alembic==1.14.1 # Alembic
|
||||
|
||||
SQLAlchemy-Utils==0.41.2 # SQLAlchemy
|
||||
|
||||
click==8.1.8 # Uvicorn
|
||||
uvicorn==0.31.1 # Uvicorn
|
||||
uvicorn==0.34.0 # Uvicorn
|
||||
|
||||
iniconfig==2.0.0 # pytest
|
||||
packaging==24.1 # pytest
|
||||
@@ -34,3 +34,6 @@ pluggy==1.5.0 # pytest
|
||||
pytest==8.3.4 # pytest
|
||||
|
||||
fastapi-filters==0.2.9 # Filters
|
||||
|
||||
APScheduler==3.11.0 # Scheduler for deletion
|
||||
tzlocal==5.2 # Scheduler for deletion
|
||||
|
||||
Reference in New Issue
Block a user