-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into file-update
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
import uuid | ||
import pandas as pd | ||
import supabase | ||
from flask import send_file | ||
|
||
def export_convo_history_csv(course_name: str, from_date= '', to_date= ''): | ||
""" | ||
Export conversation history to csv file. | ||
Optional args: from_date, to_date | ||
""" | ||
print("Exporting conversation history to csv file...") | ||
supabase_client = supabase.create_client( # type: ignore | ||
supabase_url=os.getenv('SUPABASE_URL'), # type: ignore | ||
supabase_key=os.getenv('SUPABASE_API_KEY')) # type: ignore | ||
|
||
if from_date == '' and to_date == '': | ||
# Get all data | ||
print("No dates") | ||
response = supabase_client.table("llm-convo-monitor").select("id", count = 'exact').eq("course_name", course_name).order('id', desc=False).execute() | ||
elif from_date != '' and to_date == '': | ||
print("only from_date") | ||
# Get data from from_date to now | ||
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() | ||
elif from_date == '' and to_date != '': | ||
print("only to_date") | ||
# Get data from beginning to to_date | ||
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() | ||
else: | ||
print("both from_date and to_date") | ||
# Get data from from_date to to_date | ||
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() | ||
|
||
# Fetch data | ||
if response.count > 0: | ||
print("id count greater than zero") | ||
first_id = response.data[0]['id'] | ||
last_id = response.data[-1]['id'] | ||
|
||
filename = course_name + '_' + str(uuid.uuid4()) + '_convo_history.csv' | ||
file_path = os.path.join(os.getcwd(), filename) | ||
# Fetch data in batches of 25 from first_id to last_id | ||
while first_id <= last_id: | ||
print("Fetching data from id: ", first_id) | ||
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() | ||
# Convert to pandas dataframe | ||
df = pd.DataFrame(response.data) | ||
# Append to csv file | ||
if not os.path.isfile(file_path): | ||
df.to_csv(file_path, mode='a', header=True, index=False) | ||
else: | ||
df.to_csv(file_path, mode='a', header=False, index=False) | ||
|
||
# Update first_id | ||
first_id = response.data[-1]['id'] + 1 | ||
print("updated first_id: ", first_id) | ||
|
||
# Download file | ||
try: | ||
return (file_path, filename, os.getcwd()) | ||
except Exception as e: | ||
print(e) | ||
return "Error downloading file" | ||
else: | ||
return "No data found between the dates" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters