Skip to content

Commit

Permalink
better handling for lists in inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmin3 committed Apr 2, 2024
1 parent 81c9869 commit aa55828
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ai_ta_backend/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def get_users(self, limit: int = 50, pagination: bool = True, api_key: str = "")
return all_users

def execute_flow(self, hook: str, data=None) -> None:
print("Executing flow")
if not data:
data = {'field-0': ''}
url = hook
Expand Down Expand Up @@ -67,7 +68,6 @@ def get_executions(self, limit, id=None, pagination: bool = True, api_key: str =
cursor = executions.get('nextCursor')
if id:
for execution in all_executions:
print(execution[0]['id'], ":ID:")
if execution[0]['id'] == id:
return execution

Expand Down Expand Up @@ -130,24 +130,21 @@ def format_data(self, inputted, api_key: str, workflow_name):
print("Got workflow")
values = []
if isinstance(work_flow, dict) and 'nodes' in work_flow:
print("passed if")
for node in work_flow['nodes']:
if node['name'] == 'n8n Form Trigger':
values = node['parameters']['formFields']['values']
print("found value")
data = {}
print(inputted)
inputted = json.loads(inputted)
print("Done with json")
inputted = dict(inputted)
print("Got data")
for i, value in enumerate(values):
field_name = 'field-' + str(i)
data[value['fieldLabel']] = field_name
new_data = {}
for k, v in inputted.items():
new_data[data[k]] = v
print("Done with formatting")
if type(v) == list:
new_data[data[k]] = json.dumps(v)
else:
new_data[data[k]] = v
return new_data

# TODO: activate and disactivate workflows
Expand Down Expand Up @@ -210,14 +207,20 @@ def main_flow(self, name: str, api_key: str = "", data: str = ""):

try:
start_time = time.monotonic()
print("Inserting")
self.supabase_client.table('n8n_workflows').insert({"latest_workflow_id": id, "is_locked": True}).execute()
print("inserted")
self.execute_flow(hook, new_data)
print("Executed")
print(f"⏰ Runtime to execute_flow(): {(time.monotonic() - start_time):.4f} seconds")
except:
except Exception as e:
# TODO: Decrease number by one, is locked false
# self.supabase_client.table('n8n_workflows').update({"latest_workflow_id": str(int(id) - 1), "is_locked": False}).eq('latest_workflow_id', id).execute()
self.supabase_client.table('n8n_workflows').delete().eq('latest_workflow_id', id).execute()
return {"error": str(e)}
finally:
# TODO: Remove lock from Supabase table.
print("id: ", id)
self.supabase_client.table('n8n_workflows').update({"is_locked": False}).eq('latest_workflow_id', id).execute()

try:
Expand Down

0 comments on commit aa55828

Please sign in to comment.