AI platform insights monitoring and website analysis monitoring services added

This commit is contained in:
ajaysi
2025-11-11 15:57:45 +05:30
parent d99c7c83a7
commit 7191c7e7f0
81 changed files with 10860 additions and 1567 deletions

View File

@@ -16,9 +16,7 @@ from models.oauth_token_monitoring_models import OAuthTokenMonitoringTask
from services.gsc_service import GSCService
from services.integrations.bing_oauth import BingOAuthService
from services.integrations.wordpress_oauth import WordPressOAuthService
# Note: Wix tokens are stored in frontend sessionStorage, not backend database
# So we cannot check for Wix connections from the backend yet
from services.integrations.wix_oauth import WixOAuthService
def get_connected_platforms(user_id: str) -> List[str]:
@@ -29,7 +27,7 @@ def get_connected_platforms(user_id: str) -> List[str]:
- GSC: gsc_credentials table
- Bing: bing_oauth_tokens table
- WordPress: wordpress_oauth_tokens table
- Wix: Not checked (tokens in frontend sessionStorage)
- Wix: wix_oauth_tokens table
Args:
user_id: User ID (Clerk string)
@@ -39,57 +37,84 @@ def get_connected_platforms(user_id: str) -> List[str]:
"""
connected = []
logger.warning(f"[OAuth Monitoring] Checking connected platforms for user: {user_id}")
# Use DEBUG level for routine checks (called frequently by dashboard)
logger.debug(f"[OAuth Monitoring] Checking connected platforms for user: {user_id}")
try:
# Check GSC - use absolute database path
db_path = os.path.abspath("alwrity.db")
logger.warning(f"[OAuth Monitoring] Checking GSC with db_path: {db_path}")
gsc_service = GSCService(db_path=db_path)
gsc_credentials = gsc_service.load_user_credentials(user_id)
if gsc_credentials:
connected.append('gsc')
logger.warning(f"[OAuth Monitoring] ✅ GSC connected for user {user_id}")
logger.debug(f"[OAuth Monitoring] ✅ GSC connected for user {user_id}")
else:
logger.warning(f"[OAuth Monitoring] ❌ GSC not connected for user {user_id} (no credentials found)")
logger.debug(f"[OAuth Monitoring] ❌ GSC not connected for user {user_id}")
except Exception as e:
logger.warning(f"[OAuth Monitoring] ⚠️ GSC check failed for user {user_id}: {e}", exc_info=True)
try:
# Check Bing - use absolute database path
db_path = os.path.abspath("alwrity.db")
logger.warning(f"[OAuth Monitoring] Checking Bing with db_path: {db_path}")
bing_service = BingOAuthService(db_path=db_path)
token_status = bing_service.get_user_token_status(user_id)
has_tokens = token_status.get('has_active_tokens', False)
logger.warning(f"[OAuth Monitoring] Bing token_status keys: {list(token_status.keys())}, has_active_tokens: {has_tokens}")
if has_tokens:
has_active_tokens = token_status.get('has_active_tokens', False)
has_expired_tokens = token_status.get('has_expired_tokens', False)
expired_tokens = token_status.get('expired_tokens', [])
# Check if expired tokens have refresh tokens (can be refreshed)
has_refreshable_tokens = any(token.get('refresh_token') for token in expired_tokens)
# Consider connected if user has active tokens OR expired tokens with refresh tokens
if has_active_tokens or (has_expired_tokens and has_refreshable_tokens):
connected.append('bing')
logger.warning(f"[OAuth Monitoring] ✅ Bing connected for user {user_id}")
logger.debug(f"[OAuth Monitoring] ✅ Bing connected for user {user_id}")
else:
logger.warning(f"[OAuth Monitoring] ❌ Bing not connected for user {user_id} (no active tokens)")
logger.debug(f"[OAuth Monitoring] ❌ Bing not connected for user {user_id}")
except Exception as e:
logger.warning(f"[OAuth Monitoring] ⚠️ Bing check failed for user {user_id}: {e}", exc_info=True)
try:
# Check WordPress - use absolute database path
db_path = os.path.abspath("alwrity.db")
logger.warning(f"[OAuth Monitoring] Checking WordPress with db_path: {db_path}")
wordpress_service = WordPressOAuthService(db_path=db_path)
tokens = wordpress_service.get_user_tokens(user_id)
logger.warning(f"[OAuth Monitoring] WordPress tokens found: {len(tokens) if tokens else 0}")
if tokens and len(tokens) > 0:
token_status = wordpress_service.get_user_token_status(user_id)
has_active_tokens = token_status.get('has_active_tokens', False)
has_tokens = token_status.get('has_tokens', False)
# Consider connected if user has any tokens (WordPress tokens may not have refresh tokens)
# If tokens exist, user was connected even if expired (may need re-auth)
if has_tokens:
connected.append('wordpress')
logger.warning(f"[OAuth Monitoring] ✅ WordPress connected for user {user_id} ({len(tokens)} token(s))")
logger.debug(f"[OAuth Monitoring] ✅ WordPress connected for user {user_id}")
else:
logger.warning(f"[OAuth Monitoring] ❌ WordPress not connected for user {user_id} (no tokens found)")
logger.debug(f"[OAuth Monitoring] ❌ WordPress not connected for user {user_id}")
except Exception as e:
logger.warning(f"[OAuth Monitoring] ⚠️ WordPress check failed for user {user_id}: {e}", exc_info=True)
# Wix: Not checked (tokens in frontend sessionStorage)
# TODO: Once backend storage is implemented, check wix_tokens table
try:
# Check Wix - use absolute database path
db_path = os.path.abspath("alwrity.db")
wix_service = WixOAuthService(db_path=db_path)
token_status = wix_service.get_user_token_status(user_id)
has_active_tokens = token_status.get('has_active_tokens', False)
has_expired_tokens = token_status.get('has_expired_tokens', False)
expired_tokens = token_status.get('expired_tokens', [])
# Check if expired tokens have refresh tokens (can be refreshed)
has_refreshable_tokens = any(token.get('refresh_token') for token in expired_tokens)
# Consider connected if user has active tokens OR expired tokens with refresh tokens
if has_active_tokens or (has_expired_tokens and has_refreshable_tokens):
connected.append('wix')
logger.debug(f"[OAuth Monitoring] ✅ Wix connected for user {user_id}")
else:
logger.debug(f"[OAuth Monitoring] ❌ Wix not connected for user {user_id}")
except Exception as e:
logger.warning(f"[OAuth Monitoring] ⚠️ Wix check failed for user {user_id}: {e}", exc_info=True)
logger.warning(f"[OAuth Monitoring] Connected platforms for user {user_id}: {connected}")
# Don't log here - let the caller log a formatted summary if needed
# This function is called frequently and should be silent
return connected