Spaces:
Sleeping
Sleeping
Ajit Panday
commited on
Commit
·
fdff54c
1
Parent(s):
dff9fde
Add database.py and update models to use centralized database session
Browse files- app/database.py +17 -0
- app/models.py +14 -38
app/database.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sqlalchemy import create_engine
|
| 2 |
+
from sqlalchemy.orm import sessionmaker
|
| 3 |
+
from .settings import DATABASE_URL
|
| 4 |
+
|
| 5 |
+
# Create SQLAlchemy engine
|
| 6 |
+
engine = create_engine(DATABASE_URL)
|
| 7 |
+
|
| 8 |
+
# Create session factory
|
| 9 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 10 |
+
|
| 11 |
+
def get_db():
|
| 12 |
+
"""Get database session"""
|
| 13 |
+
db = SessionLocal()
|
| 14 |
+
try:
|
| 15 |
+
yield db
|
| 16 |
+
finally:
|
| 17 |
+
db.close()
|
app/models.py
CHANGED
|
@@ -1,19 +1,11 @@
|
|
| 1 |
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean, create_engine, Text
|
| 2 |
from sqlalchemy.ext.declarative import declarative_base
|
| 3 |
-
from sqlalchemy.orm import
|
| 4 |
from datetime import datetime
|
| 5 |
-
import os
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
-
import json
|
| 8 |
import requests
|
| 9 |
from typing import Optional, List, Dict
|
| 10 |
-
from .settings import
|
| 11 |
-
|
| 12 |
-
# Load environment variables
|
| 13 |
-
load_dotenv()
|
| 14 |
-
|
| 15 |
-
# Create SQLAlchemy engine
|
| 16 |
-
engine = create_engine(DATABASE_URL)
|
| 17 |
|
| 18 |
# Create declarative base
|
| 19 |
Base = declarative_base()
|
|
@@ -29,32 +21,34 @@ class Customer(Base):
|
|
| 29 |
is_active = Column(Boolean, default=True)
|
| 30 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 31 |
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 32 |
-
|
| 33 |
# Database credentials for customer's own database
|
| 34 |
db_host = Column(String(255))
|
| 35 |
db_port = Column(Integer)
|
| 36 |
db_name = Column(String(100))
|
| 37 |
db_user = Column(String(100))
|
| 38 |
db_password = Column(String(255))
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
if all([self.db_host, self.db_port, self.db_name, self.db_user, self.db_password]):
|
| 43 |
return f"mysql+pymysql://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}"
|
| 44 |
return None
|
| 45 |
|
| 46 |
def get_db_engine(self):
|
| 47 |
-
"""Get
|
| 48 |
db_url = self.get_db_url()
|
| 49 |
if db_url:
|
| 50 |
return create_engine(db_url)
|
| 51 |
return None
|
| 52 |
|
| 53 |
def create_tables(self):
|
| 54 |
-
"""Create
|
| 55 |
engine = self.get_db_engine()
|
| 56 |
if engine:
|
| 57 |
-
|
| 58 |
|
| 59 |
def get_call_records(self, start_date: Optional[str] = None, end_date: Optional[str] = None) -> List[Dict]:
|
| 60 |
"""Get call records via API"""
|
|
@@ -94,9 +88,6 @@ class Customer(Base):
|
|
| 94 |
response.raise_for_status()
|
| 95 |
return response.json()
|
| 96 |
|
| 97 |
-
# Relationship with call records
|
| 98 |
-
call_records = relationship("CallRecord", back_populates="customer")
|
| 99 |
-
|
| 100 |
class CallRecord(Base):
|
| 101 |
__tablename__ = "call_records"
|
| 102 |
|
|
@@ -111,8 +102,7 @@ class CallRecord(Base):
|
|
| 111 |
keywords = Column(Text)
|
| 112 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 113 |
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 114 |
-
|
| 115 |
-
# Relationship with customer
|
| 116 |
customer = relationship("Customer", back_populates="call_records")
|
| 117 |
|
| 118 |
@classmethod
|
|
@@ -128,18 +118,4 @@ class CallRecord(Base):
|
|
| 128 |
summary=data.get('summary'),
|
| 129 |
sentiment=data.get('sentiment'),
|
| 130 |
keywords=data.get('keywords')
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
# Create all tables in the main database
|
| 134 |
-
Base.metadata.create_all(engine)
|
| 135 |
-
|
| 136 |
-
# Create session factory
|
| 137 |
-
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 138 |
-
|
| 139 |
-
# Dependency to get database session
|
| 140 |
-
def get_db():
|
| 141 |
-
db = SessionLocal()
|
| 142 |
-
try:
|
| 143 |
-
yield db
|
| 144 |
-
finally:
|
| 145 |
-
db.close()
|
|
|
|
| 1 |
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean, create_engine, Text
|
| 2 |
from sqlalchemy.ext.declarative import declarative_base
|
| 3 |
+
from sqlalchemy.orm import relationship
|
| 4 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 5 |
import requests
|
| 6 |
from typing import Optional, List, Dict
|
| 7 |
+
from .settings import API_BASE_URL
|
| 8 |
+
from .database import get_db
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Create declarative base
|
| 11 |
Base = declarative_base()
|
|
|
|
| 21 |
is_active = Column(Boolean, default=True)
|
| 22 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 23 |
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
| 24 |
# Database credentials for customer's own database
|
| 25 |
db_host = Column(String(255))
|
| 26 |
db_port = Column(Integer)
|
| 27 |
db_name = Column(String(100))
|
| 28 |
db_user = Column(String(100))
|
| 29 |
db_password = Column(String(255))
|
| 30 |
+
|
| 31 |
+
# Relationship with call records
|
| 32 |
+
call_records = relationship("CallRecord", back_populates="customer")
|
| 33 |
+
|
| 34 |
+
def get_db_url(self) -> Optional[str]:
|
| 35 |
+
"""Get database URL if credentials are configured"""
|
| 36 |
if all([self.db_host, self.db_port, self.db_name, self.db_user, self.db_password]):
|
| 37 |
return f"mysql+pymysql://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}"
|
| 38 |
return None
|
| 39 |
|
| 40 |
def get_db_engine(self):
|
| 41 |
+
"""Get SQLAlchemy engine for customer's database"""
|
| 42 |
db_url = self.get_db_url()
|
| 43 |
if db_url:
|
| 44 |
return create_engine(db_url)
|
| 45 |
return None
|
| 46 |
|
| 47 |
def create_tables(self):
|
| 48 |
+
"""Create tables in customer's database"""
|
| 49 |
engine = self.get_db_engine()
|
| 50 |
if engine:
|
| 51 |
+
Base.metadata.create_all(engine)
|
| 52 |
|
| 53 |
def get_call_records(self, start_date: Optional[str] = None, end_date: Optional[str] = None) -> List[Dict]:
|
| 54 |
"""Get call records via API"""
|
|
|
|
| 88 |
response.raise_for_status()
|
| 89 |
return response.json()
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
class CallRecord(Base):
|
| 92 |
__tablename__ = "call_records"
|
| 93 |
|
|
|
|
| 102 |
keywords = Column(Text)
|
| 103 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 104 |
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 105 |
+
|
|
|
|
| 106 |
customer = relationship("Customer", back_populates="call_records")
|
| 107 |
|
| 108 |
@classmethod
|
|
|
|
| 118 |
summary=data.get('summary'),
|
| 119 |
sentiment=data.get('sentiment'),
|
| 120 |
keywords=data.get('keywords')
|
| 121 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|