From 206ab2decb25b15bec30d2009b0dafb68ae07ee9 Mon Sep 17 00:00:00 2001 From: frikky Date: Wed, 16 Sep 2020 20:52:11 +0200 Subject: [PATCH] #149: Properly fixed app sdk loop issues --- backend/app_sdk/app_base.py | 168 ++++++++++++++++++++---------- backend/go-app/main.go | 5 - backend/go-app/walkoff.go | 16 ++- docker-compose.yml | 2 +- frontend/src/App.jsx | 2 +- frontend/src/components/Header.js | 2 +- 6 files changed, 121 insertions(+), 74 deletions(-) diff --git a/backend/app_sdk/app_base.py b/backend/app_sdk/app_base.py index e2a7f4f5..71e50a56 100644 --- a/backend/app_sdk/app_base.py +++ b/backend/app_sdk/app_base.py @@ -361,11 +361,99 @@ class AppBase: newlist.append("parsing_error") return " ".join(newlist) + def recurse_json(basejson, parsersplit): + match = "#(\d+):?-?([0-9a-z]+)?#?" + print("Split: %s\n%s" % (parsersplit, basejson)) + try: + outercnt = 0 + for value in parsersplit: + print("VALUE: %s\n" % value) + actualitem = re.findall(match, value, re.MULTILINE) + if value == "#": + newvalue = [] + for innervalue in basejson: + # 1. Check the next item (message) + # 2. Call this function again + + try: + ret = recurse_json(innervalue, parsersplit[outercnt+1:]) + except IndexError: + print("INDEXERROR: ", parsersplit[outercnt]) + #ret = innervalue + ret = recurse_json(innervalue, parsersplit[outercnt:]) + + print(ret) + #exit() + newvalue.append(ret) + + return newvalue + elif len(actualitem) > 0: + # FIXME: This is absolutely not perfect. + print("IN HERE: ", actualitem) + + newvalue = [] + firstitem = actualitem[0][0] + seconditem = actualitem[0][1] + if seconditem == "": + print("In first") + basejson = basejson[int(firstitem)] + else: + if seconditem == "max": + seconditem = len(basejson) + if seconditem == "min": + seconditem = 0 + + newvalue = [] + for i in range(int(firstitem), int(seconditem)): + # 1. Check the next item (message) + # 2. Call this function again + print("Base: %s" % basejson[i]) + + try: + ret = recurse_loop(basejson[i], parsersplit[outercnt+1:]) + except IndexError: + print("INDEXERROR: ", parsersplit[outercnt]) + #ret = innervalue + ret = recurse_loop(innervalue, parsersplit[outercnt:]) + + print(ret) + #exit() + newvalue.append(ret) + + return newvalue + + # FIXME: Add specific loop for other indexes + else: + #print("BEFORE NORMAL VALUE: ", basejson, value) + if len(value) == 0: + return basejson + + if isinstance(basejson[value], str): + print(f"LOADING STRING '%s' AS JSON" % basejson[value]) + try: + basejson = json.loads(basejson[value]) + except json.decoder.JSONDecodeError as e: + print("RETURNING BECAUSE '%s' IS A NORMAL STRING" % basejson[value]) + return basejson[value] + else: + basejson = basejson[value] + + outercnt += 1 + + except KeyError as e: + print("Lower keyerror: %s" % e) + #return basejson + #return "KeyError: Couldn't find key: %s" % e + + return basejson + # Takes a workflow execution as argument # Returns a string if the result is single, or a list if it's a list def get_json_value(execution_data, input_data): parsersplit = input_data.split(".") actionname = parsersplit[0][1:].replace(" ", "_", -1) + #Actionname: Start_node + print(f"Actionname: {actionname}") # 1. Find the action @@ -433,59 +521,7 @@ class AppBase: except json.decoder.JSONDecodeError as e: return baseresult - # This whole thing should be recursive. - try: - cnt = 0 - for value in parsersplit[1:]: - cnt += 1 - - print("VALUE: %s" % value) - if value == "#": - # FIXME - not recursive - should go deeper if there are more # - print("HANDLE RECURSIVE LOOP OF %s" % basejson) - returnlist = [] - try: - for innervalue in basejson: - print("Value: %s" % innervalue[parsersplit[cnt+1]]) - returnlist.append(innervalue[parsersplit[cnt+1]]) - except IndexError as e: - print("Indexerror inner: %s" % e) - # Basically means its a normal list, not a crazy one :) - # Custom format for ${name[0,1,2,...]}$ - indexvalue = "${NO_SPLITTER%s}$" % json.dumps(basejson) - if len(returnlist) > 0: - indexvalue = "${NO_SPLITTER%s}$" % json.dumps(returnlist) - - print("INDEXVAL: ", indexvalue) - return indexvalue - except TypeError as e: - print("TypeError inner: %s" % e) - - # Example format: ${[]}$ - parseditem = "${%s%s}$" % (parsersplit[cnt+1], json.dumps(returnlist)) - print("PARSED LOOP ITEM: %s" % parseditem) - return parseditem - - else: - print("BEFORE NORMAL VALUE: ", basejson, value) - if len(value) == 0: - return basejson - - if isinstance(basejson[value], str): - print(f"LOADING STRING '%s' AS JSON" % basejson[value]) - try: - basejson = json.loads(basejson[value]) - except json.decoder.JSONDecodeError as e: - print("RETURNING BECAUSE '%s' IS A NORMAL STRING" % basejson[value]) - return basejson[value] - else: - basejson = basejson[value] - - except KeyError as e: - print("Lower keyerror: %s" % e) - return "KeyError: Couldn't find key: %s" % e - - return basejson + return recurse_json(basejson, parsersplit[1:]) # Parses parameters sent to it and returns whether it did it successfully with the values found def parse_params(action, fullexecution, parameter): @@ -510,6 +546,7 @@ class AppBase: except IndexError: continue + # Handles for loops etc. value = get_json_value(fullexecution, to_be_replaced) if isinstance(value, str): parameter["value"] = parameter["value"].replace(to_be_replaced, value) @@ -849,7 +886,7 @@ class AppBase: replacement = replacement[1:len(replacement)-1] #except json.decoder.JSONDecodeError as e: - print("REPLACING %s with %s" % (key, replacement)) + #print("REPLACING %s with %s" % (key, replacement)) #replacement = parse_wrapper_start(replacement) tmpitem = tmpitem.replace(key, replacement, -1) @@ -911,6 +948,7 @@ class AppBase: results.append(json.loads(ret)) json_object = True except json.decoder.JSONDecodeError as e: + #print("Json: %s" % e) results.append(ret) # Dump the result as a string of a list @@ -920,7 +958,25 @@ class AppBase: if json_object: result = json.dumps(results) else: - result = "[\""+"\", \"".join(results)+"\"]" + result = "[" + for item in results: + try: + json.loads(item) + result += item + except json.decoder.JSONDecodeError as e: + # Common nested issue which puts " around everything + try: + tmpitem = item.replace("\\\"", "\"", -1) + json.loads(tmpitem) + result += tmpitem + + except: + result += "\"%s\"" % item + + result += ", " + + result = result[:-2] + result += "]" else: print("Normal result?") result = results @@ -932,7 +988,7 @@ class AppBase: action_result["result"] = result self.logger.debug(f"Executed {action['label']}-{action['id']} with result: {result}") - self.logger.debug(f"Data: %s" % action_result) + #self.logger.debug(f"Data: %s" % action_result) except TypeError as e: print("TypeError issue: %s" % e) action_result["status"] = "FAILURE" diff --git a/backend/go-app/main.go b/backend/go-app/main.go index 3530ad73..bf365926 100644 --- a/backend/go-app/main.go +++ b/backend/go-app/main.go @@ -6467,11 +6467,6 @@ func init() { r := mux.NewRouter() r.HandleFunc("/api/v1/_ah/health", healthCheckHandler) - // Sends an email if the right things are specified - r.HandleFunc("/functions/sendmail", handleSendalert).Methods("POST", "OPTIONS") - r.HandleFunc("/functions/outlook/register", handleNewOutlookRegister).Methods("GET", "OPTIONS") - r.HandleFunc("/functions/outlook/getFolders", handleGetOutlookFolders).Methods("GET", "OPTIONS") - // Make user related locations r.HandleFunc("/api/v1/users/generateapikey", handleApiGeneration).Methods("GET", "POST", "OPTIONS") r.HandleFunc("/api/v1/users/login", handleLogin).Methods("POST", "OPTIONS") diff --git a/backend/go-app/walkoff.go b/backend/go-app/walkoff.go index 0d06dc37..2d80874e 100644 --- a/backend/go-app/walkoff.go +++ b/backend/go-app/walkoff.go @@ -2042,16 +2042,14 @@ func cleanupExecutions(resp http.ResponseWriter, request *http.Request) { return } - //if user.Role != "admin" { - // resp.WriteHeader(401) - // resp.Write([]byte(`{"success": false, "message": "Insufficient permissions"}`)) - // return - //} - - log.Printf("CLEANUP!") - log.Printf("%#v", user) + if user.Role != "admin" { + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false, "message": "Insufficient permissions"}`)) + return + } ctx := context.Background() + // Removes three months from today timestamp := int64(time.Now().AddDate(0, -2, 0).Unix()) log.Println(timestamp) @@ -2065,8 +2063,6 @@ func cleanupExecutions(resp http.ResponseWriter, request *http.Request) { return } - log.Println(len(workflowExecutions)) - resp.WriteHeader(200) resp.Write([]byte("OK")) } diff --git a/docker-compose.yml b/docker-compose.yml index 02efa84a..4f0c008d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '3' services: frontend: - #build: ./frontend + build: ./frontend image: frikky/shuffle:frontend container_name: shuffle-frontend hostname: shuffle-frontend diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index ea4224f0..5ff1969b 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -146,7 +146,7 @@ const App = (message, props) => { { window.location.pathname = "/docs/about" }} /> } /> } /> - { window.location.pathname = "/login" }} /> + } /> //
diff --git a/frontend/src/components/Header.js b/frontend/src/components/Header.js index 7495d06d..6c37c125 100644 --- a/frontend/src/components/Header.js +++ b/frontend/src/components/Header.js @@ -195,7 +195,7 @@ const Header = props => { - {userdata === undefined || userdata.orgs.length <= 1 ? null : + {userdata === undefined || userdata.orgs === undefined || userdata.orgs === null || userdata.orgs.length <= 1 ? null :