Skip to content

Commit

Permalink
Merge branch 'flows' of https://github.com/UIUC-Chatbot/ai-ta-backend
Browse files Browse the repository at this point in the history
…into flows
  • Loading branch information
jkmin3 committed Apr 1, 2024
2 parents 3628dd0 + 11cb80d commit bd90f4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 14 additions & 3 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,20 @@ def main_flow(self, name: str, api_key: str = "", data: str = ""):
raise Exception('No executions found')
id = str(id)

# start job
try:
self.supabase_client.table('n8n_api_keys').insert({"id": id}).execute()
start_time = time.monotonic()
self.supabase_client.table('n8n_api_keys').insert({"in_progress_workflow_id": id}).execute()
self.execute_flow(hook, new_data)
print("Executed")
print(f"⏰ Runtime to execute_flow(): {(time.monotonic() - start_time):.4f} seconds")
except:
pass
finally:
# TODO: Remove lock from Supabase table.
pass

try:
executions = self.get_executions(20, id, True, api_key)
print("Got executions", executions)
while executions is None:
Expand All @@ -220,10 +230,11 @@ def main_flow(self, name: str, api_key: str = "", data: str = ""):
print("Can't find id in executions")
time.sleep(1)
print("Found id in executions ")
self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute()
self.supabase_client.table('n8n_api_keys').delete().eq('in_progress_workflow_id', id).execute()
print("Deleted id")
print("Returning")
except Exception as e:
self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute()
self.supabase_client.table('n8n_api_keys').delete().eq('in_progress_workflow_id', id).execute()
return {"error": str(e)}

return executions
8 changes: 7 additions & 1 deletion ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ def get_all_workflows() -> Response:
name = request.args.get('workflow_name', default='', type=str)
print(request.args)

print("In get_all_workflows.. api_key: ", api_key)

if api_key == '':
# proper web error "400 Bad request"
abort(400, description=f"Missing N8N API_KEY: 'api_key' must be provided. Search query: `{api_key}`")
Expand All @@ -724,8 +726,12 @@ def get_all_workflows() -> Response:
response.headers.add('Access-Control-Allow-Origin', '*')
return response
except Exception as e:
if e == "Unauthorized":
if "unauthorized" in str(e).lower():
print("Unauthorized error in get_all_workflows: ", e)
abort(401, description=f"Unauthorized: 'api_key' is invalid. Search query: `{api_key}`")
else:
print("Error in get_all_workflows: ", e)
abort(500, description=f"Failed to fetch n8n workflows: {e}")


@app.route('/switch_workflow', methods=['GET'])
Expand Down

0 comments on commit bd90f4b

Please sign in to comment.