Initial commit: LogHive centralized log management system

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
v6ole
2026-05-09 14:55:14 +08:00
commit abfd07331e
54 changed files with 3816 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
from app.models.project import Project
from app.models.alert import AlertRule, AlertHistory
from app.models.log import LogEntry
__all__ = ["Project", "AlertRule", "AlertHistory", "LogEntry"]
+71
View File
@@ -0,0 +1,71 @@
"""Alert models — rules and history."""
import uuid
from datetime import datetime, timezone
from sqlalchemy import String, Integer, Float, Boolean, DateTime, Text, Enum as SAEnum
from sqlalchemy.orm import Mapped, mapped_column
import enum
from app.database import Base
class AlertLevel(str, enum.Enum):
INFO = "info"
WARNING = "warning"
CRITICAL = "critical"
class AlertOperator(str, enum.Enum):
GT = "gt"
GTE = "gte"
LT = "lt"
LTE = "lte"
EQ = "eq"
class AlertRule(Base):
__tablename__ = "alert_rules"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
project_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
name: Mapped[str] = mapped_column(String(128), nullable=False)
level: Mapped[AlertLevel] = mapped_column(
SAEnum(AlertLevel), default=AlertLevel.WARNING
)
field: Mapped[str] = mapped_column(
String(64), nullable=False, default="level"
) # log field to evaluate
operator: Mapped[AlertOperator] = mapped_column(
SAEnum(AlertOperator), default=AlertOperator.GTE
)
threshold: Mapped[float] = mapped_column(Float, nullable=False)
window_minutes: Mapped[int] = mapped_column(Integer, default=5)
is_enabled: Mapped[bool] = mapped_column(Boolean, default=True)
notify_channels: Mapped[str] = mapped_column(
String(256), default=""
) # comma-separated
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
)
class AlertHistory(Base):
__tablename__ = "alert_history"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
rule_id: Mapped[str] = mapped_column(
String(36), nullable=False, index=True
)
project_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
level: Mapped[AlertLevel] = mapped_column(SAEnum(AlertLevel))
message: Mapped[str] = mapped_column(Text, nullable=False)
triggered_value: Mapped[float] = mapped_column(Float, nullable=True)
is_acknowledged: Mapped[bool] = mapped_column(Boolean, default=False)
triggered_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
)
+37
View File
@@ -0,0 +1,37 @@
"""Log entry model — stored in PostgreSQL."""
import uuid
from datetime import datetime, timezone
from sqlalchemy import DateTime, Index, Integer, String, Text
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class LogEntry(Base):
__tablename__ = "log_entries"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
project_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
project_name: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, index=True,
default=lambda: datetime.now(timezone.utc),
)
level: Mapped[str] = mapped_column(String(16), nullable=False, index=True)
logger: Mapped[str] = mapped_column(String(128), default="root")
message: Mapped[str] = mapped_column(Text, nullable=False)
module: Mapped[str | None] = mapped_column(String(256), nullable=True)
function: Mapped[str | None] = mapped_column(String(128), nullable=True)
line_no: Mapped[int | None] = mapped_column(Integer, nullable=True)
trace_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
exception: Mapped[str | None] = mapped_column(Text, nullable=True)
extra: Mapped[dict] = mapped_column(JSONB, default=dict)
__table_args__ = (
Index("ix_log_entries_project_time", "project_id", timestamp.desc()),
)
+34
View File
@@ -0,0 +1,34 @@
"""Project model — represents an external service that pushes logs."""
import uuid
from datetime import datetime, timezone
from sqlalchemy import String, Boolean, DateTime, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class Project(Base):
__tablename__ = "projects"
id: Mapped[str] = mapped_column(
String(36), primary_key=True, default=lambda: str(uuid.uuid4())
)
name: Mapped[str] = mapped_column(String(128), unique=True, nullable=False)
description: Mapped[str] = mapped_column(Text, default="")
api_key: Mapped[str] = mapped_column(
String(64), unique=True, nullable=False, default=lambda: uuid.uuid4().hex
)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(timezone.utc),
onupdate=lambda: datetime.now(timezone.utc),
)
def __repr__(self) -> str:
return f"<Project {self.name}>"