ibraheem007 commited on
Commit
17850d3
·
verified ·
1 Parent(s): e8dad37

Update db/helpers.py

Browse files
Files changed (1) hide show
  1. db/helpers.py +122 -98
db/helpers.py CHANGED
@@ -495,21 +495,21 @@ def get_advanced_research_metrics():
495
  """Get comprehensive research metrics including user types, levels, etc. - FIXED VERSION"""
496
  with next(get_db()) as db:
497
  try:
498
- # Initialize metrics structure
499
  metrics = {
500
  'models': {
501
  'groq': {
502
  'user_types': {},
503
  'student_levels': {},
504
  'complexity_distribution': {},
505
- 'comment_analysis': {},
506
  'regeneration_types': {}
507
  },
508
  'phi3': {
509
  'user_types': {},
510
  'student_levels': {},
511
  'complexity_distribution': {},
512
- 'comment_analysis': {},
513
  'regeneration_types': {}
514
  }
515
  },
@@ -521,106 +521,130 @@ def get_advanced_research_metrics():
521
  }
522
 
523
  # Database summary - FIXED QUERIES
524
- total_users = db.query(User).count()
525
- total_content = db.query(ContentHistory).count()
526
- total_feedback = db.query(Feedback).count()
527
-
528
- metrics['database_summary'] = {
529
- 'total_users': total_users,
530
- 'total_content': total_content,
531
- 'total_feedback': total_feedback
532
- }
533
-
534
- # User type analysis - FIXED
535
- user_type_query = db.query(
536
- ContentHistory.user_type,
537
- ContentHistory.generated_model,
538
- func.count(Feedback.id).label('count'),
539
- func.avg(Feedback.clarity).label('avg_clarity'),
540
- func.avg(Feedback.depth).label('avg_depth')
541
- ).join(Feedback, ContentHistory.id == Feedback.content_id).group_by(
542
- ContentHistory.user_type, ContentHistory.generated_model
543
- ).all()
544
-
545
- for row in user_type_query:
546
- user_type, model, count, avg_clarity, avg_depth = row
547
- if model in metrics['models']:
548
- metrics['models'][model]['user_types'][user_type] = {
549
- 'count': count,
550
- 'avg_clarity': float(avg_clarity) if avg_clarity else 0,
551
- 'avg_depth': float(avg_depth) if avg_depth else 0
552
- }
553
-
554
- # Student level analysis - FIXED
555
- level_query = db.query(
556
- ContentHistory.student_level,
557
- ContentHistory.generated_model,
558
- func.count(Feedback.id).label('count'),
559
- func.avg(Feedback.clarity).label('avg_clarity')
560
- ).join(Feedback, ContentHistory.id == Feedback.content_id).group_by(
561
- ContentHistory.student_level, ContentHistory.generated_model
562
- ).all()
563
-
564
- for row in level_query:
565
- level, model, count, avg_clarity = row
566
- if model in metrics['models']:
567
- metrics['models'][model]['student_levels'][level] = {
568
- 'count': count,
569
- 'avg_clarity': float(avg_clarity) if avg_clarity else 0
570
- }
571
 
572
- # Complexity distribution - FIXED
573
- complexity_query = db.query(
574
- ContentHistory.generated_model,
575
- Feedback.complexity,
576
- func.count(Feedback.id).label('count')
577
- ).join(Feedback, ContentHistory.id == Feedback.content_id).filter(
578
- Feedback.complexity.isnot(None)
579
- ).group_by(
580
- ContentHistory.generated_model, Feedback.complexity
581
- ).all()
 
 
 
 
 
 
 
 
 
 
 
 
582
 
583
- for row in complexity_query:
584
- model, complexity, count = row
585
- if model in metrics['models']:
586
- metrics['models'][model]['complexity_distribution'][complexity] = count
587
-
588
- # Comment analysis - FIXED
589
- comment_query = db.query(
590
- ContentHistory.generated_model,
591
- func.avg(func.length(Feedback.comments)).label('avg_length'),
592
- func.count().filter(
593
- Feedback.clarity >= 4,
594
- Feedback.depth >= 4,
595
- func.length(Feedback.comments) > 25
596
- ).label('high_quality_count')
597
- ).join(Feedback, ContentHistory.id == Feedback.content_id).group_by(
598
- ContentHistory.generated_model
599
- ).all()
 
 
 
600
 
601
- for row in comment_query:
602
- model, avg_length, hq_count = row
603
- if model in metrics['models']:
604
- metrics['models'][model]['comment_analysis'] = {
605
- 'avg_length': float(avg_length) if avg_length else 0,
606
- 'high_quality_count': hq_count or 0
607
- }
 
 
 
 
 
 
 
 
 
 
 
608
 
609
- # Regeneration type analysis - FIXED
610
- regeneration_query = db.query(
611
- ContentHistory.generated_model,
612
- Feedback.regeneration_type,
613
- func.count(Feedback.id).label('count')
614
- ).join(Feedback, ContentHistory.id == Feedback.content_id).filter(
615
- Feedback.regeneration_type.isnot(None)
616
- ).group_by(
617
- ContentHistory.generated_model, Feedback.regeneration_type
618
- ).all()
 
 
 
 
 
 
 
 
 
 
 
 
 
619
 
620
- for row in regeneration_query:
621
- model, regeneration_type, count = row
622
- if model in metrics['models']:
623
- metrics['models'][model]['regeneration_types'][regeneration_type] = count
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
 
625
  print(f"✅ Advanced metrics loaded: {total_users} users, {total_content} content, {total_feedback} feedback")
626
  return metrics
 
495
  """Get comprehensive research metrics including user types, levels, etc. - FIXED VERSION"""
496
  with next(get_db()) as db:
497
  try:
498
+ # Initialize metrics structure with default values
499
  metrics = {
500
  'models': {
501
  'groq': {
502
  'user_types': {},
503
  'student_levels': {},
504
  'complexity_distribution': {},
505
+ 'comment_analysis': {'avg_length': 0, 'high_quality_count': 0},
506
  'regeneration_types': {}
507
  },
508
  'phi3': {
509
  'user_types': {},
510
  'student_levels': {},
511
  'complexity_distribution': {},
512
+ 'comment_analysis': {'avg_length': 0, 'high_quality_count': 0},
513
  'regeneration_types': {}
514
  }
515
  },
 
521
  }
522
 
523
  # Database summary - FIXED QUERIES
524
+ try:
525
+ total_users = db.query(User).count()
526
+ total_content = db.query(ContentHistory).count()
527
+ total_feedback = db.query(Feedback).count()
528
+
529
+ metrics['database_summary'] = {
530
+ 'total_users': total_users,
531
+ 'total_content': total_content,
532
+ 'total_feedback': total_feedback
533
+ }
534
+ except Exception as e:
535
+ print(f"⚠️ Database summary error: {e}")
536
+ # Use fallback values
537
+ metrics['database_summary'] = {
538
+ 'total_users': 0,
539
+ 'total_content': 0,
540
+ 'total_feedback': 0
541
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
 
543
+ # User type analysis - FIXED with error handling
544
+ try:
545
+ user_type_query = db.query(
546
+ ContentHistory.user_type,
547
+ ContentHistory.generated_model,
548
+ func.count(Feedback.id).label('count'),
549
+ func.avg(Feedback.clarity).label('avg_clarity'),
550
+ func.avg(Feedback.depth).label('avg_depth')
551
+ ).join(Feedback, ContentHistory.id == Feedback.content_id).group_by(
552
+ ContentHistory.user_type, ContentHistory.generated_model
553
+ ).all()
554
+
555
+ for row in user_type_query:
556
+ user_type, model, count, avg_clarity, avg_depth = row
557
+ if model in metrics['models']:
558
+ metrics['models'][model]['user_types'][user_type] = {
559
+ 'count': count,
560
+ 'avg_clarity': float(avg_clarity) if avg_clarity else 0,
561
+ 'avg_depth': float(avg_depth) if avg_depth else 0
562
+ }
563
+ except Exception as e:
564
+ print(f"⚠️ User type analysis error: {e}")
565
 
566
+ # Student level analysis - FIXED with error handling
567
+ try:
568
+ level_query = db.query(
569
+ ContentHistory.student_level,
570
+ ContentHistory.generated_model,
571
+ func.count(Feedback.id).label('count'),
572
+ func.avg(Feedback.clarity).label('avg_clarity')
573
+ ).join(Feedback, ContentHistory.id == Feedback.content_id).group_by(
574
+ ContentHistory.student_level, ContentHistory.generated_model
575
+ ).all()
576
+
577
+ for row in level_query:
578
+ level, model, count, avg_clarity = row
579
+ if model in metrics['models']:
580
+ metrics['models'][model]['student_levels'][level] = {
581
+ 'count': count,
582
+ 'avg_clarity': float(avg_clarity) if avg_clarity else 0
583
+ }
584
+ except Exception as e:
585
+ print(f"⚠️ Student level analysis error: {e}")
586
 
587
+ # Complexity distribution - FIXED with error handling
588
+ try:
589
+ complexity_query = db.query(
590
+ ContentHistory.generated_model,
591
+ Feedback.complexity,
592
+ func.count(Feedback.id).label('count')
593
+ ).join(Feedback, ContentHistory.id == Feedback.content_id).filter(
594
+ Feedback.complexity.isnot(None)
595
+ ).group_by(
596
+ ContentHistory.generated_model, Feedback.complexity
597
+ ).all()
598
+
599
+ for row in complexity_query:
600
+ model, complexity, count = row
601
+ if model in metrics['models']:
602
+ metrics['models'][model]['complexity_distribution'][complexity] = count
603
+ except Exception as e:
604
+ print(f"⚠️ Complexity distribution error: {e}")
605
 
606
+ # Comment analysis - FIXED with error handling
607
+ try:
608
+ comment_query = db.query(
609
+ ContentHistory.generated_model,
610
+ func.avg(func.length(Feedback.comments)).label('avg_length'),
611
+ func.count().filter(
612
+ Feedback.clarity >= 4,
613
+ Feedback.depth >= 4,
614
+ func.length(Feedback.comments) > 25
615
+ ).label('high_quality_count')
616
+ ).join(Feedback, ContentHistory.id == Feedback.content_id).group_by(
617
+ ContentHistory.generated_model
618
+ ).all()
619
+
620
+ for row in comment_query:
621
+ model, avg_length, hq_count = row
622
+ if model in metrics['models']:
623
+ metrics['models'][model]['comment_analysis'] = {
624
+ 'avg_length': float(avg_length) if avg_length else 0,
625
+ 'high_quality_count': hq_count or 0
626
+ }
627
+ except Exception as e:
628
+ print(f"⚠️ Comment analysis error: {e}")
629
 
630
+ # Regeneration type analysis - FIXED with error handling
631
+ try:
632
+ regeneration_query = db.query(
633
+ ContentHistory.generated_model,
634
+ Feedback.regeneration_type,
635
+ func.count(Feedback.id).label('count')
636
+ ).join(Feedback, ContentHistory.id == Feedback.content_id).filter(
637
+ Feedback.regeneration_type.isnot(None)
638
+ ).group_by(
639
+ ContentHistory.generated_model, Feedback.regeneration_type
640
+ ).all()
641
+
642
+ for row in regeneration_query:
643
+ model, regeneration_type, count = row
644
+ if model in metrics['models']:
645
+ metrics['models'][model]['regeneration_types'][regeneration_type] = count
646
+ except Exception as e:
647
+ print(f"⚠️ Regeneration type analysis error: {e}")
648
 
649
  print(f"✅ Advanced metrics loaded: {total_users} users, {total_content} content, {total_feedback} feedback")
650
  return metrics