From b3bd6cae7ce25ea979a7068de46b19e96366dcea Mon Sep 17 00:00:00 2001 From: Harduino Date: Fri, 18 Sep 2020 16:26:50 +0300 Subject: [PATCH 1/2] siemonster :: recalculate authenticators stats on workflows import/delete --- backend/go-app/walkoff.go | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/backend/go-app/walkoff.go b/backend/go-app/walkoff.go index 0d06dc37..d61f9b92 100644 --- a/backend/go-app/walkoff.go +++ b/backend/go-app/walkoff.go @@ -1411,6 +1411,12 @@ func deleteWorkflow(resp http.ResponseWriter, request *http.Request) { log.Printf("Failed to increase total workflows: %s", err) } + // recalculate authenticators stats + err = recalculateAppAuthentications() + if err != nil { + log.Printf("Authentications recalculation failed: %s", err) + } + //memcacheName := fmt.Sprintf("%s_%s", user.Username, fileId) //memcache.Delete(ctx, memcacheName) //memcacheName = fmt.Sprintf("%s_workflows", user.Username) @@ -4282,10 +4288,78 @@ func loadSpecificWorkflows(resp http.ResponseWriter, request *http.Request) { return } + // recalculate authenticators stats + err = recalculateAppAuthentications() + if err != nil { + log.Printf("Authentications recalculation failed: %s", err) + } + resp.WriteHeader(200) resp.Write([]byte(fmt.Sprintf(`{"success": true}`))) } +func recalculateAppAuthentications() error { + // create context + ctx := context.Background() + + // form workflows list + workflows, err := getAllWorkflows(ctx) + if err != nil { + log.Printf("Error: Failed getting workflows: %s", err) + return err + } + + // form authenticators list + auths, err := getAllWorkflowAppAuth(ctx) + if err != nil { + log.Printf("Error: Failed getting auths: %s", err) + return err + } + + // iterate through auths + for _, auth := range auths { + // reset calculated values + auth.WorkflowCount = 0 + auth.NodeCount = 0 + auth.Usage = []AuthenticationUsage{} + + // iterate through workflows to find which uses this auth + for _, workflow := range workflows { + hasCurrentAuth := false + usageItem := AuthenticationUsage{ + WorkflowId: workflow.ID, + Nodes: []string{}, + } + + // iterate through actions + for _, action := range workflow.Actions { + if action.AuthenticationId == auth.Id { + // this workflow should be added to "usage" field + hasCurrentAuth = true + + // add this action to list + usageItem.Nodes = append(usageItem.Nodes, action.ID) + } + } + + // update current auth with found workflow + if hasCurrentAuth { + auth.WorkflowCount += 1 + auth.NodeCount += int64(len(usageItem.Nodes)) + auth.Usage = append(auth.Usage, usageItem) + } + } + + // update record in database + err := setWorkflowAppAuthDatastore(ctx, auth, auth.Id) + if err != nil { + log.Printf("Failed setting up app auth %s: %s", auth.Id, err) + } + } + + return nil +} + func handleAppHotloadRequest(resp http.ResponseWriter, request *http.Request) { cors := handleCors(resp, request) if cors { From 67161009f8f34ccf3f697528725cecef30b23086 Mon Sep 17 00:00:00 2001 From: frikky Date: Sat, 10 Oct 2020 06:56:52 +0200 Subject: [PATCH 2/2] Fixed null pointer exception when deleted auth --- frontend/src/views/Admin.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/views/Admin.jsx b/frontend/src/views/Admin.jsx index 4de8c320..997aedf8 100644 --- a/frontend/src/views/Admin.jsx +++ b/frontend/src/views/Admin.jsx @@ -1031,7 +1031,7 @@ const Admin = (props) => { console.log("Show apps with this category") }} > - Find app ({data.apps.length}) + Find app ({data.apps === null ? 0 : data.apps.length}) @@ -1094,7 +1094,7 @@ const Admin = (props) => { style={{minWidth: 150, maxWidth: 150}} />