Init first files
This commit is contained in:
3
backend/app/models/__init__.py
Normal file
3
backend/app/models/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from app.models.models import AuditLog, Metric, QueryStat, Target, User
|
||||
|
||||
__all__ = ["User", "Target", "Metric", "QueryStat", "AuditLog"]
|
||||
75
backend/app/models/models.py
Normal file
75
backend/app/models/models.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import JSON, DateTime, Float, ForeignKey, Integer, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from app.core.db import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False)
|
||||
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
role: Mapped[str] = mapped_column(String(20), nullable=False, default="viewer")
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
|
||||
audit_logs: Mapped[list["AuditLog"]] = relationship(back_populates="user")
|
||||
|
||||
|
||||
class Target(Base):
|
||||
__tablename__ = "targets"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(120), unique=True, index=True, nullable=False)
|
||||
host: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
port: Mapped[int] = mapped_column(Integer, nullable=False, default=5432)
|
||||
dbname: Mapped[str] = mapped_column(String(120), nullable=False)
|
||||
username: Mapped[str] = mapped_column(String(120), nullable=False)
|
||||
encrypted_password: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
sslmode: Mapped[str] = mapped_column(String(20), nullable=False, default="prefer")
|
||||
tags: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
|
||||
metrics: Mapped[list["Metric"]] = relationship(back_populates="target", cascade="all, delete-orphan")
|
||||
query_stats: Mapped[list["QueryStat"]] = relationship(back_populates="target", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class Metric(Base):
|
||||
__tablename__ = "metrics"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
target_id: Mapped[int] = mapped_column(ForeignKey("targets.id", ondelete="CASCADE"), nullable=False, index=True)
|
||||
ts: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now(), index=True)
|
||||
metric_name: Mapped[str] = mapped_column(String(120), nullable=False, index=True)
|
||||
value: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
labels: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
|
||||
|
||||
target: Mapped[Target] = relationship(back_populates="metrics")
|
||||
|
||||
|
||||
class QueryStat(Base):
|
||||
__tablename__ = "query_stats"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
target_id: Mapped[int] = mapped_column(ForeignKey("targets.id", ondelete="CASCADE"), nullable=False, index=True)
|
||||
ts: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now(), index=True)
|
||||
queryid: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
calls: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
total_time: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
||||
mean_time: Mapped[float] = mapped_column(Float, nullable=False, default=0.0)
|
||||
rows: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
query_text: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
|
||||
target: Mapped[Target] = relationship(back_populates="query_stats")
|
||||
|
||||
|
||||
class AuditLog(Base):
|
||||
__tablename__ = "audit_logs"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
ts: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now(), index=True)
|
||||
user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True, index=True)
|
||||
action: Mapped[str] = mapped_column(String(120), nullable=False)
|
||||
payload: Mapped[dict] = mapped_column(JSON, nullable=False, default=dict)
|
||||
|
||||
user: Mapped[User | None] = relationship(back_populates="audit_logs")
|
||||
Reference in New Issue
Block a user