refactor: Add ai-learning-status and chat-stats API to blueprints
- Add api_ai_learning_status and api_chat_stats to routes_insights.py - Update chat_analytics template to use new API path - Add endpoint aliases for backward compatibility Phase 6.2b - AI API routes Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
42f04e065f
commit
9b9ae40932
12
app.py
12
app.py
@ -4478,9 +4478,9 @@ def admin_analytics_export():
|
||||
db.close()
|
||||
|
||||
|
||||
@app.route('/api/admin/ai-learning-status')
|
||||
@login_required
|
||||
def api_ai_learning_status():
|
||||
# @app.route('/api/admin/ai-learning-status') # MOVED TO admin.api_ai_learning_status
|
||||
# @login_required
|
||||
def _old_api_ai_learning_status():
|
||||
"""API: Get AI feedback learning status and examples"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Not authorized'}), 403
|
||||
@ -5313,9 +5313,9 @@ def admin_ai_usage_user(user_id):
|
||||
db.close()
|
||||
|
||||
|
||||
@app.route('/api/admin/chat-stats')
|
||||
@login_required
|
||||
def api_chat_stats():
|
||||
# @app.route('/api/admin/chat-stats') # MOVED TO admin.api_chat_stats
|
||||
# @login_required
|
||||
def _old_api_chat_stats():
|
||||
"""API: Get chat statistics for dashboard"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Not authorized'}), 403
|
||||
|
||||
@ -259,6 +259,8 @@ def register_blueprints(app):
|
||||
'api_update_insight_status': 'admin.api_update_insight_status',
|
||||
'api_sync_insights': 'admin.api_sync_insights',
|
||||
'api_insights_stats': 'admin.api_insights_stats',
|
||||
'api_ai_learning_status': 'admin.api_ai_learning_status',
|
||||
'api_chat_stats': 'admin.api_chat_stats',
|
||||
})
|
||||
logger.info("Created admin endpoint aliases")
|
||||
except ImportError as e:
|
||||
|
||||
@ -144,3 +144,101 @@ def api_insights_stats():
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting stats: {e}")
|
||||
return jsonify({'success': False, 'error': str(e)}), 500
|
||||
|
||||
|
||||
# ============================================================
|
||||
# AI LEARNING STATUS API
|
||||
# ============================================================
|
||||
|
||||
@bp.route('/ai-learning-status')
|
||||
@login_required
|
||||
def api_ai_learning_status():
|
||||
"""API: Get AI feedback learning status and examples"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Not authorized'}), 403
|
||||
|
||||
try:
|
||||
from feedback_learning_service import get_feedback_learning_service
|
||||
service = get_feedback_learning_service()
|
||||
context = service.get_learning_context()
|
||||
|
||||
# Format examples for JSON response
|
||||
positive_examples = []
|
||||
for ex in context.get('positive_examples', []):
|
||||
positive_examples.append({
|
||||
'query': ex.query,
|
||||
'response': ex.response[:300] + '...' if len(ex.response) > 300 else ex.response,
|
||||
'companies': ex.companies_mentioned or []
|
||||
})
|
||||
|
||||
negative_examples = []
|
||||
for ex in context.get('negative_examples', []):
|
||||
negative_examples.append({
|
||||
'query': ex.query,
|
||||
'response': ex.response,
|
||||
'comment': ex.feedback_comment
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'learning_active': True,
|
||||
'stats': context.get('stats', {}),
|
||||
'using_seed_examples': context.get('stats', {}).get('using_seed_examples', False),
|
||||
'positive_examples_count': len(positive_examples),
|
||||
'negative_examples_count': len(negative_examples),
|
||||
'positive_examples': positive_examples,
|
||||
'negative_examples': negative_examples,
|
||||
'negative_patterns': context.get('negative_patterns', []),
|
||||
'generated_at': context.get('generated_at')
|
||||
})
|
||||
except ImportError:
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'learning_active': False,
|
||||
'message': 'Feedback learning service not available'
|
||||
})
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting AI learning status: {e}")
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': str(e)
|
||||
}), 500
|
||||
|
||||
|
||||
# ============================================================
|
||||
# CHAT STATS API
|
||||
# ============================================================
|
||||
|
||||
@bp.route('/chat-stats')
|
||||
@login_required
|
||||
def api_chat_stats():
|
||||
"""API: Get chat statistics for dashboard"""
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'success': False, 'error': 'Not authorized'}), 403
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from database import SessionLocal, AIChatMessage
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
from sqlalchemy import func
|
||||
|
||||
# Stats for last 7 days
|
||||
week_ago = datetime.now() - timedelta(days=7)
|
||||
|
||||
daily_stats = db.query(
|
||||
func.date(AIChatMessage.created_at).label('date'),
|
||||
func.count(AIChatMessage.id).label('count')
|
||||
).filter(
|
||||
AIChatMessage.created_at >= week_ago,
|
||||
AIChatMessage.role == 'user'
|
||||
).group_by(
|
||||
func.date(AIChatMessage.created_at)
|
||||
).order_by('date').all()
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'daily_queries': [{'date': str(d.date), 'count': d.count} for d in daily_stats]
|
||||
})
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@ -318,7 +318,7 @@
|
||||
// Load AI Learning Status
|
||||
async function loadLearningStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/admin/ai-learning-status');
|
||||
const response = await fetch('/admin/ai-learning-status');
|
||||
const data = await response.json();
|
||||
|
||||
if (!data.success) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user