Spaces:
Sleeping
Sleeping
File size: 4,632 Bytes
c6af53f 5a695a2 c6af53f 9894c09 c6af53f 9894c09 c6af53f 450f9be c6af53f b7162be c6af53f 9894c09 c6af53f 9894c09 c6af53f 9894c09 c6af53f 9894c09 c6af53f |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import requests
import pandas as pd
from typing import Dict, List, Optional
import json
class DatabaseConnection:
def __init__(self, base_url: str = "https://database-1-xzm0.onrender.com"):
self.base_url = base_url
self.session = requests.Session()
def get_student_feedback_counts(self) -> pd.DataFrame:
"""Fetch student feedback data from the database"""
try:
url = f"{self.base_url}/student_feedback_counts"
response = self.session.get(url, timeout=10) # Add timeout
response.raise_for_status()
data = response.json()
if isinstance(data, list):
return pd.DataFrame(data)
elif isinstance(data, dict) and 'feedback_counts' in data:
# Handle nested structure
feedback_data = data['feedback_counts']
if isinstance(feedback_data, list):
return pd.DataFrame(feedback_data)
else:
return pd.DataFrame([feedback_data])
else:
return pd.DataFrame([data])
except requests.exceptions.Timeout:
print("⚠️ Database request timed out")
return pd.DataFrame()
except requests.exceptions.ConnectionError:
print("⚠️ Could not connect to database")
return pd.DataFrame()
except Exception as e:
print(f"Error fetching data: {e}")
return pd.DataFrame()
def add_feedback(self, course: str, stanine: int, gwa: float, strand: str,
rating: str, hobbies: str) -> bool:
"""Add new feedback to the database"""
try:
url = f"{self.base_url}/student_feedback_counts"
data = {
'course': course,
'stanine': stanine,
'gwa': gwa,
'strand': strand,
'rating': rating,
'hobbies': hobbies,
'count': 1
}
print(f"Attempting to add feedback: {course}, rating: {rating}")
response = self.session.post(url, json=data, timeout=10)
response.raise_for_status()
result = response.json()
print(f"✅ Feedback added successfully: {result}")
return True
except requests.exceptions.Timeout:
print("⚠️ Database request timed out")
return False
except requests.exceptions.ConnectionError:
print("⚠️ Could not connect to database")
return False
except Exception as e:
print(f"❌ Error adding feedback: {e}")
return False
def update_feedback_count(self, feedback_id: int, count: int) -> bool:
"""Update the count for existing feedback"""
try:
url = f"{self.base_url}/student_feedback_counts/{feedback_id}"
data = {"count": count}
response = self.session.put(url, json=data, timeout=10)
response.raise_for_status()
return True
except requests.exceptions.Timeout:
print("⚠️ Database request timed out")
return False
except requests.exceptions.ConnectionError:
print("⚠️ Could not connect to database")
return False
except Exception as e:
print(f"Error updating feedback count: {e}")
return False
def get_available_courses(self) -> List[str]:
"""Fetch available courses from the database"""
try:
url = f"{self.base_url}/courses"
response = self.session.get(url, timeout=10)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
# Extract course names from the data
courses = []
for item in data:
if isinstance(item, dict) and 'name' in item:
courses.append(item['name'])
elif isinstance(item, str):
courses.append(item)
return courses
else:
return []
except requests.exceptions.Timeout:
print("⚠️ Database request timed out")
return []
except requests.exceptions.ConnectionError:
print("⚠️ Could not connect to database")
return []
except Exception as e:
print(f"Error fetching courses: {e}")
return []
|