Skip to content

Commit

Permalink
Fixed more notification health things to ensure we can properly catch…
Browse files Browse the repository at this point in the history
… problems
  • Loading branch information
frikky committed Nov 29, 2023
1 parent f8d69b8 commit 340e28c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
29 changes: 20 additions & 9 deletions db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4793,9 +4793,9 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {
// May be better to just list all, then set to true?
// Is this the slow one?
if len(publicApps) == 0 {
//for _, name := range importantApps {
//query := datastore.NewQuery(nameKey).Filter("Name =", name).Limit(queryLimit)
query := datastore.NewQuery(nameKey).Filter("public =", true).Limit(queryLimit)
for _, name := range importantApps {
query := datastore.NewQuery(nameKey).Filter("Name =", name).Limit(queryLimit)
//query := datastore.NewQuery(nameKey).Filter("public =", true).Limit(queryLimit)
for {
it := project.Dbclient.Run(ctx, query)

Expand Down Expand Up @@ -4867,7 +4867,7 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {
break
}
}
//}
}

newbody, err := json.Marshal(publicApps)
if err != nil {
Expand Down Expand Up @@ -5022,8 +5022,13 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {
found := false
replaceIndex := -1
for dedupIndex, dedupApp := range dedupedApps {
if len(strings.TrimSpace(dedupApp.Name)) == 0 {
continue
}

// Name, owner, ID, parent ID
if strings.ToLower(dedupApp.Name) == strings.ToLower(app.Name) {
//log.Printf("[DEBUG] Found duplicate app: %s (%s). Dedup index: %d", app.Name, app.ID, dedupIndex)
found = true
replaceIndex = dedupIndex
}
Expand All @@ -5036,10 +5041,12 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {

//log.Printf("[INFO] Found duplicate app: %s (%s). Dedup index: %d", app.Name, app.ID, replaceIndex)
// If owner of dedup, don't change
/*
if dedupedApps[replaceIndex].Owner == user.Id {
//log.Printf("[INFO] Owner of deduped app is user. Not replacing.")
log.Printf("[INFO] Owner of deduped app is user. Not replacing.")
continue
}
*/

if app.Edited > dedupedApps[replaceIndex].Edited {
//log.Printf("[INFO] Replacing deduped app with newer app in get apps: %s", app.Name)
Expand All @@ -5049,12 +5056,13 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {

// Check if image, and other doesn't have
if len(dedupedApps[replaceIndex].LargeImage) == 0 && len(app.LargeImage) > 0 {
//log.Printf("[INFO] Replacing deduped app with image in get apps (2): %s", app.Name)
log.Printf("[INFO] Replacing deduped app with image in get apps (2): %s", app.Name)
dedupedApps[replaceIndex] = app
continue
}
}



allApps = dedupedApps
for appIndex, app := range allApps {
for actionIndex, action := range app.Actions {
Expand Down Expand Up @@ -5085,6 +5093,7 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {
return allApps[i].Edited > allApps[j].Edited
})


if len(allApps) > 0 {
// Finds references
allApps = findReferenceAppDocs(ctx, allApps)
Expand All @@ -5102,7 +5111,6 @@ func GetPrioritizedApps(ctx context.Context, user User) ([]WorkflowApp, error) {
}
}


return allApps, nil
}

Expand Down Expand Up @@ -7104,6 +7112,10 @@ func GetOrgNotifications(ctx context.Context, orgId string) ([]Notification, err

notifications = []Notification{}
for _, hit := range wrapped.Hits.Hits {
if hit.Source.Personal {
continue
}

if hit.Source.OrgId == orgId {
notifications = append(notifications, hit.Source)
}
Expand Down Expand Up @@ -8808,7 +8820,6 @@ func GetAppExecutionValues(ctx context.Context, parameterNames, orgId, workflowI
return workflows, err
}

log.Printf("\n\nFOUND: %d", len(wrapped.Hits.Hits))
workflows = []NewValue{}
for _, hit := range wrapped.Hits.Hits {
if hit.Source.Value == value && hit.Source.OrgId == orgId {
Expand Down
6 changes: 3 additions & 3 deletions notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ func HandleGetNotifications(resp http.ResponseWriter, request *http.Request) {
newNotifications := []Notification{}
for _, notification := range notifications {
// Check how long ago?
if notification.Read {
continue
}
//if notification.Read {
// continue
//}

if notification.Personal {
continue
Expand Down
54 changes: 38 additions & 16 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -4169,6 +4169,9 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {

correctUser := false
if user.Id != tmpworkflow.Owner || tmpworkflow.Public == true {
log.Printf("[AUDIT] User %s is accessing workflow %s (save workflow)", user.Username, tmpworkflow.ID)

// if,ifelse: Public, Org owns it, or user owns it
if tmpworkflow.Public {
// FIXME:
// If the user Id is part of the creator: DONT update this way.
Expand Down Expand Up @@ -4292,7 +4295,7 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {
},
},
Running: true,
OrgId: user.ActiveOrg.Id,
OrgId: workflow.OrgId,
Environment: "cloud",
Auth: hookAuth,
CustomResponse: customResponse,
Expand Down Expand Up @@ -4394,6 +4397,7 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {
return
}
} else {
log.Printf("[AUDIT] User %s is creating or modifying workflow with ID %s as they are the owner / it's public", user.Username, workflow.ID)

if workflow.Public {
log.Printf("[WARNING] Rolling back public as the user set it to true themselves")
Expand All @@ -4402,6 +4406,8 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {

if len(workflow.PublishedId) > 0 {
log.Printf("[INFO] Workflow %s has the published ID %s", workflow.ID, workflow.PublishedId)

// Overwrite ID here?
}
}

Expand Down Expand Up @@ -4453,25 +4459,25 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {
//workflow.DefaultReturnValue
}

log.Printf("[INFO] Saving workflow '%s' with %d action(s) and %d trigger(s)", workflow.Name, len(workflow.Actions), len(workflow.Triggers))
log.Printf("[INFO] Saving workflow '%s' with %d action(s) and %d trigger(s). Org: %s", workflow.Name, len(workflow.Actions), len(workflow.Triggers), workflow.OrgId)

if len(user.ActiveOrg.Id) > 0 {
if len(workflow.OrgId) == 0 && len(user.ActiveOrg.Id) > 0 {
if len(workflow.ExecutingOrg.Id) == 0 {
log.Printf("[INFO] Setting executing org for workflow to %s", user.ActiveOrg.Id)
user.ActiveOrg.Users = []UserMini{}
workflow.ExecutingOrg = user.ActiveOrg
}

//if len(workflow.Org) == 0 {
// user.ActiveOrg.Users = []UserMini{}
// //workflow.Org = user.ActiveOrg
//}

if len(workflow.OrgId) == 0 {
workflow.OrgId = user.ActiveOrg.Id
}
} else if len(workflow.OrgId) != 0 && len(workflow.ExecutingOrg.Id) == 0 {
log.Printf("[INFO] Setting executing org for workflow to %s", workflow.OrgId)
workflow.ExecutingOrg.Id = workflow.OrgId
workflow.ExecutingOrg.Name = ""
}


newActions := []Action{}
allNodes := []string{}
workflow.Categories = Categories{}
Expand All @@ -4488,13 +4494,11 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {
},
}

//if project.Environment != "cloud" {
environments, err = GetEnvironments(ctx, user.ActiveOrg.Id)
if err != nil {
log.Printf("[WARNING] Failed getting environments for org %s", user.ActiveOrg.Id)
environments = []Environment{}
}
//}

defaultEnv := ""
for _, env := range environments {
Expand Down Expand Up @@ -5578,10 +5582,17 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {

// TBD: Is this too drastic? May lead to issues in the future.
if workflow.OrgId != user.ActiveOrg.Id {
log.Printf("[WARNING] Editing workflow to be owned by org %s", user.ActiveOrg.Id)
log.Printf("[WARNING] NOT Editing workflow to be owned by org %s. Instead just editing. Original org: %s", user.ActiveOrg.Id, workflow.OrgId)


/*
workflow.OrgId = user.ActiveOrg.Id
workflow.ExecutingOrg = user.ActiveOrg
workflow.Org = append(workflow.Org, user.ActiveOrg)
*/
//resp.WriteHeader(500)
//resp.Write([]byte(`{"success": false, "error": "Workflow does not belong to this org"}`))
//return
}

// Only happens if the workflow is public and being edited
Expand Down Expand Up @@ -6193,7 +6204,6 @@ func HandlePasswordChange(resp http.ResponseWriter, request *http.Request) {
}
}

// log.Printf("FOUND: %s", curUserFound)
foundUser = userInfo
//userInfo, err := HandleApiAuthentication(resp, request)
}
Expand Down Expand Up @@ -7433,6 +7443,7 @@ func HandleCreateSubOrg(resp http.ResponseWriter, request *http.Request) {

type ReturnData struct {
OrgId string `json:"org_id" datastore:"org_id"`
OrgName string `json:"org_name" datastore:"org_name"`
Name string `json:"name" datastore:"name"`
}

Expand All @@ -7445,10 +7456,14 @@ func HandleCreateSubOrg(resp http.ResponseWriter, request *http.Request) {
return
}

if len(tmpData.OrgName) > 0 && len(tmpData.Name) == 0 {
tmpData.Name = tmpData.OrgName
}

if len(tmpData.Name) < 3 {
log.Printf("[WARNING] Suborgname too short (min 3) %s", tmpData.Name)
resp.WriteHeader(400)
resp.Write([]byte(`{"success": false, "reason": "Name must at least be 3 characters."}`))
resp.Write([]byte(`{"success": false, "reason": "Name must at least be 3 characters. Required fields: org_id, name"}`))
return
}

Expand Down Expand Up @@ -7501,6 +7516,12 @@ func HandleCreateSubOrg(resp http.ResponseWriter, request *http.Request) {
}

orgId := uuid.NewV4().String()
newApps := parentOrg.ActiveApps
if len(newApps) > 11 {
// Do the last 10 apps, not 10 first
newApps = newApps[len(newApps)-10:]
}

newOrg := Org{
Name: tmpData.Name,
Description: fmt.Sprintf("Sub-org by user %s in parent-org %s", user.Username, parentOrg.Name),
Expand All @@ -7520,9 +7541,12 @@ func HandleCreateSubOrg(resp http.ResponseWriter, request *http.Request) {
},
CloudSyncActive: parentOrg.CloudSyncActive,
CreatorOrg: tmpData.OrgId,
ActiveApps: parentOrg.ActiveApps,
Region: parentOrg.Region,
RegionUrl: parentOrg.RegionUrl,

// FIXME: Should this be here? Makes things slow~
// Should only append apps owned by the parentorg itself
ActiveApps: newApps,
}

parentOrg.ChildOrgs = append(parentOrg.ChildOrgs, OrgMini{
Expand Down Expand Up @@ -9693,8 +9717,6 @@ func updateExecutionParent(ctx context.Context, executionParent, returnValue, pa
}
}

//log.Printf("\n\nPARENTNODEFOUND: %t\n\n", parentNodeFound)

// If found, loop through and make sure to check the result for ALL of them. If they're not in there, add them as values.
if parentNodeFound {
log.Printf("[DEBUG] Found result for subflow (parentNodeFound). Got %d parentSubflowResults", len(parentSubflowResult))
Expand Down

0 comments on commit 340e28c

Please sign in to comment.