66import zipfile
77
88import pandas as pd
9- import requests
9+ import redis
1010from injector import inject
1111
1212from ai_ta_backend .database .aws import AWSStorage
@@ -89,7 +89,7 @@ def export_documents_json(self, course_name: str, from_date='', to_date=''):
8989
9090 curr_doc_count = 0
9191 # create a temporary directory
92- temp_dir = tempfile .mkdtemp (prefix = "export_" )
92+ temp_dir = tempfile .mkdtemp (prefix = "export_" , dir = tempfile . gettempdir () )
9393
9494 filename = course_name + '_' + str (uuid .uuid4 ()) + '_documents.jsonl'
9595 file_path = os .path .join (temp_dir , filename )
@@ -160,7 +160,7 @@ def export_convo_history_json(self, course_name: str, from_date='', to_date=''):
160160 last_id = response ["data" ][- 1 ]['id' ]
161161 total_count = count
162162
163- temp_dir = tempfile .mkdtemp (prefix = "export_" )
163+ temp_dir = tempfile .mkdtemp (prefix = "export_" , dir = tempfile . gettempdir () )
164164
165165 filename = course_name [0 :10 ] + '-convos.jsonl'
166166 file_path = os .path .join (temp_dir , filename )
@@ -231,7 +231,7 @@ def export_conversations(self, course_name: str, from_date: str, to_date: str, e
231231 last_id = response ["data" ][- 1 ]['id' ]
232232 total_count = count
233233
234- temp_dir = tempfile .mkdtemp (prefix = "export_" )
234+ temp_dir = tempfile .mkdtemp (prefix = "export_" , dir = tempfile . gettempdir () )
235235
236236 filename = course_name [0 :10 ] + '-convos.jsonl'
237237 file_path = os .path .join (temp_dir , filename )
@@ -538,13 +538,15 @@ def export_data_in_bg_extended(response, download_type, course_name, s3_path):
538538 os .remove (zip_file_path )
539539 s3_url = s3 .generatePresignedUrl ('get_object' , os .environ ['S3_BUCKET_NAME' ], s3_path , 172800 )
540540
541- # Fetch course metadata to get admin emails
542- headers = {"Authorization" : f"Bearer { os .environ ['VERCEL_READ_ONLY_API_KEY' ]} " , "Content-Type" : "application/json" }
543- hget_url = str (os .environ ['VERCEL_BASE_URL' ]) + "course_metadatas/" + course_name
544- response = requests .get (hget_url , headers = headers )
545- course_metadata = response .json ()
546- course_metadata = json .loads (course_metadata ['result' ])
547- admin_emails = course_metadata ['course_admins' ]
541+ # get admin email IDs from Redis
542+ print ("Connecting to Redis... with url: " , os .environ ['REDIS_URL' ])
543+ redis_client = redis .Redis .from_url (os .environ ['REDIS_URL' ], db = 0 )
544+ course_metadata_json = redis_client .hget ('course_metadatas' , key = course_name )
545+ if not course_metadata_json :
546+ raise ValueError (f"No course metadata found in Redis for project '{ course_name } '" )
547+ course_metadata = json .loads (course_metadata_json )
548+ admin_emails = course_metadata .get ('course_admins' , [])
549+
548550 bcc_emails = []
549551
550552 # Handle specific email cases
@@ -584,7 +586,7 @@ def export_data_in_bg(response, download_type, course_name, s3_path):
584586 response (dict): The response from the Supabase query.
585587 download_type (str): The type of download - 'documents' or 'conversations'.
586588 course_name (str): The name of the course.
587- s3_path (str): The S3 path where the file will be uploaded.
589+ s3_path (str): The S3 path where the file will be uploaded.
588590 """
589591 s3 = AWSStorage ()
590592 sql = SQLDatabase ()
@@ -595,7 +597,7 @@ def export_data_in_bg(response, download_type, course_name, s3_path):
595597 print ("pre-defined s3_path: " , s3_path )
596598
597599 curr_doc_count = 0
598- temp_dir = tempfile .mkdtemp (prefix = "export_" )
600+ temp_dir = tempfile .mkdtemp (prefix = "export_" , dir = tempfile . gettempdir () )
599601
600602 filename = s3_path .split ('/' )[- 1 ].split ('.' )[0 ] + '.jsonl'
601603 file_path = os .path .join (temp_dir , filename )
@@ -641,17 +643,18 @@ def export_data_in_bg(response, download_type, course_name, s3_path):
641643 # generate presigned URL
642644 s3_url = s3 .generatePresignedUrl ('get_object' , os .environ ['S3_BUCKET_NAME' ], s3_path , 172800 )
643645
644- # get admin email IDs
645- headers = {"Authorization" : f"Bearer { os .environ ['VERCEL_READ_ONLY_API_KEY' ]} " , "Content-Type" : "application/json" }
646+ # get admin email IDs from Redis
647+ print ("Connecting to Redis... with url: " , os .environ ['REDIS_URL' ])
648+ redis_client = redis .Redis .from_url (os .environ ['REDIS_URL' ], db = 0 )
649+ course_metadata_json = redis_client .hget ('course_metadatas' , key = course_name )
650+ if not course_metadata_json :
651+ raise ValueError (f"No course metadata found in Redis for project '{ course_name } '" )
652+ course_metadata = json .loads (course_metadata_json )
653+ admin_emails = course_metadata .get ('course_admins' , [])
646654
647- hget_url = str (os .environ ['VERCEL_BASE_URL' ]) + "course_metadatas/" + course_name
648- response = requests .get (hget_url , headers = headers )
649- course_metadata = response .json ()
650- course_metadata = json .loads (course_metadata ['result' ])
651- admin_emails = course_metadata ['course_admins' ]
652655 bcc_emails = []
653656
654- # check for Kastan 's email and move to bcc
657+ # check for dev 's email and move to bcc
655658 if 'rohan13@illinois.edu' in admin_emails :
656659 admin_emails .remove ('rohan13@illinois.edu' )
657660 bcc_emails .append ('rohan13@illinois.edu' )
@@ -707,7 +710,7 @@ def export_data_in_bg_emails(response, download_type, course_name, s3_path, emai
707710
708711 curr_doc_count = 0
709712
710- temp_dir = tempfile .mkdtemp (prefix = "export_" )
713+ temp_dir = tempfile .mkdtemp (prefix = "export_" , dir = tempfile . gettempdir () )
711714
712715 filename = s3_path .split ('/' )[- 1 ].split ('.' )[0 ] + '.jsonl'
713716 file_path = os .path .join (temp_dir , filename )
0 commit comments