Spaces:
Running
Running
File size: 1,594 Bytes
c2ea5ed fa515bb c2ea5ed ea856a6 795b72e ea856a6 032c9f9 697eb00 c2ea5ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
"""
Database functionality for the agent monitoring system.
This package provides database access and utilities for agent monitoring.
"""
import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
# Import DB_URI from central config (handles HF Spaces vs local dev)
from utils.config import DB_URI
DATABASE_URL = DB_URI
# Create engine
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
# Create session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create a scoped session for thread safety
Session = scoped_session(SessionLocal)
# Base class for models
Base = declarative_base()
# Function to get database session
def get_db():
db = Session()
try:
yield db
finally:
db.close()
# Import models and utility functions
from backend.database.models import KnowledgeGraph, Entity, Relation, Base
from backend.database.utils import (
save_knowledge_graph,
update_knowledge_graph_status,
get_knowledge_graph,
get_all_knowledge_graphs,
delete_knowledge_graph
)
# Function to initialize database
def init_db():
"""Initialize the database by creating all tables."""
Base.metadata.create_all(bind=engine)
__all__ = [
'get_db',
'models',
'init_db',
'save_knowledge_graph',
'update_knowledge_graph_status',
'get_knowledge_graph',
'get_all_knowledge_graphs',
'delete_knowledge_graph',
'KnowledgeGraph',
'Entity',
'Relation'
]
|