Skip to content

Commit

Permalink
code to remove file from repo after download
Browse files Browse the repository at this point in the history
  • Loading branch information
star-nox committed Nov 13, 2023
1 parent 01c962e commit 4b4b75a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
12 changes: 5 additions & 7 deletions ai_ta_backend/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,29 @@ def export_convo_history_csv(course_name: str, from_date= '', to_date= ''):
first_id = response.data[0]['id']
last_id = response.data[-1]['id']
filename = course_name + '_convo_history.csv'

file_path = os.path.join(os.getcwd(), 'docs/' + 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('docs/' + filename):
df.to_csv('docs/' + filename, mode='a', header=True, index=False)
if not os.path.isfile(file_path):
df.to_csv(file_path, mode='a', header=True, index=False)
else:
df.to_csv('docs/' + filename, mode='a', header=False, index=False)
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
file_path = os.path.join(os.getcwd(), 'docs/' + filename)
try:
return (file_path, 'docs/' + filename, os.getcwd())
return (file_path, filename, os.getcwd())
except Exception as e:
print(e)
return "Error downloading file"
else:
print("No data found between the dates")
return "No data found between the dates"

6 changes: 2 additions & 4 deletions ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,12 @@ def export_convo_history():

export_status = export_convo_history_csv(course_name, from_date, to_date)
print(export_status)
print("path: ", os.getcwd() + '/docs/cropwizard_convo_history.csv')
print("path exists: ", os.path.exists(os.getcwd() + '/docs/cropwizard_convo_history.csv'))

response = make_response(send_file(os.getcwd() + '/docs/cropwizard_convo_history.csv', as_attachment=True))
#response = make_response(send_from_directory('docs', 'cropwizard_convo_history.csv', as_attachment=True))
response = make_response(send_from_directory(export_status[2], export_status[1], as_attachment=True))
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers["Content-Disposition"] = f"attachment; filename={export_status[1]}"

os.remove(export_status[0])
return response


Expand Down

0 comments on commit 4b4b75a

Please sign in to comment.