|
| 1 | +import os |
| 2 | +import uuid |
| 3 | +import pandas as pd |
| 4 | +import supabase |
| 5 | +from flask import send_file |
| 6 | + |
| 7 | +def export_convo_history_csv(course_name: str, from_date= '', to_date= ''): |
| 8 | + """ |
| 9 | + Export conversation history to csv file. |
| 10 | + Optional args: from_date, to_date |
| 11 | + """ |
| 12 | + print("Exporting conversation history to csv file...") |
| 13 | + supabase_client = supabase.create_client( # type: ignore |
| 14 | + supabase_url=os.getenv('SUPABASE_URL'), # type: ignore |
| 15 | + supabase_key=os.getenv('SUPABASE_API_KEY')) # type: ignore |
| 16 | + |
| 17 | + if from_date == '' and to_date == '': |
| 18 | + # Get all data |
| 19 | + print("No dates") |
| 20 | + response = supabase_client.table("llm-convo-monitor").select("id", count = 'exact').eq("course_name", course_name).order('id', desc=False).execute() |
| 21 | + elif from_date != '' and to_date == '': |
| 22 | + print("only from_date") |
| 23 | + # Get data from from_date to now |
| 24 | + response = supabase_client.table("llm-convo-monitor").select("id", count = 'exact').eq("course_name", course_name).gte('created_at', from_date).order('id', desc=False).execute() |
| 25 | + elif from_date == '' and to_date != '': |
| 26 | + print("only to_date") |
| 27 | + # Get data from beginning to to_date |
| 28 | + response = supabase_client.table("llm-convo-monitor").select("id", count = 'exact').eq("course_name", course_name).lte('created_at', to_date).order('id', desc=False).execute() |
| 29 | + else: |
| 30 | + print("both from_date and to_date") |
| 31 | + # Get data from from_date to to_date |
| 32 | + response = supabase_client.table("llm-convo-monitor").select("id", count = 'exact').eq("course_name", course_name).gte('created_at', from_date).lte('created_at', to_date).order('id', desc=False).execute() |
| 33 | + |
| 34 | + # Fetch data |
| 35 | + if response.count > 0: |
| 36 | + print("id count greater than zero") |
| 37 | + first_id = response.data[0]['id'] |
| 38 | + last_id = response.data[-1]['id'] |
| 39 | + |
| 40 | + filename = course_name + '_' + str(uuid.uuid4()) + '_convo_history.csv' |
| 41 | + file_path = os.path.join(os.getcwd(), filename) |
| 42 | + # Fetch data in batches of 25 from first_id to last_id |
| 43 | + while first_id <= last_id: |
| 44 | + print("Fetching data from id: ", first_id) |
| 45 | + response = supabase_client.table("llm-convo-monitor").select("*").eq("course_name", course_name).gte('id', first_id).lte('id', last_id).order('id', desc=False).limit(25).execute() |
| 46 | + # Convert to pandas dataframe |
| 47 | + df = pd.DataFrame(response.data) |
| 48 | + # Append to csv file |
| 49 | + if not os.path.isfile(file_path): |
| 50 | + df.to_csv(file_path, mode='a', header=True, index=False) |
| 51 | + else: |
| 52 | + df.to_csv(file_path, mode='a', header=False, index=False) |
| 53 | + |
| 54 | + # Update first_id |
| 55 | + first_id = response.data[-1]['id'] + 1 |
| 56 | + print("updated first_id: ", first_id) |
| 57 | + |
| 58 | + # Download file |
| 59 | + try: |
| 60 | + return (file_path, filename, os.getcwd()) |
| 61 | + except Exception as e: |
| 62 | + print(e) |
| 63 | + return "Error downloading file" |
| 64 | + else: |
| 65 | + return "No data found between the dates" |
| 66 | + |
0 commit comments