103 lines
3.0 KiB
Python
103 lines
3.0 KiB
Python
from sqlalchemy import create_engine, Column, Integer, String, Text, ForeignKey, Boolean
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.sql.functions import func
|
|
from sqlalchemy.sql.sqltypes import DateTime
|
|
|
|
# Initialize the database
|
|
DATABASE_URL = "sqlite:///./project_monitor.db"
|
|
|
|
# SQLAlchemy setup
|
|
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
Base = declarative_base()
|
|
|
|
|
|
# Define the table model
|
|
|
|
|
|
class Script(Base):
|
|
__tablename__ = "scripts"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String, nullable=False)
|
|
script_content = Column(Text, nullable=True)
|
|
enabled = Column(Boolean, default=False)
|
|
created_at = Column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class Log(Base):
|
|
__tablename__ = "logs"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
message = Column(String, nullable=False)
|
|
error_code = Column(Integer, nullable=False, default=0)
|
|
error_message = Column(String, nullable=True)
|
|
created_at = Column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
script_id = Column(Integer, ForeignKey("scripts.id"), nullable=False)
|
|
|
|
|
|
class Settings(Base):
|
|
__tablename__ = "user_settings"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
requirements = Column(String, nullable=False)
|
|
environment = Column(String, nullable=False)
|
|
user = Column(String, nullable=False)
|
|
ntfy_url = Column(String, nullable=True)
|
|
|
|
|
|
class Subscription(Base):
|
|
__tablename__ = "subscriptions"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
topic = Column(String, nullable=False, unique=True)
|
|
last_message_id = Column(String, nullable=True)
|
|
created_at = Column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
class Notification(Base):
|
|
__tablename__ = "notifications"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
title = Column(String(255), nullable=False)
|
|
message = Column(Text, nullable=False)
|
|
priority = Column(Integer, nullable=False, default=3)
|
|
viewed = Column(Boolean, default=False)
|
|
sent = Column(Boolean, default=False)
|
|
|
|
subscription_id = Column(Integer, ForeignKey("subscriptions.id"), nullable=False)
|
|
created_at = Column(
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
|
|
# Create the database tables
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
# Ensure a default setting line exists
|
|
def ensure_default_setting():
|
|
db = SessionLocal()
|
|
default_setting = db.query(Settings).filter(Settings.user == "default").first()
|
|
if not default_setting:
|
|
new_setting = Settings(
|
|
requirements="",
|
|
environment="",
|
|
user="default",
|
|
ntfy_url="https://ntfy.abzk.fr",
|
|
)
|
|
db.add(new_setting)
|
|
db.commit()
|
|
db.close()
|
|
|
|
|
|
ensure_default_setting()
|