From 6c3c89861b7e5940d6dedb8898154b66b9342534 Mon Sep 17 00:00:00 2001 From: Max McAdam Date: Mon, 24 Jun 2024 10:15:42 -0700 Subject: [PATCH] Bug: Eventlog cleanup changes Signed-off-by: Max McAdam --- api/api_eventlog.go | 10 ++++++---- api/path_eventlog.go | 10 +++++----- api/path_node.go | 2 +- cli/cliutils/cliutils.go | 7 +++++++ cli/eventlog/eventlog.go | 17 +++++++++++------ cli/hzn.go | 6 +++--- eventlog/eventlog_manager.go | 2 +- persistence/eventlogs.go | 14 +++++++++++--- 8 files changed, 45 insertions(+), 23 deletions(-) diff --git a/api/api_eventlog.go b/api/api_eventlog.go index f764e1c82..1dcdd91f5 100644 --- a/api/api_eventlog.go +++ b/api/api_eventlog.go @@ -61,14 +61,16 @@ func (a *API) eventlog(w http.ResponseWriter, r *http.Request) { glog.V(5).Infof(apiLogString(fmt.Sprintf("Handling %v on resource %v with selection %v. Language: %v", r.Method, resource, r.Form, lan))) - if err := DeleteEventLogs(a.db, prune, r.Form, msgPrinter); err != nil { + if count, err := DeleteEventLogs(a.db, prune, r.Form, msgPrinter); err != nil { errorHandler(NewSystemError(msgPrinter.Sprintf("Error deleting %v, error %v", resource, err))) - } else { + } else if count > 0 { if prune { - writeResponse(w, "Successfully pruned event logs.", http.StatusOK) + writeResponse(w, fmt.Sprintf("%v", count), http.StatusOK) } else { - writeResponse(w, "Successfully deleted event logs.", http.StatusOK) + writeResponse(w, fmt.Sprintf("%v", count), http.StatusOK) } + } else { + writeResponse(w, fmt.Sprintf("No matching event log entries found."), http.StatusNoContent) } case "OPTIONS": w.Header().Set("Allow", "GET, OPTIONS") diff --git a/api/path_eventlog.go b/api/path_eventlog.go index 5e73977b8..344f8454d 100644 --- a/api/path_eventlog.go +++ b/api/path_eventlog.go @@ -33,12 +33,12 @@ func FindEventLogsForOutput(db *bolt.DB, all_logs bool, selections map[string][] } // This API deletes the selected event logs saved on the db. -func DeleteEventLogs(db *bolt.DB, prune bool, selections map[string][]string, msgPrinter *message.Printer) error { +func DeleteEventLogs(db *bolt.DB, prune bool, selections map[string][]string, msgPrinter *message.Printer) (int, error) { s := map[string][]persistence.Selector{} if prune { lastUnreg, err := persistence.GetLastUnregistrationTime(db) if err != nil { - return fmt.Errorf("Failed to get the last unregistration time stamp from db. %v", err) + return 0, fmt.Errorf("Failed to get the last unregistration time stamp from db. %v", err) } s["timestamp"] = []persistence.Selector{persistence.Selector{Op: "<", MatchValue: lastUnreg}} @@ -51,14 +51,14 @@ func DeleteEventLogs(db *bolt.DB, prune bool, selections map[string][]string, ms var err error s, err = persistence.ConvertToSelectors(selections) if err != nil { - return fmt.Errorf(msgPrinter.Sprintf("Error converting the selections into Selectors: %v", err)) + return 0, fmt.Errorf(msgPrinter.Sprintf("Error converting the selections into Selectors: %v", err)) } else { glog.V(5).Infof(apiLogString(fmt.Sprintf("Converted selections into a map of persistence.Selector arrays: %v.", s))) } } - err := eventlog.DeleteEventLogs(db, s, msgPrinter) - return err + count, err := eventlog.DeleteEventLogs(db, s, msgPrinter) + return count, err } func FindSurfaceLogsForOutput(db *bolt.DB, msgPrinter *message.Printer) ([]persistence.SurfaceError, error) { diff --git a/api/path_node.go b/api/path_node.go index 9d9847ea5..ef1d6bd48 100644 --- a/api/path_node.go +++ b/api/path_node.go @@ -36,7 +36,7 @@ func LogDeviceEvent(db *bolt.DB, severity string, message *persistence.MessageMe if d.Org != nil { org = *d.Org } - if d.Pattern != nil { + if d.Pattern != nil && *d.Pattern != "" { pattern = fmt.Sprintf("%v/%v", org, *d.Pattern) } if d.Config != nil { diff --git a/cli/cliutils/cliutils.go b/cli/cliutils/cliutils.go index a7c06d848..bf244f061 100644 --- a/cli/cliutils/cliutils.go +++ b/cli/cliutils/cliutils.go @@ -2302,3 +2302,10 @@ func ValidateOrg(org string) bool { } return invalidCheck } + +// remove leading and trailing quotation marks if present +func RemoveQuotes(s string) string { + s = strings.TrimPrefix(s, "\"") + s = strings.TrimSuffix(s, "\"") + return s +} diff --git a/cli/eventlog/eventlog.go b/cli/eventlog/eventlog.go index 309cb6bd0..2acd37716 100644 --- a/cli/eventlog/eventlog.go +++ b/cli/eventlog/eventlog.go @@ -6,6 +6,7 @@ import ( "github.com/open-horizon/anax/cli/cliutils" "github.com/open-horizon/anax/i18n" "github.com/open-horizon/anax/persistence" + "net/http" "regexp" "strings" "time" @@ -80,12 +81,12 @@ func Delete(selections []string, force bool) { cliutils.ConfirmRemove(i18n.GetMessagePrinter().Sprintf("Are you sure you want to remove all event logs?")) } - cliutils.HorizonDelete(url_s, []int{200, 204}, []int{}, false) + retCode, count := cliutils.HorizonDelete(url_s, []int{204}, []int{200}, false) - if len(selections) > 0 { - fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully deleted the selected eventlogs.")) + if retCode == http.StatusOK { + fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully deleted %v matching event log entries.", cliutils.RemoveQuotes(fmt.Sprintf("%v",count)))) } else { - fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully deleted the eventlogs.")) + fmt.Println(i18n.GetMessagePrinter().Sprintf("No event log entries matching the given selectors were found.")) } } @@ -96,9 +97,13 @@ func Prune(force bool) { cliutils.ConfirmRemove(i18n.GetMessagePrinter().Sprintf("Are you sure you want to remove all event logs from previous registrations?")) } - cliutils.HorizonDelete(url, []int{200, 204}, []int{}, false) + retCode, count := cliutils.HorizonDelete(url, []int{204}, []int{200}, false) - fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully pruned the eventlogs.")) + if retCode == http.StatusOK { + fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully pruned %v matching event log entries.", cliutils.RemoveQuotes(fmt.Sprintf("%v",count)))) + } else { + fmt.Println(i18n.GetMessagePrinter().Sprintf("No event log entries from previous registrations were found.")) + } } func List(all bool, detail bool, selections []string, tailing bool) { diff --git a/cli/hzn.go b/cli/hzn.go index 81e2f1201..c068f5b44 100644 --- a/cli/hzn.go +++ b/cli/hzn.go @@ -291,9 +291,9 @@ Environment Variables: listTail := eventlogListCmd.Flag("tail", msgPrinter.Sprintf("Continuously polls the event log to display the most recent records, similar to tail -F behavior.")).Short('f').Bool() listAllEventlogs := eventlogListCmd.Flag("all", msgPrinter.Sprintf("List all the event logs including the previous registrations.")).Short('a').Bool() listDetailedEventlogs := eventlogListCmd.Flag("long", msgPrinter.Sprintf("List event logs with details.")).Short('l').Bool() - listSelectedEventlogs := eventlogListCmd.Flag("select", msgPrinter.Sprintf("Selection string. This flag can be repeated which means 'AND'. Each flag should be in the format of attribute=value, attribute~value, \"attribute>value\" or \"attributevalue\" or\"attributevalue\" or \"attributevalue\" or\"attribute